-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
executable file
·92 lines (75 loc) · 2.28 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"io/ioutil"
"log"
"os"
"github.com/jedib0t/go-pretty/v6/table"
"golang.org/x/crypto/ssh"
"gopkg.in/yaml.v2"
)
var tunnelRadarConfig TunnelRadarConfig
type TunnelRadarConfig struct {
CliServerHost string `yaml:"cliServerHost"`
CliServerPort int `yaml:"cliServerPort"`
Tunnels map[string]*Tunnel `yaml:"tunnels"`
}
// LoadConfig - Load the configuration from ./config.yml
func loadConfig(configPath string) {
f, err := os.Open(configPath)
if err != nil {
log.Fatalln(err)
os.Exit(1)
}
defer f.Close()
decoder := yaml.NewDecoder(f)
err = decoder.Decode(&tunnelRadarConfig)
if err != nil {
log.Fatalln(err)
os.Exit(1)
}
// Set the Host and Port for the CLI
if tunnelRadarConfig.CliServerHost == "" {
tunnelRadarConfig.CliServerHost = "127.0.0.1"
}
if tunnelRadarConfig.CliServerPort == 0 {
tunnelRadarConfig.CliServerPort = 7779
}
// Configure clients
for alias, tunnel := range tunnelRadarConfig.Tunnels {
// Set the Alias
tunnel.Alias = alias
// Set the Authentication method
var authMethod ssh.AuthMethod
if tunnel.Auth.Password != "" {
authMethod = ssh.Password(tunnel.Auth.Password)
} else if tunnel.Auth.Key != "" {
buffer, err := ioutil.ReadFile(tunnel.Auth.Key)
if err != nil {
log.Fatalf("An error occurred loading the SSH key %s. Error: %s\r\n", tunnel.Auth.Key, err)
}
key, err := ssh.ParsePrivateKey(buffer)
if err != nil {
log.Fatalf("An error occurred loading the SSH key %s. Error: %s\r\n", tunnel.Auth.Key, err)
}
authMethod = ssh.PublicKeys(key)
} else {
log.Fatalf("You must specify an authentication method for %s\r\n", tunnel.Alias)
}
// Set the ClientConfig
tunnel.ClientConfig = &ssh.ClientConfig{
User: tunnel.Auth.User,
Auth: []ssh.AuthMethod{authMethod},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
}
}
func (config *TunnelRadarConfig) list() (list string, err error) {
t := table.NewWriter()
t.AppendHeader(table.Row{"Alias", "Listen", "Remote", "Destination", "Status"})
for alias, tunnel := range tunnelRadarConfig.Tunnels {
t.AppendRow([]interface{}{alias, tunnel.Source, tunnel.Remote, tunnel.Destination, tunnel.Status})
}
t.SetStyle(table.StyleColoredBright)
list = t.Render()
return list, nil
}