forked from jxhczhl/JsRpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
232 lines (209 loc) · 5.49 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
package main
import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/unrolled/secure"
"net/http"
"strings"
"sync"
)
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{}
gchan = make(chan string)
)
type Clients struct {
clientGroup string
clientName string
Data map[string]string
clientWs *websocket.Conn
}
type Message struct {
Action string `json:"action"`
Param string `json:"param"`
}
// NewClient initializes a new Clients instance
func NewClient(group string, name string, ws *websocket.Conn) *Clients {
return &Clients{
clientGroup: group,
clientName: name,
Data: make(map[string]string),
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
}
ws, err := upGrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
fmt.Println("websocket err:", err)
return
}
client := NewClient(group, name, ws)
hlSyncMap.Store(group+"->"+name, client)
for {
//等待数据
_, message, err := ws.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.Data[action] = msg[strIndex+5:]
fmt.Println("get_message:", client.Data[action])
gchan <- msg[strIndex+5:]
hlSyncMap.Store(group+"->"+name, client)
} else {
fmt.Println(msg, "message error")
}
}
defer func(ws *websocket.Conn) {
_ = ws.Close()
fmt.Println(group+"->"+name, "下线了")
hlSyncMap.Range(func(key, value interface{}) bool {
//client, _ := value.(*Clients)
if key == group+"->"+name {
hlSyncMap.Delete(key)
}
return true
})
}(ws)
}
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)
ws := client.clientWs
gm.Lock()
err := ws.WriteMessage(2, data)
gm.Unlock()
if err != nil {
fmt.Println(err, "写入数据失败")
}
res := <-gchan
fmt.Printf("res: %v\n", res)
resChan <- res
}
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.String(200, "input group and name")
return
}
clientName, ok := hlSyncMap.Load(getGroup + "->" + getName)
if ok == false {
c.String(200, "注入了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)
go GQueryFunc(client, Action, Param, c2)
//把管道传过去,获得值就返回了
c.JSON(200, gin.H{"status": "200", "group": client.clientGroup, "name": client.clientName, "data": <-c2})
}
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.String(200, "input group and name")
return
}
fmt.Println(getGroup, getName, JsCode)
clientName, ok := hlSyncMap.Load(getGroup + "->" + getName)
if ok == false {
c.String(200, "注入了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() {
//gin.SetMode(gin.ReleaseMode)
r := gin.Default()
r.GET("/", Index)
r.GET("/go", ResultSet)
r.POST("/go", ResultSet)
r.GET("/ws", ws)
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")
}