forked from jxhczhl/JsRpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
300 lines (274 loc) · 7.19 KB
/
main.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package main
import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/unrolled/secure"
"net/http"
"os"
"strings"
"sync"
"time"
)
var (
// BasicPort The original port without SSL certificate
BasicPort = `:12080`
// SSLPort "Secure" port with SSL certificate
SSLPort = `:12443`
// websocket.Upgrader specifies parameters for upgrading an HTTP connection to a
// WebSocket connection.
upGrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool { return true },
}
hlSyncMap sync.Map
gm = &sync.Mutex{}
// 默认超时时间,没有得到数据的超时时间 单位:秒
defaultTimeout = 30
isPrint = false
)
type Clients struct {
clientGroup string
clientName string
actionData map[string]chan string
clientWs *websocket.Conn
}
type Message struct {
Action string `json:"action"`
Param string `json:"param"`
}
type logWriter struct{}
func (w logWriter) Write(p []byte) (n int, err error) {
return len(p), nil
}
// is print?
func logPrint(p ...interface{}) {
if isPrint {
fmt.Println(p)
}
}
// NewClient initializes a new Clients instance
func NewClient(group string, name string, ws *websocket.Conn) *Clients {
return &Clients{
clientGroup: group,
clientName: name,
actionData: make(map[string]chan string, 1), // action有消息后就保存到chan里
clientWs: ws,
}
}
// ws, provides inject function for a job
func ws(c *gin.Context) {
group, name := c.Query("group"), c.Query("name")
if group == "" || name == "" {
return
}
wsClient, err := upGrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
fmt.Println("websocket err:", err)
return
}
client := NewClient(group, name, wsClient)
hlSyncMap.Store(group+"->"+name, client)
for {
//等待数据
_, message, err := wsClient.ReadMessage()
if err != nil {
break
}
msg := string(message)
check := []uint8{104, 108, 94, 95, 94}
strIndex := strings.Index(msg, string(check))
if strIndex >= 1 {
action := msg[:strIndex]
client.actionData[action] <- msg[strIndex+5:]
logPrint("get_message:", msg[strIndex+5:])
//hlSyncMap.Store(group+"->"+name, client)
} else {
fmt.Println(msg, "message error")
}
}
defer func(ws *websocket.Conn) {
_ = ws.Close()
logPrint(group+"->"+name, "下线了")
hlSyncMap.Range(func(key, value interface{}) bool {
//client, _ := value.(*Clients)
if key == group+"->"+name {
hlSyncMap.Delete(key)
}
return true
})
}(wsClient)
}
func wsTest(c *gin.Context) {
testClient, _ := upGrader.Upgrade(c.Writer, c.Request, nil)
for {
//等待数据
_, message, err := testClient.ReadMessage()
if err != nil {
break
}
msg := string(message)
logPrint("接收到测试消息", msg)
_ = testClient.WriteMessage(1, []byte(msg))
}
defer func(ws *websocket.Conn) {
_ = ws.Close()
}(testClient)
}
func GQueryFunc(client *Clients, funcName string, param string, resChan chan<- string) {
WriteDate := Message{}
WriteDate.Action = funcName
if param == "" {
WriteDate.Param = ""
} else {
WriteDate.Param = param
}
data, _ := json.Marshal(WriteDate)
clientWs := client.clientWs
if client.actionData[funcName] == nil {
client.actionData[funcName] = make(chan string, 1) //此次action初始化1个消息
}
gm.Lock()
err := clientWs.WriteMessage(2, data)
gm.Unlock()
if err != nil {
fmt.Println(err, "写入数据失败")
}
resultFlag := false
for i := 0; i < defaultTimeout*10; i++ {
if len(client.actionData[funcName]) > 0 {
res := <-client.actionData[funcName]
resChan <- res
resultFlag = true
break
}
time.Sleep(time.Millisecond * 100)
}
// 循环完了还是没有数据,那就超时退出
if true != resultFlag {
resChan <- "黑脸怪:timeout"
}
defer func() {
close(resChan)
}()
}
func ResultSet(c *gin.Context) {
var getGroup, getName, Action, Param string
//获取参数
getGroup, getName, Action, Param = c.Query("group"), c.Query("name"), c.Query("action"), c.Query("param")
//如果获取不到 说明是post提交的
if getGroup == "" && getName == "" {
//切换post获取方式
getGroup, getName, Action, Param = c.PostForm("group"), c.PostForm("name"), c.PostForm("action"), c.PostForm("param")
}
if getGroup == "" || getName == "" {
c.JSON(400, gin.H{"status": 400, "data": "input group and name"})
return
}
clientName, ok := hlSyncMap.Load(getGroup + "->" + getName)
if ok == false {
c.JSON(400, gin.H{"status": 400, "data": "注入了ws?没有找到当前组和名字"})
return
}
if Action == "" {
c.JSON(200, gin.H{"group": getGroup, "name": getName})
return
}
//取一个ws客户端
client, ko := clientName.(*Clients)
if !ko {
return
}
c2 := make(chan string, 1)
go GQueryFunc(client, Action, Param, c2)
//把管道传过去,获得值就返回了
c.JSON(200, gin.H{"status": 200, "group": client.clientGroup, "name": client.clientName, "data": <-c2})
}
func checkTimeout(c2 chan string) {
// 100ms检查一次
for i := 0; i < defaultTimeout*10; i++ {
if len(c2) > 0 {
return
}
time.Sleep(time.Millisecond * 100)
}
// 循环完了还是没有数据,那就超时退出
c2 <- "黑脸怪:timeout"
}
func Execjs(c *gin.Context) {
var getGroup, getName, JsCode string
Action := "_execjs"
//获取参数
getGroup, getName, JsCode = c.Query("group"), c.Query("name"), c.Query("jscode")
//如果获取不到 说明是post提交的
if getGroup == "" && getName == "" {
//切换post获取方式
getGroup, getName, JsCode = c.PostForm("group"), c.PostForm("name"), c.PostForm("jscode")
}
if getGroup == "" || getName == "" {
c.JSON(400, gin.H{"status": 400, "data": "input group and name"})
return
}
logPrint(getGroup, getName, JsCode)
clientName, ok := hlSyncMap.Load(getGroup + "->" + getName)
if ok == false {
c.JSON(400, gin.H{"status": 400, "data": "注入了ws?没有找到当前组和名字"})
return
}
//取一个ws客户端
client, ko := clientName.(*Clients)
if !ko {
return
}
c2 := make(chan string)
go GQueryFunc(client, Action, JsCode, c2)
c.JSON(200, gin.H{"status": "200", "group": client.clientGroup, "name": client.clientName, "data": <-c2})
}
func getList(c *gin.Context) {
resList := "黑脸怪:\r\n\t"
hlSyncMap.Range(func(key, value interface{}) bool {
resList += key.(string) + "\r\n\t"
return true
})
c.String(200, resList)
}
func Index(c *gin.Context) {
c.String(200, "你好,我是黑脸怪~")
}
func TlsHandler() gin.HandlerFunc {
return func(c *gin.Context) {
secureMiddleware := secure.New(secure.Options{
SSLRedirect: true,
SSLHost: SSLPort,
})
err := secureMiddleware.Process(c.Writer, c.Request)
if err != nil {
c.Abort()
return
}
c.Next()
}
}
func main() {
for _, v := range os.Args {
if v == "log" {
isPrint = true
}
}
// 将默认的日志输出器设置为空
//gin.DefaultWriter = logWriter{}
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
r.GET("/", Index)
r.GET("/go", ResultSet)
r.POST("/go", ResultSet)
r.GET("/ws", ws)
r.GET("/wst", wsTest)
r.GET("/execjs", Execjs)
r.POST("/execjs", Execjs)
r.GET("/list", getList)
r.Use(TlsHandler())
_ = r.Run(BasicPort)
//编译https版放开下面这行注释代码 并且把上一行注释
//_ = r.RunTLS(SSLPort, "zhengshu.pem", "zhengshu.key")
}