1
0

main.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "github.com/gorilla/websocket"
  6. "github.com/unrolled/secure"
  7. "net/http"
  8. "strings"
  9. "sync"
  10. )
  11. var (
  12. // BasicPort The original port without SSL certificate
  13. BasicPort = `:12080`
  14. // SSLPort "Secure" port with SSL certificate
  15. SSLPort = `:12443`
  16. // websocket.Upgrader specifies parameters for upgrading an HTTP connection to a
  17. // WebSocket connection.
  18. upGrader = websocket.Upgrader{
  19. CheckOrigin: func(r *http.Request) bool { return true },
  20. }
  21. hlSyncMap sync.Map
  22. )
  23. // Clients provides Connect instance for a job
  24. type Clients struct {
  25. clientGroup string
  26. clientName string
  27. //Action map[string]string
  28. Data map[string]chan string
  29. clientWs *websocket.Conn
  30. }
  31. // NewClients initializes a new Clients instance
  32. func NewClients(clientGroup string, clientName string, clientWs *websocket.Conn) *Clients {
  33. return &Clients{
  34. clientGroup: clientGroup,
  35. clientName: clientName,
  36. clientWs: clientWs,
  37. }
  38. }
  39. // QueryFunc Provides context-sensitive methods
  40. func QueryFunc(client *Clients, funcName string, param string) {
  41. var WriteDate string
  42. if param == "" {
  43. WriteDate = "{\"action\":\"" + funcName + "\"}"
  44. } else {
  45. WriteDate = "{\"action\":\"" + funcName + "\",\"param\":\"" + param + "\"}"
  46. }
  47. fmt.Println(WriteDate)
  48. ws := client.clientWs
  49. err := ws.WriteMessage(1, []byte(WriteDate))
  50. if err != nil {
  51. fmt.Println(err)
  52. }
  53. }
  54. // ws, provides inject function for a job
  55. func ws(c *gin.Context) {
  56. group, name := c.Query("group"), c.Query("name")
  57. if group == "" || name == "" {
  58. return
  59. }
  60. ws, err := upGrader.Upgrade(c.Writer, c.Request, nil)
  61. if err != nil {
  62. fmt.Println("websocket err:", err)
  63. return
  64. }
  65. client := NewClients(group, name, ws)
  66. hlSyncMap.Store(group+"->"+name, client)
  67. for {
  68. _, message, err := ws.ReadMessage()
  69. if err != nil {
  70. break
  71. }
  72. msg := string(message)
  73. check := []uint8{104, 108, 94, 95, 94}
  74. strIndex := strings.Index(msg, string(check))
  75. if strIndex >= 1 {
  76. action := msg[:strIndex]
  77. //fmt.Println(action,"save msg")
  78. if client.Data[action] == nil {
  79. client.Data[action] = make(chan string, 1)
  80. }
  81. client.Data[action] <- msg[strIndex+5:]
  82. hlSyncMap.Store(group+"->"+name, client)
  83. } else {
  84. fmt.Println(msg)
  85. }
  86. }
  87. hlSyncMap.Delete(group + "->" + name)
  88. defer ws.Close()
  89. }
  90. // ResultSet provides get result function for a job
  91. // you can use it to Remote operation and get results
  92. func ResultSet(c *gin.Context) {
  93. group := c.Query("group")
  94. name := c.Query("name")
  95. action := c.Query("action")
  96. param := c.Query("param")
  97. if group == "" || name == "" {
  98. c.String(200, "input group and name")
  99. return
  100. }
  101. fmt.Println(group + "->" + name)
  102. clientName, ok := hlSyncMap.Load(group + "->" + name)
  103. fmt.Println(clientName)
  104. if ok == false {
  105. c.String(200, "注入了ws?没有找到当前组和名字")
  106. return
  107. }
  108. if action == "" {
  109. c.JSON(200, gin.H{"group": group, "name": name})
  110. return
  111. }
  112. value, ko := clientName.(*Clients)
  113. if value.Data[action] == nil {
  114. value.Data[action] = make(chan string, 1)
  115. }
  116. QueryFunc(value, action, param)
  117. data := <-value.Data[action]
  118. if ko {
  119. c.JSON(200, gin.H{"status": "200", "group": value.clientGroup, "name": value.clientName, action: data})
  120. } else {
  121. c.JSON(666, gin.H{"message": "?"})
  122. }
  123. }
  124. // ClientConnectionList provides get client connect list for a job
  125. // you can use it see all Connection
  126. func ClientConnectionList(c *gin.Context) {
  127. resList := "hliang:\r\n"
  128. hlSyncMap.Range(func(key, value interface{}) bool {
  129. resList += key.(string) + "\r\n\t"
  130. return true
  131. })
  132. c.String(200, resList)
  133. }
  134. func TlsHandler() gin.HandlerFunc {
  135. return func(c *gin.Context) {
  136. secureMiddleware := secure.New(secure.Options{
  137. SSLRedirect: true,
  138. SSLHost: SSLPort,
  139. })
  140. err := secureMiddleware.Process(c.Writer, c.Request)
  141. if err != nil {
  142. c.Abort()
  143. return
  144. }
  145. c.Next()
  146. }
  147. }
  148. func main() {
  149. r := gin.Default()
  150. r.GET("/result", ResultSet)
  151. r.GET("/ws", ws)
  152. r.GET("/list", ClientConnectionList)
  153. r.Use(TlsHandler())
  154. r.Run(BasicPort)
  155. //r.RunTLS(SSLPort, "zhengshu.pem", "zhengshu.key")
  156. }