|
@@ -1,6 +1,4 @@
|
|
|
-//go:build with_wireguard
|
|
|
-
|
|
|
-package outbound
|
|
|
+package wireguard
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
@@ -12,6 +10,7 @@ import (
|
|
|
"strings"
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
|
|
+ "github.com/sagernet/sing-box/adapter/outbound"
|
|
|
"github.com/sagernet/sing-box/common/dialer"
|
|
|
C "github.com/sagernet/sing-box/constant"
|
|
|
"github.com/sagernet/sing-box/log"
|
|
@@ -21,6 +20,7 @@ import (
|
|
|
"github.com/sagernet/sing-tun"
|
|
|
"github.com/sagernet/sing/common"
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
|
+ "github.com/sagernet/sing/common/logger"
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
|
N "github.com/sagernet/sing/common/network"
|
|
|
"github.com/sagernet/sing/common/x/list"
|
|
@@ -30,14 +30,17 @@ import (
|
|
|
"github.com/sagernet/wireguard-go/device"
|
|
|
)
|
|
|
|
|
|
-var (
|
|
|
- _ adapter.Outbound = (*WireGuard)(nil)
|
|
|
- _ adapter.InterfaceUpdateListener = (*WireGuard)(nil)
|
|
|
-)
|
|
|
+func RegisterOutbound(registry *outbound.Registry) {
|
|
|
+ outbound.Register[option.WireGuardOutboundOptions](registry, C.TypeWireGuard, NewOutbound)
|
|
|
+}
|
|
|
|
|
|
-type WireGuard struct {
|
|
|
- myOutboundAdapter
|
|
|
+var _ adapter.InterfaceUpdateListener = (*Outbound)(nil)
|
|
|
+
|
|
|
+type Outbound struct {
|
|
|
+ outbound.Adapter
|
|
|
ctx context.Context
|
|
|
+ router adapter.Router
|
|
|
+ logger logger.ContextLogger
|
|
|
workers int
|
|
|
peers []wireguard.PeerConfig
|
|
|
useStdNetBind bool
|
|
@@ -51,17 +54,12 @@ type WireGuard struct {
|
|
|
tunDevice wireguard.Device
|
|
|
}
|
|
|
|
|
|
-func NewWireGuard(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.WireGuardOutboundOptions) (*WireGuard, error) {
|
|
|
- outbound := &WireGuard{
|
|
|
- myOutboundAdapter: myOutboundAdapter{
|
|
|
- protocol: C.TypeWireGuard,
|
|
|
- network: options.Network.Build(),
|
|
|
- router: router,
|
|
|
- logger: logger,
|
|
|
- tag: tag,
|
|
|
- dependencies: withDialerDependency(options.DialerOptions),
|
|
|
- },
|
|
|
+func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.WireGuardOutboundOptions) (adapter.Outbound, error) {
|
|
|
+ outbound := &Outbound{
|
|
|
+ Adapter: outbound.NewAdapterWithDialerOptions(C.TypeWireGuard, options.Network.Build(), tag, options.DialerOptions),
|
|
|
ctx: ctx,
|
|
|
+ router: router,
|
|
|
+ logger: logger,
|
|
|
workers: options.Workers,
|
|
|
pauseManager: service.FromContext[pause.Manager](ctx),
|
|
|
}
|
|
@@ -111,7 +109,7 @@ func NewWireGuard(ctx context.Context, router adapter.Router, logger log.Context
|
|
|
return outbound, nil
|
|
|
}
|
|
|
|
|
|
-func (w *WireGuard) Start() error {
|
|
|
+func (w *Outbound) Start() error {
|
|
|
if common.Any(w.peers, func(peer wireguard.PeerConfig) bool {
|
|
|
return !peer.Endpoint.IsValid()
|
|
|
}) {
|
|
@@ -121,7 +119,7 @@ func (w *WireGuard) Start() error {
|
|
|
return w.start()
|
|
|
}
|
|
|
|
|
|
-func (w *WireGuard) PostStart() error {
|
|
|
+func (w *Outbound) PostStart() error {
|
|
|
if common.All(w.peers, func(peer wireguard.PeerConfig) bool {
|
|
|
return peer.Endpoint.IsValid()
|
|
|
}) {
|
|
@@ -130,7 +128,7 @@ func (w *WireGuard) PostStart() error {
|
|
|
return w.start()
|
|
|
}
|
|
|
|
|
|
-func (w *WireGuard) start() error {
|
|
|
+func (w *Outbound) start() error {
|
|
|
err := wireguard.ResolvePeers(w.ctx, w.router, w.peers)
|
|
|
if err != nil {
|
|
|
return err
|
|
@@ -150,7 +148,7 @@ func (w *WireGuard) start() error {
|
|
|
connectAddr = w.peers[0].Endpoint
|
|
|
reserved = w.peers[0].Reserved
|
|
|
}
|
|
|
- bind = wireguard.NewClientBind(w.ctx, w, w.listener, isConnect, connectAddr, reserved)
|
|
|
+ bind = wireguard.NewClientBind(w.ctx, w.logger, w.listener, isConnect, connectAddr, reserved)
|
|
|
}
|
|
|
err = w.tunDevice.Start()
|
|
|
if err != nil {
|
|
@@ -177,7 +175,7 @@ func (w *WireGuard) start() error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func (w *WireGuard) Close() error {
|
|
|
+func (w *Outbound) Close() error {
|
|
|
if w.device != nil {
|
|
|
w.device.Close()
|
|
|
}
|
|
@@ -187,12 +185,12 @@ func (w *WireGuard) Close() error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func (w *WireGuard) InterfaceUpdated() {
|
|
|
+func (w *Outbound) InterfaceUpdated() {
|
|
|
w.device.BindUpdate()
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-func (w *WireGuard) onPauseUpdated(event int) {
|
|
|
+func (w *Outbound) onPauseUpdated(event int) {
|
|
|
switch event {
|
|
|
case pause.EventDevicePaused:
|
|
|
w.device.Down()
|
|
@@ -201,7 +199,7 @@ func (w *WireGuard) onPauseUpdated(event int) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func (w *WireGuard) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
|
|
|
+func (w *Outbound) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
|
|
|
switch network {
|
|
|
case N.NetworkTCP:
|
|
|
w.logger.InfoContext(ctx, "outbound connection to ", destination)
|
|
@@ -218,7 +216,7 @@ func (w *WireGuard) DialContext(ctx context.Context, network string, destination
|
|
|
return w.tunDevice.DialContext(ctx, network, destination)
|
|
|
}
|
|
|
|
|
|
-func (w *WireGuard) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
|
|
|
+func (w *Outbound) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
|
|
|
w.logger.InfoContext(ctx, "outbound packet connection to ", destination)
|
|
|
if destination.IsFqdn() {
|
|
|
destinationAddresses, err := w.router.LookupDefault(ctx, destination.Fqdn)
|
|
@@ -236,12 +234,12 @@ func (w *WireGuard) ListenPacket(ctx context.Context, destination M.Socksaddr) (
|
|
|
|
|
|
// TODO
|
|
|
// Deprecated
|
|
|
-func (w *WireGuard) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
|
|
|
- return NewDirectConnection(ctx, w.router, w, conn, metadata, dns.DomainStrategyAsIS)
|
|
|
+func (w *Outbound) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
|
|
|
+ return outbound.NewDirectConnection(ctx, w.router, w, conn, metadata, dns.DomainStrategyAsIS)
|
|
|
}
|
|
|
|
|
|
// TODO
|
|
|
// Deprecated
|
|
|
-func (w *WireGuard) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
|
|
|
- return NewDirectPacketConnection(ctx, w.router, w, conn, metadata, dns.DomainStrategyAsIS)
|
|
|
+func (w *Outbound) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
|
|
|
+ return outbound.NewDirectPacketConnection(ctx, w.router, w, conn, metadata, dns.DomainStrategyAsIS)
|
|
|
}
|