Commit 66502d8d1bd002a2e4159425b9a441cfc9bcbcfb
1 parent
88e31175
从token中获取uid
Showing
10 changed files
with
122 additions
and
30 deletions
Show diff stats
Makefile
| 1 | 1 | ||
| 2 | 2 | ||
| 3 | -all: ge build run | 3 | +all: gen build |
| 4 | 4 | ||
| 5 | gen: | 5 | gen: |
| 6 | protoc -I./protos --go_out=./protos --go-grpc_out=./protos ./protos/*proto | 6 | protoc -I./protos --go_out=./protos --go-grpc_out=./protos ./protos/*proto |
| @@ -31,4 +31,4 @@ cert: | @@ -31,4 +31,4 @@ cert: | ||
| 31 | 31 | ||
| 32 | 32 | ||
| 33 | 33 | ||
| 34 | -.PHONY: all build protos test | ||
| 35 | \ No newline at end of file | 34 | \ No newline at end of file |
| 35 | +.PHONY: all build protos test cert | ||
| 36 | \ No newline at end of file | 36 | \ No newline at end of file |
actions/roleaction.go
| @@ -3,6 +3,7 @@ package actions | @@ -3,6 +3,7 @@ package actions | ||
| 3 | import ( | 3 | import ( |
| 4 | "context" | 4 | "context" |
| 5 | "errors" | 5 | "errors" |
| 6 | + "fmt" | ||
| 6 | "google.golang.org/grpc/metadata" | 7 | "google.golang.org/grpc/metadata" |
| 7 | "google.golang.org/protobuf/types/known/emptypb" | 8 | "google.golang.org/protobuf/types/known/emptypb" |
| 8 | "pro2d/conf" | 9 | "pro2d/conf" |
| @@ -25,11 +26,16 @@ func (s *GameServer) HeartBeatHandler(ctx context.Context, empty *emptypb.Empty) | @@ -25,11 +26,16 @@ func (s *GameServer) HeartBeatHandler(ctx context.Context, empty *emptypb.Empty) | ||
| 25 | } | 26 | } |
| 26 | 27 | ||
| 27 | func (s *GameServer) CreateRoleHandler(ctx context.Context, in *pb.LoginReq) (*pb.RoleRsp, error) { | 28 | func (s *GameServer) CreateRoleHandler(ctx context.Context, in *pb.LoginReq) (*pb.RoleRsp, error) { |
| 28 | - ok, role := models.RoleExistByUid(in.Uid) | 29 | + account := utils.CheckAuth(ctx) |
| 30 | + if account == nil { | ||
| 31 | + return nil, fmt.Errorf("token error") | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + ok, role := models.RoleExistByUid(account.Uid) | ||
| 29 | if !ok { | 35 | if !ok { |
| 30 | role = models.NewRole(conf.SnowFlack.NextVal()) | 36 | role = models.NewRole(conf.SnowFlack.NextVal()) |
| 31 | role.Role.Device = in.Device | 37 | role.Role.Device = in.Device |
| 32 | - role.Role.Uid = in.Uid | 38 | + role.Role.Uid = account.Uid |
| 33 | role.Create() | 39 | role.Create() |
| 34 | } | 40 | } |
| 35 | return &pb.RoleRsp{ | 41 | return &pb.RoleRsp{ |
| @@ -39,7 +45,12 @@ func (s *GameServer) CreateRoleHandler(ctx context.Context, in *pb.LoginReq) (* | @@ -39,7 +45,12 @@ func (s *GameServer) CreateRoleHandler(ctx context.Context, in *pb.LoginReq) (* | ||
| 39 | } | 45 | } |
| 40 | 46 | ||
| 41 | func (s *GameServer) LoginHandler(ctx context.Context, in *pb.LoginReq) (*pb.RoleRsp, error) { | 47 | func (s *GameServer) LoginHandler(ctx context.Context, in *pb.LoginReq) (*pb.RoleRsp, error) { |
| 42 | - ok, role := models.RoleExistByUid(in.Uid) | 48 | + account := utils.CheckAuth(ctx) |
| 49 | + if account == nil { | ||
| 50 | + return nil, fmt.Errorf("token error") | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + ok, role := models.RoleExistByUid(account.Uid) | ||
| 43 | if !ok { | 54 | if !ok { |
| 44 | return &pb.RoleRsp{ | 55 | return &pb.RoleRsp{ |
| 45 | Code: 1, | 56 | Code: 1, |
actions/server.go
| @@ -2,7 +2,6 @@ package actions | @@ -2,7 +2,6 @@ package actions | ||
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "context" | 4 | "context" |
| 5 | - "fmt" | ||
| 6 | "google.golang.org/grpc" | 5 | "google.golang.org/grpc" |
| 7 | "google.golang.org/grpc/credentials" | 6 | "google.golang.org/grpc/credentials" |
| 8 | "google.golang.org/grpc/reflection" | 7 | "google.golang.org/grpc/reflection" |
| @@ -41,13 +40,16 @@ func (s *LoginServer)Start() error { | @@ -41,13 +40,16 @@ func (s *LoginServer)Start() error { | ||
| 41 | models.InitAccountServerModels() | 40 | models.InitAccountServerModels() |
| 42 | 41 | ||
| 43 | var opts []grpc.ServerOption | 42 | var opts []grpc.ServerOption |
| 44 | - //TLS | ||
| 45 | - creds, err := credentials.NewServerTLSFromFile("keys/server.pem", "keys/server.key") | ||
| 46 | - if err != nil { | ||
| 47 | - utils.Sugar.Errorf("Failed to generate credentials %v", err) | ||
| 48 | - return err | 43 | + |
| 44 | + if conf.GlobalConf.TLS.Status { | ||
| 45 | + //TLS | ||
| 46 | + creds, err := credentials.NewServerTLSFromFile("keys/server.pem", "keys/server.key") | ||
| 47 | + if err != nil { | ||
| 48 | + utils.Sugar.Errorf("Failed to generate credentials %v", err) | ||
| 49 | + return err | ||
| 50 | + } | ||
| 51 | + opts = append(opts, grpc.Creds(creds)) | ||
| 49 | } | 52 | } |
| 50 | - opts = append(opts, grpc.Creds(creds)) | ||
| 51 | 53 | ||
| 52 | //拦截器 | 54 | //拦截器 |
| 53 | opts = append(opts, grpc.UnaryInterceptor(AccountServerInterceptor)) | 55 | opts = append(opts, grpc.UnaryInterceptor(AccountServerInterceptor)) |
| @@ -84,10 +86,6 @@ func GameServerInterceptor(ctx context.Context, req interface{}, info *grpc.Unar | @@ -84,10 +86,6 @@ func GameServerInterceptor(ctx context.Context, req interface{}, info *grpc.Unar | ||
| 84 | handler grpc.UnaryHandler) (interface{}, error) { | 86 | handler grpc.UnaryHandler) (interface{}, error) { |
| 85 | 87 | ||
| 86 | //utils.Sugar.Debugf("gRPC method: %s, %v", info.FullMethod, req) | 88 | //utils.Sugar.Debugf("gRPC method: %s, %v", info.FullMethod, req) |
| 87 | - acc := utils.CheckAuth(ctx) | ||
| 88 | - if acc == nil { | ||
| 89 | - return nil, fmt.Errorf("token error") | ||
| 90 | - } | ||
| 91 | 89 | ||
| 92 | resp, err := handler(ctx, req) | 90 | resp, err := handler(ctx, req) |
| 93 | return resp, err | 91 | return resp, err |
| @@ -103,12 +101,15 @@ func (s *GameServer)Start() error { | @@ -103,12 +101,15 @@ func (s *GameServer)Start() error { | ||
| 103 | 101 | ||
| 104 | var opts []grpc.ServerOption | 102 | var opts []grpc.ServerOption |
| 105 | //TLS | 103 | //TLS |
| 106 | - creds, err := credentials.NewServerTLSFromFile("keys/server.pem", "keys/server.key") | ||
| 107 | - if err != nil { | ||
| 108 | - utils.Sugar.Errorf("Failed to generate credentials %v", err) | ||
| 109 | - return err | 104 | + if conf.GlobalConf.TLS.Status { |
| 105 | + //TLS | ||
| 106 | + creds, err := credentials.NewServerTLSFromFile("keys/server.pem", "keys/server.key") | ||
| 107 | + if err != nil { | ||
| 108 | + utils.Sugar.Errorf("Failed to generate credentials %v", err) | ||
| 109 | + return err | ||
| 110 | + } | ||
| 111 | + opts = append(opts, grpc.Creds(creds)) | ||
| 110 | } | 112 | } |
| 111 | - opts = append(opts, grpc.Creds(creds)) | ||
| 112 | 113 | ||
| 113 | //拦截器 | 114 | //拦截器 |
| 114 | opts = append(opts, grpc.UnaryInterceptor(GameServerInterceptor)) | 115 | opts = append(opts, grpc.UnaryInterceptor(GameServerInterceptor)) |
conf/conf.go
| @@ -29,6 +29,12 @@ type MongoConf struct { | @@ -29,6 +29,12 @@ type MongoConf struct { | ||
| 29 | MaxNum int `yaml:"maxnum"` | 29 | MaxNum int `yaml:"maxnum"` |
| 30 | } | 30 | } |
| 31 | 31 | ||
| 32 | +type TLS struct { | ||
| 33 | + Status bool `yaml:"status"` | ||
| 34 | + Key string `yaml:"key"` | ||
| 35 | + Pem string `yaml:"pem"` | ||
| 36 | +} | ||
| 37 | + | ||
| 32 | type SConf struct { | 38 | type SConf struct { |
| 33 | ID string `yaml:"id"` | 39 | ID string `yaml:"id"` |
| 34 | Name string `yaml:"name"` | 40 | Name string `yaml:"name"` |
| @@ -43,6 +49,7 @@ type ServerConf struct { | @@ -43,6 +49,7 @@ type ServerConf struct { | ||
| 43 | WorkerID int64 `yaml:"workerid"` | 49 | WorkerID int64 `yaml:"workerid"` |
| 44 | DatacenterID int64 `yaml:"datacenterid"` | 50 | DatacenterID int64 `yaml:"datacenterid"` |
| 45 | MongoConf *MongoConf `yaml:"mongo"` | 51 | MongoConf *MongoConf `yaml:"mongo"` |
| 52 | + TLS *TLS `yaml:"tls"` | ||
| 46 | AccountConf *SConf `yaml:"server_account"` | 53 | AccountConf *SConf `yaml:"server_account"` |
| 47 | GameConf *SConf `yaml:"server_game"` | 54 | GameConf *SConf `yaml:"server_game"` |
| 48 | RedisConf *RedisConf `yaml:"redis"` | 55 | RedisConf *RedisConf `yaml:"redis"` |
conf/conf.yaml
| @@ -16,6 +16,11 @@ etcd: | @@ -16,6 +16,11 @@ etcd: | ||
| 16 | endpoints: | 16 | endpoints: |
| 17 | - "192.168.0.206:2379" | 17 | - "192.168.0.206:2379" |
| 18 | 18 | ||
| 19 | +TLS: | ||
| 20 | + status: true | ||
| 21 | + key: "keys/server.key" | ||
| 22 | + pem: "keys/server.pem" | ||
| 23 | + | ||
| 19 | server_account: | 24 | server_account: |
| 20 | id: "1" | 25 | id: "1" |
| 21 | name: "account" | 26 | name: "account" |
test/client.go
| @@ -56,7 +56,6 @@ func Login(loginUri, token, uid string) { | @@ -56,7 +56,6 @@ func Login(loginUri, token, uid string) { | ||
| 56 | client:= pb.NewGameClient(gameConn) | 56 | client:= pb.NewGameClient(gameConn) |
| 57 | var role *pb.Role | 57 | var role *pb.Role |
| 58 | loginRsp, err := client.LoginHandler(context.Background(), &pb.LoginReq{ | 58 | loginRsp, err := client.LoginHandler(context.Background(), &pb.LoginReq{ |
| 59 | - Uid: uid, | ||
| 60 | Device: "111111", | 59 | Device: "111111", |
| 61 | }) | 60 | }) |
| 62 | 61 | ||
| @@ -68,7 +67,7 @@ func Login(loginUri, token, uid string) { | @@ -68,7 +67,7 @@ func Login(loginUri, token, uid string) { | ||
| 68 | 67 | ||
| 69 | if loginRsp.Code != 0 { | 68 | if loginRsp.Code != 0 { |
| 70 | utils.Sugar.Debugf("login fail, role not exist") | 69 | utils.Sugar.Debugf("login fail, role not exist") |
| 71 | - createRole, err := client.CreateRoleHandler(context.Background(), &pb.LoginReq{Uid: uid, Device: "11111"}) | 70 | + createRole, err := client.CreateRoleHandler(context.Background(), &pb.LoginReq{Device: "11111"}) |
| 72 | if err != nil { | 71 | if err != nil { |
| 73 | utils.Sugar.Errorf("create role err: %v", err) | 72 | utils.Sugar.Errorf("create role err: %v", err) |
| 74 | return | 73 | return |
| @@ -0,0 +1,28 @@ | @@ -0,0 +1,28 @@ | ||
| 1 | +package main | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "context" | ||
| 5 | + "google.golang.org/grpc" | ||
| 6 | + "google.golang.org/grpc/credentials" | ||
| 7 | + "log" | ||
| 8 | + "pro2d/protos/pb" | ||
| 9 | +) | ||
| 10 | + | ||
| 11 | +func main() { | ||
| 12 | + var opts []grpc.DialOption | ||
| 13 | + creds, err := credentials.NewClientTLSFromFile("keys/server.pem", "pro2d") | ||
| 14 | + if err != nil { | ||
| 15 | + log.Fatal(err) | ||
| 16 | + return | ||
| 17 | + } | ||
| 18 | + opts = append(opts, grpc.WithTransportCredentials(creds)) | ||
| 19 | + conn, err := grpc.Dial("localhost:8948", opts...) | ||
| 20 | + | ||
| 21 | + helloClient := pb.NewHelloClient(conn) | ||
| 22 | + rsp, err := helloClient.SayHello(context.TODO(), &pb.HelloWorld{Msg: "hello world"}) | ||
| 23 | + if err != nil { | ||
| 24 | + log.Fatal(err) | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + log.Printf("sayhello rsp: %v", rsp) | ||
| 28 | +} |
| @@ -0,0 +1,44 @@ | @@ -0,0 +1,44 @@ | ||
| 1 | +package main | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "context" | ||
| 5 | + "google.golang.org/grpc" | ||
| 6 | + "google.golang.org/grpc/credentials" | ||
| 7 | + "log" | ||
| 8 | + "net" | ||
| 9 | + "pro2d/protos/pb" | ||
| 10 | +) | ||
| 11 | + | ||
| 12 | +type Server struct { | ||
| 13 | + pb.UnimplementedHelloServer | ||
| 14 | +} | ||
| 15 | + | ||
| 16 | +func (s *Server) SayHello(ctx context.Context, in *pb.HelloWorld) (*pb.HelloWorld, error) { | ||
| 17 | + return in, nil | ||
| 18 | +} | ||
| 19 | + | ||
| 20 | +func main() { | ||
| 21 | + // 监听本地端口 | ||
| 22 | + listener, err := net.Listen("tcp", ":8948") | ||
| 23 | + if err != nil { | ||
| 24 | + log.Fatalf("net.Listen err: %v", err) | ||
| 25 | + } | ||
| 26 | + var opts []grpc.ServerOption | ||
| 27 | + // 从输入证书文件和密钥文件为服务端构造TLS凭证 | ||
| 28 | + creds, err := credentials.NewServerTLSFromFile("keys/server.pem", "keys/server.key") | ||
| 29 | + if err != nil { | ||
| 30 | + log.Fatalf("Failed to generate credentials %v", err) | ||
| 31 | + } | ||
| 32 | + opts = append(opts, grpc.Creds(creds)) | ||
| 33 | + // 新建gRPC服务器实例,并开启TLS认证 | ||
| 34 | + grpcServer := grpc.NewServer(opts...) | ||
| 35 | + | ||
| 36 | + // 在gRPC服务器注册我们的服务 | ||
| 37 | + pb.RegisterHelloServer(grpcServer, &Server{}) | ||
| 38 | + log.Println(" net.Listing whth TLS") | ||
| 39 | + //用服务器 Serve() 方法以及我们的端口信息区实现阻塞等待,直到进程被杀死或者 Stop() 被调用 | ||
| 40 | + err = grpcServer.Serve(listener) | ||
| 41 | + if err != nil { | ||
| 42 | + log.Fatalf("grpcServer.Serve err: %v", err) | ||
| 43 | + } | ||
| 44 | +} | ||
| 0 | \ No newline at end of file | 45 | \ No newline at end of file |
utils/common.go
| 1 | package utils | 1 | package utils |
| 2 | 2 | ||
| 3 | const ( | 3 | const ( |
| 4 | + APPID = "Pro2D" | ||
| 5 | + APPKEY = "905c86c1ba58d2d7ea6e9d5549c709a7" //md5(Pro2DSecret) 32位 | ||
| 4 | Pro2DTokenSignedString = "Pro2DSecret" | 6 | Pro2DTokenSignedString = "Pro2DSecret" |
| 5 | - | ||
| 6 | - ACCOUNTDB = "account" | ||
| 7 | - ACCOUNT = "account" | ||
| 8 | - | ||
| 9 | - GAMEDB = "game" | ||
| 10 | - ROLET = "role" | ||
| 11 | ) | 7 | ) |
utils/jwt.go
| @@ -3,6 +3,7 @@ package utils | @@ -3,6 +3,7 @@ package utils | ||
| 3 | import ( | 3 | import ( |
| 4 | "context" | 4 | "context" |
| 5 | "fmt" | 5 | "fmt" |
| 6 | + "pro2d/conf" | ||
| 6 | "pro2d/protos/pb" | 7 | "pro2d/protos/pb" |
| 7 | "time" | 8 | "time" |
| 8 | 9 | ||
| @@ -95,5 +96,5 @@ func (c AuthToken) GetRequestMetadata(ctx context.Context, uri ...string) (map[s | @@ -95,5 +96,5 @@ func (c AuthToken) GetRequestMetadata(ctx context.Context, uri ...string) (map[s | ||
| 95 | } | 96 | } |
| 96 | 97 | ||
| 97 | func (c AuthToken) RequireTransportSecurity() bool { | 98 | func (c AuthToken) RequireTransportSecurity() bool { |
| 98 | - return false | 99 | + return conf.GlobalConf.TLS.Status |
| 99 | } | 100 | } |
| 100 | \ No newline at end of file | 101 | \ No newline at end of file |