2018-04-12 12:07:02 +00:00
|
|
|
package servlet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2018-04-12 12:39:48 +00:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
2018-04-12 12:07:02 +00:00
|
|
|
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
|
2018-04-12 12:39:48 +00:00
|
|
|
"git.loafle.net/commons/logging-go"
|
2018-04-12 12:07:02 +00:00
|
|
|
"git.loafle.net/commons/server-go"
|
2018-04-12 12:39:48 +00:00
|
|
|
"git.loafle.net/commons/server-go/socket"
|
2018-04-12 12:07:02 +00:00
|
|
|
ocpc "git.loafle.net/overflow/commons-go/probe/constants"
|
|
|
|
ocpm "git.loafle.net/overflow/commons-go/probe/model"
|
2018-04-12 12:39:48 +00:00
|
|
|
og "git.loafle.net/overflow/gateway"
|
|
|
|
"git.loafle.net/overflow/gateway/external/grpc"
|
2018-04-12 12:07:02 +00:00
|
|
|
ogs "git.loafle.net/overflow/gateway/subscribe"
|
|
|
|
ogrs "git.loafle.net/overflow/gateway_rpc/servlet"
|
2018-04-12 12:39:48 +00:00
|
|
|
"git.loafle.net/overflow/member_gateway_rpc/subscribe"
|
2018-04-12 12:07:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ProbeServlet interface {
|
|
|
|
ogrs.RPCServlet
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProbeServlets struct {
|
|
|
|
ogrs.RPCServlets
|
2018-04-13 09:51:31 +00:00
|
|
|
sessions sync.Map
|
2018-04-12 12:07:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ProbeServlets) Init(serverCtx server.ServerCtx) error {
|
|
|
|
if err := s.RPCServlets.Init(serverCtx); nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ProbeServlets) OnStart(serverCtx server.ServerCtx) error {
|
|
|
|
if err := s.RPCServlets.OnStart(serverCtx); nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
subscribeChan, err := subscribe.Subscriber.Subscribe(ocpc.HTTPEntry_Probe)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go s.handleSubscribe(serverCtx, subscribeChan)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ProbeServlets) OnStop(serverCtx server.ServerCtx) {
|
|
|
|
if err := subscribe.Subscriber.Unsubscribe(ocpc.HTTPEntry_Probe); nil != err {
|
|
|
|
logging.Logger().Warn(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.RPCServlets.OnStop(serverCtx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ProbeServlets) Destroy(serverCtx server.ServerCtx) {
|
|
|
|
|
|
|
|
s.RPCServlets.Destroy(serverCtx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ProbeServlets) Handshake(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx) (*fasthttp.ResponseHeader, error) {
|
|
|
|
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-12 12:07:02 +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()
|
|
|
|
r, err := grpc.Exec(grpcCTX, "ProbeService.readByProbeKey", probeKey)
|
|
|
|
if nil != err {
|
|
|
|
return nil, fmt.Errorf("grpc call Error: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
probe := ocpm.Probe{}
|
|
|
|
err = json.Unmarshal([]byte(r), probe)
|
|
|
|
if nil != err {
|
|
|
|
return nil, fmt.Errorf("grpc result unMarshal Error: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
extHeader := &fasthttp.ResponseHeader{}
|
|
|
|
extHeader.Add(ocpc.HTTPResponseHeaderKey_Probe_SetEncryptionKey, probe.EncryptionKey)
|
|
|
|
|
|
|
|
servletCtx.SetAttribute(og.SessionIDKey, probe.ProbeKey)
|
|
|
|
servletCtx.SetAttribute(og.SessionClientTypeKey, og.PROBE)
|
|
|
|
servletCtx.SetAttribute(og.SessionTargetIDKey, probe.ProbeKey)
|
|
|
|
|
|
|
|
return extHeader, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ProbeServlets) OnConnect(servletCtx server.ServletCtx, conn socket.Conn) {
|
|
|
|
s.RPCServlets.OnConnect(servletCtx, conn)
|
|
|
|
|
|
|
|
sessionID := servletCtx.GetAttribute(og.SessionIDKey)
|
|
|
|
targetID := servletCtx.GetAttribute(og.SessionTargetIDKey)
|
|
|
|
if nil != sessionID && nil != targetID {
|
2018-04-13 09:51:31 +00:00
|
|
|
s.sessions.Store(sessionID.(string), ogrs.RetainSession(targetID.(string), servletCtx))
|
2018-04-12 12:07:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ProbeServlets) OnDisconnect(servletCtx server.ServletCtx) {
|
|
|
|
s.RPCServlets.OnDisconnect(servletCtx)
|
|
|
|
|
|
|
|
sessionID := servletCtx.GetAttribute(og.SessionIDKey)
|
|
|
|
if nil != sessionID {
|
2018-04-13 09:51:31 +00:00
|
|
|
s.sessions.Delete(sessionID.(string))
|
2018-04-12 12:07:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ProbeServlets) handleSubscribe(serverCtx server.ServerCtx, subscribeChan <-chan *ogs.Message) {
|
2018-04-13 09:51:31 +00:00
|
|
|
|
|
|
|
LOOP:
|
2018-04-12 12:07:02 +00:00
|
|
|
for {
|
|
|
|
select {
|
2018-04-12 12:39:48 +00:00
|
|
|
case msg, ok := <-subscribeChan:
|
2018-04-12 12:07:02 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch msg.TargetType {
|
|
|
|
case ogs.PROBE:
|
2018-04-13 09:51:31 +00:00
|
|
|
sessions := s.getProbeSessions(msg.Targets)
|
|
|
|
if nil == sessions || 0 == len(sessions) {
|
|
|
|
continue LOOP
|
|
|
|
}
|
2018-04-12 12:07:02 +00:00
|
|
|
|
2018-04-13 09:51:31 +00:00
|
|
|
for _, session := range sessions {
|
|
|
|
_writeChan := session.ServletCtx.GetAttribute(og.SessionWriteChanKey)
|
|
|
|
if nil != _writeChan {
|
|
|
|
writeChan := _writeChan.(chan<- []byte)
|
2018-04-13 11:06:46 +00:00
|
|
|
writeChan <- *msg.Message
|
2018-04-12 12:07:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 11:15:07 +00:00
|
|
|
func (s *ProbeServlets) getProbeSessions(sessionIDs []string) []*ogrs.Session {
|
2018-04-13 09:07:35 +00:00
|
|
|
var sessions []*ogrs.Session
|
2018-04-12 12:07:02 +00:00
|
|
|
|
2018-04-13 11:15:07 +00:00
|
|
|
if nil == sessionIDs || 0 == len(sessionIDs) {
|
2018-04-13 09:51:31 +00:00
|
|
|
return sessions
|
|
|
|
}
|
|
|
|
|
2018-04-13 11:15:07 +00:00
|
|
|
for _, sessionID := range sessionIDs {
|
|
|
|
session, ok := s.sessions.Load(sessionID)
|
2018-04-13 09:51:31 +00:00
|
|
|
if ok {
|
|
|
|
sessions = append(sessions, session.(*ogrs.Session))
|
2018-04-12 12:07:02 +00:00
|
|
|
}
|
2018-04-13 09:51:31 +00:00
|
|
|
}
|
2018-04-12 12:07:02 +00:00
|
|
|
|
2018-04-13 09:07:35 +00:00
|
|
|
return sessions
|
2018-04-12 12:07:02 +00:00
|
|
|
}
|