diff --git a/glide.yaml b/glide.yaml index 6fafeb8..e25c77d 100644 --- a/glide.yaml +++ b/glide.yaml @@ -9,3 +9,5 @@ import: - server - package: github.com/dgrijalva/jwt-go version: v3.1.0 +- package: github.com/satori/go.uuid + version: v1.2.0 diff --git a/servlet/webapp/gateway_rpc_handlers.go b/servlet/webapp/gateway_rpc_handlers.go index 6b18dfd..ded962c 100644 --- a/servlet/webapp/gateway_rpc_handlers.go +++ b/servlet/webapp/gateway_rpc_handlers.go @@ -5,8 +5,11 @@ import ( "fmt" "io/ioutil" + "github.com/satori/go.uuid" + "git.loafle.net/commons_go/logging" cwf "git.loafle.net/commons_go/websocket_fasthttp" + oogw "git.loafle.net/overflow/overflow_gateway_websocket" "git.loafle.net/overflow/overflow_gateway_websocket/rpc" jwt "github.com/dgrijalva/jwt-go" "github.com/valyala/fasthttp" @@ -56,10 +59,8 @@ func (sh *GatewayRPCHandlers) Init(serverCTX cwf.ServerContext) error { func (sh *GatewayRPCHandlers) Handshake(socketCTX cwf.SocketContext, ctx *fasthttp.RequestCtx) (id string, extensionsHeader *fasthttp.ResponseHeader) { var ok bool // tokenString := string(ctx.Request.Header.Cookie("authToken")) - tokenString := string(ctx.QueryArgs().Peek("authToken")) - // log.Printf("authToken: %s \n", tokenString) - // log.Printf("authToken: %s \n", string(ctx.QueryArgs().Peek("authToken"))) // tokenString := "eyJhbGciOiJSUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJ3d3cub3ZlcmZsb3cuY2xvdWQiLCJleHAiOjE1MjA5MzQzMjAsImlhdCI6MTUyMDg0NzkyMCwiaXNzIjoib3ZlckZsb3ciLCJzdWIiOiJvdmVyZmxvd0Bsb2FmbGUuY29tIn0.Umjy3_qvFe4A2wv62r7s2XbqnX_P22HIvAJfx8azM-lejRzYu0wOx9T1UFjRPFzEJO8fEztG1sq3FN5g2097tS6-kbrV5TvB5HclCsK7H384k9CrZEFSgImT-_7yotcPvKKydzvSzLznlbgglr1QwHKH-5JmqyMlxaaYmARmjPHY_tt6NByfeXHEwIL0rh-4TMbx4QOrsBlOHbaRKQqxy8CLwDN2c8-eoEvu1TEIaCxR9rqPrHgsy__1WOY_QY1MdyPzmToXQiwhPyy0hU3jlpf7G9GQOgIEoBD-yrk_WbsFihB2S4uS0TsyopOnEtfSYT5sItnfzCikjNFAaKac39mhW9rPZ-XbxEHK_UYpiCIBtdmiP2ObBCS0FUT2won0V5l4pEw7MTkDehZJptGl45Xrb-ELn-T1lOE6LWuQnZz2sPu2_UHJBcxhPvX6F7cUA3Jm0UGKytq-QOyrUCoiF9YiruDrCXiUkwTQocFwhAGYYeUqQx0KybqLkmn9mkB6zfdyIbY3rk0ep0Kwgp3HTcQR2_6SVkLNAihBpPvZsE5HyVB3hWrOjq-6p0KLj4jfykykj0pA9hNs9qDp5Xb7MR0vRJo__qgvCEe1CiKWwTJY0dXiDDwcIZtqL1u_UdiwZg5Q_O-OCX90IgIETHZNiqAOCKp1IuO9BdplxyCEOBw" + 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 { @@ -80,7 +81,13 @@ func (sh *GatewayRPCHandlers) Handshake(socketCTX cwf.SocketContext, ctx *fastht return "", nil } - return claims["sub"].(string), nil + userEmail := claims["sub"].(string) + sessionID := uuid.NewV4().String() + + socketCTX.SetAttribute(oogw.ClientTypeKey, oogw.MEMBER) + socketCTX.SetAttribute(oogw.TargetIDKey, userEmail) + + return sessionID, nil } func (sh *GatewayRPCHandlers) Validate() { diff --git a/servlet/webapp/subscriber_handlers.go b/servlet/webapp/subscriber_handlers.go index b167969..44c1555 100644 --- a/servlet/webapp/subscriber_handlers.go +++ b/servlet/webapp/subscriber_handlers.go @@ -1,7 +1,12 @@ package webapp import ( + "fmt" + + "git.loafle.net/commons_go/logging" + cwf "git.loafle.net/commons_go/websocket_fasthttp" "git.loafle.net/commons_go/websocket_fasthttp/websocket" + oogw "git.loafle.net/overflow/overflow_gateway_websocket" oogws "git.loafle.net/overflow/overflow_gateway_websocket/servlet" oogwsub "git.loafle.net/overflow/overflow_gateway_websocket/subscribe" oos "git.loafle.net/overflow/overflow_subscriber" @@ -20,11 +25,40 @@ type SubscriberHandlers struct { } func (sh *SubscriberHandlers) OnSubscribe(channel string, message oos.SubscribeMessage) { - for _, uid := range message.Targets { - if soc := sh.GetSocket(uid); nil != soc { - soc.WriteMessage(websocket.TextMessage, message.Message) + switch message.TargetType { + case oos.MEMBER: + for _, uid := range message.Targets { + sockets := sh.getMemberSockets(uid) + if nil == sockets { + continue + } + + for _, soc := range sockets { + soc.WriteMessage(websocket.TextMessage, message.Message) + } + } + + case oos.MEMBER_SESSION: + for _, sid := range message.Targets { + if soc := sh.GetSocket(sid); nil != soc { + soc.WriteMessage(websocket.TextMessage, message.Message) + } + } + default: + logging.Logger().Warn(fmt.Sprintf("[WEBAPP] SubscriberHandler: Unknown TargetType %s", message.TargetType)) + } + +} + +func (sh *SubscriberHandlers) getMemberSockets(targetID string) []cwf.Socket { + var sockets []cwf.Socket + + for _, socket := range sh.GetSockets() { + if socket.Context().GetAttribute(oogw.TargetIDKey).(string) == targetID { + sockets = append(sockets, socket) } } + return sockets } func (sh *SubscriberHandlers) Validate() {