Add option for custom wireguard reserved bytes

This commit is contained in:
世界 2022-10-26 20:21:07 +08:00
parent d583b35717
commit 35886b88d7
No known key found for this signature in database
GPG Key ID: CD109927C34A63C4
3 changed files with 22 additions and 2 deletions

View File

@ -9,6 +9,7 @@ type WireGuardOutboundOptions struct {
PrivateKey string `json:"private_key"` PrivateKey string `json:"private_key"`
PeerPublicKey string `json:"peer_public_key"` PeerPublicKey string `json:"peer_public_key"`
PreSharedKey string `json:"pre_shared_key,omitempty"` PreSharedKey string `json:"pre_shared_key,omitempty"`
Reserved []uint8 `json:"reserved,omitempty"`
MTU uint32 `json:"mtu,omitempty"` MTU uint32 `json:"mtu,omitempty"`
Network NetworkList `json:"network,omitempty"` Network NetworkList `json:"network,omitempty"`
} }

View File

@ -45,8 +45,15 @@ func NewWireGuard(ctx context.Context, router adapter.Router, logger log.Context
tag: tag, tag: tag,
}, },
} }
var reserved [3]uint8
if len(options.Reserved) > 0 {
if len(options.Reserved) != 3 {
return nil, E.New("invalid reserved value, required 3 bytes, got ", len(options.Reserved))
}
copy(reserved[:], options.Reserved)
}
peerAddr := options.ServerOptions.Build() peerAddr := options.ServerOptions.Build()
outbound.bind = wireguard.NewClientBind(ctx, dialer.New(router, options.DialerOptions), peerAddr) outbound.bind = wireguard.NewClientBind(ctx, dialer.New(router, options.DialerOptions), peerAddr, reserved)
localPrefixes := common.Map(options.LocalAddress, option.ListenPrefix.Build) localPrefixes := common.Map(options.LocalAddress, option.ListenPrefix.Build)
if len(localPrefixes) == 0 { if len(localPrefixes) == 0 {
return nil, E.New("missing local address") return nil, E.New("missing local address")

View File

@ -18,16 +18,18 @@ type ClientBind struct {
ctx context.Context ctx context.Context
dialer N.Dialer dialer N.Dialer
peerAddr M.Socksaddr peerAddr M.Socksaddr
reserved [3]uint8
connAccess sync.Mutex connAccess sync.Mutex
conn *wireConn conn *wireConn
done chan struct{} done chan struct{}
} }
func NewClientBind(ctx context.Context, dialer N.Dialer, peerAddr M.Socksaddr) *ClientBind { func NewClientBind(ctx context.Context, dialer N.Dialer, peerAddr M.Socksaddr, reserved [3]uint8) *ClientBind {
return &ClientBind{ return &ClientBind{
ctx: ctx, ctx: ctx,
dialer: dialer, dialer: dialer,
peerAddr: peerAddr, peerAddr: peerAddr,
reserved: reserved,
} }
} }
@ -89,6 +91,11 @@ func (c *ClientBind) receive(b []byte) (n int, ep conn.Endpoint, err error) {
} }
return return
} }
if n > 3 {
b[1] = 0
b[2] = 0
b[3] = 0
}
ep = Endpoint(c.peerAddr) ep = Endpoint(c.peerAddr)
return return
} }
@ -119,6 +126,11 @@ func (c *ClientBind) Send(b []byte, ep conn.Endpoint) error {
if err != nil { if err != nil {
return err return err
} }
if len(b) > 3 {
b[1] = c.reserved[0]
b[2] = c.reserved[1]
b[3] = c.reserved[2]
}
_, err = udpConn.Write(b) _, err = udpConn.Write(b)
if err != nil { if err != nil {
udpConn.Close() udpConn.Close()