refactor: New tun udpnat

This commit is contained in:
世界
2026-08-30 17:41:43 +08:00
parent a072f21978
commit 1f7d571017
25 changed files with 422 additions and 96 deletions
+42
View File
@@ -87,6 +87,48 @@ type ListenOptions struct {
InboundOptions
}
type UDPNATBehavior uint8
const (
UDPNATBehaviorEndpointIndependent UDPNATBehavior = iota
UDPNATBehaviorAddressDependent
UDPNATBehaviorAddressAndPortDependent
)
func (b UDPNATBehavior) MarshalJSON() ([]byte, error) {
var value string
switch b {
case UDPNATBehaviorEndpointIndependent:
value = "endpoint_independent"
case UDPNATBehaviorAddressDependent:
value = "address_dependent"
case UDPNATBehaviorAddressAndPortDependent:
value = "address_and_port_dependent"
default:
return nil, E.New("unknown UDP NAT behavior: ", uint8(b))
}
return json.Marshal(value)
}
func (b *UDPNATBehavior) UnmarshalJSON(data []byte) error {
var value string
err := json.Unmarshal(data, &value)
if err != nil {
return err
}
switch value {
case "", "endpoint_independent":
*b = UDPNATBehaviorEndpointIndependent
case "address_dependent":
*b = UDPNATBehaviorAddressDependent
case "address_and_port_dependent":
*b = UDPNATBehaviorAddressAndPortDependent
default:
return E.New("unknown UDP NAT behavior: ", value)
}
return nil
}
type UDPTimeoutCompat badoption.Duration
func (c UDPTimeoutCompat) MarshalJSON() ([]byte, error) {
+4 -1
View File
@@ -6,5 +6,8 @@ type RedirectInboundOptions struct {
type TProxyInboundOptions struct {
ListenOptions
Network NetworkList `json:"network,omitempty"`
Network NetworkList `json:"network,omitempty"`
UDPMapping UDPNATBehavior `json:"udp_mapping,omitempty"`
UDPFiltering UDPNATBehavior `json:"udp_filtering,omitempty"`
UDPNATMax uint32 `json:"udp_nat_max,omitempty"`
}
+3
View File
@@ -45,6 +45,9 @@ type TunInboundOptions struct {
IncludeMACAddress badoption.Listable[string] `json:"include_mac_address,omitempty"`
ExcludeMACAddress badoption.Listable[string] `json:"exclude_mac_address,omitempty"`
UDPTimeout UDPTimeoutCompat `json:"udp_timeout,omitempty"`
UDPMapping UDPNATBehavior `json:"udp_mapping,omitempty"`
UDPFiltering UDPNATBehavior `json:"udp_filtering,omitempty"`
UDPNATMax uint32 `json:"udp_nat_max,omitempty"`
Stack string `json:"stack,omitempty"`
Platform *TunPlatformOptions `json:"platform,omitempty"`
InboundOptions
+12 -9
View File
@@ -7,15 +7,18 @@ import (
)
type WireGuardEndpointOptions struct {
System bool `json:"system,omitempty"`
Name string `json:"name,omitempty"`
MTU uint32 `json:"mtu,omitempty"`
Address badoption.Listable[netip.Prefix] `json:"address"`
PrivateKey string `json:"private_key"`
ListenPort uint16 `json:"listen_port,omitempty"`
Peers []WireGuardPeer `json:"peers,omitempty"`
UDPTimeout badoption.Duration `json:"udp_timeout,omitempty"`
Workers int `json:"workers,omitempty"`
System bool `json:"system,omitempty"`
Name string `json:"name,omitempty"`
MTU uint32 `json:"mtu,omitempty"`
Address badoption.Listable[netip.Prefix] `json:"address"`
PrivateKey string `json:"private_key"`
ListenPort uint16 `json:"listen_port,omitempty"`
Peers []WireGuardPeer `json:"peers,omitempty"`
UDPTimeout badoption.Duration `json:"udp_timeout,omitempty"`
UDPMapping UDPNATBehavior `json:"udp_mapping,omitempty"`
UDPFiltering UDPNATBehavior `json:"udp_filtering,omitempty"`
UDPNATMax uint32 `json:"udp_nat_max,omitempty"`
Workers int `json:"workers,omitempty"`
DialerOptions
}