2019-11-19 10:00:20 -07:00
|
|
|
package nebula
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2023-07-24 11:37:52 -06:00
|
|
|
"errors"
|
2019-11-19 10:00:20 -07:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
2023-03-29 13:30:28 -06:00
|
|
|
"runtime"
|
2019-11-19 10:00:20 -07:00
|
|
|
"runtime/pprof"
|
2021-04-14 12:50:09 -06:00
|
|
|
"sort"
|
2023-03-29 13:30:28 -06:00
|
|
|
"strconv"
|
2019-11-19 10:00:20 -07:00
|
|
|
"strings"
|
2020-06-30 16:53:30 -06:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2021-11-03 19:54:04 -06:00
|
|
|
"github.com/slackhq/nebula/config"
|
|
|
|
"github.com/slackhq/nebula/header"
|
|
|
|
"github.com/slackhq/nebula/iputil"
|
2020-06-30 16:53:30 -06:00
|
|
|
"github.com/slackhq/nebula/sshd"
|
2021-11-03 19:54:04 -06:00
|
|
|
"github.com/slackhq/nebula/udp"
|
2019-11-19 10:00:20 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type sshListHostMapFlags struct {
|
2023-03-13 11:35:14 -06:00
|
|
|
Json bool
|
|
|
|
Pretty bool
|
|
|
|
ByIndex bool
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type sshPrintCertFlags struct {
|
|
|
|
Json bool
|
|
|
|
Pretty bool
|
2021-06-07 16:06:59 -06:00
|
|
|
Raw bool
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type sshPrintTunnelFlags struct {
|
|
|
|
Pretty bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type sshChangeRemoteFlags struct {
|
|
|
|
Address string
|
|
|
|
}
|
|
|
|
|
|
|
|
type sshCloseTunnelFlags struct {
|
|
|
|
LocalOnly bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type sshCreateTunnelFlags struct {
|
|
|
|
Address string
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
func wireSSHReload(l *logrus.Logger, ssh *sshd.SSHServer, c *config.C) {
|
|
|
|
c.RegisterReloadCallback(func(c *config.C) {
|
2019-11-19 10:00:20 -07:00
|
|
|
if c.GetBool("sshd.enabled", false) {
|
2021-04-16 09:34:28 -06:00
|
|
|
sshRun, err := configSSH(l, ssh, c)
|
2019-11-19 10:00:20 -07:00
|
|
|
if err != nil {
|
|
|
|
l.WithError(err).Error("Failed to reconfigure the sshd")
|
|
|
|
ssh.Stop()
|
|
|
|
}
|
2021-04-16 09:34:28 -06:00
|
|
|
if sshRun != nil {
|
|
|
|
go sshRun()
|
|
|
|
}
|
2019-11-19 10:00:20 -07:00
|
|
|
} else {
|
|
|
|
ssh.Stop()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-16 09:34:28 -06:00
|
|
|
// configSSH reads the ssh info out of the passed-in Config and
|
|
|
|
// updates the passed-in SSHServer. On success, it returns a function
|
|
|
|
// that callers may invoke to run the configured ssh server. On
|
|
|
|
// failure, it returns nil, error.
|
2021-11-03 19:54:04 -06:00
|
|
|
func configSSH(l *logrus.Logger, ssh *sshd.SSHServer, c *config.C) (func(), error) {
|
2019-11-19 10:00:20 -07:00
|
|
|
//TODO conntrack list
|
|
|
|
//TODO print firewall rules or hash?
|
|
|
|
|
|
|
|
listen := c.GetString("sshd.listen", "")
|
|
|
|
if listen == "" {
|
2021-04-16 09:34:28 -06:00
|
|
|
return nil, fmt.Errorf("sshd.listen must be provided")
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2020-09-15 07:57:32 -06:00
|
|
|
_, port, err := net.SplitHostPort(listen)
|
|
|
|
if err != nil {
|
2021-04-16 09:34:28 -06:00
|
|
|
return nil, fmt.Errorf("invalid sshd.listen address: %s", err)
|
2020-09-15 07:57:32 -06:00
|
|
|
}
|
|
|
|
if port == "22" {
|
2021-04-16 09:34:28 -06:00
|
|
|
return nil, fmt.Errorf("sshd.listen can not use port 22")
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: no good way to reload this right now
|
2024-01-22 11:58:44 -07:00
|
|
|
hostKeyPathOrKey := c.GetString("sshd.host_key", "")
|
|
|
|
if hostKeyPathOrKey == "" {
|
2021-04-16 09:34:28 -06:00
|
|
|
return nil, fmt.Errorf("sshd.host_key must be provided")
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2024-01-22 11:58:44 -07:00
|
|
|
var hostKeyBytes []byte
|
|
|
|
if strings.Contains(hostKeyPathOrKey, "-----BEGIN") {
|
|
|
|
hostKeyBytes = []byte(hostKeyPathOrKey)
|
|
|
|
} else {
|
|
|
|
hostKeyBytes, err = os.ReadFile(hostKeyPathOrKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error while loading sshd.host_key file: %s", err)
|
|
|
|
}
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
err = ssh.SetHostKey(hostKeyBytes)
|
|
|
|
if err != nil {
|
2021-04-16 09:34:28 -06:00
|
|
|
return nil, fmt.Errorf("error while adding sshd.host_key: %s", err)
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
rawKeys := c.Get("sshd.authorized_users")
|
|
|
|
keys, ok := rawKeys.([]interface{})
|
|
|
|
if ok {
|
|
|
|
for _, rk := range keys {
|
|
|
|
kDef, ok := rk.(map[interface{}]interface{})
|
|
|
|
if !ok {
|
|
|
|
l.WithField("sshKeyConfig", rk).Warn("Authorized user had an error, ignoring")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
user, ok := kDef["user"].(string)
|
|
|
|
if !ok {
|
|
|
|
l.WithField("sshKeyConfig", rk).Warn("Authorized user is missing the user field")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
k := kDef["keys"]
|
|
|
|
switch v := k.(type) {
|
|
|
|
case string:
|
|
|
|
err := ssh.AddAuthorizedKey(user, v)
|
|
|
|
if err != nil {
|
|
|
|
l.WithError(err).WithField("sshKeyConfig", rk).WithField("sshKey", v).Warn("Failed to authorize key")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
case []interface{}:
|
|
|
|
for _, subK := range v {
|
|
|
|
sk, ok := subK.(string)
|
|
|
|
if !ok {
|
|
|
|
l.WithField("sshKeyConfig", rk).WithField("sshKey", subK).Warn("Did not understand ssh key")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err := ssh.AddAuthorizedKey(user, sk)
|
|
|
|
if err != nil {
|
|
|
|
l.WithError(err).WithField("sshKeyConfig", sk).Warn("Failed to authorize key")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
l.WithField("sshKeyConfig", rk).Warn("Authorized user is missing the keys field or was not understood")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
l.Info("no ssh users to authorize")
|
|
|
|
}
|
|
|
|
|
2021-04-16 09:34:28 -06:00
|
|
|
var runner func()
|
2019-11-19 10:00:20 -07:00
|
|
|
if c.GetBool("sshd.enabled", false) {
|
|
|
|
ssh.Stop()
|
2021-04-16 09:34:28 -06:00
|
|
|
runner = func() {
|
|
|
|
if err := ssh.Run(listen); err != nil {
|
|
|
|
l.WithField("err", err).Warn("Failed to run the SSH server")
|
|
|
|
}
|
|
|
|
}
|
2019-11-19 10:00:20 -07:00
|
|
|
} else {
|
|
|
|
ssh.Stop()
|
|
|
|
}
|
|
|
|
|
2021-04-16 09:34:28 -06:00
|
|
|
return runner, nil
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2023-07-24 11:37:52 -06:00
|
|
|
func attachCommands(l *logrus.Logger, c *config.C, ssh *sshd.SSHServer, f *Interface) {
|
2019-11-19 10:00:20 -07:00
|
|
|
ssh.RegisterCommand(&sshd.Command{
|
|
|
|
Name: "list-hostmap",
|
|
|
|
ShortDescription: "List all known previously connected hosts",
|
|
|
|
Flags: func() (*flag.FlagSet, interface{}) {
|
|
|
|
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
|
|
|
s := sshListHostMapFlags{}
|
|
|
|
fl.BoolVar(&s.Json, "json", false, "outputs as json with more information")
|
|
|
|
fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json, assumes -json")
|
2023-03-13 11:35:14 -06:00
|
|
|
fl.BoolVar(&s.ByIndex, "by-index", false, "gets all hosts in the hostmap from the index table")
|
2019-11-19 10:00:20 -07:00
|
|
|
return fl, &s
|
|
|
|
},
|
|
|
|
Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
|
2023-07-24 11:37:52 -06:00
|
|
|
return sshListHostMap(f.hostMap, fs, w)
|
2019-11-19 10:00:20 -07:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
ssh.RegisterCommand(&sshd.Command{
|
|
|
|
Name: "list-pending-hostmap",
|
|
|
|
ShortDescription: "List all handshaking hosts",
|
|
|
|
Flags: func() (*flag.FlagSet, interface{}) {
|
|
|
|
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
|
|
|
s := sshListHostMapFlags{}
|
|
|
|
fl.BoolVar(&s.Json, "json", false, "outputs as json with more information")
|
|
|
|
fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json, assumes -json")
|
2023-03-13 11:35:14 -06:00
|
|
|
fl.BoolVar(&s.ByIndex, "by-index", false, "gets all hosts in the hostmap from the index table")
|
2019-11-19 10:00:20 -07:00
|
|
|
return fl, &s
|
|
|
|
},
|
|
|
|
Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
|
2023-07-24 11:37:52 -06:00
|
|
|
return sshListHostMap(f.handshakeManager, fs, w)
|
2019-11-19 10:00:20 -07:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
ssh.RegisterCommand(&sshd.Command{
|
|
|
|
Name: "list-lighthouse-addrmap",
|
|
|
|
ShortDescription: "List all lighthouse map entries",
|
|
|
|
Flags: func() (*flag.FlagSet, interface{}) {
|
|
|
|
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
|
|
|
s := sshListHostMapFlags{}
|
|
|
|
fl.BoolVar(&s.Json, "json", false, "outputs as json with more information")
|
|
|
|
fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json, assumes -json")
|
|
|
|
return fl, &s
|
|
|
|
},
|
|
|
|
Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
|
2023-07-24 11:37:52 -06:00
|
|
|
return sshListLighthouseMap(f.lightHouse, fs, w)
|
2019-11-19 10:00:20 -07:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
ssh.RegisterCommand(&sshd.Command{
|
|
|
|
Name: "reload",
|
|
|
|
ShortDescription: "Reloads configuration from disk, same as sending HUP to the process",
|
2022-08-08 11:44:09 -06:00
|
|
|
Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
|
|
|
|
return sshReload(c, w)
|
|
|
|
},
|
2019-11-19 10:00:20 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
ssh.RegisterCommand(&sshd.Command{
|
|
|
|
Name: "start-cpu-profile",
|
|
|
|
ShortDescription: "Starts a cpu profile and write output to the provided file",
|
|
|
|
Callback: sshStartCpuProfile,
|
|
|
|
})
|
|
|
|
|
|
|
|
ssh.RegisterCommand(&sshd.Command{
|
|
|
|
Name: "stop-cpu-profile",
|
|
|
|
ShortDescription: "Stops a cpu profile and writes output to the previously provided file",
|
|
|
|
Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
|
|
|
|
pprof.StopCPUProfile()
|
|
|
|
return w.WriteLine("If a CPU profile was running it is now stopped")
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
ssh.RegisterCommand(&sshd.Command{
|
|
|
|
Name: "save-heap-profile",
|
|
|
|
ShortDescription: "Saves a heap profile to the provided path",
|
|
|
|
Callback: sshGetHeapProfile,
|
|
|
|
})
|
|
|
|
|
2023-03-29 13:30:28 -06:00
|
|
|
ssh.RegisterCommand(&sshd.Command{
|
|
|
|
Name: "mutex-profile-fraction",
|
|
|
|
ShortDescription: "Gets or sets runtime.SetMutexProfileFraction",
|
|
|
|
Callback: sshMutexProfileFraction,
|
|
|
|
})
|
|
|
|
|
|
|
|
ssh.RegisterCommand(&sshd.Command{
|
|
|
|
Name: "save-mutex-profile",
|
|
|
|
ShortDescription: "Saves a mutex profile to the provided path",
|
|
|
|
Callback: sshGetMutexProfile,
|
|
|
|
})
|
|
|
|
|
2019-11-19 10:00:20 -07:00
|
|
|
ssh.RegisterCommand(&sshd.Command{
|
|
|
|
Name: "log-level",
|
|
|
|
ShortDescription: "Gets or sets the current log level",
|
2021-03-26 08:46:30 -06:00
|
|
|
Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
|
|
|
|
return sshLogLevel(l, fs, a, w)
|
|
|
|
},
|
2019-11-19 10:00:20 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
ssh.RegisterCommand(&sshd.Command{
|
|
|
|
Name: "log-format",
|
|
|
|
ShortDescription: "Gets or sets the current log format",
|
2021-03-26 08:46:30 -06:00
|
|
|
Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
|
|
|
|
return sshLogFormat(l, fs, a, w)
|
|
|
|
},
|
2019-11-19 10:00:20 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
ssh.RegisterCommand(&sshd.Command{
|
|
|
|
Name: "version",
|
|
|
|
ShortDescription: "Prints the currently running version of nebula",
|
|
|
|
Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
|
2023-07-24 11:37:52 -06:00
|
|
|
return sshVersion(f, fs, a, w)
|
2019-11-19 10:00:20 -07:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
ssh.RegisterCommand(&sshd.Command{
|
|
|
|
Name: "print-cert",
|
|
|
|
ShortDescription: "Prints the current certificate being used or the certificate for the provided vpn ip",
|
|
|
|
Flags: func() (*flag.FlagSet, interface{}) {
|
|
|
|
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
|
|
|
s := sshPrintCertFlags{}
|
|
|
|
fl.BoolVar(&s.Json, "json", false, "outputs as json")
|
|
|
|
fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json, assumes -json")
|
2021-06-07 16:06:59 -06:00
|
|
|
fl.BoolVar(&s.Raw, "raw", false, "raw prints the PEM encoded certificate, not compatible with -json or -pretty")
|
2019-11-19 10:00:20 -07:00
|
|
|
return fl, &s
|
|
|
|
},
|
|
|
|
Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
|
2023-07-24 11:37:52 -06:00
|
|
|
return sshPrintCert(f, fs, a, w)
|
2019-11-19 10:00:20 -07:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
ssh.RegisterCommand(&sshd.Command{
|
|
|
|
Name: "print-tunnel",
|
|
|
|
ShortDescription: "Prints json details about a tunnel for the provided vpn ip",
|
|
|
|
Flags: func() (*flag.FlagSet, interface{}) {
|
|
|
|
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
|
|
|
s := sshPrintTunnelFlags{}
|
|
|
|
fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json")
|
|
|
|
return fl, &s
|
|
|
|
},
|
|
|
|
Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
|
2023-07-24 11:37:52 -06:00
|
|
|
return sshPrintTunnel(f, fs, a, w)
|
2019-11-19 10:00:20 -07:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2022-06-21 12:35:23 -06:00
|
|
|
ssh.RegisterCommand(&sshd.Command{
|
|
|
|
Name: "print-relays",
|
|
|
|
ShortDescription: "Prints json details about all relay info",
|
|
|
|
Flags: func() (*flag.FlagSet, interface{}) {
|
|
|
|
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
|
|
|
s := sshPrintTunnelFlags{}
|
|
|
|
fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json")
|
|
|
|
return fl, &s
|
|
|
|
},
|
|
|
|
Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
|
2023-07-24 11:37:52 -06:00
|
|
|
return sshPrintRelays(f, fs, a, w)
|
2022-06-21 12:35:23 -06:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2019-11-19 10:00:20 -07:00
|
|
|
ssh.RegisterCommand(&sshd.Command{
|
|
|
|
Name: "change-remote",
|
|
|
|
ShortDescription: "Changes the remote address used in the tunnel for the provided vpn ip",
|
|
|
|
Flags: func() (*flag.FlagSet, interface{}) {
|
|
|
|
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
|
|
|
s := sshChangeRemoteFlags{}
|
|
|
|
fl.StringVar(&s.Address, "address", "", "The new remote address, ip:port")
|
|
|
|
return fl, &s
|
|
|
|
},
|
|
|
|
Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
|
2023-07-24 11:37:52 -06:00
|
|
|
return sshChangeRemote(f, fs, a, w)
|
2019-11-19 10:00:20 -07:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
ssh.RegisterCommand(&sshd.Command{
|
|
|
|
Name: "close-tunnel",
|
|
|
|
ShortDescription: "Closes a tunnel for the provided vpn ip",
|
|
|
|
Flags: func() (*flag.FlagSet, interface{}) {
|
|
|
|
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
|
|
|
s := sshCloseTunnelFlags{}
|
|
|
|
fl.BoolVar(&s.LocalOnly, "local-only", false, "Disables notifying the remote that the tunnel is shutting down")
|
|
|
|
return fl, &s
|
|
|
|
},
|
|
|
|
Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
|
2023-07-24 11:37:52 -06:00
|
|
|
return sshCloseTunnel(f, fs, a, w)
|
2019-11-19 10:00:20 -07:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
ssh.RegisterCommand(&sshd.Command{
|
|
|
|
Name: "create-tunnel",
|
|
|
|
ShortDescription: "Creates a tunnel for the provided vpn ip and address",
|
|
|
|
Help: "The lighthouses will be queried for real addresses but you can provide one as well.",
|
|
|
|
Flags: func() (*flag.FlagSet, interface{}) {
|
|
|
|
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
|
|
|
s := sshCreateTunnelFlags{}
|
|
|
|
fl.StringVar(&s.Address, "address", "", "Optionally provide a real remote address, ip:port ")
|
|
|
|
return fl, &s
|
|
|
|
},
|
|
|
|
Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
|
2023-07-24 11:37:52 -06:00
|
|
|
return sshCreateTunnel(f, fs, a, w)
|
2019-11-19 10:00:20 -07:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
ssh.RegisterCommand(&sshd.Command{
|
|
|
|
Name: "query-lighthouse",
|
|
|
|
ShortDescription: "Query the lighthouses for the provided vpn ip",
|
|
|
|
Help: "This command is asynchronous. Only currently known udp ips will be printed.",
|
|
|
|
Callback: func(fs interface{}, a []string, w sshd.StringWriter) error {
|
2023-07-24 11:37:52 -06:00
|
|
|
return sshQueryLighthouse(f, fs, a, w)
|
2019-11-19 10:00:20 -07:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-24 11:37:52 -06:00
|
|
|
func sshListHostMap(hl controlHostLister, a interface{}, w sshd.StringWriter) error {
|
2019-11-19 10:00:20 -07:00
|
|
|
fs, ok := a.(*sshListHostMapFlags)
|
|
|
|
if !ok {
|
|
|
|
//TODO: error
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-13 11:35:14 -06:00
|
|
|
var hm []ControlHostInfo
|
|
|
|
if fs.ByIndex {
|
2023-07-24 11:37:52 -06:00
|
|
|
hm = listHostMapIndexes(hl)
|
2023-03-13 11:35:14 -06:00
|
|
|
} else {
|
2023-07-24 11:37:52 -06:00
|
|
|
hm = listHostMapHosts(hl)
|
2023-03-13 11:35:14 -06:00
|
|
|
}
|
|
|
|
|
2021-04-14 12:50:09 -06:00
|
|
|
sort.Slice(hm, func(i, j int) bool {
|
2021-11-03 19:54:04 -06:00
|
|
|
return bytes.Compare(hm[i].VpnIp, hm[j].VpnIp) < 0
|
2021-04-14 12:50:09 -06:00
|
|
|
})
|
2019-11-19 10:00:20 -07:00
|
|
|
|
|
|
|
if fs.Json || fs.Pretty {
|
|
|
|
js := json.NewEncoder(w.GetWriter())
|
|
|
|
if fs.Pretty {
|
|
|
|
js.SetIndent("", " ")
|
|
|
|
}
|
|
|
|
|
2021-04-14 12:50:09 -06:00
|
|
|
err := js.Encode(hm)
|
2019-11-19 10:00:20 -07:00
|
|
|
if err != nil {
|
|
|
|
//TODO
|
|
|
|
return nil
|
|
|
|
}
|
2021-04-14 12:50:09 -06:00
|
|
|
|
2019-11-19 10:00:20 -07:00
|
|
|
} else {
|
2021-04-14 12:50:09 -06:00
|
|
|
for _, v := range hm {
|
2021-11-03 19:54:04 -06:00
|
|
|
err := w.WriteLine(fmt.Sprintf("%s: %s", v.VpnIp, v.RemoteAddrs))
|
2019-11-19 10:00:20 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sshListLighthouseMap(lightHouse *LightHouse, a interface{}, w sshd.StringWriter) error {
|
|
|
|
fs, ok := a.(*sshListHostMapFlags)
|
|
|
|
if !ok {
|
|
|
|
//TODO: error
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-14 12:50:09 -06:00
|
|
|
type lighthouseInfo struct {
|
2021-11-03 19:54:04 -06:00
|
|
|
VpnIp string `json:"vpnIp"`
|
2021-04-14 12:50:09 -06:00
|
|
|
Addrs *CacheMap `json:"addrs"`
|
|
|
|
}
|
|
|
|
|
2019-11-19 10:00:20 -07:00
|
|
|
lightHouse.RLock()
|
2021-04-14 12:50:09 -06:00
|
|
|
addrMap := make([]lighthouseInfo, len(lightHouse.addrMap))
|
|
|
|
x := 0
|
|
|
|
for k, v := range lightHouse.addrMap {
|
|
|
|
addrMap[x] = lighthouseInfo{
|
2021-11-03 19:54:04 -06:00
|
|
|
VpnIp: k.String(),
|
2021-04-14 12:50:09 -06:00
|
|
|
Addrs: v.CopyCache(),
|
|
|
|
}
|
|
|
|
x++
|
|
|
|
}
|
|
|
|
lightHouse.RUnlock()
|
|
|
|
|
|
|
|
sort.Slice(addrMap, func(i, j int) bool {
|
2021-11-03 19:54:04 -06:00
|
|
|
return strings.Compare(addrMap[i].VpnIp, addrMap[j].VpnIp) < 0
|
2021-04-14 12:50:09 -06:00
|
|
|
})
|
2019-11-19 10:00:20 -07:00
|
|
|
|
|
|
|
if fs.Json || fs.Pretty {
|
|
|
|
js := json.NewEncoder(w.GetWriter())
|
|
|
|
if fs.Pretty {
|
|
|
|
js.SetIndent("", " ")
|
|
|
|
}
|
|
|
|
|
2021-04-14 12:50:09 -06:00
|
|
|
err := js.Encode(addrMap)
|
2019-11-19 10:00:20 -07:00
|
|
|
if err != nil {
|
|
|
|
//TODO
|
|
|
|
return nil
|
|
|
|
}
|
2021-04-14 12:50:09 -06:00
|
|
|
|
2019-11-19 10:00:20 -07:00
|
|
|
} else {
|
2021-04-14 12:50:09 -06:00
|
|
|
for _, v := range addrMap {
|
|
|
|
b, err := json.Marshal(v.Addrs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-03 19:54:04 -06:00
|
|
|
err = w.WriteLine(fmt.Sprintf("%s: %s", v.VpnIp, string(b)))
|
2019-11-19 10:00:20 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sshStartCpuProfile(fs interface{}, a []string, w sshd.StringWriter) error {
|
|
|
|
if len(a) == 0 {
|
|
|
|
err := w.WriteLine("No path to write profile provided")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Create(a[0])
|
|
|
|
if err != nil {
|
|
|
|
err = w.WriteLine(fmt.Sprintf("Unable to create profile file: %s", err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pprof.StartCPUProfile(file)
|
|
|
|
if err != nil {
|
|
|
|
err = w.WriteLine(fmt.Sprintf("Unable to start cpu profile: %s", err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = w.WriteLine(fmt.Sprintf("Started cpu profile, issue stop-cpu-profile to write the output to %s", a))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func sshVersion(ifce *Interface, fs interface{}, a []string, w sshd.StringWriter) error {
|
|
|
|
return w.WriteLine(fmt.Sprintf("%s", ifce.version))
|
|
|
|
}
|
|
|
|
|
|
|
|
func sshQueryLighthouse(ifce *Interface, fs interface{}, a []string, w sshd.StringWriter) error {
|
|
|
|
if len(a) == 0 {
|
|
|
|
return w.WriteLine("No vpn ip was provided")
|
|
|
|
}
|
|
|
|
|
2020-09-18 08:10:25 -06:00
|
|
|
parsedIp := net.ParseIP(a[0])
|
|
|
|
if parsedIp == nil {
|
|
|
|
return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0]))
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
vpnIp := iputil.Ip2VpnIp(parsedIp)
|
2019-11-19 10:00:20 -07:00
|
|
|
if vpnIp == 0 {
|
|
|
|
return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0]))
|
|
|
|
}
|
|
|
|
|
2021-04-15 08:12:21 -06:00
|
|
|
var cm *CacheMap
|
2023-12-19 10:58:31 -07:00
|
|
|
rl := ifce.lightHouse.Query(vpnIp)
|
2021-04-15 08:12:21 -06:00
|
|
|
if rl != nil {
|
|
|
|
cm = rl.CopyCache()
|
|
|
|
}
|
|
|
|
return json.NewEncoder(w.GetWriter()).Encode(cm)
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func sshCloseTunnel(ifce *Interface, fs interface{}, a []string, w sshd.StringWriter) error {
|
|
|
|
flags, ok := fs.(*sshCloseTunnelFlags)
|
|
|
|
if !ok {
|
|
|
|
//TODO: error
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(a) == 0 {
|
|
|
|
return w.WriteLine("No vpn ip was provided")
|
|
|
|
}
|
|
|
|
|
2020-09-18 08:10:25 -06:00
|
|
|
parsedIp := net.ParseIP(a[0])
|
|
|
|
if parsedIp == nil {
|
|
|
|
return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0]))
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
vpnIp := iputil.Ip2VpnIp(parsedIp)
|
2019-11-19 10:00:20 -07:00
|
|
|
if vpnIp == 0 {
|
|
|
|
return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0]))
|
|
|
|
}
|
|
|
|
|
2023-07-24 11:37:52 -06:00
|
|
|
hostInfo := ifce.hostMap.QueryVpnIp(vpnIp)
|
|
|
|
if hostInfo == nil {
|
2019-11-19 10:00:20 -07:00
|
|
|
return w.WriteLine(fmt.Sprintf("Could not find tunnel for vpn ip: %v", a[0]))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !flags.LocalOnly {
|
|
|
|
ifce.send(
|
2021-11-03 19:54:04 -06:00
|
|
|
header.CloseTunnel,
|
2019-11-19 10:00:20 -07:00
|
|
|
0,
|
|
|
|
hostInfo.ConnectionState,
|
|
|
|
hostInfo,
|
|
|
|
[]byte{},
|
|
|
|
make([]byte, 12, 12),
|
|
|
|
make([]byte, mtu),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-21 12:35:23 -06:00
|
|
|
ifce.closeTunnel(hostInfo)
|
2019-11-19 10:00:20 -07:00
|
|
|
return w.WriteLine("Closed")
|
|
|
|
}
|
|
|
|
|
|
|
|
func sshCreateTunnel(ifce *Interface, fs interface{}, a []string, w sshd.StringWriter) error {
|
|
|
|
flags, ok := fs.(*sshCreateTunnelFlags)
|
|
|
|
if !ok {
|
|
|
|
//TODO: error
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(a) == 0 {
|
|
|
|
return w.WriteLine("No vpn ip was provided")
|
|
|
|
}
|
|
|
|
|
2020-09-18 08:10:25 -06:00
|
|
|
parsedIp := net.ParseIP(a[0])
|
|
|
|
if parsedIp == nil {
|
|
|
|
return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0]))
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
vpnIp := iputil.Ip2VpnIp(parsedIp)
|
2019-11-19 10:00:20 -07:00
|
|
|
if vpnIp == 0 {
|
|
|
|
return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0]))
|
|
|
|
}
|
|
|
|
|
2023-07-24 11:37:52 -06:00
|
|
|
hostInfo := ifce.hostMap.QueryVpnIp(vpnIp)
|
2019-11-19 10:00:20 -07:00
|
|
|
if hostInfo != nil {
|
|
|
|
return w.WriteLine(fmt.Sprintf("Tunnel already exists"))
|
|
|
|
}
|
|
|
|
|
2023-07-24 11:37:52 -06:00
|
|
|
hostInfo = ifce.handshakeManager.QueryVpnIp(vpnIp)
|
2019-11-19 10:00:20 -07:00
|
|
|
if hostInfo != nil {
|
|
|
|
return w.WriteLine(fmt.Sprintf("Tunnel already handshaking"))
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
var addr *udp.Addr
|
2019-11-19 10:00:20 -07:00
|
|
|
if flags.Address != "" {
|
2021-11-03 19:54:04 -06:00
|
|
|
addr = udp.NewAddrFromString(flags.Address)
|
2019-11-19 10:00:20 -07:00
|
|
|
if addr == nil {
|
|
|
|
return w.WriteLine("Address could not be parsed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-21 17:51:45 -06:00
|
|
|
hostInfo = ifce.handshakeManager.StartHandshake(vpnIp, nil)
|
2019-11-19 10:00:20 -07:00
|
|
|
if addr != nil {
|
2021-03-18 19:37:24 -06:00
|
|
|
hostInfo.SetRemote(addr)
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return w.WriteLine("Created")
|
|
|
|
}
|
|
|
|
|
|
|
|
func sshChangeRemote(ifce *Interface, fs interface{}, a []string, w sshd.StringWriter) error {
|
|
|
|
flags, ok := fs.(*sshChangeRemoteFlags)
|
|
|
|
if !ok {
|
|
|
|
//TODO: error
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(a) == 0 {
|
|
|
|
return w.WriteLine("No vpn ip was provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
if flags.Address == "" {
|
|
|
|
return w.WriteLine("No address was provided")
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
addr := udp.NewAddrFromString(flags.Address)
|
2019-11-19 10:00:20 -07:00
|
|
|
if addr == nil {
|
|
|
|
return w.WriteLine("Address could not be parsed")
|
|
|
|
}
|
|
|
|
|
2020-09-18 08:10:25 -06:00
|
|
|
parsedIp := net.ParseIP(a[0])
|
|
|
|
if parsedIp == nil {
|
|
|
|
return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0]))
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
vpnIp := iputil.Ip2VpnIp(parsedIp)
|
2019-11-19 10:00:20 -07:00
|
|
|
if vpnIp == 0 {
|
|
|
|
return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0]))
|
|
|
|
}
|
|
|
|
|
2023-07-24 11:37:52 -06:00
|
|
|
hostInfo := ifce.hostMap.QueryVpnIp(vpnIp)
|
|
|
|
if hostInfo == nil {
|
2019-11-19 10:00:20 -07:00
|
|
|
return w.WriteLine(fmt.Sprintf("Could not find tunnel for vpn ip: %v", a[0]))
|
|
|
|
}
|
|
|
|
|
2021-03-18 19:37:24 -06:00
|
|
|
hostInfo.SetRemote(addr)
|
2019-11-19 10:00:20 -07:00
|
|
|
return w.WriteLine("Changed")
|
|
|
|
}
|
|
|
|
|
|
|
|
func sshGetHeapProfile(fs interface{}, a []string, w sshd.StringWriter) error {
|
|
|
|
if len(a) == 0 {
|
|
|
|
return w.WriteLine("No path to write profile provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Create(a[0])
|
|
|
|
if err != nil {
|
|
|
|
err = w.WriteLine(fmt.Sprintf("Unable to create profile file: %s", err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pprof.WriteHeapProfile(file)
|
|
|
|
if err != nil {
|
|
|
|
err = w.WriteLine(fmt.Sprintf("Unable to write profile: %s", err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = w.WriteLine(fmt.Sprintf("Mem profile created at %s", a))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-29 13:30:28 -06:00
|
|
|
func sshMutexProfileFraction(fs interface{}, a []string, w sshd.StringWriter) error {
|
|
|
|
if len(a) == 0 {
|
|
|
|
rate := runtime.SetMutexProfileFraction(-1)
|
|
|
|
return w.WriteLine(fmt.Sprintf("Current value: %d", rate))
|
|
|
|
}
|
|
|
|
|
|
|
|
newRate, err := strconv.Atoi(a[0])
|
|
|
|
if err != nil {
|
|
|
|
return w.WriteLine(fmt.Sprintf("Invalid argument: %s", a[0]))
|
|
|
|
}
|
|
|
|
|
|
|
|
oldRate := runtime.SetMutexProfileFraction(newRate)
|
|
|
|
return w.WriteLine(fmt.Sprintf("New value: %d. Old value: %d", newRate, oldRate))
|
|
|
|
}
|
|
|
|
|
|
|
|
func sshGetMutexProfile(fs interface{}, a []string, w sshd.StringWriter) error {
|
|
|
|
if len(a) == 0 {
|
|
|
|
return w.WriteLine("No path to write profile provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Create(a[0])
|
|
|
|
if err != nil {
|
|
|
|
return w.WriteLine(fmt.Sprintf("Unable to create profile file: %s", err))
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
mutexProfile := pprof.Lookup("mutex")
|
|
|
|
if mutexProfile == nil {
|
|
|
|
return w.WriteLine("Unable to get pprof.Lookup(\"mutex\")")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = mutexProfile.WriteTo(file, 0)
|
|
|
|
if err != nil {
|
|
|
|
return w.WriteLine(fmt.Sprintf("Unable to write profile: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return w.WriteLine(fmt.Sprintf("Mutex profile created at %s", a))
|
|
|
|
}
|
|
|
|
|
2021-03-26 08:46:30 -06:00
|
|
|
func sshLogLevel(l *logrus.Logger, fs interface{}, a []string, w sshd.StringWriter) error {
|
2019-11-19 10:00:20 -07:00
|
|
|
if len(a) == 0 {
|
|
|
|
return w.WriteLine(fmt.Sprintf("Log level is: %s", l.Level))
|
|
|
|
}
|
|
|
|
|
|
|
|
level, err := logrus.ParseLevel(a[0])
|
|
|
|
if err != nil {
|
|
|
|
return w.WriteLine(fmt.Sprintf("Unknown log level %s. Possible log levels: %s", a, logrus.AllLevels))
|
|
|
|
}
|
|
|
|
|
|
|
|
l.SetLevel(level)
|
|
|
|
return w.WriteLine(fmt.Sprintf("Log level is: %s", l.Level))
|
|
|
|
}
|
|
|
|
|
2021-03-26 08:46:30 -06:00
|
|
|
func sshLogFormat(l *logrus.Logger, fs interface{}, a []string, w sshd.StringWriter) error {
|
2019-11-19 10:00:20 -07:00
|
|
|
if len(a) == 0 {
|
|
|
|
return w.WriteLine(fmt.Sprintf("Log format is: %s", reflect.TypeOf(l.Formatter)))
|
|
|
|
}
|
|
|
|
|
|
|
|
logFormat := strings.ToLower(a[0])
|
|
|
|
switch logFormat {
|
|
|
|
case "text":
|
|
|
|
l.Formatter = &logrus.TextFormatter{}
|
|
|
|
case "json":
|
|
|
|
l.Formatter = &logrus.JSONFormatter{}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown log format `%s`. possible formats: %s", logFormat, []string{"text", "json"})
|
|
|
|
}
|
|
|
|
|
|
|
|
return w.WriteLine(fmt.Sprintf("Log format is: %s", reflect.TypeOf(l.Formatter)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func sshPrintCert(ifce *Interface, fs interface{}, a []string, w sshd.StringWriter) error {
|
|
|
|
args, ok := fs.(*sshPrintCertFlags)
|
|
|
|
if !ok {
|
|
|
|
//TODO: error
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-14 20:32:40 -06:00
|
|
|
cert := ifce.pki.GetCertState().Certificate
|
2019-11-19 10:00:20 -07:00
|
|
|
if len(a) > 0 {
|
2020-09-18 08:10:25 -06:00
|
|
|
parsedIp := net.ParseIP(a[0])
|
|
|
|
if parsedIp == nil {
|
|
|
|
return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0]))
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
vpnIp := iputil.Ip2VpnIp(parsedIp)
|
2019-11-19 10:00:20 -07:00
|
|
|
if vpnIp == 0 {
|
|
|
|
return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0]))
|
|
|
|
}
|
|
|
|
|
2023-07-24 11:37:52 -06:00
|
|
|
hostInfo := ifce.hostMap.QueryVpnIp(vpnIp)
|
|
|
|
if hostInfo == nil {
|
2019-11-19 10:00:20 -07:00
|
|
|
return w.WriteLine(fmt.Sprintf("Could not find tunnel for vpn ip: %v", a[0]))
|
|
|
|
}
|
|
|
|
|
|
|
|
cert = hostInfo.GetCert()
|
|
|
|
}
|
|
|
|
|
|
|
|
if args.Json || args.Pretty {
|
|
|
|
b, err := cert.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
//TODO: handle it
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if args.Pretty {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
err := json.Indent(buf, b, "", " ")
|
|
|
|
b = buf.Bytes()
|
|
|
|
if err != nil {
|
|
|
|
//TODO: handle it
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return w.WriteBytes(b)
|
|
|
|
}
|
|
|
|
|
2021-06-07 16:06:59 -06:00
|
|
|
if args.Raw {
|
|
|
|
b, err := cert.MarshalToPEM()
|
|
|
|
if err != nil {
|
|
|
|
//TODO: handle it
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return w.WriteBytes(b)
|
|
|
|
}
|
|
|
|
|
2019-11-19 10:00:20 -07:00
|
|
|
return w.WriteLine(cert.String())
|
|
|
|
}
|
|
|
|
|
2022-06-21 12:35:23 -06:00
|
|
|
func sshPrintRelays(ifce *Interface, fs interface{}, a []string, w sshd.StringWriter) error {
|
|
|
|
args, ok := fs.(*sshPrintTunnelFlags)
|
|
|
|
if !ok {
|
|
|
|
//TODO: error
|
|
|
|
w.WriteLine(fmt.Sprintf("sshPrintRelays failed to convert args type"))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
relays := map[uint32]*HostInfo{}
|
|
|
|
ifce.hostMap.Lock()
|
|
|
|
for k, v := range ifce.hostMap.Relays {
|
|
|
|
relays[k] = v
|
|
|
|
}
|
|
|
|
ifce.hostMap.Unlock()
|
|
|
|
|
|
|
|
type RelayFor struct {
|
|
|
|
Error error
|
|
|
|
Type string
|
|
|
|
State string
|
|
|
|
PeerIp iputil.VpnIp
|
|
|
|
LocalIndex uint32
|
|
|
|
RemoteIndex uint32
|
|
|
|
RelayedThrough []iputil.VpnIp
|
|
|
|
}
|
|
|
|
|
|
|
|
type RelayOutput struct {
|
|
|
|
NebulaIp iputil.VpnIp
|
|
|
|
RelayForIps []RelayFor
|
|
|
|
}
|
|
|
|
|
|
|
|
type CmdOutput struct {
|
|
|
|
Relays []*RelayOutput
|
|
|
|
}
|
|
|
|
|
|
|
|
co := CmdOutput{}
|
|
|
|
|
|
|
|
enc := json.NewEncoder(w.GetWriter())
|
|
|
|
|
|
|
|
if args.Pretty {
|
|
|
|
enc.SetIndent("", " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range relays {
|
|
|
|
ro := RelayOutput{NebulaIp: v.vpnIp}
|
|
|
|
co.Relays = append(co.Relays, &ro)
|
2023-07-24 11:37:52 -06:00
|
|
|
relayHI := ifce.hostMap.QueryVpnIp(v.vpnIp)
|
|
|
|
if relayHI == nil {
|
|
|
|
ro.RelayForIps = append(ro.RelayForIps, RelayFor{Error: errors.New("could not find hostinfo")})
|
2022-06-21 12:35:23 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, vpnIp := range relayHI.relayState.CopyRelayForIps() {
|
|
|
|
rf := RelayFor{Error: nil}
|
|
|
|
r, ok := relayHI.relayState.GetRelayForByIp(vpnIp)
|
|
|
|
if ok {
|
|
|
|
t := ""
|
|
|
|
switch r.Type {
|
|
|
|
case ForwardingType:
|
|
|
|
t = "forwarding"
|
|
|
|
case TerminalType:
|
|
|
|
t = "terminal"
|
|
|
|
default:
|
2022-12-19 10:28:27 -07:00
|
|
|
t = "unknown"
|
2022-06-21 12:35:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
s := ""
|
|
|
|
switch r.State {
|
|
|
|
case Requested:
|
|
|
|
s = "requested"
|
|
|
|
case Established:
|
|
|
|
s = "established"
|
|
|
|
default:
|
|
|
|
s = "unknown"
|
|
|
|
}
|
|
|
|
|
|
|
|
rf.LocalIndex = r.LocalIndex
|
|
|
|
rf.RemoteIndex = r.RemoteIndex
|
|
|
|
rf.PeerIp = r.PeerIp
|
|
|
|
rf.Type = t
|
|
|
|
rf.State = s
|
|
|
|
if rf.LocalIndex != k {
|
|
|
|
rf.Error = fmt.Errorf("hostmap LocalIndex '%v' does not match RelayState LocalIndex", k)
|
|
|
|
}
|
|
|
|
}
|
2023-07-24 11:37:52 -06:00
|
|
|
relayedHI := ifce.hostMap.QueryVpnIp(vpnIp)
|
|
|
|
if relayedHI != nil {
|
2022-06-21 12:35:23 -06:00
|
|
|
rf.RelayedThrough = append(rf.RelayedThrough, relayedHI.relayState.CopyRelayIps()...)
|
|
|
|
}
|
|
|
|
|
|
|
|
ro.RelayForIps = append(ro.RelayForIps, rf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err := enc.Encode(co)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-19 10:00:20 -07:00
|
|
|
func sshPrintTunnel(ifce *Interface, fs interface{}, a []string, w sshd.StringWriter) error {
|
|
|
|
args, ok := fs.(*sshPrintTunnelFlags)
|
|
|
|
if !ok {
|
|
|
|
//TODO: error
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(a) == 0 {
|
|
|
|
return w.WriteLine("No vpn ip was provided")
|
|
|
|
}
|
|
|
|
|
2020-09-18 08:10:25 -06:00
|
|
|
parsedIp := net.ParseIP(a[0])
|
|
|
|
if parsedIp == nil {
|
|
|
|
return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0]))
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
vpnIp := iputil.Ip2VpnIp(parsedIp)
|
2019-11-19 10:00:20 -07:00
|
|
|
if vpnIp == 0 {
|
|
|
|
return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0]))
|
|
|
|
}
|
|
|
|
|
2023-07-24 11:37:52 -06:00
|
|
|
hostInfo := ifce.hostMap.QueryVpnIp(vpnIp)
|
|
|
|
if hostInfo == nil {
|
2019-11-19 10:00:20 -07:00
|
|
|
return w.WriteLine(fmt.Sprintf("Could not find tunnel for vpn ip: %v", a[0]))
|
|
|
|
}
|
|
|
|
|
|
|
|
enc := json.NewEncoder(w.GetWriter())
|
|
|
|
if args.Pretty {
|
|
|
|
enc.SetIndent("", " ")
|
|
|
|
}
|
|
|
|
|
2024-04-03 21:14:51 -06:00
|
|
|
return enc.Encode(copyHostInfo(hostInfo, ifce.hostMap.GetPreferredRanges()))
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2022-08-08 11:44:09 -06:00
|
|
|
func sshReload(c *config.C, w sshd.StringWriter) error {
|
|
|
|
err := w.WriteLine("Reloading config")
|
|
|
|
c.ReloadConfig()
|
|
|
|
return err
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|