2022-11-22 03:05:54 +02:00
|
|
|
package wireguard
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-11-12 22:10:01 +02:00
|
|
|
"errors"
|
2022-11-22 03:05:54 +02:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/netip"
|
2023-11-12 22:10:01 +02:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2022-11-22 03:05:54 +02:00
|
|
|
|
2023-11-12 22:10:01 +02:00
|
|
|
"github.com/xtls/xray-core/common/log"
|
2022-11-22 03:05:54 +02:00
|
|
|
|
2023-11-12 22:10:01 +02:00
|
|
|
"golang.zx2c4.com/wireguard/conn"
|
|
|
|
"golang.zx2c4.com/wireguard/device"
|
|
|
|
"golang.zx2c4.com/wireguard/tun"
|
|
|
|
)
|
2022-11-22 03:05:54 +02:00
|
|
|
|
2023-11-12 22:10:01 +02:00
|
|
|
type Tunnel interface {
|
|
|
|
BuildDevice(ipc string, bind conn.Bind) error
|
|
|
|
DialContextTCPAddrPort(ctx context.Context, addr netip.AddrPort) (net.Conn, error)
|
|
|
|
DialUDPAddrPort(laddr, raddr netip.AddrPort) (net.Conn, error)
|
|
|
|
Close() error
|
2022-11-22 03:05:54 +02:00
|
|
|
}
|
|
|
|
|
2023-11-12 22:10:01 +02:00
|
|
|
type tunnel struct {
|
|
|
|
tun tun.Device
|
|
|
|
device *device.Device
|
|
|
|
rw sync.Mutex
|
2022-11-22 03:05:54 +02:00
|
|
|
}
|
|
|
|
|
2023-11-12 22:10:01 +02:00
|
|
|
func (t *tunnel) BuildDevice(ipc string, bind conn.Bind) (err error) {
|
|
|
|
t.rw.Lock()
|
|
|
|
defer t.rw.Unlock()
|
2022-11-22 03:05:54 +02:00
|
|
|
|
2023-11-12 22:10:01 +02:00
|
|
|
if t.device != nil {
|
|
|
|
return errors.New("device is already initialized")
|
2022-11-22 03:05:54 +02:00
|
|
|
}
|
|
|
|
|
2023-11-12 22:10:01 +02:00
|
|
|
logger := &device.Logger{
|
|
|
|
Verbosef: func(format string, args ...any) {
|
|
|
|
log.Record(&log.GeneralMessage{
|
|
|
|
Severity: log.Severity_Debug,
|
|
|
|
Content: fmt.Sprintf(format, args...),
|
|
|
|
})
|
|
|
|
},
|
|
|
|
Errorf: func(format string, args ...any) {
|
|
|
|
log.Record(&log.GeneralMessage{
|
|
|
|
Severity: log.Severity_Error,
|
|
|
|
Content: fmt.Sprintf(format, args...),
|
|
|
|
})
|
|
|
|
},
|
2022-11-22 03:05:54 +02:00
|
|
|
}
|
|
|
|
|
2023-11-12 22:10:01 +02:00
|
|
|
t.device = device.NewDevice(t.tun, bind, logger)
|
|
|
|
if err = t.device.IpcSet(ipc); err != nil {
|
|
|
|
return err
|
2022-11-22 03:05:54 +02:00
|
|
|
}
|
2023-11-12 22:10:01 +02:00
|
|
|
if err = t.device.Up(); err != nil {
|
|
|
|
return err
|
2022-11-22 03:05:54 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-12 22:10:01 +02:00
|
|
|
func (t *tunnel) Close() (err error) {
|
|
|
|
t.rw.Lock()
|
|
|
|
defer t.rw.Unlock()
|
2022-11-22 03:05:54 +02:00
|
|
|
|
2023-11-12 22:10:01 +02:00
|
|
|
if t.device == nil {
|
|
|
|
return nil
|
2022-11-22 03:05:54 +02:00
|
|
|
}
|
|
|
|
|
2023-11-12 22:10:01 +02:00
|
|
|
t.device.Close()
|
|
|
|
t.device = nil
|
|
|
|
err = t.tun.Close()
|
|
|
|
t.tun = nil
|
2022-11-22 03:05:54 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-12 22:10:01 +02:00
|
|
|
func CalculateInterfaceName(name string) (tunName string) {
|
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
tunName = "utun"
|
|
|
|
} else if name != "" {
|
|
|
|
tunName = name
|
2022-11-22 03:05:54 +02:00
|
|
|
} else {
|
2023-11-12 22:10:01 +02:00
|
|
|
tunName = "tun"
|
2022-11-22 03:05:54 +02:00
|
|
|
}
|
2023-11-12 22:10:01 +02:00
|
|
|
interfaces, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
return
|
2022-11-22 03:05:54 +02:00
|
|
|
}
|
2023-11-12 22:10:01 +02:00
|
|
|
var tunIndex int
|
|
|
|
for _, netInterface := range interfaces {
|
|
|
|
if strings.HasPrefix(netInterface.Name, tunName) {
|
|
|
|
index, parseErr := strconv.ParseInt(netInterface.Name[len(tunName):], 10, 16)
|
|
|
|
if parseErr == nil {
|
|
|
|
tunIndex = int(index) + 1
|
2022-11-22 03:05:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-12 22:10:01 +02:00
|
|
|
tunName = fmt.Sprintf("%s%d", tunName, tunIndex)
|
|
|
|
return
|
2022-11-22 03:05:54 +02:00
|
|
|
}
|