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"
)
type ClashServer interface {
Service
TrafficController
}
type TrafficController interface {
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

15
box.go
View file

@ -7,7 +7,7 @@ import (
"time"
"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/log"
"github.com/sagernet/sing-box/option"
@ -28,7 +28,7 @@ type Box struct {
logFactory log.Factory
logger log.ContextLogger
logFile *os.File
clashServer *clashapi.Server
clashServer adapter.ClashServer
}
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
}
var clashServer *clashapi.Server
var clashServer adapter.ClashServer
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)
}
return &Box{
@ -175,7 +178,7 @@ func (s *Box) Start() error {
if s.clashServer != nil {
err = s.clashServer.Start()
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)")
@ -191,7 +194,7 @@ func (s *Box) Close() error {
}
return common.Close(
s.router,
s.clashServer,
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"
)
var (
_ adapter.Service = (*Server)(nil)
_ adapter.TrafficController = (*Server)(nil)
)
var _ adapter.ClashServer = (*Server)(nil)
type Server struct {
logger log.Logger
@ -81,7 +78,7 @@ func (s *Server) Start() error {
go func() {
err = s.httpServer.Serve(listener)
if err != nil && !E.IsClosed(err) {
log.Error("external controller serve error: ", err)
s.logger.Error("external controller serve error: ", err)
}
}()
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) {
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

View file

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

View file

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

View file

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