github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/app/instman/command/command.go (about) 1 package command 2 3 import ( 4 "context" 5 "encoding/base64" 6 7 "google.golang.org/grpc" 8 9 core "github.com/v2fly/v2ray-core/v5" 10 "github.com/v2fly/v2ray-core/v5/common" 11 "github.com/v2fly/v2ray-core/v5/features/extension" 12 ) 13 14 type service struct { 15 UnimplementedInstanceManagementServiceServer 16 17 instman extension.InstanceManagement 18 } 19 20 func (s service) ListInstance(ctx context.Context, req *ListInstanceReq) (*ListInstanceResp, error) { 21 instanceNames, err := s.instman.ListInstance(ctx) 22 if err != nil { 23 return nil, err 24 } 25 return &ListInstanceResp{Name: instanceNames}, nil 26 } 27 28 func (s service) AddInstance(ctx context.Context, req *AddInstanceReq) (*AddInstanceResp, error) { 29 configContent, err := base64.StdEncoding.DecodeString(req.ConfigContentB64) 30 if err != nil { 31 return nil, err 32 } 33 err = s.instman.AddInstance(ctx, req.Name, configContent, req.ConfigType) 34 if err != nil { 35 return nil, err 36 } 37 return &AddInstanceResp{}, nil 38 } 39 40 func (s service) StartInstance(ctx context.Context, req *StartInstanceReq) (*StartInstanceResp, error) { 41 err := s.instman.StartInstance(ctx, req.Name) 42 if err != nil { 43 return nil, err 44 } 45 return &StartInstanceResp{}, nil 46 } 47 48 func (s service) Register(server *grpc.Server) { 49 RegisterInstanceManagementServiceServer(server, s) 50 } 51 52 func init() { 53 common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, cfg interface{}) (interface{}, error) { 54 s := core.MustFromContext(ctx) 55 sv := &service{} 56 err := s.RequireFeatures(func(instman extension.InstanceManagement) { 57 sv.instman = instman 58 }) 59 if err != nil { 60 return nil, err 61 } 62 return sv, nil 63 })) 64 }