mirror of
https://github.com/psviderski/uncloud.git
synced 2026-10-06 13:18:58 +00:00
chore: move legacy unused WG tunnel and client connector to experiment/wg
This commit is contained in:
1 parent
e81e130544
commit
e9e7b3a61e
7 files changed
+45
-33
No files matched your search
@@ -7,8 +7,6 @@ import (
|
||||
"net/netip"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/psviderski/uncloud/internal/secret"
|
||||
)
|
||||
|
||||
type MachineConnection struct {
|
||||
@@ -23,10 +21,8 @@ type MachineConnection struct {
|
||||
// The pointer is used to omit the field when not set. Otherwise, yaml marshalling includes an empty object.
|
||||
TCP *netip.AddrPort `yaml:"tcp,omitempty"`
|
||||
// Unix is the path to the machine's API unix socket.
|
||||
Unix string `yaml:"unix,omitempty"`
|
||||
Host string `yaml:"host,omitempty"`
|
||||
PublicKey secret.Secret `yaml:"public_key,omitempty"`
|
||||
MachineID string `yaml:"machine_id,omitempty"`
|
||||
Unix string `yaml:"unix,omitempty"`
|
||||
MachineID string `yaml:"machine_id,omitempty"`
|
||||
}
|
||||
|
||||
func (c *MachineConnection) String() string {
|
||||
|
||||
@@ -1,97 +0,0 @@
|
||||
package tunnel
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/netip"
|
||||
"time"
|
||||
|
||||
"github.com/psviderski/uncloud/internal/secret"
|
||||
"golang.zx2c4.com/wireguard/conn"
|
||||
"golang.zx2c4.com/wireguard/device"
|
||||
"golang.zx2c4.com/wireguard/tun/netstack"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultEndpointPort = 51820
|
||||
// DefaultKeepaliveInterval is sensible interval that works with a wide variety of firewalls.
|
||||
DefaultKeepaliveInterval = 25 * time.Second
|
||||
)
|
||||
|
||||
type Tunnel struct {
|
||||
dev *device.Device
|
||||
net *netstack.Net
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
LocalAddress netip.Addr
|
||||
LocalPrivateKey secret.Secret
|
||||
Endpoint netip.AddrPort
|
||||
RemotePublicKey secret.Secret
|
||||
RemoteNetwork netip.Prefix
|
||||
DNS *netip.Addr
|
||||
MTU int
|
||||
KeepAlive time.Duration
|
||||
}
|
||||
|
||||
func Connect(config *Config) (*Tunnel, error) {
|
||||
var dns netip.Addr
|
||||
if config.DNS != nil {
|
||||
dns = *config.DNS
|
||||
} else {
|
||||
dns = netip.MustParseAddr("1.1.1.1")
|
||||
}
|
||||
mtu := config.MTU
|
||||
if mtu == 0 {
|
||||
mtu = device.DefaultMTU
|
||||
}
|
||||
keepAlive := config.KeepAlive
|
||||
if keepAlive == 0 {
|
||||
keepAlive = DefaultKeepaliveInterval
|
||||
}
|
||||
|
||||
tun, tnet, err := netstack.CreateNetTUN([]netip.Addr{config.LocalAddress}, []netip.Addr{dns}, mtu)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create WireGuard TUN device: %w", err)
|
||||
}
|
||||
|
||||
dev := device.NewDevice(tun, conn.NewDefaultBind(), device.NewLogger(device.LogLevelError, "WireGuard tunnel: "))
|
||||
conf := fmt.Sprintf(
|
||||
"private_key=%s\n"+
|
||||
"public_key=%s\n"+
|
||||
"endpoint=%s\n"+
|
||||
"allowed_ip=%s\n"+
|
||||
"persistent_keepalive_interval=%d\n",
|
||||
config.LocalPrivateKey.String(),
|
||||
config.RemotePublicKey.String(),
|
||||
config.Endpoint.String(),
|
||||
config.RemoteNetwork.String(),
|
||||
int(keepAlive.Seconds()),
|
||||
)
|
||||
err = dev.IpcSet(conf)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("configure WireGuard device: %w", err)
|
||||
}
|
||||
|
||||
err = dev.Up()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("enable WireGuard device: %w", err)
|
||||
}
|
||||
|
||||
return &Tunnel{
|
||||
dev: dev,
|
||||
net: tnet,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (t *Tunnel) Close() {
|
||||
if t.dev != nil {
|
||||
t.dev.Close()
|
||||
}
|
||||
t.dev, t.net = nil, nil
|
||||
}
|
||||
|
||||
func (t *Tunnel) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
return t.net.DialContext(ctx, network, address)
|
||||
}
|
||||
Reference in new issue
Block a user