mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-08-08 19:45:18 +03:00
Add DefaultGateway
This commit is contained in:
@@ -0,0 +1,53 @@
|
|||||||
|
{
|
||||||
|
"log": {
|
||||||
|
"level": "error"
|
||||||
|
},
|
||||||
|
"dns": {
|
||||||
|
"servers": [
|
||||||
|
{
|
||||||
|
"type": "local",
|
||||||
|
"tag": "default"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"endpoints": [
|
||||||
|
{
|
||||||
|
"type": "vpn-client",
|
||||||
|
"tag": "vpn",
|
||||||
|
"address": "10.0.0.2",
|
||||||
|
"key": "1c9b2ccf-b0c0-4c26-868d-a55a4edad3fe",
|
||||||
|
"outbound": {
|
||||||
|
"type": "vless",
|
||||||
|
"tag": "vless-out",
|
||||||
|
"server": "0.0.0.0",
|
||||||
|
"server_port": 8000,
|
||||||
|
"uuid": "9b65b7e1-04c8-4717-8f45-2aa61fd25937",
|
||||||
|
"network": "tcp"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"inbounds": [
|
||||||
|
{
|
||||||
|
"type": "mixed",
|
||||||
|
"tag": "mixed-in",
|
||||||
|
"listen_port": 7897
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"outbounds": [
|
||||||
|
{
|
||||||
|
"type": "direct",
|
||||||
|
"tag": "direct-out"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"route": {
|
||||||
|
"rules": [
|
||||||
|
{
|
||||||
|
"domain_suffix": ".internal",
|
||||||
|
"outbound": "vpn",
|
||||||
|
"override_gateway": "10.0.0.3"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"final": "direct-out",
|
||||||
|
"auto_detect_interface": true
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
{
|
||||||
|
"log": {
|
||||||
|
"level": "error"
|
||||||
|
},
|
||||||
|
"dns": {
|
||||||
|
"servers": [
|
||||||
|
{
|
||||||
|
"type": "local",
|
||||||
|
"tag": "default"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"endpoints": [
|
||||||
|
{
|
||||||
|
"type": "vpn-client",
|
||||||
|
"tag": "vpn",
|
||||||
|
"address": "10.0.0.3",
|
||||||
|
"key": "3d74d616-2502-4c17-9cc3-92c366550f4f",
|
||||||
|
"outbound": {
|
||||||
|
"type": "vless",
|
||||||
|
"tag": "vless-out",
|
||||||
|
"server": "0.0.0.0",
|
||||||
|
"server_port": 8000,
|
||||||
|
"uuid": "9b65b7e1-04c8-4717-8f45-2aa61fd25937",
|
||||||
|
"network": "tcp"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"outbounds": [
|
||||||
|
{
|
||||||
|
"type": "direct",
|
||||||
|
"tag": "direct-out"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"route": {
|
||||||
|
"final": "direct-out",
|
||||||
|
"default_domain_resolver": "default",
|
||||||
|
"auto_detect_interface": true
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
{
|
||||||
|
"log": {
|
||||||
|
"level": "error"
|
||||||
|
},
|
||||||
|
"dns": {
|
||||||
|
"servers": [
|
||||||
|
{
|
||||||
|
"type": "local",
|
||||||
|
"tag": "default"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"endpoints": [
|
||||||
|
{
|
||||||
|
"type": "vpn-server",
|
||||||
|
"tag": "vpn",
|
||||||
|
"address": "10.0.0.1",
|
||||||
|
"users": [
|
||||||
|
{
|
||||||
|
"address": "10.0.0.2",
|
||||||
|
"key": "1c9b2ccf-b0c0-4c26-868d-a55a4edad3fe"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"address": "10.0.0.3",
|
||||||
|
"key": "3d74d616-2502-4c17-9cc3-92c366550f4f"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"inbounds": [
|
||||||
|
{
|
||||||
|
"type": "vless",
|
||||||
|
"tag": "vless-in",
|
||||||
|
"listen": "0.0.0.0",
|
||||||
|
"listen_port": 8000,
|
||||||
|
"users": [
|
||||||
|
{
|
||||||
|
"name": "vless",
|
||||||
|
"uuid": "9b65b7e1-04c8-4717-8f45-2aa61fd25937"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"outbounds": [
|
||||||
|
{
|
||||||
|
"type": "direct",
|
||||||
|
"tag": "direct-out"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"route": {
|
||||||
|
"rules": [
|
||||||
|
{
|
||||||
|
"source_ip_cidr": "10.0.0.0/24",
|
||||||
|
"outbound": "vpn"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"final": "direct-out",
|
||||||
|
"default_domain_resolver": "default",
|
||||||
|
"auto_detect_interface": true
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,6 +16,7 @@
|
|||||||
"tag": "vpn",
|
"tag": "vpn",
|
||||||
"address": "10.0.0.2",
|
"address": "10.0.0.2",
|
||||||
"key": "1c9b2ccf-b0c0-4c26-868d-a55a4edad3fe",
|
"key": "1c9b2ccf-b0c0-4c26-868d-a55a4edad3fe",
|
||||||
|
"default_gateway": "10.0.0.3",
|
||||||
"outbound": {
|
"outbound": {
|
||||||
"type": "vless",
|
"type": "vless",
|
||||||
"tag": "vless-out",
|
"tag": "vless-out",
|
||||||
@@ -40,13 +41,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"route": {
|
"route": {
|
||||||
"rules": [
|
"final": "vpn",
|
||||||
{
|
|
||||||
"outbound": "vpn",
|
|
||||||
"override_gateway": "10.0.0.3"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"final": "direct-out",
|
|
||||||
"auto_detect_interface": true
|
"auto_detect_interface": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -15,6 +15,7 @@
|
|||||||
"type": "vpn-server",
|
"type": "vpn-server",
|
||||||
"tag": "vpn",
|
"tag": "vpn",
|
||||||
"address": "10.0.0.1",
|
"address": "10.0.0.1",
|
||||||
|
"default_gateway": "10.0.0.2",
|
||||||
"users": [
|
"users": [
|
||||||
{
|
{
|
||||||
"address": "10.0.0.2",
|
"address": "10.0.0.2",
|
||||||
@@ -47,8 +48,7 @@
|
|||||||
"rules": [
|
"rules": [
|
||||||
{
|
{
|
||||||
"inbound": "vless-in",
|
"inbound": "vless-in",
|
||||||
"outbound": "vpn",
|
"outbound": "vpn"
|
||||||
"override_gateway": "10.0.0.2"
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"final": "direct-out",
|
"final": "direct-out",
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
"type": "vpn-server",
|
"type": "vpn-server",
|
||||||
"tag": "vpn",
|
"tag": "vpn",
|
||||||
"address": "10.0.0.1",
|
"address": "10.0.0.1",
|
||||||
|
"default_gateway": "10.0.0.2",
|
||||||
"users": [
|
"users": [
|
||||||
{
|
{
|
||||||
"address": "10.0.0.2",
|
"address": "10.0.0.2",
|
||||||
@@ -51,13 +52,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"route": {
|
"route": {
|
||||||
"rules": [
|
"final": "vpn",
|
||||||
{
|
|
||||||
"outbound": "vpn",
|
|
||||||
"override_gateway": "10.0.0.2"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"final": "direct-out",
|
|
||||||
"default_domain_resolver": "default",
|
"default_domain_resolver": "default",
|
||||||
"auto_detect_interface": true
|
"auto_detect_interface": true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,13 +29,14 @@ func RegisterClientEndpoint(registry *endpoint.Registry) {
|
|||||||
|
|
||||||
type ClientEndpoint struct {
|
type ClientEndpoint struct {
|
||||||
outbound.Adapter
|
outbound.Adapter
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
outbound adapter.Outbound
|
outbound adapter.Outbound
|
||||||
router adapter.ConnectionRouterEx
|
router adapter.ConnectionRouterEx
|
||||||
logger logger.ContextLogger
|
logger logger.ContextLogger
|
||||||
address IPv4
|
address IPv4
|
||||||
key uuid.UUID
|
key uuid.UUID
|
||||||
uotClient *uot.Client
|
defaultGateway IPv4
|
||||||
|
uotClient *uot.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClientEndpoint(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.VPNClientEndpointOptions) (adapter.Endpoint, error) {
|
func NewClientEndpoint(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.VPNClientEndpointOptions) (adapter.Endpoint, error) {
|
||||||
@@ -47,13 +48,21 @@ func NewClientEndpoint(ctx context.Context, router adapter.Router, logger log.Co
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
defaultGateway := Loopback.As4()
|
||||||
|
if options.DefaultGateway.IsValid() {
|
||||||
|
if !options.DefaultGateway.Is4() {
|
||||||
|
return nil, E.New("invalid default_gateway: ", options.DefaultGateway)
|
||||||
|
}
|
||||||
|
defaultGateway = options.DefaultGateway.As4()
|
||||||
|
}
|
||||||
client := &ClientEndpoint{
|
client := &ClientEndpoint{
|
||||||
Adapter: outbound.NewAdapter(C.TypeVPNClient, tag, []string{N.NetworkTCP, N.NetworkUDP}, []string{}),
|
Adapter: outbound.NewAdapter(C.TypeVPNClient, tag, []string{N.NetworkTCP, N.NetworkUDP}, []string{}),
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
router: sbUot.NewRouter(router, logger),
|
router: sbUot.NewRouter(router, logger),
|
||||||
logger: logger,
|
logger: logger,
|
||||||
address: address.As4(),
|
address: address.As4(),
|
||||||
key: key,
|
key: key,
|
||||||
|
defaultGateway: defaultGateway,
|
||||||
}
|
}
|
||||||
outboundRegistry := service.FromContext[adapter.OutboundRegistry](ctx)
|
outboundRegistry := service.FromContext[adapter.OutboundRegistry](ctx)
|
||||||
outbound, err := outboundRegistry.CreateOutbound(ctx, router, logger, options.Outbound.Tag, options.Outbound.Type, options.Outbound.Options)
|
outbound, err := outboundRegistry.CreateOutbound(ctx, router, logger, options.Outbound.Tag, options.Outbound.Type, options.Outbound.Options)
|
||||||
@@ -102,7 +111,7 @@ func (c *ClientEndpoint) DialContext(ctx context.Context, network string, destin
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
gateway := Loopback.As4()
|
gateway := c.defaultGateway
|
||||||
if metadata := adapter.ContextFrom(ctx); metadata != nil {
|
if metadata := adapter.ContextFrom(ctx); metadata != nil {
|
||||||
if metadata.Gateway != nil {
|
if metadata.Gateway != nil {
|
||||||
gateway = metadata.Gateway.As4()
|
gateway = metadata.Gateway.As4()
|
||||||
|
|||||||
@@ -29,15 +29,16 @@ func RegisterServerEndpoint(registry *endpoint.Registry) {
|
|||||||
|
|
||||||
type ServerEndpoint struct {
|
type ServerEndpoint struct {
|
||||||
outbound.Adapter
|
outbound.Adapter
|
||||||
logger logger.ContextLogger
|
logger logger.ContextLogger
|
||||||
inbounds []adapter.Inbound
|
inbounds []adapter.Inbound
|
||||||
router adapter.ConnectionRouterEx
|
router adapter.ConnectionRouterEx
|
||||||
address IPv4
|
address IPv4
|
||||||
addresses map[uuid.UUID]IPv4
|
addresses map[uuid.UUID]IPv4
|
||||||
keys map[IPv4]uuid.UUID
|
keys map[IPv4]uuid.UUID
|
||||||
conns map[IPv4]chan net.Conn
|
conns map[IPv4]chan net.Conn
|
||||||
timeout time.Duration
|
timeout time.Duration
|
||||||
uotClient *uot.Client
|
defaultGateway *netip.Addr
|
||||||
|
uotClient *uot.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServerEndpoint(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.VPNServerEndpointOptions) (adapter.Endpoint, error) {
|
func NewServerEndpoint(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.VPNServerEndpointOptions) (adapter.Endpoint, error) {
|
||||||
@@ -51,6 +52,13 @@ func NewServerEndpoint(ctx context.Context, router adapter.Router, logger log.Co
|
|||||||
router: sbUot.NewRouter(router, logger),
|
router: sbUot.NewRouter(router, logger),
|
||||||
address: address.As4(),
|
address: address.As4(),
|
||||||
}
|
}
|
||||||
|
if options.DefaultGateway.IsValid() {
|
||||||
|
if !options.DefaultGateway.Is4() {
|
||||||
|
return nil, E.New("invalid default_gateway: ", options.DefaultGateway)
|
||||||
|
}
|
||||||
|
defaultGateway := options.DefaultGateway
|
||||||
|
server.defaultGateway = &defaultGateway
|
||||||
|
}
|
||||||
router = NewRouter(router, logger, server.connHandler)
|
router = NewRouter(router, logger, server.connHandler)
|
||||||
inboundRegistry := service.FromContext[adapter.InboundRegistry](ctx)
|
inboundRegistry := service.FromContext[adapter.InboundRegistry](ctx)
|
||||||
inbounds := make([]adapter.Inbound, len(options.Inbounds))
|
inbounds := make([]adapter.Inbound, len(options.Inbounds))
|
||||||
@@ -124,6 +132,8 @@ func (s *ServerEndpoint) DialContext(ctx context.Context, network string, destin
|
|||||||
Addr: Loopback,
|
Addr: Loopback,
|
||||||
Port: destination.Port,
|
Port: destination.Port,
|
||||||
}
|
}
|
||||||
|
} else if s.defaultGateway != nil {
|
||||||
|
gateway = s.defaultGateway
|
||||||
} else {
|
} else {
|
||||||
return nil, E.New("missing gateway")
|
return nil, E.New("missing gateway")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user