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 | 5 | gen: |
6 | 6 | protoc -I./protos --go_out=./protos --go-grpc_out=./protos ./protos/*proto |
... | ... | @@ -31,4 +31,4 @@ cert: |
31 | 31 | |
32 | 32 | |
33 | 33 | |
34 | -.PHONY: all build protos test | |
35 | 34 | \ No newline at end of file |
35 | +.PHONY: all build protos test cert | |
36 | 36 | \ No newline at end of file | ... | ... |
actions/roleaction.go
... | ... | @@ -3,6 +3,7 @@ package actions |
3 | 3 | import ( |
4 | 4 | "context" |
5 | 5 | "errors" |
6 | + "fmt" | |
6 | 7 | "google.golang.org/grpc/metadata" |
7 | 8 | "google.golang.org/protobuf/types/known/emptypb" |
8 | 9 | "pro2d/conf" |
... | ... | @@ -25,11 +26,16 @@ func (s *GameServer) HeartBeatHandler(ctx context.Context, empty *emptypb.Empty) |
25 | 26 | } |
26 | 27 | |
27 | 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 | 35 | if !ok { |
30 | 36 | role = models.NewRole(conf.SnowFlack.NextVal()) |
31 | 37 | role.Role.Device = in.Device |
32 | - role.Role.Uid = in.Uid | |
38 | + role.Role.Uid = account.Uid | |
33 | 39 | role.Create() |
34 | 40 | } |
35 | 41 | return &pb.RoleRsp{ |
... | ... | @@ -39,7 +45,12 @@ func (s *GameServer) CreateRoleHandler(ctx context.Context, in *pb.LoginReq) (* |
39 | 45 | } |
40 | 46 | |
41 | 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 | 54 | if !ok { |
44 | 55 | return &pb.RoleRsp{ |
45 | 56 | Code: 1, | ... | ... |
actions/server.go
... | ... | @@ -2,7 +2,6 @@ package actions |
2 | 2 | |
3 | 3 | import ( |
4 | 4 | "context" |
5 | - "fmt" | |
6 | 5 | "google.golang.org/grpc" |
7 | 6 | "google.golang.org/grpc/credentials" |
8 | 7 | "google.golang.org/grpc/reflection" |
... | ... | @@ -41,13 +40,16 @@ func (s *LoginServer)Start() error { |
41 | 40 | models.InitAccountServerModels() |
42 | 41 | |
43 | 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 | 55 | opts = append(opts, grpc.UnaryInterceptor(AccountServerInterceptor)) |
... | ... | @@ -84,10 +86,6 @@ func GameServerInterceptor(ctx context.Context, req interface{}, info *grpc.Unar |
84 | 86 | handler grpc.UnaryHandler) (interface{}, error) { |
85 | 87 | |
86 | 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 | 90 | resp, err := handler(ctx, req) |
93 | 91 | return resp, err |
... | ... | @@ -103,12 +101,15 @@ func (s *GameServer)Start() error { |
103 | 101 | |
104 | 102 | var opts []grpc.ServerOption |
105 | 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 | 115 | opts = append(opts, grpc.UnaryInterceptor(GameServerInterceptor)) | ... | ... |
conf/conf.go
... | ... | @@ -29,6 +29,12 @@ type MongoConf struct { |
29 | 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 | 38 | type SConf struct { |
33 | 39 | ID string `yaml:"id"` |
34 | 40 | Name string `yaml:"name"` |
... | ... | @@ -43,6 +49,7 @@ type ServerConf struct { |
43 | 49 | WorkerID int64 `yaml:"workerid"` |
44 | 50 | DatacenterID int64 `yaml:"datacenterid"` |
45 | 51 | MongoConf *MongoConf `yaml:"mongo"` |
52 | + TLS *TLS `yaml:"tls"` | |
46 | 53 | AccountConf *SConf `yaml:"server_account"` |
47 | 54 | GameConf *SConf `yaml:"server_game"` |
48 | 55 | RedisConf *RedisConf `yaml:"redis"` | ... | ... |
conf/conf.yaml
test/client.go
... | ... | @@ -56,7 +56,6 @@ func Login(loginUri, token, uid string) { |
56 | 56 | client:= pb.NewGameClient(gameConn) |
57 | 57 | var role *pb.Role |
58 | 58 | loginRsp, err := client.LoginHandler(context.Background(), &pb.LoginReq{ |
59 | - Uid: uid, | |
60 | 59 | Device: "111111", |
61 | 60 | }) |
62 | 61 | |
... | ... | @@ -68,7 +67,7 @@ func Login(loginUri, token, uid string) { |
68 | 67 | |
69 | 68 | if loginRsp.Code != 0 { |
70 | 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 | 71 | if err != nil { |
73 | 72 | utils.Sugar.Errorf("create role err: %v", err) |
74 | 73 | return | ... | ... |
... | ... | @@ -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 @@ |
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 | 45 | \ No newline at end of file | ... | ... |
utils/common.go
utils/jwt.go
... | ... | @@ -3,6 +3,7 @@ package utils |
3 | 3 | import ( |
4 | 4 | "context" |
5 | 5 | "fmt" |
6 | + "pro2d/conf" | |
6 | 7 | "pro2d/protos/pb" |
7 | 8 | "time" |
8 | 9 | |
... | ... | @@ -95,5 +96,5 @@ func (c AuthToken) GetRequestMetadata(ctx context.Context, uri ...string) (map[s |
95 | 96 | } |
96 | 97 | |
97 | 98 | func (c AuthToken) RequireTransportSecurity() bool { |
98 | - return false | |
99 | + return conf.GlobalConf.TLS.Status | |
99 | 100 | } |
100 | 101 | \ No newline at end of file | ... | ... |