Skip to content

Commit

Permalink
Add reserved to WireGuard config
Browse files Browse the repository at this point in the history
Fixes #1730
  • Loading branch information
RPRX authored Mar 2, 2023
1 parent 6526e74 commit ccba465
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
6 changes: 6 additions & 0 deletions infra/conf/wireguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type WireGuardConfig struct {
Peers []*WireGuardPeerConfig `json:"peers"`
MTU int `json:"mtu"`
NumWorkers int `json:"workers"`
Reserved []byte `json:"reserved"`
}

func (c *WireGuardConfig) Build() (proto.Message, error) {
Expand Down Expand Up @@ -90,6 +91,11 @@ func (c *WireGuardConfig) Build() (proto.Message, error) {
// we don't need to process fallback manually
config.NumWorkers = int32(c.NumWorkers)

if len(c.Reserved) != 0 && len(c.Reserved) != 3 {
return nil, newError(`"reserved" should be empty or 3 bytes`)
}
config.Reserved = c.Reserved

return config, nil
}

Expand Down
5 changes: 5 additions & 0 deletions proxy/wireguard/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type netBindClient struct {
dialer internet.Dialer
dns dns.Client
dnsOption dns.IPOption
reserved []byte

readQueue chan *netReadInfo
}
Expand Down Expand Up @@ -157,6 +158,10 @@ func (bind *netBindClient) Send(buff []byte, endpoint conn.Endpoint) error {
}
}

if len(buff) > 3 && len(bind.reserved) == 3 {
copy(buff[1:], bind.reserved)
}

_, err = nend.conn.Write(buff)

return err
Expand Down
27 changes: 18 additions & 9 deletions proxy/wireguard/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions proxy/wireguard/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ message DeviceConfig {
repeated PeerConfig peers = 3;
int32 mtu = 4;
int32 num_workers = 5;
bytes reserved = 6;
}
7 changes: 4 additions & 3 deletions proxy/wireguard/wireguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
})
// bind := conn.NewStdNetBind() // TODO: conn.Bind wrapper for dialer
bind := &netBindClient{
dialer: dialer,
workers: int(h.conf.NumWorkers),
dns: h.dns,
dialer: dialer,
workers: int(h.conf.NumWorkers),
dns: h.dns,
reserved: h.conf.Reserved,
}

net, err := h.makeVirtualTun(bind)
Expand Down

0 comments on commit ccba465

Please sign in to comment.