2021-09-02 09:48:11 +00:00
|
|
|
|
/*
|
|
|
|
|
Copyright 2021 Qiniu Cloud (qiniu.com)
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-06-10 12:03:47 +00:00
|
|
|
|
package server
|
|
|
|
|
|
2021-06-24 07:22:24 +00:00
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
2021-09-05 12:27:29 +00:00
|
|
|
|
"sync"
|
2021-06-24 07:22:24 +00:00
|
|
|
|
"time"
|
|
|
|
|
|
2023-08-28 02:31:52 +00:00
|
|
|
|
"github.com/RickLeee/goc/v2/pkg/log"
|
2021-06-24 07:22:24 +00:00
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (gs *gocServer) serveWatchInternalStream(c *gin.Context) {
|
|
|
|
|
// 检查插桩服务上报的信息
|
2021-09-05 12:27:29 +00:00
|
|
|
|
watchRemoteIP, _ := c.RemoteIP()
|
|
|
|
|
id := c.Query("id")
|
|
|
|
|
token := c.Query("token")
|
2021-06-24 07:22:24 +00:00
|
|
|
|
|
2021-09-05 12:27:29 +00:00
|
|
|
|
rawagent, ok := gs.agents.Load(id)
|
|
|
|
|
if !ok {
|
2021-06-24 07:22:24 +00:00
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
2021-09-05 12:27:29 +00:00
|
|
|
|
"msg": "agent not registered",
|
|
|
|
|
"code": 1,
|
2021-06-24 07:22:24 +00:00
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-09-05 12:27:29 +00:00
|
|
|
|
|
|
|
|
|
agent := rawagent.(*gocCoveredAgent)
|
|
|
|
|
if agent.Token != token {
|
2021-06-24 07:22:24 +00:00
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
2021-09-05 12:27:29 +00:00
|
|
|
|
"msg": "register token not match",
|
|
|
|
|
"code": 1,
|
2021-06-24 07:22:24 +00:00
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-09-05 12:27:29 +00:00
|
|
|
|
|
|
|
|
|
// 更新 agent 信息
|
|
|
|
|
agent.WatchRemoteIP = watchRemoteIP.String()
|
|
|
|
|
agent.Status &= ^DISCONNECT // 取消 DISCONNECT 的状态
|
|
|
|
|
agent.Status |= WATCHCONNECT // 设置为 RPC CONNECT 状态
|
|
|
|
|
var once sync.Once
|
|
|
|
|
|
2021-06-24 07:22:24 +00:00
|
|
|
|
// upgrade to websocket
|
|
|
|
|
ws, err := gs.upgrader.Upgrade(c.Writer, c.Request, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Errorf("fail to establish websocket connection with watch agent: %v", err)
|
|
|
|
|
c.JSON(http.StatusInternalServerError, nil)
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-05 12:27:29 +00:00
|
|
|
|
// 注册销毁函数
|
|
|
|
|
agent.closeWatchConnOnce = func() {
|
|
|
|
|
once.Do(func() {
|
|
|
|
|
// 关闭 ws 连接后,ws.ReadMessage() 会出错退出 goroutine,进入 defer
|
|
|
|
|
ws.Close()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-24 07:22:24 +00:00
|
|
|
|
// send close msg and close ws connection
|
|
|
|
|
defer func() {
|
2021-09-05 12:27:29 +00:00
|
|
|
|
// 取消 WATCH CONNECT 状态
|
|
|
|
|
agent.Status &= ^WATCHCONNECT
|
|
|
|
|
if agent.Status == 0 {
|
|
|
|
|
agent.Status = DISCONNECT
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
agent.closeWatchConnOnce()
|
|
|
|
|
|
|
|
|
|
log.Infof("close watch connection, %v", agent.Hostname)
|
2021-06-24 07:22:24 +00:00
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
// set pong handler
|
|
|
|
|
ws.SetReadDeadline(time.Now().Add(PongWait))
|
|
|
|
|
ws.SetPongHandler(func(string) error {
|
|
|
|
|
ws.SetReadDeadline(time.Now().Add(PongWait))
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// set ping goroutine to ping every PingWait time
|
|
|
|
|
go func() {
|
|
|
|
|
ticker := time.NewTicker(PingWait)
|
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
|
|
for range ticker.C {
|
|
|
|
|
if err := gs.wsping(ws, PongWait); err != nil {
|
2021-09-05 12:27:29 +00:00
|
|
|
|
log.Errorf("watch ping to %v failed: %v", agent.Hostname, err)
|
2021-06-24 07:22:24 +00:00
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2021-09-05 12:27:29 +00:00
|
|
|
|
log.Infof("one watch agent established, %v, cmdline: %v, pid: %v, hostname: %v", ws.RemoteAddr(), agent.CmdLine, agent.Pid, agent.Hostname)
|
2021-06-24 07:22:24 +00:00
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
mt, message, err := ws.ReadMessage()
|
|
|
|
|
if err != nil {
|
2021-09-05 12:27:29 +00:00
|
|
|
|
log.Errorf("read from %v: %v", agent.Hostname, err)
|
2021-06-24 07:22:24 +00:00
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
if mt == websocket.TextMessage {
|
2021-06-24 07:33:49 +00:00
|
|
|
|
gs.watchCh <- message
|
2021-06-24 07:22:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-10 12:03:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-24 07:22:24 +00:00
|
|
|
|
func (gs *gocServer) watchLoop() {
|
|
|
|
|
for {
|
|
|
|
|
msg := <-gs.watchCh
|
|
|
|
|
gs.watchClients.Range(func(key, value interface{}) bool {
|
|
|
|
|
// 这里是客户端的 ws 连接,不是 agent ws 连接
|
|
|
|
|
gwc := value.(*gocWatchClient)
|
|
|
|
|
err := gwc.ws.WriteMessage(websocket.TextMessage, msg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
gwc.ws.Close()
|
|
|
|
|
gwc.once.Do(func() { close(gwc.exitCh) })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
}
|
2021-06-10 12:03:47 +00:00
|
|
|
|
}
|