Skip to content

Commit d7d53d6

Browse files
committed
Add some features
Add terminal Improve single user management function
1 parent 7191735 commit d7d53d6

32 files changed

+5405
-3242
lines changed

go.mod

+1-16
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,23 @@ require (
77
github.com/Microsoft/hcsshim v0.8.22 // indirect
88
github.com/PuerkitoBio/goquery v1.7.0
99
github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46 // indirect
10-
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
11-
github.com/aquasecurity/libbpfgo v0.2.1-libbpf-0.4.0 // indirect
1210
github.com/bits-and-blooms/bitset v1.2.1 // indirect
1311
github.com/containerd/containerd v1.5.7
1412
github.com/containerd/continuity v0.2.0 // indirect
15-
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
1613
github.com/docker/docker v20.10.7+incompatible
1714
github.com/docker/go-connections v0.4.0
1815
github.com/forease/gotld v0.0.0-20190808124948-c50ff635576b
1916
github.com/gin-contrib/gzip v0.0.2 // indirect
2017
github.com/gin-gonic/gin v1.7.2
21-
github.com/go-delve/delve v1.7.2 // indirect
18+
github.com/gliderlabs/ssh v0.3.3
2219
github.com/go-ini/ini v1.62.0
2320
github.com/go-ole/go-ole v1.2.5 // indirect
24-
github.com/go-openapi/jsonreference v0.19.6 // indirect
2521
github.com/go-openapi/spec v0.20.4 // indirect
26-
github.com/go-openapi/swag v0.19.15 // indirect
2722
github.com/go-playground/validator/v10 v10.6.1 // indirect
2823
github.com/gogo/googleapis v1.4.1 // indirect
2924
github.com/golang-jwt/jwt v3.2.2+incompatible
3025
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
3126
github.com/gomodule/redigo v1.8.5
32-
github.com/google/go-dap v0.6.0 // indirect
3327
github.com/google/go-github/v36 v36.0.0
3428
github.com/google/uuid v1.3.0 // indirect
3529
github.com/gorilla/mux v1.8.0 // indirect
@@ -39,14 +33,10 @@ require (
3933
github.com/klauspost/compress v1.13.6 // indirect
4034
github.com/leodido/go-urn v1.2.1 // indirect
4135
github.com/mailru/easyjson v0.7.7 // indirect
42-
github.com/mattn/go-colorable v0.1.11 // indirect
4336
github.com/mattn/go-isatty v0.0.14 // indirect
44-
github.com/mattn/go-runewidth v0.0.13 // indirect
4537
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
4638
github.com/morikuni/aec v1.0.0 // indirect
47-
github.com/opencontainers/runc v1.0.2 // indirect
4839
github.com/opencontainers/selinux v1.8.5 // indirect
49-
github.com/peterh/liner v1.2.1 // indirect
5040
github.com/pkg/errors v0.9.1
5141
github.com/prestonTao/upnp v0.0.0-20150206124352-f4370df5e109
5242
github.com/prometheus/procfs v0.7.3 // indirect
@@ -56,23 +46,19 @@ require (
5646
github.com/sirupsen/logrus v1.8.1
5747
github.com/smartystreets/assertions v1.2.0 // indirect
5848
github.com/smartystreets/goconvey v1.6.4 // indirect
59-
github.com/spf13/cobra v1.2.1 // indirect
6049
github.com/swaggo/gin-swagger v1.3.0
6150
github.com/swaggo/swag v1.7.3
6251
github.com/tidwall/gjson v1.8.0
6352
github.com/tidwall/pretty v1.2.0 // indirect
6453
github.com/tklauser/go-sysconf v0.3.6 // indirect
6554
github.com/ugorji/go v1.2.6 // indirect
6655
go.opencensus.io v0.23.0 // indirect
67-
go.starlark.net v0.0.0-20210901212718-87f333178d59 // indirect
68-
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
6956
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5
7057
golang.org/x/mod v0.5.0 // indirect
7158
golang.org/x/net v0.0.0-20211020060615-d418f374d309 // indirect
7259
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f
7360
golang.org/x/sys v0.0.0-20211020174200-9d6173849985 // indirect
7461
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
75-
golang.org/x/text v0.3.7 // indirect
7662
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
7763
golang.org/x/tools v0.1.7 // indirect
7864
google.golang.org/appengine v1.6.7 // indirect
@@ -83,5 +69,4 @@ require (
8369
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
8470
gorm.io/driver/sqlite v1.1.5
8571
gorm.io/gorm v1.21.15
86-
src.techknowlogick.com/xgo v1.4.1-0.20211007230901-4fb1c2d7b2ab // indirect
8772
)

go.sum

+5-198
Large diffs are not rendered by default.

pkg/docker/helper.go

+163-8
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,35 @@ import (
44
"bytes"
55
json2 "encoding/json"
66
"fmt"
7-
"github.com/gorilla/websocket"
8-
"github.com/sirupsen/logrus"
9-
"golang.org/x/crypto/ssh"
107
"io"
118
"regexp"
129
"strconv"
1310
"sync"
1411
"time"
12+
13+
"github.com/gorilla/websocket"
14+
"github.com/sirupsen/logrus"
15+
"golang.org/x/crypto/ssh"
1516
)
1617

17-
func NewSshClient() (*ssh.Client, error) {
18+
func NewSshClient(user, password string) (*ssh.Client, error) {
19+
20+
// connet to ssh
21+
// addr = fmt.Sprintf("%s:%d", host, port)
22+
1823
config := &ssh.ClientConfig{
1924
Timeout: time.Second * 5,
20-
User: "root",
25+
User: user,
2126
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
2227
//HostKeyCallback: ,
2328
//HostKeyCallback: hostKeyCallBackFunc(h.Host),
2429
}
2530
//if h.Type == "password" {
26-
config.Auth = []ssh.AuthMethod{ssh.Password("123456")}
31+
config.Auth = []ssh.AuthMethod{ssh.Password(password)}
2732
//} else {
2833
// config.Auth = []ssh.AuthMethod{publicKeyAuthFunc(h.Key)}
2934
//}
30-
addr := fmt.Sprintf("%s:%d", "192.168.2.142", 22)
35+
addr := fmt.Sprintf("%s:%d", "127.0.0.1", 22)
3136
c, err := ssh.Dial("tcp", addr, config)
3237
if err != nil {
3338
return nil, err
@@ -98,6 +103,98 @@ const (
98103
wsMsgResize = "resize"
99104
)
100105

106+
//ReceiveWsMsg receive websocket msg do some handling then write into ssh.session.stdin
107+
func ReceiveWsMsgUser(wsConn *websocket.Conn, logBuff *bytes.Buffer) string {
108+
//tells other go routine quit
109+
username := ""
110+
for {
111+
112+
//read websocket msg
113+
_, wsData, err := wsConn.ReadMessage()
114+
if err != nil {
115+
116+
return ""
117+
}
118+
119+
msgObj := wsMsg{}
120+
if err := json2.Unmarshal(wsData, &msgObj); err != nil {
121+
msgObj.Type = "cmd"
122+
msgObj.Cmd = string(wsData)
123+
}
124+
//if err := json.Unmarshal(wsData, &msgObj); err != nil {
125+
// logrus.WithError(err).WithField("wsData", string(wsData)).Error("unmarshal websocket message failed")
126+
//}
127+
switch msgObj.Type {
128+
case wsMsgCmd:
129+
//handle xterm.js stdin
130+
//decodeBytes, err := base64.StdEncoding.DecodeString(msgObj.Cmd)
131+
decodeBytes := []byte(msgObj.Cmd)
132+
if msgObj.Cmd == "\u007f" {
133+
if len(username) == 0 {
134+
continue
135+
}
136+
wsConn.WriteMessage(websocket.TextMessage, []byte("\b\x1b[K"))
137+
username = username[:len(username)-1]
138+
continue
139+
}
140+
if msgObj.Cmd == "\r" {
141+
return username
142+
}
143+
username += msgObj.Cmd
144+
145+
if err := wsConn.WriteMessage(websocket.TextMessage, decodeBytes); err != nil {
146+
logrus.WithError(err).Error("ws cmd bytes write to ssh.stdin pipe failed")
147+
}
148+
//write input cmd to log buffer
149+
if _, err := logBuff.Write(decodeBytes); err != nil {
150+
logrus.WithError(err).Error("write received cmd into log buffer failed")
151+
}
152+
}
153+
154+
}
155+
}
156+
157+
func ReceiveWsMsgPassword(wsConn *websocket.Conn, logBuff *bytes.Buffer) string {
158+
//tells other go routine quit
159+
password := ""
160+
for {
161+
162+
//read websocket msg
163+
_, wsData, err := wsConn.ReadMessage()
164+
if err != nil {
165+
logrus.WithError(err).Error("reading webSocket message failed")
166+
return ""
167+
}
168+
169+
msgObj := wsMsg{}
170+
if err := json2.Unmarshal(wsData, &msgObj); err != nil {
171+
msgObj.Type = "cmd"
172+
msgObj.Cmd = string(wsData)
173+
}
174+
//if err := json.Unmarshal(wsData, &msgObj); err != nil {
175+
// logrus.WithError(err).WithField("wsData", string(wsData)).Error("unmarshal websocket message failed")
176+
//}
177+
switch msgObj.Type {
178+
case wsMsgCmd:
179+
//handle xterm.js stdin
180+
//decodeBytes, err := base64.StdEncoding.DecodeString(msgObj.Cmd)
181+
if msgObj.Cmd == "\r" {
182+
return password
183+
}
184+
185+
if msgObj.Cmd == "\u007f" {
186+
if len(password) == 0 {
187+
continue
188+
}
189+
password = password[:len(password)-1]
190+
continue
191+
}
192+
password += msgObj.Cmd
193+
}
194+
195+
}
196+
}
197+
101198
//ReceiveWsMsg receive websocket msg do some handling then write into ssh.session.stdin
102199
func (ssConn *SshConn) ReceiveWsMsg(wsConn *websocket.Conn, logBuff *bytes.Buffer, exitCh chan bool) {
103200
//tells other go routine quit
@@ -187,6 +284,64 @@ func flushComboOutput(w *wsBufferWriter, wsConn *websocket.Conn) error {
187284
}
188285
return nil
189286
}
287+
288+
//ReceiveWsMsg receive websocket msg do some handling then write into ssh.session.stdin
289+
func (ssConn *SshConn) Login(wsConn *websocket.Conn, logBuff *bytes.Buffer, exitCh chan bool) {
290+
//tells other go routine quit
291+
defer setQuit(exitCh)
292+
for {
293+
select {
294+
case <-exitCh:
295+
return
296+
default:
297+
//read websocket msg
298+
_, wsData, err := wsConn.ReadMessage()
299+
if err != nil {
300+
logrus.WithError(err).Error("reading webSocket message failed")
301+
return
302+
}
303+
//unmashal bytes into struct
304+
//msgObj := wsMsg{
305+
// Type: "cmd",
306+
// Cmd: "",
307+
// Rows: 50,
308+
// Cols: 180,
309+
//}
310+
msgObj := wsMsg{}
311+
if err := json2.Unmarshal(wsData, &msgObj); err != nil {
312+
msgObj.Type = "cmd"
313+
msgObj.Cmd = string(wsData)
314+
}
315+
//if err := json.Unmarshal(wsData, &msgObj); err != nil {
316+
// logrus.WithError(err).WithField("wsData", string(wsData)).Error("unmarshal websocket message failed")
317+
//}
318+
switch msgObj.Type {
319+
320+
case wsMsgResize:
321+
//handle xterm.js size change
322+
if msgObj.Cols > 0 && msgObj.Rows > 0 {
323+
if err := ssConn.Session.WindowChange(msgObj.Rows, msgObj.Cols); err != nil {
324+
logrus.WithError(err).Error("ssh pty change windows size failed")
325+
}
326+
}
327+
case wsMsgCmd:
328+
//handle xterm.js stdin
329+
//decodeBytes, err := base64.StdEncoding.DecodeString(msgObj.Cmd)
330+
decodeBytes := []byte(msgObj.Cmd)
331+
if err != nil {
332+
logrus.WithError(err).Error("websock cmd string base64 decoding failed")
333+
}
334+
if _, err := ssConn.StdinPipe.Write(decodeBytes); err != nil {
335+
logrus.WithError(err).Error("ws cmd bytes write to ssh.stdin pipe failed")
336+
}
337+
//write input cmd to log buffer
338+
if _, err := logBuff.Write(decodeBytes); err != nil {
339+
logrus.WithError(err).Error("write received cmd into log buffer failed")
340+
}
341+
}
342+
}
343+
}
344+
}
190345
func (ssConn *SshConn) SessionWait(quitChan chan bool) {
191346
if err := ssConn.Session.Wait(); err != nil {
192347
logrus.WithError(err).Error("ssh session wait failed")
@@ -241,7 +396,7 @@ func WsReaderCopy(reader *websocket.Conn, writer io.Writer) {
241396
if err = json2.Unmarshal(p, &msgObj); err != nil {
242397
writer.Write(p)
243398
} else if msgObj.Type == wsMsgResize {
244-
writer.Write([]byte("stty rows " + strconv.Itoa(msgObj.Rows) + " && stty cols " + strconv.Itoa(msgObj.Cols) + " \r" ))
399+
writer.Write([]byte("stty rows " + strconv.Itoa(msgObj.Rows) + " && stty cols " + strconv.Itoa(msgObj.Cols) + " \r"))
245400
}
246401
}
247402
}

0 commit comments

Comments
 (0)