Make clash api and gVisor optional

This commit is contained in:
世界 2022-07-20 07:12:40 +08:00
parent c5b3e8b042
commit 6ac1b395cf
No known key found for this signature in database
GPG Key ID: CD109927C34A63C4
9 changed files with 49 additions and 16 deletions

View File

@ -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

15
box.go
View File

@ -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
experimental/clashapi.go Normal file
View File

@ -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
}

View File

@ -24,10 +24,7 @@ import (
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
) )
var ( var _ adapter.ClashServer = (*Server)(nil)
_ adapter.Service = (*Server)(nil)
_ adapter.TrafficController = (*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})
} }

View File

@ -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`)
}

View File

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

View File

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

View File

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

View File

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