2018-02-05 22:38:24 +00: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"
|
2018-02-06 12:47:50 +00:00
|
|
|
"v2ray.com/core/common"
|
2018-05-27 12:42:53 +00:00
|
|
|
"v2ray.com/core/common/signal/done"
|
2018-02-05 22:38:24 +00:00
|
|
|
)
|
|
|
|
|
2018-04-03 22:57:44 +00:00
|
|
|
// Commander is a V2Ray feature that provides gRPC methods to external clients.
|
2018-02-05 22:38:24 +00:00
|
|
|
type Commander struct {
|
|
|
|
sync.Mutex
|
2018-02-08 22:24:35 +00:00
|
|
|
server *grpc.Server
|
|
|
|
config Config
|
|
|
|
v *core.Instance
|
|
|
|
ohm core.OutboundHandlerManager
|
2018-02-05 22:38:24 +00:00
|
|
|
}
|
|
|
|
|
2018-04-03 22:57:44 +00:00
|
|
|
// NewCommander creates a new Commander based on the given config.
|
2018-02-06 12:47:50 +00:00
|
|
|
func NewCommander(ctx context.Context, config *Config) (*Commander, error) {
|
2018-02-21 16:05:29 +00:00
|
|
|
v := core.MustFromContext(ctx)
|
2018-02-06 12:47:50 +00:00
|
|
|
c := &Commander{
|
|
|
|
config: *config,
|
|
|
|
ohm: v.OutboundHandlerManager(),
|
2018-02-08 22:24:35 +00:00
|
|
|
v: v,
|
2018-02-06 12:47:50 +00:00
|
|
|
}
|
2018-02-20 20:19:09 +00:00
|
|
|
if err := v.RegisterFeature((*Commander)(nil), c); err != nil {
|
2018-02-06 12:47:50 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2018-04-03 22:29:30 +00:00
|
|
|
// Type implements common.HasType.
|
2018-02-20 20:19:09 +00:00
|
|
|
func (c *Commander) Type() interface{} {
|
|
|
|
return (*Commander)(nil)
|
|
|
|
}
|
|
|
|
|
2018-04-03 22:29:30 +00:00
|
|
|
// Start implements common.Runnable.
|
2018-02-05 22:38:24 +00:00
|
|
|
func (c *Commander) Start() error {
|
|
|
|
c.Lock()
|
|
|
|
c.server = grpc.NewServer()
|
2018-02-08 22:24:35 +00:00
|
|
|
for _, rawConfig := range c.config.Service {
|
|
|
|
config, err := rawConfig.GetInstance()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rawService, err := c.v.CreateObject(config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
service, ok := rawService.(Service)
|
|
|
|
if !ok {
|
|
|
|
return newError("not a Service.")
|
|
|
|
}
|
|
|
|
service.Register(c.server)
|
2018-02-05 22:38:24 +00:00
|
|
|
}
|
|
|
|
c.Unlock()
|
|
|
|
|
|
|
|
listener := &OutboundListener{
|
|
|
|
buffer: make(chan net.Conn, 4),
|
2018-05-27 12:42:53 +00:00
|
|
|
done: done.New(),
|
2018-02-05 22:38:24 +00:00
|
|
|
}
|
|
|
|
|
2018-02-06 12:47:50 +00:00
|
|
|
go func() {
|
|
|
|
if err := c.server.Serve(listener); err != nil {
|
|
|
|
newError("failed to start grpc server").Base(err).AtError().WriteToLog()
|
|
|
|
}
|
|
|
|
}()
|
2018-02-05 22:38:24 +00:00
|
|
|
|
2018-04-14 11:09:58 +00: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 22:38:24 +00:00
|
|
|
tag: c.config.Tag,
|
|
|
|
listener: listener,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-03 22:29:30 +00:00
|
|
|
// Close implements common.Closable.
|
2018-02-08 14:39:46 +00:00
|
|
|
func (c *Commander) Close() error {
|
2018-02-05 22:38:24 +00:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
if c.server != nil {
|
|
|
|
c.server.Stop()
|
|
|
|
c.server = nil
|
|
|
|
}
|
2018-02-08 14:39:46 +00:00
|
|
|
|
|
|
|
return nil
|
2018-02-05 22:38:24 +00:00
|
|
|
}
|
2018-02-06 12:47:50 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, cfg interface{}) (interface{}, error) {
|
|
|
|
return NewCommander(ctx, cfg.(*Config))
|
|
|
|
}))
|
|
|
|
}
|