-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcmdsocket.go
255 lines (234 loc) · 6.09 KB
/
cmdsocket.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package main
import (
"bufio"
"context"
"github.com/gorilla/websocket"
"io"
"net/http"
"os"
"os/exec"
"strings"
"sync"
)
type CmdRoute struct {
Web_url string `json:"web_url"`
Socket_url string `json:"socket_url"`
Cmds []CmdConfig `json:"cmds"`
CmdsFile string `json:"cmds_file"`
Exit string `json:"exit_string"`
}
type CmdConfig struct {
Alias string `json:"exec_alias"`
Cmd string `json:"cmd"`
Param string `json:"param"`
}
type SocketSafeWriter struct {
Conn *websocket.Conn
mu sync.Mutex
}
func (sw *SocketSafeWriter) SendMsg(msg []byte) {
sw.mu.Lock()
defer sw.mu.Unlock()
if sw.Conn != nil {
sw.Conn.WriteMessage(websocket.TextMessage,msg)
}
}
var cmdsocket_log_prefix = "CMD Socket : "
var cmds_map map[string]CmdConfig
func serializeCmds(route CmdRoute) {
cmds_map = make(map[string]CmdConfig)
//scan file
if len(route.CmdsFile) > 0 {
file,err := os.Open(route.CmdsFile)
if err != nil {
Error.Println(cmdsocket_log_prefix,"Open cmdsfile error : ",err)
}else {
scanner := bufio.NewScanner(file)
for scanner.Scan() {
str := scanner.Text()
splitIdx := strings.Index(str,":")
if splitIdx > 0 {
alias := str[0:splitIdx]
blankSplitIdx := strings.Index(str," ")
if blankSplitIdx > 0{
cmdStr := str[splitIdx+1:blankSplitIdx]
paramStr := str[blankSplitIdx+1:]
cmds_map[alias] = CmdConfig{Alias: alias,Cmd: cmdStr,Param: paramStr}
logCmdsocket("Add cmd ",alias," ",cmdStr," ",paramStr)
}else {
cmdStr := str[splitIdx+1:]
if len(cmdStr) > 0 {
cmds_map[alias] = CmdConfig{Alias: alias,Cmd: cmdStr,Param: ""}
logCmdsocket("Add cmd ",alias," ",cmdStr)
}
}
}
}
}
}
//config file
for _,cmd := range route.Cmds {
cmds_map[cmd.Alias] = cmd
logCmdsocket("Add cmd ",cmd.Alias," ",cmd)
}
}
var wsupgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
func cmdWebSocketHandler(w http.ResponseWriter, r *http.Request) {
conn, err := wsupgrader.Upgrade(w, r, nil)
if err != nil {
Error.Println(cmdsocket_log_prefix,"Failed to set websocket upgrade: %+v", err)
return
}
safeSocket := SocketSafeWriter{Conn: conn}
remoteChan := make(chan string);
okChan := make(chan bool)
getCmd := false
ctx,cancel := context.WithCancel(context.Background())
defer close(remoteChan)
defer close(okChan)
for {
_,msg, err := conn.ReadMessage()
if err != nil {
break
}
select {
case c:= <-okChan:
getCmd = c
default:
}
if len(string(msg)) > 0 {
//if reading cmd, socket string will gothrought cmd interactive without make a new cmd.
if getCmd {
remoteChan <- string(msg)
continue
}
logCmdsocket("recieve ",string(msg))
//get str
if cmd := seekCmdFromConfig(string(msg));cmd!=nil {
excmd(ctx,*cmd,&safeSocket,remoteChan,&okChan)
safeSocket.SendMsg([]byte("CMD open success, put any key go on"))
} else {
safeSocket.SendMsg( msg)
}
}
}
//read last okchan which still waiting to receive before closed
select {
case c:= <-okChan:
logCmdsocket("no use ",c)
default:
}
okChan = nil
remoteChan = nil
safeSocket.Conn = nil
cancel()
logCmdsocket("Socket Handle Closed !")
}
func seekCmdFromConfig(alias string) *CmdConfig {
cmd := cmds_map[alias]
if len(cmd.Alias) > 0 {
return &cmd
}
return nil
}
func writeInputToCmd(cmd *exec.Cmd,file *io.WriteCloser,stringChan chan string) {
for puts := range stringChan{
logCmdsocket(">>> ",puts)
if puts == UserConfig.Cmd.Exit {
err := cmd.Process.Kill()
if err!=nil {
logCmdsocket("kill process error :",err)
}
break;
}
wNum,err := io.WriteString(*file, puts + "\n")
logCmdsocket("write length ",wNum)
if err!=nil {
logCmdsocket("write failed : ",err)
break;
}
}
//check cmd if still running
logCmdsocket("cmd process ",cmd.ProcessState)
if cmd.ProcessState == nil && cmd.Process != nil {
logCmdsocket("cmd still running, close it")
cmd.Process.Kill()
}
logCmdsocket("goroutine write loop closed ")
}
func readOutputFromCmd(cmd *exec.Cmd,file *io.PipeReader, safeSocket *SocketSafeWriter) {
logCmdsocket("output pipe file :",*file)
//bufio.NewScanner is more easier than bufio.NewReader(),and won't cause loop leak
scanner := bufio.NewScanner(file)
for scanner.Scan() {
str := scanner.Text()
logCmdsocket("read cmd script msg : ",str)
if len(str) > 0 {
if safeSocket.Conn == nil {
break
}
safeSocket.SendMsg([]byte(str))
logCmdsocket("socket : ",safeSocket.Conn)
}
}
//check cmd if still running
logCmdsocket("cmd process ",cmd.ProcessState)
if cmd.ProcessState == nil && cmd.Process != nil {
logCmdsocket("cmd still running, close it")
cmd.Process.Kill()
}
logCmdsocket("goroutine read loop closed ")
}
func startCmdScripts(ctx context.Context, config CmdConfig,safeSocket *SocketSafeWriter, inputChan chan string, okChan *chan bool) {
logCmdsocket("ready for send cmd ",config.Cmd)
*okChan <- true
var cmd *exec.Cmd
if len(config.Param) > 0{
cmd = exec.Command(config.Cmd,config.Param)
}else {
cmd = exec.Command(config.Cmd)
}
pf,perr := cmd.StdinPipe()
if perr != nil {
logCmdsocket("pipe err ",perr)
}
defer pf.Close()
go writeInputToCmd(cmd,&pf,inputChan)
r,w := io.Pipe()
cmd.Stdout = w
cmd.Stderr = w
defer r.Close()
defer w.Close()
go readOutputFromCmd(cmd,r,safeSocket)
select {
case <-ctx.Done():
logCmdsocket("cmd context forcus finished, skip cmd ",config.Cmd)
default:
err := cmd.Run()
if err!=nil {
logCmdsocket("cmd failed ",err)
safeSocket.SendMsg([]byte("CMD Error: "+err.Error()))
}
safeSocket.SendMsg([]byte("CMD Finished!"))
}
logCmdsocket("goroutine cmd exit")
//without goroutine, okchan will stuck this routine until next okchan read
go func() {
if *okChan != nil{
*okChan <- false
}
logCmdsocket("goroutine okchan finish!")
}()
}
func excmd(ctx context.Context,config CmdConfig, safeSocket *SocketSafeWriter, cmdChan chan string, okChan *chan bool) {
go startCmdScripts(ctx,config,safeSocket,cmdChan,okChan)
}
func logCmdsocket(v ...interface{}) {
Debug.Println(cmdsocket_log_prefix,v)
}