Add set_system_proxy option for windows

This commit is contained in:
世界
2022-07-14 14:04:57 +08:00
parent e7d557fd9e
commit 238afda9da
11 changed files with 203 additions and 47 deletions

View File

@@ -9,6 +9,7 @@ import (
"time"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/wininet"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
@@ -16,6 +17,7 @@ import (
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf"
E "github.com/sagernet/sing/common/exceptions"
F "github.com/sagernet/sing/common/format"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
@@ -36,6 +38,10 @@ type myInboundAdapter struct {
packetHandler adapter.PacketHandler
packetUpstream any
// http mixed
setSystemProxy bool
// internal
tcpListener *net.TCPListener
@@ -88,14 +94,24 @@ func (a *myInboundAdapter) Start() error {
go a.loopUDPOut()
a.logger.Info("udp server started at ", udpConn.LocalAddr())
}
if a.setSystemProxy {
err := wininet.SetSystemProxy(F.ToString("http://127.0.0.1:", M.SocksaddrFromNet(a.tcpListener.Addr()).Port), "local")
if err != nil {
return E.Cause(err, "set system proxy")
}
}
return nil
}
func (a *myInboundAdapter) Close() error {
return common.Close(
var err error
if a.setSystemProxy {
err = wininet.ClearSystemProxy()
}
return E.Errors(err, common.Close(
common.PtrOrNil(a.tcpListener),
common.PtrOrNil(a.udpConn),
)
))
}
func (a *myInboundAdapter) upstreamHandler(metadata adapter.InboundContext) adapter.UpstreamHandlerAdapter {

View File

@@ -21,16 +21,17 @@ type HTTP struct {
authenticator auth.Authenticator
}
func NewHTTP(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.SimpleInboundOptions) *HTTP {
func NewHTTP(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.HTTPMixedInboundOptions) *HTTP {
inbound := &HTTP{
myInboundAdapter{
protocol: C.TypeHTTP,
network: []string{C.NetworkTCP},
ctx: ctx,
router: router,
logger: logger,
tag: tag,
listenOptions: options.ListenOptions,
protocol: C.TypeHTTP,
network: []string{C.NetworkTCP},
ctx: ctx,
router: router,
logger: logger,
tag: tag,
listenOptions: options.ListenOptions,
setSystemProxy: options.SetSystemProxy,
},
auth.NewAuthenticator(options.Users),
}

View File

@@ -27,16 +27,17 @@ type Mixed struct {
authenticator auth.Authenticator
}
func NewMixed(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.SimpleInboundOptions) *Mixed {
func NewMixed(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.HTTPMixedInboundOptions) *Mixed {
inbound := &Mixed{
myInboundAdapter{
protocol: C.TypeMixed,
network: []string{C.NetworkTCP},
ctx: ctx,
router: router,
logger: logger,
tag: tag,
listenOptions: options.ListenOptions,
protocol: C.TypeMixed,
network: []string{C.NetworkTCP},
ctx: ctx,
router: router,
logger: logger,
tag: tag,
listenOptions: options.ListenOptions,
setSystemProxy: options.SetSystemProxy,
},
auth.NewAuthenticator(options.Users),
}

View File

@@ -20,7 +20,7 @@ type Socks struct {
authenticator auth.Authenticator
}
func NewSocks(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.SimpleInboundOptions) *Socks {
func NewSocks(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.SocksInboundOptions) *Socks {
inbound := &Socks{
myInboundAdapter{
protocol: C.TypeSocks,