feat: прямой туннель без релея
This commit is contained in:
1 parent
fa9549e6e0
commit
2812ded2ed
18 files changed
+586
-26
No files matched your search
@@ -3,6 +3,7 @@ package awg
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -18,9 +19,10 @@ import (
|
||||
type Deps struct {
|
||||
Bind conn.Bind
|
||||
Log logx.Logger
|
||||
// Protect исключает сокеты bind из туннеля (VpnService.protect); nil - бэкенд поверх пайпа релея.
|
||||
Protect func(fd int)
|
||||
}
|
||||
|
||||
// Backend управляет userspace WireGuard/AmneziaWG устройством.
|
||||
type Backend struct {
|
||||
deps Deps
|
||||
|
||||
@@ -69,6 +71,8 @@ func (b *Backend) Up(cfg *tunnel.Config, tunFD int) error {
|
||||
return err
|
||||
}
|
||||
|
||||
b.protectBind(dev)
|
||||
|
||||
b.dev = dev
|
||||
mode := "wg"
|
||||
if cfg.Amnezia.Enabled() {
|
||||
@@ -78,7 +82,45 @@ func (b *Backend) Up(cfg *tunnel.Config, tunFD int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Down останавливает устройство и закрывает tun-дескриптор.
|
||||
func (b *Backend) Rebind() error {
|
||||
b.mu.Lock()
|
||||
dev := b.dev
|
||||
b.mu.Unlock()
|
||||
|
||||
if dev == nil {
|
||||
return nil
|
||||
}
|
||||
if err := dev.BindUpdate(); err != nil {
|
||||
return fmt.Errorf("awg: bind update: %w", err)
|
||||
}
|
||||
b.protectBind(dev)
|
||||
b.deps.Log.Infof("tunnel: rebound")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Backend) protectBind(dev *device.Device) {
|
||||
if b.deps.Protect == nil {
|
||||
return
|
||||
}
|
||||
peek, ok := dev.Bind().(conn.PeekLookAtSocketFd)
|
||||
if !ok {
|
||||
b.deps.Log.Warnf("tunnel: bind does not expose socket fd, protect skipped")
|
||||
return
|
||||
}
|
||||
protected := 0
|
||||
if fd, err := peek.PeekLookAtSocketFd4(); err == nil {
|
||||
b.deps.Protect(fd)
|
||||
protected++
|
||||
}
|
||||
if fd, err := peek.PeekLookAtSocketFd6(); err == nil {
|
||||
b.deps.Protect(fd)
|
||||
protected++
|
||||
}
|
||||
if protected == 0 {
|
||||
b.deps.Log.Warnf("tunnel: no bind socket to protect")
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Backend) Down() error {
|
||||
b.mu.Lock()
|
||||
dev := b.dev
|
||||
@@ -93,7 +135,6 @@ func (b *Backend) Down() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stats возвращает текущие счетчики трафика туннеля.
|
||||
func (b *Backend) Stats() (tunnel.Stats, error) {
|
||||
b.mu.Lock()
|
||||
dev := b.dev
|
||||
|
||||
@@ -24,7 +24,7 @@ type Peer struct {
|
||||
PresharedKey Key
|
||||
AllowedIPs []netip.Prefix
|
||||
Endpoint string
|
||||
Keepalive int
|
||||
Keepalive string
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
@@ -95,8 +95,8 @@ func (c *Config) Validate() error {
|
||||
if len(p.AllowedIPs) == 0 {
|
||||
return fmt.Errorf("tunnel: peer[%d]: AllowedIPs is required", i)
|
||||
}
|
||||
if p.Keepalive < 0 {
|
||||
return fmt.Errorf("tunnel: peer[%d]: negative keepalive", i)
|
||||
if err := ValidateRange(p.Keepalive); err != nil {
|
||||
return fmt.Errorf("tunnel: peer[%d]: keepalive: %w", i, err)
|
||||
}
|
||||
}
|
||||
if c.MTU <= 0 {
|
||||
|
||||
@@ -36,3 +36,8 @@ type Backend interface {
|
||||
Down() error
|
||||
Stats() (Stats, error)
|
||||
}
|
||||
|
||||
// Rebinder реализует бэкенд с собственными сокетами
|
||||
type Rebinder interface {
|
||||
Rebind() error
|
||||
}
|
||||
@@ -36,8 +36,8 @@ func UAPI(c *Config) (string, error) {
|
||||
} else {
|
||||
line("endpoint", EndpointSinglePeer)
|
||||
}
|
||||
if p.Keepalive > 0 {
|
||||
line("persistent_keepalive_interval", strconv.Itoa(p.Keepalive))
|
||||
if p.Keepalive != "" && p.Keepalive != "0" {
|
||||
line("persistent_keepalive_interval", p.Keepalive)
|
||||
}
|
||||
line("replace_allowed_ips", "true")
|
||||
for _, ip := range p.AllowedIPs {
|
||||
|
||||
@@ -92,7 +92,7 @@ func TestUAPIFullPeer(t *testing.T) {
|
||||
cfg := minimalConfig()
|
||||
cfg.Peers[0].PresharedKey = testKey(0x03)
|
||||
cfg.Peers[0].Endpoint = "1.2.3.4:51820"
|
||||
cfg.Peers[0].Keepalive = 25
|
||||
cfg.Peers[0].Keepalive = "25"
|
||||
cfg.Peers[0].AllowedIPs = []netip.Prefix{
|
||||
netip.MustParsePrefix("10.0.0.0/24"),
|
||||
netip.MustParsePrefix("::/0"),
|
||||
@@ -144,7 +144,7 @@ func TestUAPIRejectsInvalidConfig(t *testing.T) {
|
||||
{"no public key", func(c *Config) { c.Peers[0].PublicKey = Key{} }},
|
||||
{"no allowed ips", func(c *Config) { c.Peers[0].AllowedIPs = nil }},
|
||||
{"zero mtu", func(c *Config) { c.MTU = 0 }},
|
||||
{"negative keepalive", func(c *Config) { c.Peers[0].Keepalive = -1 }},
|
||||
{"bad keepalive range", func(c *Config) { c.Peers[0].Keepalive = "35-25" }},
|
||||
{"partial junk", func(c *Config) { c.Amnezia = AmneziaParams{Jc: 4} }},
|
||||
{"junk range", func(c *Config) { c.Amnezia = AmneziaParams{Jc: 4, Jmin: 70, Jmax: 40} }},
|
||||
{"negative padding", func(c *Config) { c.Amnezia = AmneziaParams{S1: -1} }},
|
||||
|
||||
@@ -136,7 +136,7 @@ func applyInterface(cfg *tunnel.Config, key, value string) error {
|
||||
}
|
||||
setAmneziaRange(&cfg.Amnezia, key, value)
|
||||
case "randomtrailers", "disablecookies":
|
||||
b, err := strconv.ParseBool(value)
|
||||
b, err := parseBool(value)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %w", key, err)
|
||||
}
|
||||
@@ -151,6 +151,17 @@ func applyInterface(cfg *tunnel.Config, key, value string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// AmneziaWG пишет булевы как on/off и yes/no.
|
||||
func parseBool(value string) (bool, error) {
|
||||
switch strings.ToLower(value) {
|
||||
case "on", "yes":
|
||||
return true, nil
|
||||
case "off", "no":
|
||||
return false, nil
|
||||
}
|
||||
return strconv.ParseBool(value)
|
||||
}
|
||||
|
||||
func setAmneziaRange(p *tunnel.AmneziaParams, key, value string) {
|
||||
switch key {
|
||||
case "contentpaddingaddition":
|
||||
@@ -211,14 +222,13 @@ func applyPeer(peer *tunnel.Peer, key, value string) error {
|
||||
peer.Endpoint = value
|
||||
case "persistentkeepalive":
|
||||
if strings.EqualFold(value, "off") {
|
||||
peer.Keepalive = 0
|
||||
peer.Keepalive = ""
|
||||
return nil
|
||||
}
|
||||
seconds, err := strconv.Atoi(value)
|
||||
if err != nil {
|
||||
if err := tunnel.ValidateRange(value); err != nil {
|
||||
return fmt.Errorf("persistentkeepalive: %w", err)
|
||||
}
|
||||
peer.Keepalive = seconds
|
||||
peer.Keepalive = value
|
||||
default:
|
||||
return fmt.Errorf("unknown [Peer] key %q", key)
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ PersistentKeepalive = 25
|
||||
t.Errorf("DNS = %v, want two addresses", cfg.DNS)
|
||||
}
|
||||
peer := cfg.Peers[0]
|
||||
if peer.PresharedKey.IsZero() || peer.Endpoint != "1.2.3.4:51820" || peer.Keepalive != 25 {
|
||||
if peer.PresharedKey.IsZero() || peer.Endpoint != "1.2.3.4:51820" || peer.Keepalive != "25" {
|
||||
t.Errorf("peer = %+v", peer)
|
||||
}
|
||||
if len(peer.AllowedIPs) != 2 {
|
||||
@@ -155,8 +155,8 @@ func TestParseKeepaliveOff(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("Parse() error = %v", err)
|
||||
}
|
||||
if cfg.Peers[0].Keepalive != 0 {
|
||||
t.Errorf("Keepalive = %d, want 0", cfg.Peers[0].Keepalive)
|
||||
if cfg.Peers[0].Keepalive != "" {
|
||||
t.Errorf("Keepalive = %q, want empty", cfg.Peers[0].Keepalive)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -239,6 +239,36 @@ func TestParseAWG3Params(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Конфиг в том виде, в каком его выдаёт AmneziaVPN: булевы on/off, keepalive диапазоном.
|
||||
func TestParseAmneziaClientConf(t *testing.T) {
|
||||
hpKey := base64.StdEncoding.EncodeToString(bytesOf(0x04))
|
||||
text := "[Interface]\nPrivateKey = " + privKey + "\nAddress = 10.8.0.2/32\n" +
|
||||
"S1 = 12\nS2 = 12\nS3 = 12\nS4 = 12\n" +
|
||||
"HeaderProtectionKey = " + hpKey + "\n" +
|
||||
"RandomTrailers = on\nDisableCookies = off\n\n" +
|
||||
"[Peer]\nPublicKey = " + pubKey + "\nAllowedIPs = 0.0.0.0/0\n" +
|
||||
"Endpoint = 1.2.3.4:55424\nPersistentKeepalive = 25-35\n"
|
||||
|
||||
cfg, err := Parse(text)
|
||||
if err != nil {
|
||||
t.Fatalf("Parse() error = %v", err)
|
||||
}
|
||||
if !cfg.Amnezia.RandomTrailers || cfg.Amnezia.DisableCookies {
|
||||
t.Errorf("bool flags = %v/%v, want true/false",
|
||||
cfg.Amnezia.RandomTrailers, cfg.Amnezia.DisableCookies)
|
||||
}
|
||||
if cfg.Peers[0].Keepalive != "25-35" {
|
||||
t.Errorf("Keepalive = %q, want 25-35", cfg.Peers[0].Keepalive)
|
||||
}
|
||||
uapi, err := tunnel.UAPI(cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("UAPI() error = %v", err)
|
||||
}
|
||||
if !strings.Contains(uapi, "persistent_keepalive_interval=25-35\n") {
|
||||
t.Errorf("UAPI() = %s, want keepalive range passed through", uapi)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseAWG3Errors(t *testing.T) {
|
||||
base := "[Interface]\nPrivateKey = " + privKey + "\nAddress = 10.8.0.2/32\n"
|
||||
peer := "\n[Peer]\nPublicKey = " + pubKey + "\nAllowedIPs = 0.0.0.0/0\n"
|
||||
@@ -255,3 +285,15 @@ func TestParseAWG3Errors(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseKeepaliveErrors(t *testing.T) {
|
||||
for name, value := range map[string]string{
|
||||
"non-numeric": "soon",
|
||||
"reversed": "35-25",
|
||||
"broken bounds": "25-",
|
||||
} {
|
||||
if _, err := Parse(minimalConf() + "PersistentKeepalive = " + value + "\n"); err == nil {
|
||||
t.Errorf("%s: Parse() = nil, want error", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user