/
provider.go
95 lines (82 loc) · 2.81 KB
/
provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package server
import (
"context"
"math"
"net"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
"github.com/mylxsw/asteria/log"
"github.com/mylxsw/container"
"github.com/mylxsw/glacier"
"github.com/mylxsw/graceful"
"github.com/mylxsw/sync/config"
"github.com/mylxsw/sync/protocol"
"github.com/mylxsw/sync/storage"
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
type ServiceProvider struct{}
func (s *ServiceProvider) Register(app *container.Container) {
app.MustSingleton(func(conf *config.Config, cc *container.Container) *grpc.Server {
authFunc := s.authFunc(cc)
return grpc.NewServer(
grpc.MaxRecvMsgSize(math.MaxInt32),
grpc_middleware.WithStreamServerChain(
grpc_auth.StreamServerInterceptor(authFunc),
grpc_recovery.StreamServerInterceptor(),
),
grpc_middleware.WithUnaryServerChain(
grpc_auth.UnaryServerInterceptor(authFunc),
grpc_recovery.UnaryServerInterceptor(),
),
)
})
}
func (s *ServiceProvider) Boot(app *glacier.Glacier) {
app.MustResolve(func(server *grpc.Server, conf *config.Config, statusStore storage.JobStatusStore) {
// 注册 GRPC Service
protocol.RegisterSyncServiceServer(server, NewSyncServer(conf.FileTransferBufferSize, statusStore))
})
}
func (s *ServiceProvider) Daemon(ctx context.Context, app *glacier.Glacier) {
app.MustResolve(func(server *grpc.Server, gf *graceful.Graceful, conf *config.Config) error {
listener, err := net.Listen("tcp", conf.RPCListenAddr)
if err != nil {
return errors.Wrapf(err, "grpc listen to addr %d failed", conf.RPCListenAddr)
}
// 平滑关闭 Server
gf.AddShutdownHandler(func() {
log.Warning("closing grpc server...")
server.GracefulStop()
})
log.Debugf("grpc server started, listening on %s", conf.RPCListenAddr)
if err := server.Serve(listener); err != nil {
return errors.Wrap(err, "grpc server stopped")
}
return nil
})
}
// authFunc 鉴权中间件
func (s *ServiceProvider) authFunc(cc *container.Container) func(ctx context.Context) (context.Context, error) {
var conf = cc.MustGet(&config.Config{}).(*config.Config)
return func(ctx context.Context) (context.Context, error) {
if conf.RPCToken != "" {
meta, ok := metadata.FromIncomingContext(ctx)
if !ok {
return ctx, status.Errorf(codes.Unauthenticated, "auth failed: token required")
}
token := meta.Get("token")
if len(token) != 1 {
return ctx, status.Errorf(codes.Unauthenticated, "auth failed: invalid token")
}
if conf.RPCToken != token[0] {
return ctx, status.Errorf(codes.Unauthenticated, "auth failed: token not match")
}
}
return ctx, nil
}
}