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
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"crypto/sha256"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/rpc"
|
|
|
|
|
"net/rpc/jsonrpc"
|
2021-09-05 12:27:29 +00:00
|
|
|
|
"sync"
|
2021-06-10 12:03:47 +00:00
|
|
|
|
"time"
|
|
|
|
|
|
2023-08-28 04:34:44 +00:00
|
|
|
|
"github.com/RickLeee/goc-v2/pkg/log"
|
2021-06-10 12:03:47 +00:00
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
|
|
|
|
|
2021-06-19 08:17:29 +00:00
|
|
|
|
// serveRpcStream holds connection between goc server and agent.
|
|
|
|
|
//
|
|
|
|
|
// 1. goc server 作为 rpc 客户端
|
|
|
|
|
//
|
|
|
|
|
// 2. 每个链接的 goc agent 作为 rpc 服务端
|
2021-06-10 12:03:47 +00:00
|
|
|
|
func (gs *gocServer) serveRpcStream(c *gin.Context) {
|
|
|
|
|
// 检查插桩服务上报的信息
|
2021-09-05 12:27:29 +00:00
|
|
|
|
rpcRemoteIP, _ := c.RemoteIP()
|
|
|
|
|
id := c.Query("id")
|
|
|
|
|
token := c.Query("token")
|
2021-06-10 12:03:47 +00:00
|
|
|
|
|
2021-09-05 12:27:29 +00:00
|
|
|
|
rawagent, ok := gs.agents.Load(id)
|
|
|
|
|
if !ok {
|
2021-06-10 12:03:47 +00:00
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
2021-09-05 12:27:29 +00:00
|
|
|
|
"msg": "agent not registered",
|
|
|
|
|
"code": 1,
|
2021-06-10 12:03:47 +00:00
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-09-05 12:27:29 +00:00
|
|
|
|
|
|
|
|
|
agent := rawagent.(*gocCoveredAgent)
|
|
|
|
|
if agent.Token != token {
|
2021-06-10 12:03:47 +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-10 12:03:47 +00:00
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-05 12:27:29 +00:00
|
|
|
|
// 更新 agent 信息
|
|
|
|
|
agent.RpcRemoteIP = rpcRemoteIP.String()
|
|
|
|
|
agent.exitCh = make(chan int)
|
|
|
|
|
agent.Status &= ^DISCONNECT // 取消 DISCONNECT 的状态
|
|
|
|
|
agent.Status |= RPCCONNECT // 设置为 RPC CONNECT 状态
|
|
|
|
|
// 注册销毁函数
|
|
|
|
|
var once sync.Once
|
|
|
|
|
agent.closeRpcConnOnce = func() {
|
|
|
|
|
once.Do(func() {
|
|
|
|
|
// 为什么只是关闭 channel?其它资源如何释放?
|
|
|
|
|
// close channel 后,本 goroutine 会进入到 defer
|
|
|
|
|
close(agent.exitCh)
|
|
|
|
|
})
|
2021-06-10 12:03:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// upgrade to websocket
|
|
|
|
|
ws, err := gs.upgrader.Upgrade(c.Writer, c.Request, nil)
|
|
|
|
|
if err != nil {
|
2021-06-24 07:22:24 +00:00
|
|
|
|
log.Errorf("fail to establish websocket connection with rpc agent: %v", err)
|
2021-06-10 12:03:47 +00:00
|
|
|
|
c.JSON(http.StatusInternalServerError, nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// send close msg and close ws connection
|
|
|
|
|
defer func() {
|
|
|
|
|
deadline := 1 * time.Second
|
2021-06-19 08:17:29 +00:00
|
|
|
|
// 发送 close msg
|
2021-06-10 12:03:47 +00:00
|
|
|
|
gs.wsclose(ws, deadline)
|
|
|
|
|
time.Sleep(deadline)
|
2021-09-05 12:27:29 +00:00
|
|
|
|
|
|
|
|
|
// 取消 RPC CONNECT 状态
|
|
|
|
|
agent.Status &= ^RPCCONNECT
|
|
|
|
|
if agent.Status == 0 {
|
|
|
|
|
agent.Status = DISCONNECT
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-10 12:03:47 +00:00
|
|
|
|
ws.Close()
|
2021-09-05 12:27:29 +00:00
|
|
|
|
log.Infof("close rpc connection, %v", agent.Hostname)
|
2021-09-08 02:54:18 +00:00
|
|
|
|
// reset rpc client
|
|
|
|
|
agent.rpc = nil
|
2021-06-10 12:03:47 +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() {
|
2021-06-19 08:17:29 +00:00
|
|
|
|
ticker := time.NewTicker(PingWait)
|
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
|
|
for range ticker.C {
|
2021-06-10 12:03:47 +00:00
|
|
|
|
if err := gs.wsping(ws, PongWait); err != nil {
|
2021-09-05 12:27:29 +00:00
|
|
|
|
log.Errorf("rpc ping to %v failed: %v", agent.Hostname, err)
|
2021-06-10 12:03:47 +00:00
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-17 11:53:00 +00:00
|
|
|
|
|
2021-09-05 12:27:29 +00:00
|
|
|
|
agent.closeRpcConnOnce()
|
2021-06-10 12:03:47 +00:00
|
|
|
|
}()
|
|
|
|
|
|
2021-09-05 12:27:29 +00:00
|
|
|
|
log.Infof("one rpc 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
|
|
|
|
// new rpc agent
|
2021-06-10 12:03:47 +00:00
|
|
|
|
// 在这里 websocket server 作为 rpc 的客户端,
|
|
|
|
|
// 发送 rpc 请求,
|
|
|
|
|
// 由被插桩服务返回 rpc 应答
|
|
|
|
|
rwc := &ReadWriteCloser{ws: ws}
|
|
|
|
|
codec := jsonrpc.NewClientCodec(rwc)
|
|
|
|
|
|
2021-09-05 12:27:29 +00:00
|
|
|
|
agent.rpc = rpc.NewClientWithCodec(codec)
|
|
|
|
|
|
2021-06-10 12:03:47 +00:00
|
|
|
|
// wait for exit
|
2021-09-05 12:27:29 +00:00
|
|
|
|
<-agent.exitCh
|
2021-06-10 12:03:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-24 07:22:24 +00:00
|
|
|
|
// generateAgentId generate id based on agent's meta infomation
|
|
|
|
|
func (gs *gocServer) generateAgentId(args ...string) gocCliendId {
|
2021-06-10 12:03:47 +00:00
|
|
|
|
var path string
|
|
|
|
|
for _, arg := range args {
|
|
|
|
|
path += arg
|
|
|
|
|
}
|
|
|
|
|
sum := sha256.Sum256([]byte(path))
|
|
|
|
|
h := fmt.Sprintf("%x", sum[:6])
|
|
|
|
|
|
|
|
|
|
return gocCliendId(h)
|
|
|
|
|
}
|