|
1 | 1 | package models |
2 | 2 |
|
3 | | -import ( |
4 | | - "encoding/json" |
5 | | - "errors" |
6 | | - "fmt" |
7 | | - "os" |
8 | | - |
9 | | - "github.com/tech-thinker/telepath/constants" |
10 | | -) |
11 | | - |
12 | 3 | type Config struct { |
13 | | - Hosts map[string]HostConfig `json:"hosts"` |
14 | | - Credientials map[string]Crediential `json:"credientials"` |
15 | | - Tunnels map[string]Tunnel `json:"tunnels"` |
16 | | -} |
17 | | - |
18 | | -type Tunnel struct { |
19 | | - Name string `json:"name"` |
20 | | - LocalPort int `json:"localPort"` |
21 | | - RemoteHost string `json:"remoteHost"` |
22 | | - RemotePort int `json:"remotePort"` |
23 | | - HostChain []string `json:"hostChain"` |
24 | | -} |
25 | | - |
26 | | -func (t *Tunnel) ToByte() []byte { |
27 | | - data, _ := json.Marshal(t) |
28 | | - return data |
29 | | -} |
30 | | - |
31 | | -func (t *Tunnel) Validate(hosts map[string]HostConfig) (bool, error) { |
32 | | - if len(t.Name) == 0 { |
33 | | - return false, errors.New("Tunnel name must not be empty.") |
34 | | - } |
35 | | - |
36 | | - if t.LocalPort <= 0 { |
37 | | - return false, errors.New("Local port invalid.") |
38 | | - } |
39 | | - |
40 | | - if len(t.RemoteHost) == 0 { |
41 | | - return false, errors.New("Remote host must not be empty.") |
42 | | - } |
43 | | - |
44 | | - if t.RemotePort <= 0 { |
45 | | - return false, errors.New("Remote port invalid.") |
46 | | - } |
47 | | - |
48 | | - if len(t.HostChain) == 0 { |
49 | | - return false, errors.New("Host chain must not be empty.") |
50 | | - } |
51 | | - |
52 | | - for _, host := range t.HostChain { |
53 | | - _, ok := hosts[host] |
54 | | - if !ok { |
55 | | - return false, errors.New("Host doesn't exists.") |
56 | | - } |
57 | | - } |
58 | | - |
59 | | - return true, nil |
60 | | -} |
61 | | - |
62 | | -type HostConfig struct { |
63 | | - Name string `json:"name"` |
64 | | - Host string `json:"host"` |
65 | | - Port int `json:"port"` |
66 | | - User string `json:"user"` |
67 | | - CredientialName string `json:"credientialName"` |
68 | | -} |
69 | | - |
70 | | -func (h *HostConfig) ToByte() []byte { |
71 | | - data, _ := json.Marshal(h) |
72 | | - return data |
73 | | -} |
74 | | - |
75 | | -func (h *HostConfig) Validate(creds map[string]Crediential) (bool, error) { |
76 | | - if len(h.Name) == 0 { |
77 | | - return false, errors.New("Host name must not be empty.") |
78 | | - } |
79 | | - |
80 | | - if len(h.Host) == 0 { |
81 | | - return false, errors.New("Host address must not be empty.") |
82 | | - } |
83 | | - |
84 | | - if h.Port <= 0 { |
85 | | - return false, errors.New("Host port invalid.") |
86 | | - } |
87 | | - |
88 | | - if len(h.User) == 0 { |
89 | | - return false, errors.New("Host user must not be empty.") |
90 | | - } |
91 | | - |
92 | | - if len(h.CredientialName) == 0 { |
93 | | - return false, errors.New("Host crediential name must not be empty.") |
94 | | - } |
95 | | - |
96 | | - _, ok := creds[h.CredientialName] |
97 | | - if !ok { |
98 | | - return false, errors.New("Host crediential doesn't exists.") |
99 | | - } |
100 | | - |
101 | | - return true, nil |
102 | | -} |
103 | | - |
104 | | -type Crediential struct { |
105 | | - Name string `json:"name"` |
106 | | - Type string `json:"type"` // Password or SSH_KEY_FILE |
107 | | - Password string `json:"password"` |
108 | | - KeyFile string `json:"keyFile"` |
109 | | -} |
110 | | - |
111 | | -func (c *Crediential) ToByte() []byte { |
112 | | - data, _ := json.Marshal(c) |
113 | | - return data |
114 | | -} |
115 | | - |
116 | | -func (c *Crediential) Validate() (bool, error) { |
117 | | - if len(c.Name) == 0 { |
118 | | - return false, errors.New("Crediential name must not be empty.") |
119 | | - } |
120 | | - |
121 | | - if c.Type != constants.CREDIENTIAL_PASS && c.Type != constants.CREDIENTIAL_KEY { |
122 | | - return false, errors.New("Crediential type must be PASS or KEY.") |
123 | | - } |
124 | | - |
125 | | - if c.Type == constants.CREDIENTIAL_PASS && len(c.Password) == 0 { |
126 | | - return false, errors.New("Crediential password must not be empty.") |
127 | | - } |
128 | | - |
129 | | - if c.Type == constants.CREDIENTIAL_KEY && len(c.KeyFile) == 0 { |
130 | | - return false, errors.New("Crediential key file path must not be empty.") |
131 | | - } |
132 | | - |
133 | | - if c.Type == constants.CREDIENTIAL_KEY && len(c.KeyFile) > 0 { |
134 | | - if _, err := os.Stat(c.KeyFile); os.IsNotExist(err) { |
135 | | - return false, fmt.Errorf("crediential key file '%s' does not exist", c.KeyFile) |
136 | | - } |
137 | | - } |
138 | | - return true, nil |
| 4 | + Hosts map[string]HostConfig `json:"hosts,omitempty"` |
| 5 | + Credientials map[string]Crediential `json:"credientials,omitempty"` |
| 6 | + Tunnels map[string]Tunnel `json:"tunnels,omitempty"` |
139 | 7 | } |
0 commit comments