From e92a8ead8903631e1dfc893dd7f04f9bf6635d3d Mon Sep 17 00:00:00 2001 From: crusader Date: Tue, 14 Nov 2017 19:10:28 +0900 Subject: [PATCH] ing --- config.json | 2 +- glide.yaml | 2 ++ main.go | 13 +++++-- server/server_handler.go | 9 +++++ server/server_handlers.go | 30 ++++++++++++---- servlet/web/rpc_gateway_handlers.go | 56 +++++++++++++++++++++++++++++ servlet/web/servlet.go | 1 - servlet/web/subscriber_handlers.go | 30 ++++++++++++++++ servlet/web/web.go | 30 ++++++++++++++++ 9 files changed, 163 insertions(+), 10 deletions(-) create mode 100644 server/server_handler.go create mode 100644 servlet/web/rpc_gateway_handlers.go delete mode 100644 servlet/web/servlet.go create mode 100644 servlet/web/web.go diff --git a/config.json b/config.json index d558ea0..b1fd742 100644 --- a/config.json +++ b/config.json @@ -37,7 +37,7 @@ "wait": false } }, - "handlers": { + "servlets": { "web": { "entry": "/web", "socket": { diff --git a/glide.yaml b/glide.yaml index 8a13bc4..6fafeb8 100644 --- a/glide.yaml +++ b/glide.yaml @@ -7,3 +7,5 @@ import: - package: git.loafle.net/overflow/overflow_gateway_websocket subpackages: - server +- package: github.com/dgrijalva/jwt-go + version: v3.1.0 diff --git a/main.go b/main.go index d02eb44..c43fed8 100644 --- a/main.go +++ b/main.go @@ -9,14 +9,14 @@ import ( "git.loafle.net/commons_go/logging" "git.loafle.net/overflow/overflow_gateway_webapp/server" + "git.loafle.net/overflow/overflow_gateway_webapp/servlet/web" oogws "git.loafle.net/overflow/overflow_gateway_websocket/server" ) func main() { defer logging.Logger().Sync() - sh := &server.ServerHandlers{} - s := oogws.New(sh) + s := initializeServer() stop := make(chan os.Signal) signal.Notify(stop, syscall.SIGINT) @@ -36,3 +36,12 @@ func main() { s.Stop() } + +func initializeServer() oogws.Server { + sh := server.NewServerHandler() + web.Initialize(sh) + + s := oogws.New(sh) + + return s +} diff --git a/server/server_handler.go b/server/server_handler.go new file mode 100644 index 0000000..b244ea9 --- /dev/null +++ b/server/server_handler.go @@ -0,0 +1,9 @@ +package server + +import ( + oogws "git.loafle.net/overflow/overflow_gateway_websocket/server" +) + +type ServerHandler interface { + oogws.ServerHandler +} diff --git a/server/server_handlers.go b/server/server_handlers.go index 0309621..2b6b5b9 100644 --- a/server/server_handlers.go +++ b/server/server_handlers.go @@ -1,17 +1,24 @@ package server import ( - oofws "git.loafle.net/overflow/overflow_gateway_websocket/server" + oogws "git.loafle.net/overflow/overflow_gateway_websocket/server" + "github.com/valyala/fasthttp" ) +func NewServerHandler() ServerHandler { + sh := &ServerHandlers{} + sh.ServerHandler = oogws.NewServerHandler() + return sh +} + type ServerHandlers struct { - oofws.ServerHandlers + oogws.ServerHandler } // Init invoked before the server is started // If you override ths method, must call func (sh *ServerHandlers) Init() error { - if err := sh.ServerHandlers.Init(); nil != err { + if err := sh.ServerHandler.Init(); nil != err { return err } @@ -19,15 +26,26 @@ func (sh *ServerHandlers) Init() error { } func (sh *ServerHandlers) OnStart() { - sh.ServerHandlers.OnStart() + sh.ServerHandler.OnStart() } +func (sh *ServerHandlers) CheckOrigin(ctx *fasthttp.RequestCtx) bool { + if origin := string(ctx.Request.Header.Peek("Origin")); origin != "" { + ctx.Response.Header.Set("Access-Control-Allow-Origin", origin) + if string(ctx.Method()) == "OPTIONS" && string(ctx.Request.Header.Peek("Access-Control-Request-Method")) != "" { + ctx.Response.Header.Set("Access-Control-Allow-Headers", "Content-Type, Accept") + ctx.Response.Header.Set("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE") + } + } + return true +} + func (sh *ServerHandlers) OnStop() { - sh.ServerHandlers.OnStop() + sh.ServerHandler.OnStop() } func (sh *ServerHandlers) Validate() { - sh.ServerHandlers.Validate() + sh.ServerHandler.Validate() } diff --git a/servlet/web/rpc_gateway_handlers.go b/servlet/web/rpc_gateway_handlers.go new file mode 100644 index 0000000..d3fb28c --- /dev/null +++ b/servlet/web/rpc_gateway_handlers.go @@ -0,0 +1,56 @@ +package web + +import ( + "fmt" + + "git.loafle.net/commons_go/logging" + "git.loafle.net/overflow/overflow_gateway_websocket/config" + "git.loafle.net/overflow/overflow_gateway_websocket/rpc" + jwt "github.com/dgrijalva/jwt-go" + "github.com/valyala/fasthttp" +) + +func newRPCGatewayHandler() rpc.RPCGatewayHandler { + gh := &RPCGatewayHandlers{} + + return gh +} + +type RPCGatewayHandlers struct { + rpc.RPCGatewayHandlers +} + +func (sh *RPCGatewayHandlers) Init() error { + return nil +} + +func (sh *RPCGatewayHandlers) Handshake(ctx *fasthttp.RequestCtx) (id string, extensionsHeader *fasthttp.ResponseHeader) { + var ok bool + // tokenString := string(soc.Conn().Headers().Cookie("AuthToken")) + tokenString := "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJvdmVyRmxvdyIsImlhdCI6MTUwNDU5NTExOSwiZXhwIjoxNTM2MTMxMTE5LCJhdWQiOiJ3d3cub3ZlcmZsb3cuY2xvdWQiLCJzdWIiOiJvdmVyZmxvd0Bsb2FmbGUuY29tIn0.-WQi3OykPlJ9x8RcZGhWXEtGw4GhU6wmyJ_AWh2rMeUatQylfPzvmum2Xfp6pwKLMmcP76XoDPNyq06i7RKWNQ" + 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.SigningMethodHMAC); !ok { + return nil, fmt.Errorf("Webapp: Unexpected signing method: %v", token.Header["alg"]) + } + + // hmacSampleSecret is a []byte containing your secret, e.g. []byte("my_secret_key") + return []byte(config.Config.Auth.SigningKey), nil + }) + + if nil != err { + return "", nil + } + + var claims jwt.MapClaims + if claims, ok = token.Claims.(jwt.MapClaims); !ok || !token.Valid { + logging.Logger().Warn(fmt.Sprintf("Webapp: Token is not valid %v", token)) + return "", nil + } + + return claims["sub"].(string), nil +} + +func (sh *RPCGatewayHandlers) Validate() { + sh.RPCGatewayHandlers.Validate() +} diff --git a/servlet/web/servlet.go b/servlet/web/servlet.go deleted file mode 100644 index efb3895..0000000 --- a/servlet/web/servlet.go +++ /dev/null @@ -1 +0,0 @@ -package web diff --git a/servlet/web/subscriber_handlers.go b/servlet/web/subscriber_handlers.go index 24df326..8932d6a 100644 --- a/servlet/web/subscriber_handlers.go +++ b/servlet/web/subscriber_handlers.go @@ -1,2 +1,32 @@ package web +import ( + "git.loafle.net/commons_go/websocket_fasthttp/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" +) + +func newSubscriberHandler(servletHandler oogws.ServletHandler) oogwsub.SubscriberHandler { + sh := &SubscriberHandlers{} + sh.ServletHandler = servletHandler + sh.Channel = cfg.Entry + + return sh +} + +type SubscriberHandlers struct { + oogwsub.SubscriberHandlers +} + +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) + } + } +} + +func (sh *SubscriberHandlers) Validate() { + sh.SubscriberHandlers.Validate() +} diff --git a/servlet/web/web.go b/servlet/web/web.go new file mode 100644 index 0000000..4ce3065 --- /dev/null +++ b/servlet/web/web.go @@ -0,0 +1,30 @@ +package web + +import ( + "fmt" + + "git.loafle.net/commons_go/logging" + oogwc "git.loafle.net/overflow/overflow_gateway_websocket/config" + oogws "git.loafle.net/overflow/overflow_gateway_websocket/server" +) + +const ( + Name = "web" +) + +var ( + cfg *oogwc.Servlet +) + +func Initialize(sh oogws.ServerHandler) { + cfg = oogwc.Config.Servlets[Name] + if nil == cfg { + logging.Logger().Panic(fmt.Sprintf("Webapp: config of servlet[%s] is not exist", Name)) + } + + rpcGH := newRPCGatewayHandler() + rpcSH := sh.RegisterRPCGatewayServlet(Name, rpcGH) + sub := newSubscriberHandler(rpcSH) + + sh.RegisterSubscriber(sub) +}