sing-box/experimental/v2rayapi/server.go

83 lines
1.8 KiB
Go
Raw Normal View History

2022-09-26 11:37:06 +00:00
package v2rayapi
import (
"errors"
"net"
"net/http"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/experimental"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func init() {
experimental.RegisterV2RayServerConstructor(NewServer)
}
var _ adapter.V2RayServer = (*Server)(nil)
type Server struct {
logger log.Logger
listen string
tcpListener net.Listener
grpcServer *grpc.Server
statsService *StatsService
}
func NewServer(logger log.Logger, options option.V2RayAPIOptions) (adapter.V2RayServer, error) {
grpcServer := grpc.NewServer(grpc.Creds(insecure.NewCredentials()))
statsService := NewStatsService(common.PtrValueOrDefault(options.Stats))
if statsService != nil {
RegisterStatsServiceServer(grpcServer, statsService)
}
server := &Server{
logger: logger,
listen: options.Listen,
grpcServer: grpcServer,
statsService: statsService,
}
return server, nil
}
2024-11-10 08:46:59 +00:00
func (s *Server) Name() string {
return "v2ray server"
}
func (s *Server) Start(stage adapter.StartStage) error {
if stage != adapter.StartStatePostStart {
return nil
}
2022-09-26 11:37:06 +00:00
listener, err := net.Listen("tcp", s.listen)
if err != nil {
return err
}
s.logger.Info("grpc server started at ", listener.Addr())
s.tcpListener = listener
go func() {
err = s.grpcServer.Serve(listener)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
s.logger.Error(err)
}
}()
return nil
}
func (s *Server) Close() error {
if s.grpcServer != nil {
s.grpcServer.Stop()
}
return common.Close(
common.PtrOrNil(s.grpcServer),
s.tcpListener,
)
}
2024-11-10 08:46:59 +00:00
func (s *Server) StatsService() adapter.ConnectionTracker {
2022-09-26 11:37:06 +00:00
return s.statsService
}