sing-box/outbound/wireguard.go

248 lines
7.3 KiB
Go
Raw Permalink Normal View History

2022-08-16 15:37:51 +00:00
//go:build with_wireguard
package outbound
import (
"context"
"encoding/base64"
"encoding/hex"
"fmt"
"net"
"strings"
"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-05 16:15:09 +00:00
"github.com/sagernet/sing-box/transport/wireguard"
2023-10-08 14:11:03 +00:00
"github.com/sagernet/sing-dns"
2022-09-15 04:20:38 +00:00
"github.com/sagernet/sing-tun"
2022-08-16 15:37:51 +00:00
"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 15:37:51 +00:00
)
2022-11-06 02:36:19 +00:00
var (
_ adapter.Outbound = (*WireGuard)(nil)
_ adapter.InterfaceUpdateListener = (*WireGuard)(nil)
)
2022-08-16 15:37:51 +00:00
type WireGuard struct {
myOutboundAdapter
2022-09-05 16:15:09 +00:00
bind *wireguard.ClientBind
device *device.Device
tunDevice wireguard.Device
2022-08-16 15:37:51 +00:00
}
func NewWireGuard(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.WireGuardOutboundOptions) (*WireGuard, error) {
outbound := &WireGuard{
myOutboundAdapter: myOutboundAdapter{
2023-06-13 14:38:05 +00:00
protocol: C.TypeWireGuard,
network: options.Network.Build(),
router: router,
logger: logger,
tag: tag,
dependencies: withDialerDependency(options.DialerOptions),
2022-08-16 15:37:51 +00:00
},
}
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)
}
var isConnect bool
var connectAddr M.Socksaddr
if len(options.Peers) < 2 {
isConnect = true
if len(options.Peers) == 1 {
connectAddr = options.Peers[0].ServerOptions.Build()
} else {
connectAddr = options.ServerOptions.Build()
}
}
2023-08-08 08:14:03 +00:00
outboundDialer, err := dialer.New(router, options.DialerOptions)
if err != nil {
return nil, err
}
outbound.bind = wireguard.NewClientBind(ctx, outbound, outboundDialer, isConnect, connectAddr, reserved)
2023-10-21 04:00:00 +00:00
if len(options.LocalAddress) == 0 {
2022-08-16 15:37:51 +00:00
return nil, E.New("missing local address")
}
var privateKey string
2022-08-16 15:37:51 +00:00
{
bytes, err := base64.StdEncoding.DecodeString(options.PrivateKey)
if err != nil {
return nil, E.Cause(err, "decode private key")
}
privateKey = hex.EncodeToString(bytes)
}
ipcConf := "private_key=" + privateKey
if len(options.Peers) > 0 {
for i, peer := range options.Peers {
var peerPublicKey, preSharedKey string
{
bytes, err := base64.StdEncoding.DecodeString(peer.PublicKey)
if err != nil {
return nil, E.Cause(err, "decode public key for peer ", i)
}
peerPublicKey = hex.EncodeToString(bytes)
}
if peer.PreSharedKey != "" {
bytes, err := base64.StdEncoding.DecodeString(peer.PreSharedKey)
if err != nil {
return nil, E.Cause(err, "decode pre shared key for peer ", i)
}
preSharedKey = hex.EncodeToString(bytes)
}
destination := peer.ServerOptions.Build()
ipcConf += "\npublic_key=" + peerPublicKey
ipcConf += "\nendpoint=" + destination.String()
if preSharedKey != "" {
ipcConf += "\npreshared_key=" + preSharedKey
}
if len(peer.AllowedIPs) == 0 {
return nil, E.New("missing allowed_ips for peer ", i)
}
for _, allowedIP := range peer.AllowedIPs {
ipcConf += "\nallowed_ip=" + allowedIP
}
if len(peer.Reserved) > 0 {
if len(peer.Reserved) != 3 {
return nil, E.New("invalid reserved value for peer ", i, ", required 3 bytes, got ", len(peer.Reserved))
}
copy(reserved[:], options.Reserved)
outbound.bind.SetReservedForEndpoint(destination, reserved)
}
2022-08-16 15:37:51 +00:00
}
} else {
var peerPublicKey, preSharedKey string
{
bytes, err := base64.StdEncoding.DecodeString(options.PeerPublicKey)
if err != nil {
return nil, E.Cause(err, "decode peer public key")
}
peerPublicKey = hex.EncodeToString(bytes)
2022-08-16 15:37:51 +00:00
}
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 += "\npublic_key=" + peerPublicKey
ipcConf += "\nendpoint=" + options.ServerOptions.Build().String()
if preSharedKey != "" {
ipcConf += "\npreshared_key=" + preSharedKey
}
var has4, has6 bool
2023-10-21 04:00:00 +00:00
for _, address := range options.LocalAddress {
if address.Addr().Is4() {
has4 = true
} else {
has6 = true
}
}
if has4 {
ipcConf += "\nallowed_ip=0.0.0.0/0"
}
if has6 {
ipcConf += "\nallowed_ip=::/0"
2022-08-16 15:37:51 +00:00
}
}
mtu := options.MTU
if mtu == 0 {
2022-08-17 07:19:10 +00:00
mtu = 1408
2022-08-16 15:37:51 +00:00
}
2022-09-05 16:15:09 +00:00
var wireTunDevice wireguard.Device
2022-09-15 04:20:38 +00:00
if !options.SystemInterface && tun.WithGVisor {
2023-10-21 04:00:00 +00:00
wireTunDevice, err = wireguard.NewStackDevice(options.LocalAddress, mtu)
2022-09-05 16:15:09 +00:00
} else {
2023-10-21 04:00:00 +00:00
wireTunDevice, err = wireguard.NewSystemDevice(router, options.InterfaceName, options.LocalAddress, mtu)
2022-09-05 16:15:09 +00:00
}
2022-08-16 15:37:51 +00:00
if err != nil {
2022-09-05 16:15:09 +00:00
return nil, E.Cause(err, "create WireGuard device")
2022-08-16 15:37:51 +00:00
}
2023-08-07 09:46:51 +00:00
wgDevice := device.NewDevice(ctx, wireTunDevice, outbound.bind, &device.Logger{
2022-08-16 15:37:51 +00: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 15:37:51 +00: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
2022-09-05 16:15:09 +00:00
outbound.tunDevice = wireTunDevice
2022-08-16 15:37:51 +00:00
return outbound, nil
}
2023-07-23 06:42:19 +00:00
func (w *WireGuard) InterfaceUpdated() {
2022-11-06 02:36:19 +00:00
w.bind.Reset()
2023-07-23 06:42:19 +00:00
return
2022-11-06 02:36:19 +00:00
}
2022-08-16 15:37:51 +00: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() {
2023-09-06 11:13:39 +00:00
destinationAddresses, err := w.router.LookupDefault(ctx, destination.Fqdn)
2022-08-16 15:37:51 +00:00
if err != nil {
return nil, err
}
2023-09-06 11:13:39 +00:00
return N.DialSerial(ctx, w.tunDevice, network, destination, destinationAddresses)
2022-08-16 15:37:51 +00:00
}
2022-09-05 16:15:09 +00:00
return w.tunDevice.DialContext(ctx, network, destination)
2022-08-16 15:37:51 +00:00
}
func (w *WireGuard) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
w.logger.InfoContext(ctx, "outbound packet connection to ", destination)
2023-09-06 11:13:39 +00:00
if destination.IsFqdn() {
destinationAddresses, err := w.router.LookupDefault(ctx, destination.Fqdn)
if err != nil {
return nil, err
}
packetConn, _, err := N.ListenSerial(ctx, w.tunDevice, destination, destinationAddresses)
if err != nil {
return nil, err
}
return packetConn, err
}
2022-09-05 16:15:09 +00:00
return w.tunDevice.ListenPacket(ctx, destination)
2022-08-16 15:37:51 +00:00
}
func (w *WireGuard) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
2023-10-08 14:11:03 +00:00
return NewDirectConnection(ctx, w.router, w, conn, metadata, dns.DomainStrategyAsIS)
2022-08-16 15:37:51 +00:00
}
func (w *WireGuard) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
2023-10-08 14:11:03 +00:00
return NewDirectPacketConnection(ctx, w.router, w, conn, metadata, dns.DomainStrategyAsIS)
2022-08-16 15:37:51 +00:00
}
func (w *WireGuard) Start() error {
2022-09-05 16:15:09 +00:00
return w.tunDevice.Start()
2022-08-16 15:37:51 +00:00
}
func (w *WireGuard) Close() error {
2022-09-23 05:14:31 +00:00
if w.device != nil {
w.device.Close()
}
2023-04-05 13:41:06 +00:00
w.tunDevice.Close()
return nil
2022-08-16 15:37:51 +00:00
}