mirror of
https://github.com/XTLS/Xray-core.git
synced 2024-11-27 19:11:27 +00:00
079d0bd8a9
* Refactor log * Add new log methods * Fix logger test * Change all logging code * Clean up pathObj * Rebase to latest main * Remove invoking method name after the dot
124 lines
2.8 KiB
Go
124 lines
2.8 KiB
Go
package commander
|
|
|
|
//go:generate go run github.com/xtls/xray-core/common/errors/errorgen
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"sync"
|
|
|
|
"github.com/xtls/xray-core/common"
|
|
"github.com/xtls/xray-core/common/errors"
|
|
"github.com/xtls/xray-core/common/signal/done"
|
|
core "github.com/xtls/xray-core/core"
|
|
"github.com/xtls/xray-core/features/outbound"
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
// Commander is a Xray feature that provides gRPC methods to external clients.
|
|
type Commander struct {
|
|
sync.Mutex
|
|
server *grpc.Server
|
|
services []Service
|
|
ohm outbound.Manager
|
|
tag string
|
|
listen string
|
|
}
|
|
|
|
// NewCommander creates a new Commander based on the given config.
|
|
func NewCommander(ctx context.Context, config *Config) (*Commander, error) {
|
|
c := &Commander{
|
|
tag: config.Tag,
|
|
listen: config.Listen,
|
|
}
|
|
|
|
common.Must(core.RequireFeatures(ctx, func(om outbound.Manager) {
|
|
c.ohm = om
|
|
}))
|
|
|
|
for _, rawConfig := range config.Service {
|
|
config, err := rawConfig.GetInstance()
|
|
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, errors.New("not a Service.")
|
|
}
|
|
c.services = append(c.services, service)
|
|
}
|
|
|
|
return c, nil
|
|
}
|
|
|
|
// Type implements common.HasType.
|
|
func (c *Commander) Type() interface{} {
|
|
return (*Commander)(nil)
|
|
}
|
|
|
|
// Start implements common.Runnable.
|
|
func (c *Commander) Start() error {
|
|
c.Lock()
|
|
c.server = grpc.NewServer()
|
|
for _, service := range c.services {
|
|
service.Register(c.server)
|
|
}
|
|
c.Unlock()
|
|
|
|
var listen = func(listener net.Listener) {
|
|
if err := c.server.Serve(listener); err != nil {
|
|
errors.LogErrorInner(context.Background(), err, "failed to start grpc server")
|
|
}
|
|
}
|
|
|
|
if len(c.listen) > 0 {
|
|
if l, err := net.Listen("tcp", c.listen); err != nil {
|
|
errors.LogErrorInner(context.Background(), err, "API server failed to listen on ", c.listen)
|
|
return err
|
|
} else {
|
|
errors.LogInfo(context.Background(), "API server listening on ", l.Addr())
|
|
go listen(l)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
listener := &OutboundListener{
|
|
buffer: make(chan net.Conn, 4),
|
|
done: done.New(),
|
|
}
|
|
|
|
go listen(listener)
|
|
|
|
if err := c.ohm.RemoveHandler(context.Background(), c.tag); err != nil {
|
|
errors.LogInfoInner(context.Background(), err, "failed to remove existing handler")
|
|
}
|
|
|
|
return c.ohm.AddHandler(context.Background(), &Outbound{
|
|
tag: c.tag,
|
|
listener: listener,
|
|
})
|
|
}
|
|
|
|
// Close implements common.Closable.
|
|
func (c *Commander) Close() error {
|
|
c.Lock()
|
|
defer c.Unlock()
|
|
|
|
if c.server != nil {
|
|
c.server.Stop()
|
|
c.server = nil
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func init() {
|
|
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, cfg interface{}) (interface{}, error) {
|
|
return NewCommander(ctx, cfg.(*Config))
|
|
}))
|
|
}
|