probe_gateway_rpc/servlet/data-servlet.go

100 lines
2.3 KiB
Go
Raw Normal View History

2018-04-12 12:07:02 +00:00
package servlet
import (
2018-04-13 07:18:50 +00:00
"context"
"crypto/rsa"
2018-04-13 07:21:17 +00:00
"fmt"
2018-04-13 07:18:50 +00:00
"sync"
"github.com/valyala/fasthttp"
"git.loafle.net/commons/server-go"
"git.loafle.net/commons/server-go/socket"
ocpc "git.loafle.net/overflow/commons-go/probe/constants"
2018-04-13 07:21:17 +00:00
og "git.loafle.net/overflow/gateway"
"git.loafle.net/overflow/gateway/external/grpc"
2018-04-13 07:18:50 +00:00
ogrs "git.loafle.net/overflow/gateway_rpc/servlet"
2018-04-12 12:07:02 +00:00
)
2018-04-13 07:18:50 +00:00
type DataServlet interface {
ogrs.RPCServlet
}
type DataServlets struct {
ogrs.RPCServlets
VerifyKey *rsa.PublicKey
SignKey *rsa.PrivateKey
connections sync.Map
}
func (s *DataServlets) Init(serverCtx server.ServerCtx) error {
if err := s.RPCServlets.Init(serverCtx); nil != err {
return err
}
return nil
}
func (s *DataServlets) OnStart(serverCtx server.ServerCtx) error {
if err := s.RPCServlets.OnStart(serverCtx); nil != err {
return err
}
return nil
}
func (s *DataServlets) OnStop(serverCtx server.ServerCtx) {
s.RPCServlets.OnStop(serverCtx)
}
func (s *DataServlets) Destroy(serverCtx server.ServerCtx) {
s.RPCServlets.Destroy(serverCtx)
}
func (s *DataServlets) Handshake(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx) (*fasthttp.ResponseHeader, error) {
// probe key extraction
bMethod := ctx.Request.Header.Peek(ocpc.HTTPRequestHeaderKey_Probe_Method)
if nil == bMethod {
return nil, fmt.Errorf("Unexpected probe method: %v", bMethod)
}
method := string(bMethod)
switch method {
case ocpc.HTTPRequestHeaderValue_Probe_Method_Connect:
default:
2018-04-13 07:21:17 +00:00
return nil, fmt.Errorf("Unexpected probe method: %s", method)
2018-04-13 07:18:50 +00:00
}
bProbeKey := ctx.Request.Header.Peek(ocpc.HTTPRequestHeaderKey_Probe_ProbeKey)
if nil == bProbeKey {
return nil, fmt.Errorf("Unexpected probe key : %v", bProbeKey)
}
probeKey := string(bProbeKey)
grpcCTX := context.Background()
2018-04-17 14:10:13 +00:00
_, err := grpc.Exec(grpcCTX, "ProbeService.readByProbeKey", probeKey)
2018-04-13 07:18:50 +00:00
if nil != err {
return nil, fmt.Errorf("grpc call Error: %s", err.Error())
}
2018-04-17 14:10:13 +00:00
servletCtx.SetAttribute(og.SessionIDKey, probeKey)
2018-04-13 07:18:50 +00:00
servletCtx.SetAttribute(og.SessionClientTypeKey, og.PROBE)
2018-04-17 14:10:13 +00:00
servletCtx.SetAttribute(og.SessionTargetIDKey, probeKey)
2018-04-13 07:18:50 +00:00
2018-04-17 14:10:13 +00:00
return nil, nil
2018-04-13 07:18:50 +00:00
}
func (s *DataServlets) OnConnect(servletCtx server.ServletCtx, conn socket.Conn) {
s.RPCServlets.OnConnect(servletCtx, conn)
}
func (s *DataServlets) OnDisconnect(servletCtx server.ServletCtx) {
s.RPCServlets.OnDisconnect(servletCtx)
2018-04-13 07:21:17 +00:00
}