probe_gateway_rpc/servlet/data-servlet.go
crusader 3fb41cf0b3 ing
2018-04-26 17:17:12 +09:00

100 lines
2.3 KiB
Go

package servlet
import (
"context"
"crypto/rsa"
"fmt"
"sync"
"github.com/valyala/fasthttp"
"git.loafle.net/commons/server-go"
"git.loafle.net/commons/server-go/socket"
occp "git.loafle.net/overflow/commons-go/config/probe"
og "git.loafle.net/overflow/gateway"
"git.loafle.net/overflow/gateway/external/grpc"
ogrs "git.loafle.net/overflow/gateway_rpc/servlet"
)
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(occp.HTTPRequestHeaderKey_Probe_Method)
if nil == bMethod {
return nil, fmt.Errorf("Unexpected probe method: %v", bMethod)
}
method := string(bMethod)
switch method {
case occp.HTTPRequestHeaderValue_Probe_Method_Connect:
default:
return nil, fmt.Errorf("Unexpected probe method: %s", method)
}
bProbeKey := ctx.Request.Header.Peek(occp.HTTPRequestHeaderKey_Probe_ProbeKey)
if nil == bProbeKey {
return nil, fmt.Errorf("Unexpected probe key : %v", bProbeKey)
}
probeKey := string(bProbeKey)
grpcCTX := context.Background()
_, err := grpc.Exec(grpcCTX, "ProbeService.readByProbeKey", probeKey)
if nil != err {
return nil, fmt.Errorf("grpc call Error: %s", err.Error())
}
servletCtx.SetAttribute(og.SessionIDKey, probeKey)
servletCtx.SetAttribute(og.SessionClientTypeKey, og.PROBE)
servletCtx.SetAttribute(og.SessionTargetIDKey, probeKey)
return nil, nil
}
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)
}