Skip to content

Commit

Permalink
Replace socks proxy lib
Browse files Browse the repository at this point in the history
Fix wg interface configuration order
  • Loading branch information
pappz committed Dec 27, 2023
1 parent c3ecfe7 commit 16875df
Show file tree
Hide file tree
Showing 26 changed files with 342 additions and 247 deletions.
6 changes: 3 additions & 3 deletions client/internal/dns/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func TestUpdateDNSServer(t *testing.T) {
if err != nil {
t.Fatal(err)
}
wgIface, err := iface.NewWGIFace(context.Background(), fmt.Sprintf("utun230%d", n), fmt.Sprintf("100.66.100.%d/32", n+1), 33100, iface.DefaultMTU, newNet, nil)
wgIface, err := iface.NewWGIFace(fmt.Sprintf("utun230%d", n), fmt.Sprintf("100.66.100.%d/32", n+1), 33100, "", iface.DefaultMTU, newNet, nil)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -331,7 +331,7 @@ func TestDNSFakeResolverHandleUpdates(t *testing.T) {
return
}

wgIface, err := iface.NewWGIFace(context.Background(), "utun2301", "100.66.100.1/32", 33100, iface.DefaultMTU, newNet, nil)
wgIface, err := iface.NewWGIFace("utun2301", "100.66.100.1/32", 33100, "", iface.DefaultMTU, newNet, nil)
if err != nil {
t.Errorf("build interface wireguard: %v", err)
return
Expand Down Expand Up @@ -782,7 +782,7 @@ func createWgInterfaceWithBind(t *testing.T) (*iface.WGIface, error) {
return nil, err
}

wgIface, err := iface.NewWGIFace(context.Background(), "utun2301", "100.66.100.2/24", 33100, iface.DefaultMTU, newNet, nil)
wgIface, err := iface.NewWGIFace("utun2301", "100.66.100.2/24", 33100, "", iface.DefaultMTU, newNet, nil)
if err != nil {
t.Fatalf("build interface wireguard: %v", err)
return nil, err
Expand Down
7 changes: 3 additions & 4 deletions client/internal/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func (e *Engine) Start() error {
default:
}

e.wgInterface, err = iface.NewWGIFace(e.ctx, wgIFaceName, wgAddr, e.config.WgPort, iface.DefaultMTU, transportNet, mArgs)
e.wgInterface, err = iface.NewWGIFace(wgIFaceName, wgAddr, e.config.WgPort, myPrivateKey.String(), iface.DefaultMTU, transportNet, mArgs)
if err != nil {
log.Errorf("failed creating wireguard interface instance %s: [%s]", wgIFaceName, err.Error())
return err
Expand Down Expand Up @@ -265,13 +265,12 @@ func (e *Engine) Start() error {
}
}

err = e.wgInterface.Configure(myPrivateKey.String(), e.config.WgPort)
e.udpMux, err = e.wgInterface.Up()
if err != nil {
log.Errorf("failed configuring Wireguard interface [%s]: %s", wgIFaceName, err.Error())
log.Errorf("failed to pull up wgInterface [%s]: %s", wgIFaceName, err.Error())
e.close()
return err
}
e.udpMux = e.wgInterface.GetUdpMux()

if e.firewall != nil {
e.acl = acl.NewDefaultManager(e.firewall)
Expand Down
6 changes: 3 additions & 3 deletions client/internal/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func TestEngine_UpdateNetworkMap(t *testing.T) {
if err != nil {
t.Fatal(err)
}
engine.wgInterface, err = iface.NewWGIFace(ctx, "utun102", "100.64.0.1/24", engine.config.WgPort, iface.DefaultMTU, newNet, nil)
engine.wgInterface, err = iface.NewWGIFace("utun102", "100.64.0.1/24", engine.config.WgPort, key.String(), iface.DefaultMTU, newNet, nil)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -567,7 +567,7 @@ func TestEngine_UpdateNetworkMapWithRoutes(t *testing.T) {
if err != nil {
t.Fatal(err)
}
engine.wgInterface, err = iface.NewWGIFace(ctx, wgIfaceName, wgAddr, engine.config.WgPort, iface.DefaultMTU, newNet, nil)
engine.wgInterface, err = iface.NewWGIFace(wgIfaceName, wgAddr, engine.config.WgPort, key.String(), iface.DefaultMTU, newNet, nil)
assert.NoError(t, err, "shouldn't return error")
input := struct {
inputSerial uint64
Expand Down Expand Up @@ -736,7 +736,7 @@ func TestEngine_UpdateNetworkMapWithDNSUpdate(t *testing.T) {
if err != nil {
t.Fatal(err)
}
engine.wgInterface, err = iface.NewWGIFace(ctx, wgIfaceName, wgAddr, engine.config.WgPort, iface.DefaultMTU, newNet, nil)
engine.wgInterface, err = iface.NewWGIFace(wgIfaceName, wgAddr, 33100, key.String(), iface.DefaultMTU, newNet, nil)
assert.NoError(t, err, "shouldn't return error")

mockRouteManager := &routemanager.MockManager{
Expand Down
2 changes: 1 addition & 1 deletion client/internal/routemanager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ func TestManagerUpdateRoutes(t *testing.T) {
if err != nil {
t.Fatal(err)
}
wgInterface, err := iface.NewWGIFace(context.Background(), fmt.Sprintf("utun43%d", n), "100.65.65.2/24", 33100, iface.DefaultMTU, newNet, nil)
wgInterface, err := iface.NewWGIFace(fmt.Sprintf("utun43%d", n), "100.65.65.2/24", 33100, "", iface.DefaultMTU, newNet, nil)
require.NoError(t, err, "should create testing WGIface interface")
defer wgInterface.Close()

Expand Down
5 changes: 2 additions & 3 deletions client/internal/routemanager/systemops_nonandroid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package routemanager

import (
"bytes"
"context"
"fmt"
"net"
"net/netip"
Expand Down Expand Up @@ -46,7 +45,7 @@ func TestAddRemoveRoutes(t *testing.T) {
if err != nil {
t.Fatal(err)
}
wgInterface, err := iface.NewWGIFace(context.Background(), fmt.Sprintf("utun53%d", n), "100.65.75.2/24", 33100, iface.DefaultMTU, newNet, nil)
wgInterface, err := iface.NewWGIFace(fmt.Sprintf("utun53%d", n), "100.65.75.2/24", 33100, "", iface.DefaultMTU, newNet, nil)
require.NoError(t, err, "should create testing WGIface interface")
defer wgInterface.Close()

Expand Down Expand Up @@ -180,7 +179,7 @@ func TestAddExistAndRemoveRouteNonAndroid(t *testing.T) {
if err != nil {
t.Fatal(err)
}
wgInterface, err := iface.NewWGIFace(context.Background(), fmt.Sprintf("utun53%d", n), "100.65.75.2/24", 33100, iface.DefaultMTU, newNet, nil)
wgInterface, err := iface.NewWGIFace(fmt.Sprintf("utun53%d", n), "100.65.75.2/24", 33100, "", iface.DefaultMTU, newNet, nil)
require.NoError(t, err, "should create testing WGIface interface")
defer wgInterface.Close()

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ require (
require (
fyne.io/fyne/v2 v2.1.4
github.com/TheJumpCloud/jcapi-go v3.0.0+incompatible
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5
github.com/c-robinson/iplib v1.0.3
github.com/cilium/ebpf v0.10.0
github.com/coreos/go-iptables v0.7.0
Expand Down Expand Up @@ -63,6 +62,7 @@ require (
github.com/rs/xid v1.3.0
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
github.com/stretchr/testify v1.8.4
github.com/things-go/go-socks5 v0.0.4
github.com/yusufpapurcu/wmi v1.2.3
go.opentelemetry.io/otel v1.11.1
go.opentelemetry.io/otel/exporters/prometheus v0.33.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFI
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
github.com/bazelbuild/rules_go v0.30.0/go.mod h1:MC23Dc/wkXEyk3Wpq6lCqz0ZAYOZDw2DR5y3N1q2i7M=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
Expand Down Expand Up @@ -667,6 +665,8 @@ github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXl
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/syndtr/gocapability v0.0.0-20180916011248-d98352740cb2/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
github.com/things-go/go-socks5 v0.0.4 h1:jMQjIc+qhD4z9cITOMnBiwo9dDmpGuXmBlkRFrl/qD0=
github.com/things-go/go-socks5 v0.0.4/go.mod h1:sh4K6WHrmHZpjxLTCHyYtXYH8OUuD+yZun41NomR1IQ=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/tv42/httpunix v0.0.0-20191220191345-2ba4b9c3382c/go.mod h1:hzIxponao9Kjc7aWznkXaL4U4TWaDSs8zcsY4Ka08nM=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
Expand Down
14 changes: 5 additions & 9 deletions iface/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ func (w *WGIface) Address() WGAddress {
return w.tun.WgAddress()
}

// Configure configures a Wireguard interface
// Up configures a Wireguard interface
// The interface must exist before calling this method (e.g. call interface.Create() before)
func (w *WGIface) Configure(privateKey string, port int) error {
func (w *WGIface) Up() (*bind.UniversalUDPMuxDefault, error) {
w.mu.Lock()
defer w.mu.Unlock()
log.Debugf("configuring Wireguard interface %s", w.tun.DeviceName())
return w.configurer.configureInterface(privateKey, port)

return w.tun.Up()
}

// UpdateAddr updates address of the interface
Expand All @@ -70,7 +70,7 @@ func (w *WGIface) UpdatePeer(peerKey string, allowedIps string, keepAlive time.D
w.mu.Lock()
defer w.mu.Unlock()

log.Debugf("updating interface %s peer %s, endpoint %s ", w.tun.DeviceName(), peerKey, endpoint)
log.Debugf("updating interface %s peer %s, endpoint %s", w.tun.DeviceName(), peerKey, endpoint)
return w.configurer.updatePeer(peerKey, allowedIps, keepAlive, endpoint, preSharedKey)
}

Expand Down Expand Up @@ -139,7 +139,3 @@ func (w *WGIface) GetDevice() *DeviceWrapper {

return w.tun.Wrapper()
}

func (w *WGIface) GetUdpMux() *bind.UniversalUDPMuxDefault {
return w.tun.UdpMux()
}
5 changes: 2 additions & 3 deletions iface/iface_android.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package iface

import (
"context"
"fmt"

"github.com/pion/transport/v3"
)

// NewWGIFace Creates a new WireGuard interface instance
func NewWGIFace(ctx context.Context, ifaceName string, address string, wgPort int, mtu int, transportNet transport.Net, args *MobileIFaceArguments) (*WGIface, error) {
func NewWGIFace(iFaceName string, address string, wgPort int, wgPrivKey string, mtu int, transportNet transport.Net, args *MobileIFaceArguments) (*WGIface, error) {
wgAddress, err := parseWGAddress(address)
if err != nil {
return nil, err
}

wgIFace := &WGIface{
tun: newTunDevice(wgAddress, mtu, transportNet, args.TunAdapter),
tun: newTunDevice(wgAddress, wgPort, wgPrivKey, mtu, transportNet, args.TunAdapter),
userspaceBind: false,
}
return wgIFace, nil
Expand Down
3 changes: 0 additions & 3 deletions iface/iface_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

package iface

import log "github.com/sirupsen/logrus"

// Create creates a new Wireguard interface, sets a given IP and brings it up.
// Will reuse an existing one.
// this function is different on Android
Expand All @@ -16,7 +14,6 @@ func (w *WGIface) Create() error {
if err != nil {
return err
}
log.Infof("using userspace bind mode: %s", w.tun.UdpMux().LocalAddr().String())

w.configurer = cfgr
return nil
Expand Down
7 changes: 3 additions & 4 deletions iface/iface_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package iface

import (
"context"
"fmt"

"github.com/pion/transport/v3"
Expand All @@ -13,7 +12,7 @@ import (
)

// NewWGIFace Creates a new WireGuard interface instance
func NewWGIFace(ctx context.Context, iFaceName string, address string, wgPort int, mtu int, transportNet transport.Net, args *MobileIFaceArguments) (*WGIface, error) {
func NewWGIFace(iFaceName string, address string, wgPort int, wgPrivKey string, mtu int, transportNet transport.Net, args *MobileIFaceArguments) (*WGIface, error) {
wgAddress, err := parseWGAddress(address)
if err != nil {
return nil, err
Expand All @@ -22,12 +21,12 @@ func NewWGIFace(ctx context.Context, iFaceName string, address string, wgPort in
wgIFace := &WGIface{}

if netstack.IsEnabled() {
wgIFace.tun = newTunNetstackDevice(iFaceName, wgAddress, wgPort, transportNet, netstack.ListenAddr())
wgIFace.tun = newTunNetstackDevice(iFaceName, wgAddress, wgPort, wgPrivKey, mtu, transportNet, netstack.ListenAddr())
wgIFace.userspaceBind = true
return wgIFace, nil
}

wgIFace.tun = newTunDevice(iFaceName, wgAddress, wgPort, transportNet)
wgIFace.tun = newTunDevice(iFaceName, wgAddress, wgPort, wgPrivKey, mtu, transportNet)
wgIFace.userspaceBind = false

return wgIFace, nil
Expand Down
7 changes: 3 additions & 4 deletions iface/iface_ios.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@
package iface

import (
"context"
"fmt"

"github.com/pion/transport/v3"
)

// NewWGIFace Creates a new WireGuard interface instance
func NewWGIFace(ctx context.Context, ifaceName string, address string, wgPort int, mtu int, transportNet transport.Net, mobileIFaceArgs *MobileIFaceArguments) (*WGIface, error) {
func NewWGIFace(iFaceName string, address string, wgPort int, wgPrivKey string, mtu int, transportNet transport.Net, args *MobileIFaceArguments) (*WGIface, error) {
wgAddress, err := parseWGAddress(address)
if err != nil {
return nil, err
}

wgIFace := &WGIface{
tun: newTunDevice(ifaceName, wgAddress, transportNet, mobileIFaceArgs.TunFd),

tun: newTunDevice(iFaceName, wgAddress, wgPort, wgPrivKey, transportNet, args.TunFd),
userspaceBind: false,
}
return wgIFace, nil
Expand Down
9 changes: 4 additions & 5 deletions iface/iface_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package iface

import (
"context"
"fmt"

"github.com/pion/transport/v3"
Expand All @@ -13,7 +12,7 @@ import (
)

// NewWGIFace Creates a new WireGuard interface instance
func NewWGIFace(ctx context.Context, iFaceName string, address string, wgPort int, mtu int, transportNet transport.Net, args *MobileIFaceArguments) (*WGIface, error) {
func NewWGIFace(iFaceName string, address string, wgPort int, wgPrivKey string, mtu int, transportNet transport.Net, args *MobileIFaceArguments) (*WGIface, error) {
wgAddress, err := parseWGAddress(address)
if err != nil {
return nil, err
Expand All @@ -23,21 +22,21 @@ func NewWGIFace(ctx context.Context, iFaceName string, address string, wgPort in

// move the kernel/usp/netstack preference evaluation to upper layer
if netstack.IsEnabled() {
wgIFace.tun = newTunNetstackDevice(iFaceName, wgAddress, mtu, transportNet, netstack.ListenAddr())
wgIFace.tun = newTunNetstackDevice(iFaceName, wgAddress, wgPort, wgPrivKey, mtu, transportNet, netstack.ListenAddr())
wgIFace.userspaceBind = true
return wgIFace, nil
}

if WireGuardModuleIsLoaded() {
wgIFace.tun = newTunDevice(ctx, iFaceName, wgAddress, wgPort, mtu, transportNet)
wgIFace.tun = newTunDevice(iFaceName, wgAddress, wgPort, wgPrivKey, mtu, transportNet)
wgIFace.userspaceBind = false
return wgIFace, nil
}

if !tunModuleIsLoaded() {
return nil, fmt.Errorf("couldn't check or load tun module")
}
wgIFace.tun = newTunUSPDevice(iFaceName, wgAddress, mtu, transportNet)
wgIFace.tun = newTunUSPDevice(iFaceName, wgAddress, wgPort, wgPrivKey, mtu, transportNet)
wgIFace.userspaceBind = true
return wgIFace, nil

Expand Down

0 comments on commit 16875df

Please sign in to comment.