Skip to content

Commit 94147e0

Browse files
committed
restructure model
1 parent 290f53b commit 94147e0

File tree

6 files changed

+161
-144
lines changed

6 files changed

+161
-144
lines changed

cmd/hostCmd.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ func (a *app) Host() *cli.Command {
3030
Usage: "SSH server port.",
3131
},
3232
&cli.StringFlag{
33-
Name: "user",
34-
Aliases: []string{"U"},
35-
Usage: "SSH server user name.",
33+
Name: "user",
34+
Aliases: []string{"U"},
35+
Usage: "SSH server user name.",
36+
Required: true,
3637
},
3738
&cli.StringFlag{
38-
Name: "cred",
39-
Aliases: []string{"C"},
40-
Usage: "Crediential name.",
39+
Name: "cred",
40+
Aliases: []string{"C"},
41+
Usage: "Crediential name.",
42+
Required: true,
4143
},
4244
},
4345
Action: func(c *cli.Context) error {

cmd/tunnelCmd.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ func (a *app) Tunnel() *cli.Command {
3131
Usage: "Remote host.",
3232
},
3333
&cli.IntFlag{
34-
Name: "remote-port",
35-
Aliases: []string{"R"},
36-
Usage: "Remote port.",
34+
Name: "remote-port",
35+
Aliases: []string{"R"},
36+
Usage: "Remote port.",
37+
Required: true,
3738
},
3839
&cli.StringFlag{
3940
Name: "host-chain",

models/config.go

Lines changed: 3 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1,139 +1,7 @@
11
package models
22

3-
import (
4-
"encoding/json"
5-
"errors"
6-
"fmt"
7-
"os"
8-
9-
"github.com/tech-thinker/telepath/constants"
10-
)
11-
123
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"`
1397
}

models/crediential.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package models
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
"os"
8+
9+
"github.com/tech-thinker/telepath/constants"
10+
)
11+
12+
type Crediential struct {
13+
Name string `json:"name,omitempty"`
14+
Type string `json:"type,omitempty"` // Password or SSH_KEY_FILE
15+
Password string `json:"password,omitempty"`
16+
KeyFile string `json:"keyFile,omitempty"`
17+
Passphrase string `json:"passphrase,omitempty"`
18+
}
19+
20+
func (c *Crediential) ToByte() []byte {
21+
data, _ := json.Marshal(c)
22+
return data
23+
}
24+
25+
func (c *Crediential) Validate() (bool, error) {
26+
if len(c.Name) == 0 {
27+
return false, errors.New("Crediential name must not be empty.")
28+
}
29+
30+
if c.Type != constants.CREDIENTIAL_PASS && c.Type != constants.CREDIENTIAL_KEY {
31+
return false, errors.New("Crediential type must be PASS or KEY.")
32+
}
33+
34+
if c.Type == constants.CREDIENTIAL_PASS && len(c.Password) == 0 {
35+
return false, errors.New("Crediential password must not be empty.")
36+
}
37+
38+
if c.Type == constants.CREDIENTIAL_KEY && len(c.KeyFile) == 0 {
39+
return false, errors.New("Crediential key file path must not be empty.")
40+
}
41+
42+
if c.Type == constants.CREDIENTIAL_KEY && len(c.KeyFile) > 0 {
43+
if _, err := os.Stat(c.KeyFile); os.IsNotExist(err) {
44+
return false, fmt.Errorf("crediential key file '%s' does not exist", c.KeyFile)
45+
}
46+
}
47+
return true, nil
48+
}

models/host.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package models
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
)
7+
8+
type HostConfig struct {
9+
Name string `json:"name,omitempty"`
10+
Host string `json:"host,omitempty"`
11+
Port int `json:"port,omitempty"`
12+
User string `json:"user,omitempty"`
13+
CredientialName string `json:"credientialName,omitempty"`
14+
}
15+
16+
func (h *HostConfig) ToByte() []byte {
17+
data, _ := json.Marshal(h)
18+
return data
19+
}
20+
21+
func (h *HostConfig) Validate(creds map[string]Crediential) (bool, error) {
22+
if len(h.Name) == 0 {
23+
return false, errors.New("Host name must not be empty.")
24+
}
25+
26+
if len(h.Host) == 0 {
27+
return false, errors.New("Host address must not be empty.")
28+
}
29+
30+
if h.Port <= 0 {
31+
return false, errors.New("Host port invalid.")
32+
}
33+
34+
if len(h.User) == 0 {
35+
return false, errors.New("Host user must not be empty.")
36+
}
37+
38+
if len(h.CredientialName) == 0 {
39+
return false, errors.New("Host crediential name must not be empty.")
40+
}
41+
42+
_, ok := creds[h.CredientialName]
43+
if !ok {
44+
return false, errors.New("Host crediential doesn't exists.")
45+
}
46+
47+
return true, nil
48+
}

models/tunnel.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package models
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
)
7+
8+
type Tunnel struct {
9+
Name string `json:"name,omitempty"`
10+
LocalPort int `json:"localPort,omitempty"`
11+
RemoteHost string `json:"remoteHost,omitempty"`
12+
RemotePort int `json:"remotePort,omitempty"`
13+
HostChain []string `json:"hostChain,omitempty"`
14+
}
15+
16+
func (t *Tunnel) ToByte() []byte {
17+
data, _ := json.Marshal(t)
18+
return data
19+
}
20+
21+
func (t *Tunnel) Validate(hosts map[string]HostConfig) (bool, error) {
22+
if len(t.Name) == 0 {
23+
return false, errors.New("Tunnel name must not be empty.")
24+
}
25+
26+
if t.LocalPort <= 0 {
27+
return false, errors.New("Local port invalid.")
28+
}
29+
30+
if len(t.RemoteHost) == 0 {
31+
return false, errors.New("Remote host must not be empty.")
32+
}
33+
34+
if t.RemotePort <= 0 {
35+
return false, errors.New("Remote port invalid.")
36+
}
37+
38+
if len(t.HostChain) == 0 {
39+
return false, errors.New("Host chain must not be empty.")
40+
}
41+
42+
for _, host := range t.HostChain {
43+
_, ok := hosts[host]
44+
if !ok {
45+
return false, errors.New("Host doesn't exists.")
46+
}
47+
}
48+
49+
return true, nil
50+
}

0 commit comments

Comments
 (0)