diff --git a/config.json b/config.json index 08eefe8..9700d8a 100644 --- a/config.json +++ b/config.json @@ -32,6 +32,19 @@ } }, "handlers": { + "auth": { + "entry": "/auth", + "socket": { + "MaxMessageSize": 8192, + "WriteTimeout": 0, + "ReadTimeout": 0, + "PongTimeout": 60, + "PingTimeout": 10, + "PingPeriod": 10, + "BinaryMessage": false + } + }, + "probe": { "entry": "/probe", "socket": { @@ -44,8 +57,20 @@ "BinaryMessage": false } }, - "auth": { - "entry": "/auth", + "metric": { + "entry": "/metric", + "socket": { + "MaxMessageSize": 8192, + "WriteTimeout": 0, + "ReadTimeout": 0, + "PongTimeout": 60, + "PingTimeout": 10, + "PingPeriod": 10, + "BinaryMessage": false + } + }, + "file": { + "entry": "/file", "socket": { "MaxMessageSize": 8192, "WriteTimeout": 0, diff --git a/server/file_servlet.go b/server/file_servlet.go new file mode 100644 index 0000000..843319c --- /dev/null +++ b/server/file_servlet.go @@ -0,0 +1,50 @@ +package server + +import ( + "context" + "encoding/json" + "fmt" + + "git.loafle.net/commons_go/logging" + "git.loafle.net/overflow/overflow_gateway_probe/grpc" + ogw "git.loafle.net/overflow/overflow_gateway_websocket" + "github.com/valyala/fasthttp" +) + +func newFileServlet() Servlet { + s := &fileServlet{} + return s +} + +type fileServlet struct { +} + +func (s *fileServlet) IsCanConnect(ctx *fasthttp.RequestCtx) bool { + var buf []byte + if buf = ctx.Request.Header.Peek(ProbeHeader_ProbeKey); nil != buf { + return false + } + probeKey := string(buf) + + var err error + gctx := context.Background() + params := []string{probeKey} + + var result string + if result, err = grpc.Exec(gctx, "ProbeService.readByProbeKey", params); nil != err { + logging.Logger.Warn(fmt.Sprintf("Probe: Invalid connect ProbeKey[%s] ip[%s]", probeKey, ctx.RemoteAddr().String())) + return false + } + var probe Probe + if err = json.Unmarshal([]byte(result), &probe); nil != err { + logging.Logger.Warn(fmt.Sprintf("Probe: Cannot conver json[%s] ip[%s]", result, ctx.RemoteAddr().String())) + return false + } + + return true +} + +func (s *fileServlet) SessionUID(soc ogw.Socket) string { + + return "" +} diff --git a/server/metric_servlet.go b/server/metric_servlet.go new file mode 100644 index 0000000..07f9d67 --- /dev/null +++ b/server/metric_servlet.go @@ -0,0 +1,50 @@ +package server + +import ( + "context" + "encoding/json" + "fmt" + + "git.loafle.net/commons_go/logging" + "git.loafle.net/overflow/overflow_gateway_probe/grpc" + ogw "git.loafle.net/overflow/overflow_gateway_websocket" + "github.com/valyala/fasthttp" +) + +func newMetricServlet() Servlet { + s := &metricServlet{} + return s +} + +type metricServlet struct { +} + +func (s *metricServlet) IsCanConnect(ctx *fasthttp.RequestCtx) bool { + var buf []byte + if buf = ctx.Request.Header.Peek(ProbeHeader_ProbeKey); nil != buf { + return false + } + probeKey := string(buf) + + var err error + gctx := context.Background() + params := []string{probeKey} + + var result string + if result, err = grpc.Exec(gctx, "ProbeService.readByProbeKey", params); nil != err { + logging.Logger.Warn(fmt.Sprintf("Probe: Invalid connect ProbeKey[%s] ip[%s]", probeKey, ctx.RemoteAddr().String())) + return false + } + var probe Probe + if err = json.Unmarshal([]byte(result), &probe); nil != err { + logging.Logger.Warn(fmt.Sprintf("Probe: Cannot conver json[%s] ip[%s]", result, ctx.RemoteAddr().String())) + return false + } + + return true +} + +func (s *metricServlet) SessionUID(soc ogw.Socket) string { + + return "" +} diff --git a/server/probe_servlet.go b/server/probe_servlet.go index 96032dd..fa6325f 100644 --- a/server/probe_servlet.go +++ b/server/probe_servlet.go @@ -1,10 +1,21 @@ package server import ( + "context" + "encoding/json" + "fmt" + + "git.loafle.net/commons_go/logging" + "git.loafle.net/overflow/overflow_gateway_probe/grpc" ogw "git.loafle.net/overflow/overflow_gateway_websocket" "github.com/valyala/fasthttp" ) +const ( + ProbeHeader_ProbeKey = "overFlow-Probe-Key" + ProbeHeader_Probe_EncryptionKey = "overFlow-Probe-EncryptionKey" +) + func newProbeServlet() Servlet { s := &probeServlet{} return s @@ -14,12 +25,44 @@ type probeServlet struct { } func (s *probeServlet) IsCanConnect(ctx *fasthttp.RequestCtx) bool { + var buf []byte + if buf = ctx.Request.Header.Peek(ProbeHeader_ProbeKey); nil != buf { + return false + } + probeKey := string(buf) - return false + var err error + gctx := context.Background() + params := []string{probeKey} + + var result string + if result, err = grpc.Exec(gctx, "ProbeService.readByProbeKey", params); nil != err { + logging.Logger.Warn(fmt.Sprintf("Probe: Invalid connect ProbeKey[%s] ip[%s]", probeKey, ctx.RemoteAddr().String())) + return false + } + var probe Probe + if err = json.Unmarshal([]byte(result), &probe); nil != err { + logging.Logger.Warn(fmt.Sprintf("Probe: Cannot conver json[%s] ip[%s]", result, ctx.RemoteAddr().String())) + return false + } + + ctx.Response.Header.Set(ProbeHeader_Probe_EncryptionKey, probe.EncryptionKey) + + return true } func (s *probeServlet) SessionUID(soc ogw.Socket) string { + var buf []byte + if buf = soc.Conn().Headers().Peek(ProbeHeader_ProbeKey); nil != buf { + return string(buf) + } return "" - +} + +type Probe struct { + ID uint64 `json:"id"` + Description string `json:"description"` + ProbeKey string `json:"probeKey"` + EncryptionKey string `json:"encryptionKey"` } diff --git a/server/server_handlers.go b/server/server_handlers.go index 8b5ee95..2d2b2f7 100644 --- a/server/server_handlers.go +++ b/server/server_handlers.go @@ -16,7 +16,7 @@ var ofSigningKey []byte func newServerHandler(ctx context.Context) ogw.ServerHandler { h := &serverHandlers{ ctx: ctx, - servlets: make(map[string]Servlet, 2), + servlets: make(map[string]Servlet, 4), } h.HandshakeTimeout = conf.Config.Websocket.HandshakeTimeout * time.Second h.ReadBufferSize = conf.Config.Websocket.ReadBufferSize @@ -25,6 +25,8 @@ func newServerHandler(ctx context.Context) ogw.ServerHandler { h.servlets["/auth"] = newAuthServlet() h.servlets["/probe"] = newProbeServlet() + h.servlets["/metric"] = newMetricServlet() + h.servlets["/file"] = newFileServlet() return h }