sing-box/outbound/wireguard.go

217 lines
6.4 KiB
Go
Raw Normal View History

2022-08-16 23:37:51 +08:00
//go:build with_wireguard
package outbound
import (
"context"
"encoding/base64"
"encoding/hex"
"fmt"
"net"
2023-03-21 21:36:17 +08:00
"os"
2022-08-16 23:37:51 +08:00
"strings"
2023-03-21 21:36:17 +08:00
"syscall"
2022-08-16 23:37:51 +08:00
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/dialer"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
2022-09-06 00:15:09 +08:00
"github.com/sagernet/sing-box/transport/wireguard"
2022-09-15 12:20:38 +08:00
"github.com/sagernet/sing-tun"
2022-08-16 23:37:51 +08:00
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/debug"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/wireguard-go/device"
2022-08-16 23:37:51 +08:00
)
2022-11-06 10:36:19 +08:00
var (
2023-03-21 21:36:17 +08:00
_ adapter.IPOutbound = (*WireGuard)(nil)
2022-11-06 10:36:19 +08:00
_ adapter.InterfaceUpdateListener = (*WireGuard)(nil)
)
2022-08-16 23:37:51 +08:00
type WireGuard struct {
myOutboundAdapter
2022-09-06 00:15:09 +08:00
bind *wireguard.ClientBind
device *device.Device
2023-03-21 21:36:17 +08:00
natDevice wireguard.NatDevice
2022-09-06 00:15:09 +08:00
tunDevice wireguard.Device
2022-08-16 23:37:51 +08:00
}
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,
},
}
var reserved [3]uint8
if len(options.Reserved) > 0 {
if len(options.Reserved) != 3 {
return nil, E.New("invalid reserved value, required 3 bytes, got ", len(options.Reserved))
}
copy(reserved[:], options.Reserved)
}
2022-09-06 00:15:09 +08:00
peerAddr := options.ServerOptions.Build()
outbound.bind = wireguard.NewClientBind(ctx, dialer.New(router, options.DialerOptions), peerAddr, reserved)
2022-09-06 00:15:09 +08:00
localPrefixes := common.Map(options.LocalAddress, option.ListenPrefix.Build)
if len(localPrefixes) == 0 {
2022-08-16 23:37:51 +08:00
return nil, E.New("missing local address")
}
var privateKey, peerPublicKey, preSharedKey string
{
bytes, err := base64.StdEncoding.DecodeString(options.PrivateKey)
if err != nil {
return nil, E.Cause(err, "decode private key")
}
privateKey = hex.EncodeToString(bytes)
}
{
bytes, err := base64.StdEncoding.DecodeString(options.PeerPublicKey)
if err != nil {
return nil, E.Cause(err, "decode peer public key")
}
peerPublicKey = hex.EncodeToString(bytes)
}
if options.PreSharedKey != "" {
bytes, err := base64.StdEncoding.DecodeString(options.PreSharedKey)
if err != nil {
return nil, E.Cause(err, "decode pre shared key")
}
preSharedKey = hex.EncodeToString(bytes)
}
ipcConf := "private_key=" + privateKey
ipcConf += "\npublic_key=" + peerPublicKey
2022-09-06 00:15:09 +08:00
ipcConf += "\nendpoint=" + peerAddr.String()
2022-08-16 23:37:51 +08:00
if preSharedKey != "" {
ipcConf += "\npreshared_key=" + preSharedKey
}
var has4, has6 bool
2022-09-06 00:15:09 +08:00
for _, address := range localPrefixes {
if address.Addr().Is4() {
2022-08-16 23:37:51 +08:00
has4 = true
} else {
has6 = true
}
}
if has4 {
ipcConf += "\nallowed_ip=0.0.0.0/0"
}
if has6 {
ipcConf += "\nallowed_ip=::/0"
}
mtu := options.MTU
if mtu == 0 {
2022-08-17 15:19:10 +08:00
mtu = 1408
2022-08-16 23:37:51 +08:00
}
2023-03-21 21:36:17 +08:00
var tunDevice wireguard.Device
2022-09-06 00:15:09 +08:00
var err error
2022-09-15 12:20:38 +08:00
if !options.SystemInterface && tun.WithGVisor {
2023-03-21 21:36:17 +08:00
tunDevice, err = wireguard.NewStackDevice(localPrefixes, mtu, options.IPRewrite)
2022-09-06 00:15:09 +08:00
} else {
2023-03-21 21:36:17 +08:00
tunDevice, err = wireguard.NewSystemDevice(router, options.InterfaceName, localPrefixes, mtu)
2022-09-06 00:15:09 +08:00
}
2022-08-16 23:37:51 +08:00
if err != nil {
2022-09-06 00:15:09 +08:00
return nil, E.Cause(err, "create WireGuard device")
2022-08-16 23:37:51 +08:00
}
2023-03-21 21:36:17 +08:00
natDevice, isNatDevice := tunDevice.(wireguard.NatDevice)
if !isNatDevice && router.NatRequired(tag) {
natDevice = wireguard.NewNATDevice(tunDevice, options.IPRewrite)
}
deviceInput := tunDevice
if natDevice != nil {
deviceInput = natDevice
}
wgDevice := device.NewDevice(deviceInput, outbound.bind, &device.Logger{
2022-08-16 23:37:51 +08:00
Verbosef: func(format string, args ...interface{}) {
logger.Debug(fmt.Sprintf(strings.ToLower(format), args...))
},
Errorf: func(format string, args ...interface{}) {
logger.Error(fmt.Sprintf(strings.ToLower(format), args...))
},
}, options.Workers)
2022-08-16 23:37:51 +08:00
if debug.Enabled {
logger.Trace("created wireguard ipc conf: \n", ipcConf)
}
err = wgDevice.IpcSet(ipcConf)
if err != nil {
return nil, E.Cause(err, "setup wireguard")
}
outbound.device = wgDevice
2023-03-21 21:36:17 +08:00
outbound.natDevice = natDevice
outbound.tunDevice = tunDevice
2022-08-16 23:37:51 +08:00
return outbound, nil
}
2022-11-06 10:36:19 +08:00
func (w *WireGuard) InterfaceUpdated() error {
w.bind.Reset()
return nil
}
2022-08-16 23:37:51 +08:00
func (w *WireGuard) 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)
case N.NetworkUDP:
w.logger.InfoContext(ctx, "outbound packet connection to ", destination)
}
if destination.IsFqdn() {
addrs, err := w.router.LookupDefault(ctx, destination.Fqdn)
if err != nil {
return nil, err
}
2022-09-06 00:15:09 +08:00
return N.DialSerial(ctx, w.tunDevice, network, destination, addrs)
2022-08-16 23:37:51 +08:00
}
2022-09-06 00:15:09 +08:00
return w.tunDevice.DialContext(ctx, network, destination)
2022-08-16 23:37:51 +08:00
}
func (w *WireGuard) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
w.logger.InfoContext(ctx, "outbound packet connection to ", destination)
2022-09-06 00:15:09 +08:00
return w.tunDevice.ListenPacket(ctx, destination)
2022-08-16 23:37:51 +08:00
}
func (w *WireGuard) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
2022-08-31 12:50:26 +08:00
return NewConnection(ctx, w, conn, metadata)
2022-08-16 23:37:51 +08:00
}
func (w *WireGuard) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
return NewPacketConnection(ctx, w, conn, metadata)
}
2023-03-21 21:36:17 +08:00
func (w *WireGuard) NewIPConnection(ctx context.Context, conn tun.RouteContext, metadata adapter.InboundContext) (tun.DirectDestination, error) {
if w.natDevice == nil {
return nil, os.ErrInvalid
}
session := tun.RouteSession{
IPVersion: metadata.IPVersion,
Network: tun.NetworkFromName(metadata.Network),
Source: metadata.Source.AddrPort(),
Destination: metadata.Destination.AddrPort(),
}
switch session.Network {
case syscall.IPPROTO_TCP:
w.logger.InfoContext(ctx, "linked connection to ", metadata.Destination)
case syscall.IPPROTO_UDP:
w.logger.InfoContext(ctx, "linked packet connection to ", metadata.Destination)
default:
w.logger.InfoContext(ctx, "linked ", metadata.Network, " connection to ", metadata.Destination.AddrString())
}
return w.natDevice.CreateDestination(session, conn), nil
}
2022-08-16 23:37:51 +08:00
func (w *WireGuard) Start() error {
2022-09-06 00:15:09 +08:00
return w.tunDevice.Start()
2022-08-16 23:37:51 +08:00
}
func (w *WireGuard) Close() error {
2022-09-23 13:14:31 +08:00
if w.device != nil {
w.device.Close()
}
return common.Close(w.tunDevice)
2022-08-16 23:37:51 +08:00
}