goc/pkg/server/server.go

63 lines
1.3 KiB
Go
Raw Normal View History

2021-06-10 12:03:47 +00:00
package server
import (
"net/rpc"
"sync"
"time"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
)
// gocServer represents a goc server
type gocServer struct {
port int
storePath string
upgrader websocket.Upgrader
rpcClients sync.Map
2021-06-19 08:17:29 +00:00
// mu sync.Mutex // used to protect concurrent rpc call to agent
2021-06-10 12:03:47 +00:00
}
type gocCliendId string
2021-06-19 08:17:29 +00:00
// gocCoveredAgent represents a covered client
type gocCoveredAgent struct {
2021-06-10 12:03:47 +00:00
Id string `json:"id"`
RemoteIP string `json:"remoteip"`
Hostname string `json:"hostname"`
CmdLine string `json:"cmdline"`
Pid string `json:"pid"`
rpc *rpc.Client `json:"-"`
2021-06-19 08:17:29 +00:00
exitCh chan int `json:"-"`
once sync.Once `json:"-"` // 保护 close(exitCh) 只执行一次
2021-06-10 12:03:47 +00:00
}
2021-06-20 13:15:51 +00:00
func RunGocServerUntilExit(host string) {
2021-06-10 12:03:47 +00:00
gs := gocServer{
storePath: "",
upgrader: websocket.Upgrader{
ReadBufferSize: 4096,
WriteBufferSize: 4096,
HandshakeTimeout: 45 * time.Second,
},
}
r := gin.Default()
v2 := r.Group("/v2")
{
2021-06-17 11:53:00 +00:00
v2.GET("/cover/profile", gs.getProfiles)
2021-06-19 08:17:29 +00:00
v2.DELETE("/cover/profile", gs.resetProfiles)
2021-06-10 12:03:47 +00:00
v2.GET("/services", gs.listServices)
v2.GET("/cover/ws/watch", nil)
// internal use only
v2.GET("/internal/ws/rpcstream", gs.serveRpcStream)
v2.GET("/internal/ws/watchstream", nil)
}
2021-06-20 13:15:51 +00:00
r.Run(host)
2021-06-10 12:03:47 +00:00
}