This commit is contained in:
geek 2018-04-11 22:08:56 +09:00
parent b486381b2a
commit 99951cc3c0
7 changed files with 45 additions and 42 deletions

Binary file not shown.

View File

@ -7,8 +7,8 @@
"keepAlive": 60, "keepAlive": 60,
"handshakeTimeout": 60, "handshakeTimeout": 60,
"maxMessageSize": 8192, "maxMessageSize": 8192,
"readBufferSize": 1024, "readBufferSize": 4096,
"writeBufferSize": 1024, "writeBufferSize": 4096,
"readTimeout": 0, "readTimeout": 0,
"writeTimeout": 0, "writeTimeout": 0,
"pongTimeout": 60, "pongTimeout": 60,

View File

@ -7,8 +7,8 @@
"keepAlive": 60, "keepAlive": 60,
"handshakeTimeout": 60, "handshakeTimeout": 60,
"maxMessageSize": 8192, "maxMessageSize": 8192,
"readBufferSize": 1024, "readBufferSize": 4096,
"writeBufferSize": 1024, "writeBufferSize": 4096,
"readTimeout": 0, "readTimeout": 0,
"writeTimeout": 0, "writeTimeout": 0,
"pongTimeout": 60, "pongTimeout": 60,

View File

@ -3,9 +3,13 @@ package config
import ( import (
occe "git.loafle.net/overflow/commons-go/config/external" occe "git.loafle.net/overflow/commons-go/config/external"
ogrs "git.loafle.net/overflow/gateway_rpc/server" ogrs "git.loafle.net/overflow/gateway_rpc/server"
"crypto/rsa"
) )
type Config struct { type Config struct {
ServerHandler *ogrs.ServerHandlers `json:"serverHandler"` ServerHandler *ogrs.ServerHandlers `json:"serverHandler"`
External *occe.External `json:"external"` External *occe.External `json:"external"`
VerifyKey *rsa.PublicKey
SignKey *rsa.PrivateKey
} }

29
main.go
View File

@ -13,6 +13,9 @@ import (
"git.loafle.net/commons/logging-go" "git.loafle.net/commons/logging-go"
"git.loafle.net/overflow/member_gateway_rpc/config" "git.loafle.net/overflow/member_gateway_rpc/config"
"git.loafle.net/overflow/member_gateway_rpc/server" "git.loafle.net/overflow/member_gateway_rpc/server"
"io/ioutil"
"github.com/dgrijalva/jwt-go"
"path"
) )
var ( var (
@ -33,8 +36,9 @@ func main() {
if err := configuration.Load(_config, "config.json"); nil != err { if err := configuration.Load(_config, "config.json"); nil != err {
logging.Logger().Panic(err) logging.Logger().Panic(err)
} }
loadKey(*configDir, _config)
s := server.New(*configDir, _config) s := server.New(_config)
go func() { go func() {
err := s.ListenAndServe() err := s.ListenAndServe()
@ -59,3 +63,26 @@ func main() {
logging.Logger().Errorf("error: %v", err) logging.Logger().Errorf("error: %v", err)
} }
} }
func loadKey(configDir string, _config *config.Config) {
signBytes, err := ioutil.ReadFile(path.Join(configDir, "overFlow-private.key"))
if nil != err {
logging.Logger().Panic(err)
}
_config.SignKey, err = jwt.ParseRSAPrivateKeyFromPEM(signBytes)
if nil != err {
logging.Logger().Panic(err)
}
verifyBytes, err := ioutil.ReadFile(path.Join(configDir, "overFlow-public.pem"))
if nil != err {
logging.Logger().Panic(err)
}
_config.VerifyKey, err = jwt.ParseRSAPublicKeyFromPEM(verifyBytes)
if nil != err {
logging.Logger().Panic(err)
}
}

View File

@ -6,9 +6,10 @@ import (
"git.loafle.net/overflow/member_gateway_rpc/servlet" "git.loafle.net/overflow/member_gateway_rpc/servlet"
) )
func New(configDir string, _config *config.Config) *csgw.Server { func New(_config *config.Config) *csgw.Server {
ws := &servlet.WebappServlets{ ws := &servlet.WebappServlets{
ConfigDir: configDir, VerifyKey: _config.VerifyKey,
SignKey: _config.SignKey,
} }
sh := &ServerHandlers{ sh := &ServerHandlers{
ServerHandlers: *_config.ServerHandler, ServerHandlers: *_config.ServerHandler,

View File

@ -3,11 +3,9 @@ package servlet
import ( import (
"crypto/rsa" "crypto/rsa"
"fmt" "fmt"
"io/ioutil"
"path"
"sync" "sync"
logging "git.loafle.net/commons/logging-go" "git.loafle.net/commons/logging-go"
"git.loafle.net/commons/server-go" "git.loafle.net/commons/server-go"
"git.loafle.net/commons/server-go/socket" "git.loafle.net/commons/server-go/socket"
og "git.loafle.net/overflow/gateway" og "git.loafle.net/overflow/gateway"
@ -15,14 +13,10 @@ import (
ogrs "git.loafle.net/overflow/gateway_rpc/servlet" ogrs "git.loafle.net/overflow/gateway_rpc/servlet"
"git.loafle.net/overflow/member_gateway_rpc/subscribe" "git.loafle.net/overflow/member_gateway_rpc/subscribe"
"github.com/dgrijalva/jwt-go" "github.com/dgrijalva/jwt-go"
uuid "github.com/satori/go.uuid" "github.com/satori/go.uuid"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
) )
var (
verifyKey *rsa.PublicKey
signKey *rsa.PrivateKey
)
type WebappServlet interface { type WebappServlet interface {
ogrs.RPCServlet ogrs.RPCServlet
@ -31,40 +25,17 @@ type WebappServlet interface {
type WebappServlets struct { type WebappServlets struct {
ogrs.RPCServlets ogrs.RPCServlets
ConfigDir string VerifyKey *rsa.PublicKey
SignKey *rsa.PrivateKey
connections sync.Map connections sync.Map
} }
func (s *WebappServlets) absolutePath(fileName string) string {
return path.Join(s.ConfigDir, fileName)
}
func (s *WebappServlets) Init(serverCtx server.ServerCtx) error { func (s *WebappServlets) Init(serverCtx server.ServerCtx) error {
if err := s.RPCServlets.Init(serverCtx); nil != err { if err := s.RPCServlets.Init(serverCtx); nil != err {
return err return err
} }
signBytes, err := ioutil.ReadFile(s.absolutePath("overFlow-private.key"))
if nil != err {
return err
}
signKey, err = jwt.ParseRSAPrivateKeyFromPEM(signBytes)
if nil != err {
return err
}
verifyBytes, err := ioutil.ReadFile(s.absolutePath("overFlow-public.pem"))
if nil != err {
return err
}
verifyKey, err = jwt.ParseRSAPublicKeyFromPEM(verifyBytes)
if nil != err {
return err
}
return nil return nil
} }
@ -105,7 +76,7 @@ func (s *WebappServlets) Handshake(servletCtx server.ServletCtx, ctx *fasthttp.R
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"]) return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
} }
// hmacSampleSecret is a []byte containing your secret, e.g. []byte("my_secret_key") // hmacSampleSecret is a []byte containing your secret, e.g. []byte("my_secret_key")
return verifyKey, nil return s.VerifyKey, nil
}) })
if nil != err { if nil != err {
@ -121,7 +92,7 @@ func (s *WebappServlets) Handshake(servletCtx server.ServletCtx, ctx *fasthttp.R
sessionID := uuid.NewV4().String() sessionID := uuid.NewV4().String()
servletCtx.SetAttribute(og.SessionIDKey, sessionID) servletCtx.SetAttribute(og.SessionIDKey, sessionID)
servletCtx.SetAttribute(og.SessionClientTypeKey, ogs.MEMBER) servletCtx.SetAttribute(og.SessionClientTypeKey, og.MEMBER)
servletCtx.SetAttribute(og.SessionTargetIDKey, userEmail) servletCtx.SetAttribute(og.SessionTargetIDKey, userEmail)
return nil, nil return nil, nil