probe_gateway_rpc/servlet/auth-servlet.go

211 lines
6.2 KiB
Go
Raw Normal View History

2018-04-12 12:07:02 +00:00
package servlet
import (
"context"
2018-04-12 12:39:48 +00:00
"encoding/base64"
2018-04-12 12:07:02 +00:00
"encoding/json"
2018-04-12 12:39:48 +00:00
"fmt"
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-26 08:17:12 +00:00
occn "git.loafle.net/overflow/commons-go/config/noauthprobe"
2018-05-10 10:03:55 +00:00
ocmm "git.loafle.net/overflow/commons-go/model/meta"
2018-04-26 08:17:12 +00:00
ocmn "git.loafle.net/overflow/commons-go/model/noauthprobe"
2018-05-11 08:20:11 +00:00
oe "git.loafle.net/overflow/external-go"
oeg "git.loafle.net/overflow/external-go/grpc"
2018-04-12 12:39:48 +00:00
og "git.loafle.net/overflow/gateway"
2018-04-12 12:07:02 +00:00
ogs "git.loafle.net/overflow/gateway/subscribe"
ogrs "git.loafle.net/overflow/gateway_rpc/servlet"
2018-05-11 08:20:11 +00:00
opgrs "git.loafle.net/overflow/probe_gateway_rpc/subscribe"
2018-04-12 12:07:02 +00:00
)
type AuthServlet interface {
ogrs.RPCServlet
}
type AuthServlets struct {
ogrs.RPCServlets
}
func (s *AuthServlets) Init(serverCtx server.ServerCtx) error {
if err := s.RPCServlets.Init(serverCtx); nil != err {
return err
}
return nil
}
func (s *AuthServlets) OnStart(serverCtx server.ServerCtx) error {
if err := s.RPCServlets.OnStart(serverCtx); nil != err {
return err
}
2018-05-11 08:20:11 +00:00
subscribeChan, err := opgrs.Subscriber.Subscribe(occn.HTTPEntry_Auth)
2018-04-12 12:07:02 +00:00
if nil != err {
return err
}
go s.handleSubscribe(serverCtx, subscribeChan)
return nil
}
func (s *AuthServlets) OnStop(serverCtx server.ServerCtx) {
2018-05-11 08:20:11 +00:00
if err := opgrs.Subscriber.Unsubscribe(occn.HTTPEntry_Auth); nil != err {
2018-04-12 12:07:02 +00:00
logging.Logger().Warn(err)
}
s.RPCServlets.OnStop(serverCtx)
}
func (s *AuthServlets) Destroy(serverCtx server.ServerCtx) {
s.RPCServlets.Destroy(serverCtx)
}
func (s *AuthServlets) Handshake(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx) (*fasthttp.ResponseHeader, error) {
2018-04-26 08:17:12 +00:00
bMethod := ctx.Request.Header.Peek(occn.HTTPRequestHeaderKey_NoAuthProbe_Method)
2018-04-12 12:07:02 +00:00
if nil == bMethod {
return nil, fmt.Errorf("Unexpected noauth probe method: %v", bMethod)
}
method := string(bMethod)
switch method {
2018-04-26 08:17:12 +00:00
case occn.HTTPRequestHeaderValue_NoAuthProbe_Method_Regist:
bInfo := ctx.Request.Header.Peek(occn.HTTPRequestHeaderKey_NoAuthProbe_Info)
2018-04-12 12:07:02 +00:00
if nil == bInfo {
2018-04-13 07:21:17 +00:00
return nil, fmt.Errorf("Unexpected Request Header Key NoAuthProbe Info")
2018-04-12 12:07:02 +00:00
}
rJSON := make([]byte, base64.StdEncoding.DecodedLen(len(bInfo)))
_, err := base64.StdEncoding.Decode(rJSON, bInfo)
if nil != err {
return nil, fmt.Errorf("Base64 Encoding Error: %s", err.Error())
}
grpcCTX := context.Background()
2018-05-11 08:20:11 +00:00
r, err := oeg.Exec(grpcCTX, "NoAuthProbeService.regist", string(rJSON))
2018-04-12 12:07:02 +00:00
if nil != err {
return nil, fmt.Errorf("grpc call Error: %s", err.Error())
}
2018-05-10 10:03:55 +00:00
2018-04-26 08:17:12 +00:00
nap := &ocmn.NoAuthProbe{}
2018-04-12 12:07:02 +00:00
err = json.Unmarshal([]byte(r), nap)
if nil != err {
return nil, fmt.Errorf("grpc result unMarshal Error: %s", err.Error())
}
extHeader := &fasthttp.ResponseHeader{}
2018-05-10 10:03:55 +00:00
extHeader.Add(occn.HTTPResponseHeaderKey_NoAuthProbe_Method, occn.HTTPResponseHeaderValue_NoAuthProbe_Method_TempProbeKey)
extHeader.Add(occn.HTTPResponseHeaderKey_NoAuthProbe_Method_Param, nap.TempProbeKey)
2018-04-12 12:07:02 +00:00
2018-04-13 09:51:31 +00:00
servletCtx.SetAttribute(og.SessionIDKey, nap.TempProbeKey)
2018-05-11 08:20:11 +00:00
servletCtx.SetAttribute(og.SessionClientTypeKey, oe.PROBE)
2018-04-13 09:51:31 +00:00
servletCtx.SetAttribute(og.SessionTargetIDKey, nap.TempProbeKey)
2018-04-12 12:07:02 +00:00
return extHeader, nil
2018-04-26 08:17:12 +00:00
case occn.HTTPRequestHeaderValue_NoAuthProbe_Method_Connect:
bTempProbeKey := ctx.Request.Header.Peek(occn.HTTPRequestHeaderKey_NoAuthProbe_TempProbeKey)
2018-04-12 12:07:02 +00:00
if nil == bTempProbeKey {
2018-04-12 12:39:48 +00:00
return nil, fmt.Errorf("Noauth probe temp key is not an existing key")
2018-04-12 12:07:02 +00:00
}
2018-04-12 12:39:48 +00:00
tempProbeKey := string(bTempProbeKey)
2018-04-12 12:07:02 +00:00
grpcCTX := context.Background()
2018-05-11 08:20:11 +00:00
r, err := oeg.Exec(grpcCTX, "NoAuthProbeService.readByTempProbeKey", tempProbeKey)
2018-04-12 12:07:02 +00:00
if nil != err {
2018-04-12 12:39:48 +00:00
return nil, fmt.Errorf("grpc result error: %s", err.Error())
2018-04-12 12:07:02 +00:00
}
2018-05-10 10:03:55 +00:00
nap := &ocmn.NoAuthProbe{}
err = json.Unmarshal([]byte(r), nap)
if nil != err {
return nil, fmt.Errorf("grpc result unMarshal Error: %s", err.Error())
}
extHeader := &fasthttp.ResponseHeader{}
2018-06-05 12:04:12 +00:00
noAuthProbeStatusType := ocmm.ToNoAuthProbeStatusType(nap.MetaNoAuthProbeStatus.Name)
2018-05-10 10:03:55 +00:00
switch noAuthProbeStatusType {
case ocmm.NoAuthProbeStatusTypeACCEPT:
extHeader.Add(occn.HTTPResponseHeaderKey_NoAuthProbe_Method, occn.HTTPResponseHeaderValue_NoAuthProbe_Method_Accept)
extHeader.Add(occn.HTTPResponseHeaderKey_NoAuthProbe_Method_Param, nap.Probe.ProbeKey)
case ocmm.NoAuthProbeStatusTypeDENY:
extHeader.Add(occn.HTTPResponseHeaderKey_NoAuthProbe_Method, occn.HTTPResponseHeaderValue_NoAuthProbe_Method_Deny)
default:
}
2018-04-12 12:39:48 +00:00
servletCtx.SetAttribute(og.SessionIDKey, tempProbeKey)
2018-05-11 08:20:11 +00:00
servletCtx.SetAttribute(og.SessionClientTypeKey, oe.PROBE)
2018-04-12 12:39:48 +00:00
servletCtx.SetAttribute(og.SessionTargetIDKey, tempProbeKey)
2018-04-12 12:07:02 +00:00
2018-05-10 10:17:21 +00:00
return extHeader, nil
2018-04-12 12:07:02 +00:00
default:
2018-04-13 07:21:17 +00:00
return nil, fmt.Errorf("Unexpected noauth probe method: %s", method)
2018-04-12 12:07:02 +00:00
}
}
func (s *AuthServlets) OnConnect(servletCtx server.ServletCtx, conn socket.Conn) {
s.RPCServlets.OnConnect(servletCtx, conn)
2018-05-10 10:03:55 +00:00
_tempProbeKey := servletCtx.GetAttribute(og.SessionIDKey)
if nil != _tempProbeKey {
grpcCTX := context.Background()
2018-05-11 08:20:11 +00:00
_, err := oeg.Exec(grpcCTX, "NoAuthProbeService.onConnect", _tempProbeKey.(string), conn.RemoteAddr().String())
2018-05-10 10:03:55 +00:00
if nil != err {
logging.Logger().Errorf("onConnect error %v", err)
}
}
2018-04-12 12:07:02 +00:00
}
func (s *AuthServlets) OnDisconnect(servletCtx server.ServletCtx) {
s.RPCServlets.OnDisconnect(servletCtx)
2018-05-10 10:03:55 +00:00
_tempProbeKey := servletCtx.GetAttribute(og.SessionIDKey)
if nil != _tempProbeKey {
grpcCTX := context.Background()
2018-05-11 08:20:11 +00:00
_, err := oeg.Exec(grpcCTX, "NoAuthProbeService.onDisconnect", _tempProbeKey.(string))
2018-05-10 10:03:55 +00:00
if nil != err {
logging.Logger().Errorf("onDisconnect error %v", err)
}
}
2018-04-12 12:07:02 +00:00
}
func (s *AuthServlets) handleSubscribe(serverCtx server.ServerCtx, subscribeChan <-chan *ogs.Message) {
2018-04-13 11:23:55 +00:00
var sessions []*ogrs.Session
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 12:17:58 +00:00
sessions = s.GetSessions(msg.Targets)
2018-04-13 11:23:55 +00:00
default:
logging.Logger().Warnf("Subscriber: Unknown TargetType %s", msg.TargetType)
continue LOOP
}
if nil == sessions || 0 == len(sessions) {
continue LOOP
}
2018-04-12 12:07:02 +00:00
2018-04-13 11:23:55 +00:00
for _, session := range sessions {
_writeChan := session.ServletCtx.GetAttribute(og.SessionWriteChanKey)
if nil != _writeChan {
writeChan := _writeChan.(chan<- []byte)
writeChan <- *msg.Message
2018-04-12 12:07:02 +00:00
}
}
}
}
}