-
Notifications
You must be signed in to change notification settings - Fork 59
/
sshd.go
116 lines (96 loc) · 2.56 KB
/
sshd.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package sshd
import (
"errors"
"fmt"
"io/ioutil"
"net"
"strings"
"github.com/TNK-Studio/gortal/config"
"github.com/TNK-Studio/gortal/utils"
"github.com/TNK-Studio/gortal/utils/logger"
"github.com/fatih/color"
"github.com/gliderlabs/ssh"
"github.com/helloyi/go-sshclient"
gossh "golang.org/x/crypto/ssh"
)
// GetClientByPasswd GetClientByPasswd
func GetClientByPasswd(username, host string, port int, passwd string) (*sshclient.Client, error) {
client, err := sshclient.DialWithPasswd(
fmt.Sprintf("%s:%d", host, port),
username,
passwd,
)
if err != nil {
return nil, err
}
return client, nil
}
// Connect connect server
func Connect(host string, port int, username string, privKeyFile string, sess *ssh.Session) error {
client, err := sshclient.DialWithKey(
fmt.Sprintf("%s:%d", host, port),
username,
utils.FilePath(privKeyFile),
)
if err != nil {
return err
}
// default terminal
terminal := client.Terminal(nil)
terminal = terminal.SetStdio(*sess, *sess, *sess)
if terminal.Start(); err != nil {
return err
}
return nil
}
// NewSSHClient NewSSHClient
func NewSSHClient(server *config.Server, sshUser *config.SSHUser) (*gossh.Client, error) {
if !utils.FileExited(sshUser.IdentityFile) {
return nil, errors.New("Jumpserver can not find the identity file of the target server. ")
}
key, err := ioutil.ReadFile(utils.FilePath(sshUser.IdentityFile))
if err != nil {
logger.Logger.Error(err)
return nil, err
}
signer, err := gossh.ParsePrivateKey(key)
if err != nil {
logger.Logger.Error(err)
return nil, err
}
config := &gossh.ClientConfig{
User: sshUser.SSHUsername,
Auth: []gossh.AuthMethod{
gossh.PublicKeys(signer),
},
HostKeyCallback: gossh.HostKeyCallback(func(hostname string, remote net.Addr, key gossh.PublicKey) error { return nil }),
}
addr := fmt.Sprintf("%s:%d", server.Host, server.Port)
client, err := gossh.Dial("tcp", addr, config)
if err != nil {
logger.Logger.Error(err)
return nil, err
}
return client, nil
}
// ParseRawCommand ParseRawCommand
func ParseRawCommand(command string) (string, []string, error) {
parts := strings.Split(command, " ")
if len(parts) < 1 {
return "", nil, errors.New("No command in payload: " + command)
}
if len(parts) < 2 {
return parts[0], []string{}, nil
}
return parts[0], parts[1:], nil
}
// ErrorInfo ErrorInfo
func ErrorInfo(err error, sess *ssh.Session) {
read := color.New(color.FgRed)
read.Fprint(*sess, fmt.Sprintf("%s\n", err))
}
// Info Info
func Info(msg string, sess *ssh.Session) {
green := color.New(color.FgGreen)
green.Fprint(*sess, msg)
}