瀏覽代碼

Make clash api and gVisor optional

世界 3 年之前
父節點
當前提交
6ac1b395cf
共有 9 個文件被更改,包括 49 次插入16 次删除
  1. 5 0
      adapter/experimental.go
  2. 9 6
      box.go
  3. 14 0
      experimental/clashapi.go
  4. 3 6
      experimental/clashapi/server.go
  5. 14 0
      experimental/clashapi_stub.go
  6. 1 1
      inbound/tun.go
  7. 1 1
      inbound/tun_stub.go
  8. 1 1
      route/iface_stub.go
  9. 1 1
      route/iface_tun.go

+ 5 - 0
adapter/traffic_controller.go → adapter/experimental.go

@@ -7,6 +7,11 @@ import (
 	N "github.com/sagernet/sing/common/network"
 	N "github.com/sagernet/sing/common/network"
 )
 )
 
 
+type ClashServer interface {
+	Service
+	TrafficController
+}
+
 type TrafficController interface {
 type TrafficController interface {
 	RoutedConnection(ctx context.Context, conn net.Conn, metadata InboundContext, matchedRule Rule) net.Conn
 	RoutedConnection(ctx context.Context, conn net.Conn, metadata InboundContext, matchedRule Rule) net.Conn
 	RoutedPacketConnection(ctx context.Context, conn N.PacketConn, metadata InboundContext, matchedRule Rule) N.PacketConn
 	RoutedPacketConnection(ctx context.Context, conn N.PacketConn, metadata InboundContext, matchedRule Rule) N.PacketConn

+ 9 - 6
box.go

@@ -7,7 +7,7 @@ import (
 	"time"
 	"time"
 
 
 	"github.com/sagernet/sing-box/adapter"
 	"github.com/sagernet/sing-box/adapter"
-	"github.com/sagernet/sing-box/experimental/clashapi"
+	"github.com/sagernet/sing-box/experimental"
 	"github.com/sagernet/sing-box/inbound"
 	"github.com/sagernet/sing-box/inbound"
 	"github.com/sagernet/sing-box/log"
 	"github.com/sagernet/sing-box/log"
 	"github.com/sagernet/sing-box/option"
 	"github.com/sagernet/sing-box/option"
@@ -28,7 +28,7 @@ type Box struct {
 	logFactory  log.Factory
 	logFactory  log.Factory
 	logger      log.ContextLogger
 	logger      log.ContextLogger
 	logFile     *os.File
 	logFile     *os.File
-	clashServer *clashapi.Server
+	clashServer adapter.ClashServer
 }
 }
 
 
 func New(ctx context.Context, options option.Options) (*Box, error) {
 func New(ctx context.Context, options option.Options) (*Box, error) {
@@ -141,9 +141,12 @@ func New(ctx context.Context, options option.Options) (*Box, error) {
 		return nil, err
 		return nil, err
 	}
 	}
 
 
-	var clashServer *clashapi.Server
+	var clashServer adapter.ClashServer
 	if needClashAPI {
 	if needClashAPI {
-		clashServer = clashapi.NewServer(router, observableLogFactory, common.PtrValueOrDefault(options.Experimental.ClashAPI))
+		clashServer, err = experimental.NewClashServer(router, observableLogFactory, common.PtrValueOrDefault(options.Experimental.ClashAPI))
+		if err != nil {
+			return nil, E.Cause(err, "create clash api server")
+		}
 		router.SetTrafficController(clashServer)
 		router.SetTrafficController(clashServer)
 	}
 	}
 	return &Box{
 	return &Box{
@@ -175,7 +178,7 @@ func (s *Box) Start() error {
 	if s.clashServer != nil {
 	if s.clashServer != nil {
 		err = s.clashServer.Start()
 		err = s.clashServer.Start()
 		if err != nil {
 		if err != nil {
-			return E.Cause(err, "start clash api")
+			return E.Cause(err, "start clash api server")
 		}
 		}
 	}
 	}
 	s.logger.Info("sing-box started (", F.Seconds(time.Since(s.createdAt).Seconds()), "s)")
 	s.logger.Info("sing-box started (", F.Seconds(time.Since(s.createdAt).Seconds()), "s)")
@@ -191,7 +194,7 @@ func (s *Box) Close() error {
 	}
 	}
 	return common.Close(
 	return common.Close(
 		s.router,
 		s.router,
+		s.clashServer,
 		common.PtrOrNil(s.logFile),
 		common.PtrOrNil(s.logFile),
-		common.PtrOrNil(s.clashServer),
 	)
 	)
 }
 }

+ 14 - 0
experimental/clashapi.go

@@ -0,0 +1,14 @@
+//go:build with_clash_api
+
+package experimental
+
+import (
+	"github.com/sagernet/sing-box/adapter"
+	"github.com/sagernet/sing-box/experimental/clashapi"
+	"github.com/sagernet/sing-box/log"
+	"github.com/sagernet/sing-box/option"
+)
+
+func NewClashServer(router adapter.Router, logFactory log.ObservableFactory, options option.ClashAPIOptions) (adapter.ClashServer, error) {
+	return clashapi.NewServer(router, logFactory, options), nil
+}

+ 3 - 6
experimental/clashapi/server.go

@@ -24,10 +24,7 @@ import (
 	"github.com/gorilla/websocket"
 	"github.com/gorilla/websocket"
 )
 )
 
 
-var (
-	_ adapter.Service           = (*Server)(nil)
-	_ adapter.TrafficController = (*Server)(nil)
-)
+var _ adapter.ClashServer = (*Server)(nil)
 
 
 type Server struct {
 type Server struct {
 	logger         log.Logger
 	logger         log.Logger
@@ -81,7 +78,7 @@ func (s *Server) Start() error {
 	go func() {
 	go func() {
 		err = s.httpServer.Serve(listener)
 		err = s.httpServer.Serve(listener)
 		if err != nil && !E.IsClosed(err) {
 		if err != nil && !E.IsClosed(err) {
-			log.Error("external controller serve error: ", err)
+			s.logger.Error("external controller serve error: ", err)
 		}
 		}
 	}()
 	}()
 	return nil
 	return nil
@@ -294,5 +291,5 @@ func getLogs(logFactory log.ObservableFactory) func(w http.ResponseWriter, r *ht
 }
 }
 
 
 func version(w http.ResponseWriter, r *http.Request) {
 func version(w http.ResponseWriter, r *http.Request) {
-	render.JSON(w, r, render.M{"version": "sing-box " + C.Version, "premium": false})
+	render.JSON(w, r, render.M{"version": "sing-box " + C.Version, "premium": true})
 }
 }

+ 14 - 0
experimental/clashapi_stub.go

@@ -0,0 +1,14 @@
+//go:build !with_clash_api
+
+package experimental
+
+import (
+	"github.com/sagernet/sing-box/adapter"
+	"github.com/sagernet/sing-box/log"
+	"github.com/sagernet/sing-box/option"
+	E "github.com/sagernet/sing/common/exceptions"
+)
+
+func NewClashServer(router adapter.Router, logFactory log.ObservableFactory, options option.ClashAPIOptions) (adapter.ClashServer, error) {
+	return nil, E.New(`clash api is not included in this build, rebuild with -tags with_clash_api`)
+}

+ 1 - 1
inbound/tun.go

@@ -1,4 +1,4 @@
-//go:build linux || windows
+//go:build (linux || windows) && !no_gvisor
 
 
 package inbound
 package inbound
 
 

+ 1 - 1
inbound/tun_stub.go

@@ -1,4 +1,4 @@
-//go:build !linux && !windows
+//go:build !(linux || windows) || no_gvisor
 
 
 package inbound
 package inbound
 
 

+ 1 - 1
route/iface_stub.go

@@ -1,4 +1,4 @@
-//go:build !linux && !windows
+//go:build !(linux || windows) || no_gvisor
 
 
 package route
 package route
 
 

+ 1 - 1
route/iface_tun.go

@@ -1,4 +1,4 @@
-//go:build linux || windows
+//go:build (linux || windows) && !no_gvisor
 
 
 package route
 package route