1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-18 02:16:10 -05:00
v2fly/app/commander/commander.go

129 lines
3.0 KiB
Go
Raw Normal View History

2018-02-05 17:38:24 -05:00
package commander
//go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen
2018-02-05 17:38:24 -05:00
import (
"context"
"net"
"sync"
"google.golang.org/grpc"
core "github.com/v2fly/v2ray-core/v5"
"github.com/v2fly/v2ray-core/v5/common"
"github.com/v2fly/v2ray-core/v5/common/serial"
"github.com/v2fly/v2ray-core/v5/common/signal/done"
"github.com/v2fly/v2ray-core/v5/features/outbound"
"github.com/v2fly/v2ray-core/v5/infra/conf/v5cfg"
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-10-21 04:27:13 -04:00
server *grpc.Server
services []Service
ohm outbound.Manager
tag string
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) {
c := &Commander{
2018-10-21 04:27:13 -04:00
tag: config.Tag,
}
2018-10-21 04:27:13 -04:00
2018-11-13 17:19:58 -05:00
common.Must(core.RequireFeatures(ctx, func(om outbound.Manager) {
2018-10-22 05:26:22 -04:00
c.ohm = om
2018-11-13 17:19:58 -05:00
}))
2018-10-21 04:27:13 -04:00
for _, rawConfig := range config.Service {
2021-06-19 09:36:54 -04:00
config, err := serial.GetInstanceOf(rawConfig)
2018-10-21 04:27:13 -04:00
if err != nil {
return nil, err
}
rawService, err := common.CreateObject(ctx, config)
if err != nil {
return nil, err
}
service, ok := rawService.(Service)
if !ok {
return nil, newError("not a Service.")
}
c.services = append(c.services, service)
}
2018-10-21 04:27:13 -04:00
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-10-21 04:27:13 -04:00
for _, service := range c.services {
2018-02-08 17:24:35 -05:00
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-10-21 04:27:13 -04:00
if err := c.ohm.RemoveHandler(context.Background(), c.tag); err != nil {
2018-04-14 07:09:58 -04:00
newError("failed to remove existing handler").WriteToLog()
}
return c.ohm.AddHandler(context.Background(), &Outbound{
2018-10-21 04:27:13 -04:00
tag: c.tag,
2018-02-05 17:38:24 -05:00
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))
}))
}
2021-09-07 09:33:00 -04:00
func init() {
common.Must(common.RegisterConfig((*SimplifiedConfig)(nil), func(ctx context.Context, cfg interface{}) (interface{}, error) {
simplifiedConfig := cfg.(*SimplifiedConfig)
fullConfig := &Config{
Tag: simplifiedConfig.Tag,
Service: nil,
}
for _, v := range simplifiedConfig.Name {
pack, err := v5cfg.LoadHeterogeneousConfigFromRawJSON(ctx, "grpcservice", v, []byte("{}"))
2021-09-07 09:33:00 -04:00
if err != nil {
return nil, err
}
fullConfig.Service = append(fullConfig.Service, serial.ToTypedMessage(pack))
}
return common.CreateObject(ctx, fullConfig)
}))
}