-
Notifications
You must be signed in to change notification settings - Fork 20
/
handle.go
146 lines (130 loc) · 2.74 KB
/
handle.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
134
135
136
137
138
139
140
141
142
143
144
145
146
package webssh
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"strings"
"sync"
"syscall"
"time"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
)
type WebSSHConfig struct {
Record bool
RecPath string
RemoteAddr string
User string
Password string
AuthModel AuthModel
PkPath string
}
type WebSSH struct {
*WebSSHConfig
}
func NewWebSSH(conf *WebSSHConfig) *WebSSH {
return &WebSSH{
WebSSHConfig: conf,
}
}
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024 * 10,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
func (w WebSSH) ServeConn(c *gin.Context) {
wsConn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
c.AbortWithStatusJSON(200, gin.H{"ok": false, "msg": err.Error()})
return
}
defer wsConn.Close()
var config *SSHClientConfig
switch w.AuthModel {
case PASSWORD:
config = SSHClientConfigPassword(
w.RemoteAddr,
w.User,
w.Password,
)
case PUBLICKEY:
config = SSHClientConfigPulicKey(
w.RemoteAddr,
w.User,
w.PkPath,
)
}
client, err := NewSSHClient(config)
if err != nil {
wsConn.WriteControl(websocket.CloseMessage,
[]byte(err.Error()), time.Now().Add(time.Second))
return
}
defer client.Close()
var recorder *Recorder
if w.Record {
mask := syscall.Umask(0)
defer syscall.Umask(mask)
os.MkdirAll(w.RecPath, 0766)
fileName := path.Join(w.RecPath, fmt.Sprintf("%s_%s_%s.cast", w.RemoteAddr, w.User, time.Now().Format("20060102_150405")))
f, err := os.OpenFile(fileName, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0766)
if err != nil {
c.AbortWithStatusJSON(200, gin.H{"ok": false, "msg": err.Error()})
}
defer f.Close()
recorder = NewRecorder(f)
}
turn, err := NewTurn(wsConn, client, recorder)
if err != nil {
wsConn.WriteControl(websocket.CloseMessage,
[]byte(err.Error()), time.Now().Add(time.Second))
return
}
defer turn.Close()
var logBuff = bufPool.Get().(*bytes.Buffer)
logBuff.Reset()
defer bufPool.Put(logBuff)
ctx, cancel := context.WithCancel(context.Background())
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
defer wg.Done()
err := turn.LoopRead(logBuff, ctx)
if err != nil {
log.Printf("%#v", err)
}
}()
go func() {
defer wg.Done()
err := turn.SessionWait()
if err != nil {
log.Printf("%#v", err)
}
cancel()
}()
wg.Wait()
}
func (w WebSSH) RecoderList(c *gin.Context) {
files, err := ioutil.ReadDir(w.RecPath)
if err != nil {
c.AbortWithStatusJSON(200, gin.H{"ok": false, "msg": err.Error()})
return
}
var filesName []string
for _, f := range files {
if f.IsDir() {
continue
}
if strings.HasSuffix(f.Name(), ".cast") {
filesName = append(filesName, f.Name())
}
}
c.JSON(200, filesName)
}