2018-04-09 13:37:03 +00:00
|
|
|
package servlet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rsa"
|
|
|
|
"fmt"
|
|
|
|
|
2018-04-11 13:08:56 +00:00
|
|
|
"git.loafle.net/commons/logging-go"
|
2018-04-09 13:37:03 +00:00
|
|
|
"git.loafle.net/commons/server-go"
|
2018-06-30 17:32:51 +00:00
|
|
|
css "git.loafle.net/commons/server-go/socket"
|
2018-05-11 08:10:21 +00:00
|
|
|
oe "git.loafle.net/overflow/external-go"
|
2018-04-09 16:08:54 +00:00
|
|
|
og "git.loafle.net/overflow/gateway"
|
2018-04-09 13:37:03 +00:00
|
|
|
ogs "git.loafle.net/overflow/gateway/subscribe"
|
|
|
|
ogrs "git.loafle.net/overflow/gateway_rpc/servlet"
|
|
|
|
"git.loafle.net/overflow/member_gateway_rpc/subscribe"
|
2018-04-13 09:07:14 +00:00
|
|
|
|
2018-04-09 13:37:03 +00:00
|
|
|
"github.com/dgrijalva/jwt-go"
|
2018-04-11 13:08:56 +00:00
|
|
|
"github.com/satori/go.uuid"
|
2018-04-09 13:37:03 +00:00
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
)
|
|
|
|
|
|
|
|
type WebappServlet interface {
|
|
|
|
ogrs.RPCServlet
|
|
|
|
}
|
|
|
|
|
|
|
|
type WebappServlets struct {
|
|
|
|
ogrs.RPCServlets
|
2018-04-09 16:08:54 +00:00
|
|
|
|
2018-04-11 13:08:56 +00:00
|
|
|
VerifyKey *rsa.PublicKey
|
|
|
|
SignKey *rsa.PrivateKey
|
2018-04-09 13:37:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WebappServlets) Init(serverCtx server.ServerCtx) error {
|
|
|
|
if err := s.RPCServlets.Init(serverCtx); nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WebappServlets) OnStart(serverCtx server.ServerCtx) error {
|
|
|
|
if err := s.RPCServlets.OnStart(serverCtx); nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
subscribeChan, err := subscribe.Subscriber.Subscribe("/webapp")
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go s.handleSubscribe(serverCtx, subscribeChan)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WebappServlets) OnStop(serverCtx server.ServerCtx) {
|
|
|
|
if err := subscribe.Subscriber.Unsubscribe("/webapp"); nil != err {
|
|
|
|
logging.Logger().Warn(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.RPCServlets.OnStop(serverCtx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WebappServlets) Destroy(serverCtx server.ServerCtx) {
|
|
|
|
|
|
|
|
s.RPCServlets.Destroy(serverCtx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WebappServlets) Handshake(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx) (*fasthttp.ResponseHeader, error) {
|
|
|
|
var ok bool
|
|
|
|
|
|
|
|
tokenString := string(ctx.QueryArgs().Peek("authToken"))
|
|
|
|
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
|
|
|
// Don't forget to validate the alg is what you expect:
|
|
|
|
if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok {
|
2018-04-10 03:58:53 +00:00
|
|
|
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
|
2018-04-09 13:37:03 +00:00
|
|
|
}
|
|
|
|
// hmacSampleSecret is a []byte containing your secret, e.g. []byte("my_secret_key")
|
2018-04-11 13:08:56 +00:00
|
|
|
return s.VerifyKey, nil
|
2018-04-09 13:37:03 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if nil != err {
|
2018-04-10 06:13:02 +00:00
|
|
|
return nil, err
|
2018-04-09 13:37:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var claims jwt.MapClaims
|
|
|
|
if claims, ok = token.Claims.(jwt.MapClaims); !ok || !token.Valid {
|
|
|
|
return nil, fmt.Errorf("Token is not valid %v", token)
|
|
|
|
}
|
|
|
|
|
|
|
|
userEmail := claims["sub"].(string)
|
|
|
|
sessionID := uuid.NewV4().String()
|
|
|
|
|
2018-04-09 16:08:54 +00:00
|
|
|
servletCtx.SetAttribute(og.SessionIDKey, sessionID)
|
2018-05-11 08:10:21 +00:00
|
|
|
servletCtx.SetAttribute(og.SessionClientTypeKey, oe.MEMBER)
|
2018-04-09 16:08:54 +00:00
|
|
|
servletCtx.SetAttribute(og.SessionTargetIDKey, userEmail)
|
2018-04-09 13:37:03 +00:00
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-06-30 17:32:51 +00:00
|
|
|
func (s *WebappServlets) OnConnect(servletCtx server.ServletCtx, conn css.Conn) {
|
2018-04-09 16:08:54 +00:00
|
|
|
s.RPCServlets.OnConnect(servletCtx, conn)
|
2018-04-09 13:37:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WebappServlets) OnDisconnect(servletCtx server.ServletCtx) {
|
2018-04-09 16:08:54 +00:00
|
|
|
s.RPCServlets.OnDisconnect(servletCtx)
|
2018-04-09 13:37:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WebappServlets) handleSubscribe(serverCtx server.ServerCtx, subscribeChan <-chan *ogs.Message) {
|
2018-04-13 11:24:06 +00:00
|
|
|
var sessions []*ogrs.Session
|
|
|
|
|
|
|
|
LOOP:
|
2018-04-09 16:08:54 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg, ok := <-subscribeChan:
|
2018-04-09 16:10:06 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2018-04-13 11:24:06 +00:00
|
|
|
|
2018-04-09 16:08:54 +00:00
|
|
|
switch msg.TargetType {
|
|
|
|
case ogs.MEMBER:
|
2018-04-13 12:20:16 +00:00
|
|
|
sessions = s.GetSessionsByTargetIDs(msg.Targets)
|
2018-04-10 03:58:53 +00:00
|
|
|
case ogs.MEMBER_SESSION:
|
2018-04-13 12:20:16 +00:00
|
|
|
sessions = s.GetSessions(msg.Targets)
|
2018-04-09 16:08:54 +00:00
|
|
|
default:
|
2018-04-10 03:58:53 +00:00
|
|
|
logging.Logger().Warnf("Subscriber: Unknown TargetType %s", msg.TargetType)
|
2018-04-13 11:24:06 +00:00
|
|
|
continue LOOP
|
2018-04-09 16:08:54 +00:00
|
|
|
}
|
2018-04-13 11:24:06 +00:00
|
|
|
|
|
|
|
if nil == sessions || 0 == len(sessions) {
|
|
|
|
continue LOOP
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, session := range sessions {
|
|
|
|
_writeChan := session.ServletCtx.GetAttribute(og.SessionWriteChanKey)
|
|
|
|
if nil != _writeChan {
|
2018-06-30 17:32:51 +00:00
|
|
|
writeChan := _writeChan.(chan<- css.SocketMessage)
|
|
|
|
writeChan <- css.MakeSocketMessage(css.TextMessage, *msg.Message)
|
2018-04-13 11:24:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|