1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-08 11:17:44 -05:00
v2fly/app/commander/commander.go

104 lines
2.2 KiB
Go
Raw Normal View History

2018-02-05 17:38:24 -05:00
package commander
//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg commander -path App,Commander
import (
"context"
"net"
"sync"
"google.golang.org/grpc"
"v2ray.com/core"
"v2ray.com/core/common"
2018-05-27 08:42:53 -04:00
"v2ray.com/core/common/signal/done"
2018-02-05 17:38:24 -05:00
)
2018-04-03 18:57:44 -04:00
// Commander is a V2Ray feature that provides gRPC methods to external clients.
2018-02-05 17:38:24 -05:00
type Commander struct {
sync.Mutex
2018-02-08 17:24:35 -05:00
server *grpc.Server
config Config
v *core.Instance
ohm core.OutboundHandlerManager
2018-02-05 17:38:24 -05:00
}
2018-04-03 18:57:44 -04:00
// NewCommander creates a new Commander based on the given config.
func NewCommander(ctx context.Context, config *Config) (*Commander, error) {
2018-02-21 11:05:29 -05:00
v := core.MustFromContext(ctx)
c := &Commander{
config: *config,
ohm: v.OutboundHandlerManager(),
2018-02-08 17:24:35 -05:00
v: v,
}
2018-02-20 15:19:09 -05:00
if err := v.RegisterFeature((*Commander)(nil), c); err != nil {
return nil, err
}
return c, nil
}
2018-04-03 18:29:30 -04:00
// Type implements common.HasType.
2018-02-20 15:19:09 -05:00
func (c *Commander) Type() interface{} {
return (*Commander)(nil)
}
2018-04-03 18:29:30 -04:00
// Start implements common.Runnable.
2018-02-05 17:38:24 -05:00
func (c *Commander) Start() error {
c.Lock()
c.server = grpc.NewServer()
2018-02-08 17:24:35 -05:00
for _, rawConfig := range c.config.Service {
config, err := rawConfig.GetInstance()
if err != nil {
return err
}
2018-05-31 05:55:11 -04:00
rawService, err := core.CreateObject(c.v, config)
2018-02-08 17:24:35 -05:00
if err != nil {
return err
}
service, ok := rawService.(Service)
if !ok {
return newError("not a Service.")
}
service.Register(c.server)
2018-02-05 17:38:24 -05:00
}
c.Unlock()
listener := &OutboundListener{
buffer: make(chan net.Conn, 4),
2018-05-27 08:42:53 -04:00
done: done.New(),
2018-02-05 17:38:24 -05:00
}
go func() {
if err := c.server.Serve(listener); err != nil {
newError("failed to start grpc server").Base(err).AtError().WriteToLog()
}
}()
2018-02-05 17:38:24 -05:00
2018-04-14 07:09:58 -04:00
if err := c.ohm.RemoveHandler(context.Background(), c.config.Tag); err != nil {
newError("failed to remove existing handler").WriteToLog()
}
return c.ohm.AddHandler(context.Background(), &Outbound{
2018-02-05 17:38:24 -05:00
tag: c.config.Tag,
listener: listener,
})
}
2018-04-03 18:29:30 -04:00
// Close implements common.Closable.
2018-02-08 09:39:46 -05:00
func (c *Commander) Close() error {
2018-02-05 17:38:24 -05:00
c.Lock()
defer c.Unlock()
if c.server != nil {
c.server.Stop()
c.server = nil
}
2018-02-08 09:39:46 -05:00
return nil
2018-02-05 17:38:24 -05:00
}
func init() {
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, cfg interface{}) (interface{}, error) {
return NewCommander(ctx, cfg.(*Config))
}))
}