--- a/go.mod +++ b/go.mod @@ -29,7 +29,6 @@ golang.org/x/sync v0.22.0 golang.org/x/sys v0.47.0 golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2 - golang.zx2c4.com/wireguard v0.0.0-20250521234502-f333402bd9cb golang.zx2c4.com/wireguard/windows v1.0.1 google.golang.org/grpc v1.82.1 google.golang.org/protobuf v1.36.11 @@ -39,9 +38,10 @@ ) require ( + github.com/amnezia-vpn/amneziawg-go/v3 v3.1.20261116 github.com/andybalholm/brotli v1.0.6 // indirect github.com/davecgh/go-spew v1.1.1 // indirect - github.com/google/btree v1.1.2 // indirect + github.com/google/btree v1.1.3 // indirect github.com/juju/ratelimit v1.0.2 // indirect github.com/klauspost/compress v1.17.4 // indirect github.com/kr/text v0.2.0 // indirect --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/amnezia-vpn/amneziawg-go/v3 v3.1.20261116 h1:/LZYudfChLgd1qmfKR9gaNg1z4xBW9hyS+Ij0MJNVzo= +github.com/amnezia-vpn/amneziawg-go/v3 v3.1.20261116/go.mod h1:YoPc6qcOZqD7TXZ1xpedD8Sx3aSKsxN05ZqEFmXDNHk= github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sxfOI= github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/apernet/quic-go v0.59.1-0.20260425001925-6c6cc9bcb716 h1:J1O+xpLuJWkdYbw5JPGwBqIHs2J8tiEP7Py9lPqkN2I= @@ -21,6 +23,8 @@ github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= github.com/google/btree v1.1.2 h1:xf4v41cLI2Z6FxbKm+8Bu+m8ifhj15JuZ9sa0jZCMUU= github.com/google/btree v1.1.2/go.mod h1:qOPhT0dTNdNzV6Z/lhRX0YXUafgPLFUh+gZMl761Gm4= +github.com/google/btree v1.1.3 h1:CVpQJjYgC4VbzxeGVHfvZrv1ctoYCAI8vbl07Fcxlyg= +github.com/google/btree v1.1.3/go.mod h1:qOPhT0dTNdNzV6Z/lhRX0YXUafgPLFUh+gZMl761Gm4= github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= --- a/infra/conf/wireguard.go +++ b/infra/conf/wireguard.go @@ -57,6 +57,7 @@ } type WireGuardConfig struct { + WireGuardAmneziaWGConfig IsClient bool `json:""` NoKernelTun bool `json:"noKernelTun"` @@ -142,6 +143,18 @@ config.IsClient = c.IsClient config.NoKernelTun = c.NoKernelTun + config.Amnezia, err = c.WireGuardAmneziaWGConfig.Build() + if err != nil { + return nil, err + } + if config.Amnezia != nil { + for _, b := range c.Reserved { + if b != 0 { + return nil, errors.New("AWG does not support nonzero reserved bytes") + } + } + } + return config, nil } --- /dev/null +++ b/infra/conf/wireguard_awg.go @@ -0,0 +1,90 @@ +package conf + +import ( + "encoding/json" + "fmt" + "strconv" + + "github.com/xtls/xray-core/proxy/wireguard" +) + +// WireGuardAWGRange accepts numeric headers from the uploaded JSON and the +// string/range notation used by AWG 3.1 timers and headers. +type WireGuardAWGRange string + +func (r *WireGuardAWGRange) UnmarshalJSON(data []byte) error { + var text string + if err := json.Unmarshal(data, &text); err == nil { + *r = WireGuardAWGRange(text) + return nil + } + var value uint32 + if err := json.Unmarshal(data, &value); err != nil { + return fmt.Errorf("AWG range must be an unsigned integer or a string") + } + *r = WireGuardAWGRange(strconv.FormatUint(uint64(value), 10)) + return nil +} + +// Embedded in WireGuardConfig so the public JSON fields remain flat. +type WireGuardAmneziaWGConfig struct { + Jc *uint32 `json:"jc"` + Jmin *uint32 `json:"jmin"` + Jmax *uint32 `json:"jmax"` + S1 *uint32 `json:"s1"` + S2 *uint32 `json:"s2"` + S3 *uint32 `json:"s3"` + S4 *uint32 `json:"s4"` + H1 WireGuardAWGRange `json:"h1"` + H2 WireGuardAWGRange `json:"h2"` + H3 WireGuardAWGRange `json:"h3"` + H4 WireGuardAWGRange `json:"h4"` + ContentPaddingAddition WireGuardAWGRange `json:"contentPaddingAddition"` + RekeyAfterTime WireGuardAWGRange `json:"rekeyAfterTime"` + RekeyTimeout WireGuardAWGRange `json:"rekeyTimeout"` + RejectAfterTime WireGuardAWGRange `json:"rejectAfterTime"` + KeepaliveTimeout WireGuardAWGRange `json:"keepaliveTimeout"` + MaxHandshakeAttempts WireGuardAWGRange `json:"maxHandshakeAttempts"` + I1 string `json:"i1"` + I2 string `json:"i2"` + I3 string `json:"i3"` + I4 string `json:"i4"` + I5 string `json:"i5"` + HeaderProtectionKey string `json:"headerProtectionKey"` +} + +func (c *WireGuardAmneziaWGConfig) Build() (*wireguard.AmneziaWGConfig, error) { + a := &wireguard.AmneziaWGConfig{ + Jc: c.Jc, + Jmin: c.Jmin, + Jmax: c.Jmax, + S1: c.S1, + S2: c.S2, + S3: c.S3, + S4: c.S4, + H1: string(c.H1), + H2: string(c.H2), + H3: string(c.H3), + H4: string(c.H4), + ContentPaddingAddition: string(c.ContentPaddingAddition), + RekeyAfterTime: string(c.RekeyAfterTime), + RekeyTimeout: string(c.RekeyTimeout), + RejectAfterTime: string(c.RejectAfterTime), + KeepaliveTimeout: string(c.KeepaliveTimeout), + MaxHandshakeAttempts: string(c.MaxHandshakeAttempts), + I1: c.I1, + I2: c.I2, + I3: c.I3, + I4: c.I4, + I5: c.I5, + HeaderProtectionKey: c.HeaderProtectionKey, + } + ipc, err := a.IPC() + if err != nil { + return nil, err + } + if ipc == "" { + return nil, nil + } + return a, nil +} --- /dev/null +++ b/proxy/wireguard/awg.go @@ -0,0 +1,81 @@ +package wireguard + +import ( + "encoding/base64" + "encoding/hex" + "fmt" + "strconv" + "strings" + + "github.com/amnezia-vpn/amneziawg-go/v3/device" +) + +// IPC emits device-wide options before the first public_key line. An omitted +// AWG configuration emits nothing, leaving amneziawg-go's WireGuard defaults. +func (a *AmneziaWGConfig) IPC() (string, error) { + if a == nil { + return "", nil + } + var out strings.Builder + for _, field := range []struct { + name string + value *uint32 + }{ + {"jc", a.Jc}, {"jmin", a.Jmin}, {"jmax", a.Jmax}, + {"s1", a.S1}, {"s2", a.S2}, {"s3", a.S3}, {"s4", a.S4}, + } { + if field.value != nil { + if strings.HasPrefix(field.name, "s") && *field.value > 65535 { + return "", fmt.Errorf("invalid AWG %s", field.name) + } + out.WriteString(field.name + "=" + strconv.FormatUint(uint64(*field.value), 10) + "\n") + } + } + for _, field := range []struct { + name string + value string + }{ + {"h1", a.H1}, {"h2", a.H2}, {"h3", a.H3}, {"h4", a.H4}, + {"content_padding_addition", a.ContentPaddingAddition}, + {"rekey_after_time", a.RekeyAfterTime}, {"rekey_timeout", a.RekeyTimeout}, + {"reject_after_time", a.RejectAfterTime}, {"keepalive_timeout", a.KeepaliveTimeout}, + {"max_handshake_attempts", a.MaxHandshakeAttempts}, + } { + if field.value != "" { + var r device.UintRange + if strings.ContainsAny(field.value, "\r\n") || r.FromString(field.value) != nil { + return "", fmt.Errorf("invalid AWG %s", field.name) + } + out.WriteString(field.name + "=" + field.value + "\n") + } + } + for i, value := range []string{a.I1, a.I2, a.I3, a.I4, a.I5} { + if value == "" { + continue + } + if strings.ContainsAny(value, "\r\n") { + return "", fmt.Errorf("invalid AWG i%d", i+1) + } + // Owner material contains both the 3.1 template notation and older + // base64-encoded packet bytes. Pass bytes as a literal template block. + if !strings.HasPrefix(value, "<") { + packet, err := base64.RawStdEncoding.DecodeString(strings.TrimRight(value, "=")) + if err != nil { + return "", fmt.Errorf("invalid AWG i%d", i+1) + } + value = "" + } + fmt.Fprintf(&out, "i%d=%s\n", i+1, value) + } + if a.HeaderProtectionKey != "" { + key, err := hex.DecodeString(a.HeaderProtectionKey) + if err != nil || len(key) != 32 { + key, err = base64.RawStdEncoding.DecodeString(strings.TrimRight(a.HeaderProtectionKey, "=")) + } + if err != nil || len(key) != 32 { + return "", fmt.Errorf("invalid AWG headerProtectionKey") + } + out.WriteString("header_protection_key=" + hex.EncodeToString(key) + "\n") + } + return out.String(), nil +} --- a/proxy/wireguard/bind.go +++ b/proxy/wireguard/bind.go @@ -10,9 +10,9 @@ "sync" "syscall" + "github.com/amnezia-vpn/amneziawg-go/v3/conn" "github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common/errors" - "golang.zx2c4.com/wireguard/conn" ) type bind struct { @@ -20,6 +20,7 @@ listenFunc func() (net.PacketConn, error) downFunc func() error reserved []byte + amnezia bool // AWG headers and padding must reach the device unchanged. net.PacketConn closeCh chan struct{} @@ -63,7 +64,7 @@ errors.LogErrorInner(context.Background(), err, "bind recv err") continue } - if n > 3 { + if !b.amnezia && n > 3 { bufs[0][1] = 0 bufs[0][2] = 0 bufs[0][3] = 0 @@ -101,7 +102,7 @@ } for i := range bufs { - if len(bufs[i]) > 3 && len(b.reserved) == 3 { + if !b.amnezia && len(bufs[i]) > 3 && len(b.reserved) == 3 { bufs[i][1] = b.reserved[0] bufs[i][2] = b.reserved[1] bufs[i][3] = b.reserved[2] --- a/proxy/wireguard/client.go +++ b/proxy/wireguard/client.go @@ -9,8 +9,9 @@ "strings" "sync" - "golang.zx2c4.com/wireguard/tun" - + "github.com/amnezia-vpn/amneziawg-go/v3/tun" + + "github.com/amnezia-vpn/amneziawg-go/v3/device" "github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/common/dice" @@ -27,7 +28,6 @@ "github.com/xtls/xray-core/features/stats" "github.com/xtls/xray-core/transport" "github.com/xtls/xray-core/transport/internet" - "golang.zx2c4.com/wireguard/device" ) type Handler struct { @@ -309,6 +309,11 @@ }) }, } + awgIPC, err := h.conf.Amnezia.IPC() + if err != nil { + return err + } + bind.amnezia = awgIPC != "" dev := device.NewDevice(h.tun, bind, logger) bind.resolveFunc = resolveFunc bind.listenFunc = listenFunc @@ -316,6 +321,7 @@ bind.reserved = h.conf.Reserved var cfg strings.Builder cfg.WriteString("private_key=" + h.conf.SecretKey + "\n") + cfg.WriteString(awgIPC) for _, peer := range h.conf.Peers { cfg.WriteString("public_key=" + peer.PublicKey + "\n") if peer.PreSharedKey != "" { @@ -329,12 +335,16 @@ cfg.WriteString("persistent_keepalive_interval=" + peer.KeepAlive + "\n") } } - err := dev.IpcSet(cfg.String()) - if err != nil { + err = dev.IpcSet(cfg.String()) + if err != nil { + dev.Close() + h.tun = nil return err } err = dev.Up() if err != nil { + dev.Close() + h.tun = nil return err } h.dev = dev --- a/proxy/wireguard/config.pb.go +++ b/proxy/wireguard/config.pb.go @@ -164,6 +164,7 @@ DomainStrategy DeviceConfig_DomainStrategy `protobuf:"varint,7,opt,name=domain_strategy,json=domainStrategy,proto3,enum=xray.proxy.wireguard.DeviceConfig_DomainStrategy" json:"domain_strategy,omitempty"` IsClient bool `protobuf:"varint,8,opt,name=is_client,json=isClient,proto3" json:"is_client,omitempty"` NoKernelTun bool `protobuf:"varint,9,opt,name=no_kernel_tun,json=noKernelTun,proto3" json:"no_kernel_tun,omitempty"` + Amnezia *AmneziaWGConfig `protobuf:"bytes,10,opt,name=amnezia,proto3" json:"amnezia,omitempty"` unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache } @@ -259,6 +260,234 @@ return x.NoKernelTun } return false +} + +func (x *DeviceConfig) GetAmnezia() *AmneziaWGConfig { + if x != nil { + return x.Amnezia + } + return nil +} + +// Optional fields retain the distinction between omitted and explicitly zero. +type AmneziaWGConfig struct { + state protoimpl.MessageState `protogen:"open.v1"` + Jc *uint32 `protobuf:"varint,1,opt,name=jc,proto3,oneof" json:"jc,omitempty"` + Jmin *uint32 `protobuf:"varint,2,opt,name=jmin,proto3,oneof" json:"jmin,omitempty"` + Jmax *uint32 `protobuf:"varint,3,opt,name=jmax,proto3,oneof" json:"jmax,omitempty"` + S1 *uint32 `protobuf:"varint,4,opt,name=s1,proto3,oneof" json:"s1,omitempty"` + S2 *uint32 `protobuf:"varint,5,opt,name=s2,proto3,oneof" json:"s2,omitempty"` + S3 *uint32 `protobuf:"varint,6,opt,name=s3,proto3,oneof" json:"s3,omitempty"` + S4 *uint32 `protobuf:"varint,7,opt,name=s4,proto3,oneof" json:"s4,omitempty"` + H1 string `protobuf:"bytes,8,opt,name=h1,proto3" json:"h1,omitempty"` + H2 string `protobuf:"bytes,9,opt,name=h2,proto3" json:"h2,omitempty"` + H3 string `protobuf:"bytes,10,opt,name=h3,proto3" json:"h3,omitempty"` + H4 string `protobuf:"bytes,11,opt,name=h4,proto3" json:"h4,omitempty"` + I1 string `protobuf:"bytes,12,opt,name=i1,proto3" json:"i1,omitempty"` + I2 string `protobuf:"bytes,13,opt,name=i2,proto3" json:"i2,omitempty"` + I3 string `protobuf:"bytes,14,opt,name=i3,proto3" json:"i3,omitempty"` + I4 string `protobuf:"bytes,15,opt,name=i4,proto3" json:"i4,omitempty"` + I5 string `protobuf:"bytes,16,opt,name=i5,proto3" json:"i5,omitempty"` + HeaderProtectionKey string `protobuf:"bytes,17,opt,name=header_protection_key,json=headerProtectionKey,proto3" json:"header_protection_key,omitempty"` + ContentPaddingAddition string `protobuf:"bytes,18,opt,name=content_padding_addition,json=contentPaddingAddition,proto3" json:"content_padding_addition,omitempty"` + RekeyAfterTime string `protobuf:"bytes,19,opt,name=rekey_after_time,json=rekeyAfterTime,proto3" json:"rekey_after_time,omitempty"` + RekeyTimeout string `protobuf:"bytes,20,opt,name=rekey_timeout,json=rekeyTimeout,proto3" json:"rekey_timeout,omitempty"` + RejectAfterTime string `protobuf:"bytes,21,opt,name=reject_after_time,json=rejectAfterTime,proto3" json:"reject_after_time,omitempty"` + KeepaliveTimeout string `protobuf:"bytes,22,opt,name=keepalive_timeout,json=keepaliveTimeout,proto3" json:"keepalive_timeout,omitempty"` + MaxHandshakeAttempts string `protobuf:"bytes,23,opt,name=max_handshake_attempts,json=maxHandshakeAttempts,proto3" json:"max_handshake_attempts,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *AmneziaWGConfig) Reset() { + *x = AmneziaWGConfig{} + mi := &file_proxy_wireguard_config_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *AmneziaWGConfig) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*AmneziaWGConfig) ProtoMessage() {} + +func (x *AmneziaWGConfig) ProtoReflect() protoreflect.Message { + mi := &file_proxy_wireguard_config_proto_msgTypes[2] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use AmneziaWGConfig.ProtoReflect.Descriptor instead. +func (*AmneziaWGConfig) Descriptor() ([]byte, []int) { + return file_proxy_wireguard_config_proto_rawDescGZIP(), []int{2} +} + +func (x *AmneziaWGConfig) GetJc() uint32 { + if x != nil && x.Jc != nil { + return *x.Jc + } + return 0 +} + +func (x *AmneziaWGConfig) GetJmin() uint32 { + if x != nil && x.Jmin != nil { + return *x.Jmin + } + return 0 +} + +func (x *AmneziaWGConfig) GetJmax() uint32 { + if x != nil && x.Jmax != nil { + return *x.Jmax + } + return 0 +} + +func (x *AmneziaWGConfig) GetS1() uint32 { + if x != nil && x.S1 != nil { + return *x.S1 + } + return 0 +} + +func (x *AmneziaWGConfig) GetS2() uint32 { + if x != nil && x.S2 != nil { + return *x.S2 + } + return 0 +} + +func (x *AmneziaWGConfig) GetS3() uint32 { + if x != nil && x.S3 != nil { + return *x.S3 + } + return 0 +} + +func (x *AmneziaWGConfig) GetS4() uint32 { + if x != nil && x.S4 != nil { + return *x.S4 + } + return 0 +} + +func (x *AmneziaWGConfig) GetH1() string { + if x != nil { + return x.H1 + } + return "" +} + +func (x *AmneziaWGConfig) GetH2() string { + if x != nil { + return x.H2 + } + return "" +} + +func (x *AmneziaWGConfig) GetH3() string { + if x != nil { + return x.H3 + } + return "" +} + +func (x *AmneziaWGConfig) GetH4() string { + if x != nil { + return x.H4 + } + return "" +} + +func (x *AmneziaWGConfig) GetI1() string { + if x != nil { + return x.I1 + } + return "" +} + +func (x *AmneziaWGConfig) GetI2() string { + if x != nil { + return x.I2 + } + return "" +} + +func (x *AmneziaWGConfig) GetI3() string { + if x != nil { + return x.I3 + } + return "" +} + +func (x *AmneziaWGConfig) GetI4() string { + if x != nil { + return x.I4 + } + return "" +} + +func (x *AmneziaWGConfig) GetI5() string { + if x != nil { + return x.I5 + } + return "" +} + +func (x *AmneziaWGConfig) GetHeaderProtectionKey() string { + if x != nil { + return x.HeaderProtectionKey + } + return "" +} + +func (x *AmneziaWGConfig) GetContentPaddingAddition() string { + if x != nil { + return x.ContentPaddingAddition + } + return "" +} + +func (x *AmneziaWGConfig) GetRekeyAfterTime() string { + if x != nil { + return x.RekeyAfterTime + } + return "" +} + +func (x *AmneziaWGConfig) GetRekeyTimeout() string { + if x != nil { + return x.RekeyTimeout + } + return "" +} + +func (x *AmneziaWGConfig) GetRejectAfterTime() string { + if x != nil { + return x.RejectAfterTime + } + return "" +} + +func (x *AmneziaWGConfig) GetKeepaliveTimeout() string { + if x != nil { + return x.KeepaliveTimeout + } + return "" +} + +func (x *AmneziaWGConfig) GetMaxHandshakeAttempts() string { + if x != nil { + return x.MaxHandshakeAttempts + } + return "" } var File_proxy_wireguard_config_proto protoreflect.FileDescriptor @@ -275,7 +504,7 @@ "\n" + "keep_alive\x18\x04 \x01(\tR\tkeepAlive\x12\x1f\n" + "\vallowed_ips\x18\x05 \x03(\tR\n" + - "allowedIps\"\xdc\x03\n" + + "allowedIps\"\x9d\x04\n" + "\fDeviceConfig\x12\x1d\n" + "\n" + "secret_key\x18\x01 \x01(\tR\tsecretKey\x12\x1a\n" + @@ -286,7 +515,9 @@ "\breserved\x18\x06 \x01(\fR\breserved\x12Z\n" + "\x0fdomain_strategy\x18\a \x01(\x0e21.xray.proxy.wireguard.DeviceConfig.DomainStrategyR\x0edomainStrategy\x12\x1b\n" + "\tis_client\x18\b \x01(\bR\bisClient\x12\"\n" + - "\rno_kernel_tun\x18\t \x01(\bR\vnoKernelTun\"\\\n" + + "\rno_kernel_tun\x18\t \x01(\bR\vnoKernelTun\x12?\n" + + "\aamnezia\x18\n" + + " \x01(\v2%.xray.proxy.wireguard.AmneziaWGConfigR\aamnezia\"\\\n" + "\x0eDomainStrategy\x12\f\n" + "\bFORCE_IP\x10\x00\x12\r\n" + "\tFORCE_IP4\x10\x01\x12\r\n" + @@ -294,7 +525,39 @@ "\n" + "FORCE_IP46\x10\x03\x12\x0e\n" + "\n" + - "FORCE_IP64\x10\x04B^\n" + + "FORCE_IP64\x10\x04\"\xbd\x05\n" + + "\x0fAmneziaWGConfig\x12\x13\n" + + "\x02jc\x18\x01 \x01(\rH\x00R\x02jc\x88\x01\x01\x12\x17\n" + + "\x04jmin\x18\x02 \x01(\rH\x01R\x04jmin\x88\x01\x01\x12\x17\n" + + "\x04jmax\x18\x03 \x01(\rH\x02R\x04jmax\x88\x01\x01\x12\x13\n" + + "\x02s1\x18\x04 \x01(\rH\x03R\x02s1\x88\x01\x01\x12\x13\n" + + "\x02s2\x18\x05 \x01(\rH\x04R\x02s2\x88\x01\x01\x12\x13\n" + + "\x02s3\x18\x06 \x01(\rH\x05R\x02s3\x88\x01\x01\x12\x13\n" + + "\x02s4\x18\a \x01(\rH\x06R\x02s4\x88\x01\x01\x12\x0e\n" + + "\x02h1\x18\b \x01(\tR\x02h1\x12\x0e\n" + + "\x02h2\x18\t \x01(\tR\x02h2\x12\x0e\n" + + "\x02h3\x18\n" + + " \x01(\tR\x02h3\x12\x0e\n" + + "\x02h4\x18\v \x01(\tR\x02h4\x12\x0e\n" + + "\x02i1\x18\f \x01(\tR\x02i1\x12\x0e\n" + + "\x02i2\x18\r \x01(\tR\x02i2\x12\x0e\n" + + "\x02i3\x18\x0e \x01(\tR\x02i3\x12\x0e\n" + + "\x02i4\x18\x0f \x01(\tR\x02i4\x12\x0e\n" + + "\x02i5\x18\x10 \x01(\tR\x02i5\x122\n" + + "\x15header_protection_key\x18\x11 \x01(\tR\x13headerProtectionKey\x128\n" + + "\x18content_padding_addition\x18\x12 \x01(\tR\x16contentPaddingAddition\x12(\n" + + "\x10rekey_after_time\x18\x13 \x01(\tR\x0erekeyAfterTime\x12#\n" + + "\rrekey_timeout\x18\x14 \x01(\tR\frekeyTimeout\x12*\n" + + "\x11reject_after_time\x18\x15 \x01(\tR\x0frejectAfterTime\x12+\n" + + "\x11keepalive_timeout\x18\x16 \x01(\tR\x10keepaliveTimeout\x124\n" + + "\x16max_handshake_attempts\x18\x17 \x01(\tR\x14maxHandshakeAttemptsB\x05\n" + + "\x03_jcB\a\n" + + "\x05_jminB\a\n" + + "\x05_jmaxB\x05\n" + + "\x03_s1B\x05\n" + + "\x03_s2B\x05\n" + + "\x03_s3B\x05\n" + + "\x03_s4B^\n" + "\x18com.xray.proxy.wireguardP\x01Z)github.com/xtls/xray-core/proxy/wireguard\xaa\x02\x14Xray.Proxy.WireGuardb\x06proto3" var ( @@ -310,22 +573,24 @@ } var file_proxy_wireguard_config_proto_enumTypes = make([]protoimpl.EnumInfo, 1) -var file_proxy_wireguard_config_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_proxy_wireguard_config_proto_msgTypes = make([]protoimpl.MessageInfo, 3) var file_proxy_wireguard_config_proto_goTypes = []any{ (DeviceConfig_DomainStrategy)(0), // 0: xray.proxy.wireguard.DeviceConfig.DomainStrategy (*PeerConfig)(nil), // 1: xray.proxy.wireguard.PeerConfig (*DeviceConfig)(nil), // 2: xray.proxy.wireguard.DeviceConfig - (*protocol.User)(nil), // 3: xray.common.protocol.User + (*AmneziaWGConfig)(nil), // 3: xray.proxy.wireguard.AmneziaWGConfig + (*protocol.User)(nil), // 4: xray.common.protocol.User } var file_proxy_wireguard_config_proto_depIdxs = []int32{ 1, // 0: xray.proxy.wireguard.DeviceConfig.peers:type_name -> xray.proxy.wireguard.PeerConfig - 3, // 1: xray.proxy.wireguard.DeviceConfig.users:type_name -> xray.common.protocol.User + 4, // 1: xray.proxy.wireguard.DeviceConfig.users:type_name -> xray.common.protocol.User 0, // 2: xray.proxy.wireguard.DeviceConfig.domain_strategy:type_name -> xray.proxy.wireguard.DeviceConfig.DomainStrategy - 3, // [3:3] is the sub-list for method output_type - 3, // [3:3] is the sub-list for method input_type - 3, // [3:3] is the sub-list for extension type_name - 3, // [3:3] is the sub-list for extension extendee - 0, // [0:3] is the sub-list for field type_name + 3, // 3: xray.proxy.wireguard.DeviceConfig.amnezia:type_name -> xray.proxy.wireguard.AmneziaWGConfig + 4, // [4:4] is the sub-list for method output_type + 4, // [4:4] is the sub-list for method input_type + 4, // [4:4] is the sub-list for extension type_name + 4, // [4:4] is the sub-list for extension extendee + 0, // [0:4] is the sub-list for field type_name } func init() { file_proxy_wireguard_config_proto_init() } @@ -333,13 +598,14 @@ if File_proxy_wireguard_config_proto != nil { return } + file_proxy_wireguard_config_proto_msgTypes[2].OneofWrappers = []any{} type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: unsafe.Slice(unsafe.StringData(file_proxy_wireguard_config_proto_rawDesc), len(file_proxy_wireguard_config_proto_rawDesc)), NumEnums: 1, - NumMessages: 2, + NumMessages: 3, NumExtensions: 0, NumServices: 0, }, --- a/proxy/wireguard/config.proto +++ b/proxy/wireguard/config.proto @@ -34,4 +34,31 @@ DomainStrategy domain_strategy = 7; bool is_client = 8; bool no_kernel_tun = 9; -} \ No newline at end of file + AmneziaWGConfig amnezia = 10; +} +// Optional fields retain the distinction between omitted and explicitly zero. +message AmneziaWGConfig { + optional uint32 jc = 1; + optional uint32 jmin = 2; + optional uint32 jmax = 3; + optional uint32 s1 = 4; + optional uint32 s2 = 5; + optional uint32 s3 = 6; + optional uint32 s4 = 7; + string h1 = 8; + string h2 = 9; + string h3 = 10; + string h4 = 11; + string i1 = 12; + string i2 = 13; + string i3 = 14; + string i4 = 15; + string i5 = 16; + string header_protection_key = 17; + string content_padding_addition = 18; + string rekey_after_time = 19; + string rekey_timeout = 20; + string reject_after_time = 21; + string keepalive_timeout = 22; + string max_handshake_attempts = 23; +} --- a/proxy/wireguard/netstack.go +++ b/proxy/wireguard/netstack.go @@ -19,8 +19,8 @@ "syscall" "time" + "github.com/amnezia-vpn/amneziawg-go/v3/tun" "github.com/xtls/xray-core/transport/internet" - "golang.zx2c4.com/wireguard/tun" "golang.org/x/net/dns/dnsmessage" "gvisor.dev/gvisor/pkg/buffer" --- a/proxy/wireguard/server.go +++ b/proxy/wireguard/server.go @@ -9,6 +9,8 @@ "strings" "sync" + "github.com/amnezia-vpn/amneziawg-go/v3/device" + "github.com/amnezia-vpn/amneziawg-go/v3/tun" "github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common/buf" c "github.com/xtls/xray-core/common/ctx" @@ -25,8 +27,6 @@ "github.com/xtls/xray-core/transport/internet" "github.com/xtls/xray-core/transport/internet/stat" "golang.org/x/crypto/curve25519" - "golang.zx2c4.com/wireguard/device" - "golang.zx2c4.com/wireguard/tun" "gvisor.dev/gvisor/pkg/tcpip/stack" ) @@ -296,9 +296,15 @@ }) }, } + awgIPC, err := s.conf.Amnezia.IPC() + if err != nil { + return err + } + bind.amnezia = awgIPC != "" dev := device.NewDevice(s.tun, bind, logger) var cfg strings.Builder cfg.WriteString("private_key=" + s.conf.SecretKey + "\n") + cfg.WriteString(awgIPC) s.users.Range(func(key, value any) bool { peer := value.(*protocol.MemoryUser).Account.(*MemoryAccount) cfg.WriteString("public_key=" + hex.EncodeToString(peer.Pub[:]) + "\n") @@ -313,12 +319,16 @@ } return true }) - err := dev.IpcSet(cfg.String()) - if err != nil { + err = dev.IpcSet(cfg.String()) + if err != nil { + dev.Close() + s.tun = nil return err } err = dev.Up() if err != nil { + dev.Close() + s.tun = nil return err } s.dev = dev --- a/proxy/wireguard/tun_default.go +++ b/proxy/wireguard/tun_default.go @@ -6,7 +6,7 @@ "errors" "net/netip" - "golang.zx2c4.com/wireguard/tun" + "github.com/amnezia-vpn/amneziawg-go/v3/tun" ) func createKernelTun([]netip.Addr, []netip.Addr, int) (tdev tun.Device, tnet *Net, err error) { --- a/proxy/wireguard/tun_linux.go +++ b/proxy/wireguard/tun_linux.go @@ -14,10 +14,10 @@ "golang.org/x/sys/unix" + "github.com/amnezia-vpn/amneziawg-go/v3/tun" "github.com/vishvananda/netlink" "github.com/xtls/xray-core/common/errors" "github.com/xtls/xray-core/transport/internet" - "golang.zx2c4.com/wireguard/tun" ) var (