-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathsshd.go
133 lines (109 loc) · 2.91 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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/elfgzp/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
}
// NewTerminal NewTerminal
func NewTerminal(server *config.Server, sshUser *config.SSHUser, sess *ssh.Session) error {
upstreamClient, err := NewSSHClient(server, sshUser)
if err != nil {
return nil
}
upstreamSess, err := upstreamClient.NewSession()
if err != nil {
return nil
}
defer upstreamSess.Close()
upstreamSess.Stdout = *sess
upstreamSess.Stdin = *sess
upstreamSess.Stderr = *sess
pty, winCh, _ := (*sess).Pty()
if err := upstreamSess.RequestPty(pty.Term, pty.Window.Height, pty.Window.Width, pty.TerminalModes); err != nil {
return err
}
if err := upstreamSess.Shell(); err != nil {
return err
}
go func () {
for win := range winCh {
upstreamSess.WindowChange(win.Height, win.Width)
}
}()
if err := upstreamSess.Wait(); 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)
}