Skip to content

Commit 345c76f

Browse files
bitwiresysclaudeLjhAUMEM
authored
WireGuard inbound: Support dynamic peer management (#6360)
#6360 (comment) Closes #6314 --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com> Co-authored-by: LjhAUMEM <llnu14702@gmail.com>
1 parent f496437 commit 345c76f

14 files changed

Lines changed: 280 additions & 114 deletions

File tree

infra/conf/hysteria.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ func (c *HysteriaClientConfig) Build() (proto.Message, error) {
2222
}
2323

2424
config := &hysteria.ClientConfig{}
25-
config.Version = c.Version
2625
config.Server = &protocol.ServerEndpoint{
2726
Address: c.Address.Build(),
2827
Port: uint32(c.Port),
@@ -44,6 +43,10 @@ type HysteriaServerConfig struct {
4443
}
4544

4645
func (c *HysteriaServerConfig) Build() (proto.Message, error) {
46+
if c.Version != 2 {
47+
return nil, errors.New("version != 2")
48+
}
49+
4750
config := new(hysteria.ServerConfig)
4851

4952
if c.Clients != nil {

infra/conf/transport_internet.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,6 @@ func (c *HysteriaConfig) Build() (proto.Message, error) {
571571
}
572572

573573
config := &hysteria.Config{}
574-
config.Version = c.Version
575574
config.Auth = c.Auth
576575
config.UdpIdleTimeout = c.UdpIdleTimeout
577576
config.MasqType = c.Masquerade.Type

infra/conf/wireguard.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import (
77
"strings"
88

99
"github.com/xtls/xray-core/common/errors"
10+
"github.com/xtls/xray-core/common/protocol"
11+
"github.com/xtls/xray-core/common/serial"
12+
"github.com/xtls/xray-core/common/task"
1013
"github.com/xtls/xray-core/proxy/wireguard"
1114
"google.golang.org/protobuf/proto"
1215
)
@@ -17,9 +20,12 @@ type WireGuardPeerConfig struct {
1720
Endpoint string `json:"endpoint"`
1821
KeepAlive uint32 `json:"keepAlive"`
1922
AllowedIPs []string `json:"allowedIPs,omitempty"`
23+
24+
Level uint32 `json:"level"`
25+
Email string `json:"email"`
2026
}
2127

22-
func (c *WireGuardPeerConfig) Build() (proto.Message, error) {
28+
func (c *WireGuardPeerConfig) Build() (*wireguard.PeerConfig, error) {
2329
var err error
2430
config := new(wireguard.PeerConfig)
2531

@@ -78,14 +84,32 @@ func (c *WireGuardConfig) Build() (proto.Message, error) {
7884
config.Endpoint = c.Address
7985
}
8086

81-
if c.Peers != nil {
87+
if c.IsClient {
8288
config.Peers = make([]*wireguard.PeerConfig, len(c.Peers))
8389
for i, p := range c.Peers {
8490
msg, err := p.Build()
8591
if err != nil {
8692
return nil, err
8793
}
88-
config.Peers[i] = msg.(*wireguard.PeerConfig)
94+
config.Peers[i] = msg
95+
}
96+
} else {
97+
config.Users = make([]*protocol.User, len(c.Peers))
98+
processUser := func(idx int) error {
99+
p := c.Peers[idx]
100+
m, err := p.Build()
101+
if err != nil {
102+
return err
103+
}
104+
config.Users[idx] = &protocol.User{
105+
Email: p.Email,
106+
Level: p.Level,
107+
Account: serial.ToTypedMessage(m),
108+
}
109+
return nil
110+
}
111+
if err := task.ParallelForN(len(c.Peers), processUser); err != nil {
112+
return nil, err
89113
}
90114
}
91115

infra/conf/wireguard_test.go

Lines changed: 0 additions & 50 deletions
This file was deleted.

proxy/hysteria/config.pb.go

Lines changed: 4 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proxy/hysteria/config.proto

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import "common/protocol/server_spec.proto";
1010
import "common/protocol/user.proto";
1111

1212
message ClientConfig {
13-
int32 version = 1;
14-
xray.common.protocol.ServerEndpoint server = 2;
13+
xray.common.protocol.ServerEndpoint server = 1;
1514
}
1615

1716
message ServerConfig {

proxy/wireguard/config.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,60 @@
11
package wireguard
2+
3+
import (
4+
"encoding/hex"
5+
"net/netip"
6+
7+
"github.com/xtls/xray-core/common/protocol"
8+
"google.golang.org/protobuf/proto"
9+
)
10+
11+
func (p *PeerConfig) AsAccount() (protocol.Account, error) {
12+
pub, err := ParseKey(p.PublicKey)
13+
if err != nil {
14+
return nil, err
15+
}
16+
17+
allowedIPs := make([]netip.Prefix, 0, len(p.AllowedIps))
18+
for i := range p.AllowedIps {
19+
p, err := netip.ParsePrefix(p.AllowedIps[i])
20+
if err != nil {
21+
return nil, err
22+
}
23+
allowedIPs = append(allowedIPs, p)
24+
}
25+
26+
return &MemoryAccount{
27+
Pub: *pub,
28+
AllowedIPs: allowedIPs,
29+
PreSharedKey: p.PreSharedKey,
30+
KeepAlive: p.KeepAlive,
31+
}, nil
32+
}
33+
34+
type MemoryAccount struct {
35+
Pub [32]byte
36+
AllowedIPs []netip.Prefix
37+
PreSharedKey string
38+
KeepAlive string
39+
}
40+
41+
func (a *MemoryAccount) Equals(other protocol.Account) bool {
42+
if b, ok := other.(*MemoryAccount); ok {
43+
return a.Pub == b.Pub
44+
}
45+
return false
46+
}
47+
48+
func (a *MemoryAccount) ToProto() proto.Message {
49+
allowedIPs := make([]string, 0, len(a.AllowedIPs))
50+
for i := range a.AllowedIPs {
51+
allowedIPs = append(allowedIPs, a.AllowedIPs[i].String())
52+
}
53+
54+
return &PeerConfig{
55+
PublicKey: hex.EncodeToString(a.Pub[:]),
56+
AllowedIps: allowedIPs,
57+
PreSharedKey: a.PreSharedKey,
58+
KeepAlive: a.KeepAlive,
59+
}
60+
}

proxy/wireguard/config.pb.go

Lines changed: 21 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proxy/wireguard/config.proto

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ option go_package = "github.com/xtls/xray-core/proxy/wireguard";
66
option java_package = "com.xray.proxy.wireguard";
77
option java_multiple_files = true;
88

9+
import "common/protocol/user.proto";
10+
911
message PeerConfig {
1012
string public_key = 1;
1113
string pre_shared_key = 2;
@@ -25,6 +27,7 @@ message DeviceConfig {
2527
string secret_key = 1;
2628
repeated string endpoint = 2;
2729
repeated PeerConfig peers = 3;
30+
repeated xray.common.protocol.User users = 5;
2831
int32 mtu = 4;
2932

3033
bytes reserved = 6;

0 commit comments

Comments
 (0)