From d6ec014ca49ed974824c061f4032d2f6fd54a254 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Tue, 8 Sep 2026 15:01:09 +0800 Subject: [PATCH] Fix omitempty for JSON struct fields Claude-Session: https://claude.ai/code/session_01NxBrtZjwwaJC8uxCNbgmKu --- option/http.go | 10 +++++----- option/openvpn.go | 8 ++++---- protocol/openvpn/client.go | 4 ++-- protocol/openvpn/server.go | 6 +++--- test/openvpn_test.go | 6 +++--- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/option/http.go b/option/http.go index 4c6da9de..c51b2643 100644 --- a/option/http.go +++ b/option/http.go @@ -12,11 +12,11 @@ import ( ) type HTTP2Options struct { - IdleTimeout badoption.Duration `json:"idle_timeout,omitempty"` - KeepAlivePeriod badoption.Duration `json:"keep_alive_period,omitempty"` - StreamReceiveWindow byteformats.MemoryBytes `json:"stream_receive_window,omitempty"` - ConnectionReceiveWindow byteformats.MemoryBytes `json:"connection_receive_window,omitempty"` - MaxConcurrentStreams int `json:"max_concurrent_streams,omitempty"` + IdleTimeout badoption.Duration `json:"idle_timeout,omitempty"` + KeepAlivePeriod badoption.Duration `json:"keep_alive_period,omitempty"` + StreamReceiveWindow *byteformats.MemoryBytes `json:"stream_receive_window,omitempty"` + ConnectionReceiveWindow *byteformats.MemoryBytes `json:"connection_receive_window,omitempty"` + MaxConcurrentStreams int `json:"max_concurrent_streams,omitempty"` } type QUICOptions struct { diff --git a/option/openvpn.go b/option/openvpn.go index 7a12a4f0..d62aaf98 100644 --- a/option/openvpn.go +++ b/option/openvpn.go @@ -25,8 +25,8 @@ type OpenVPNClientEndpointOptions struct { Servers []OpenVPNRemoteOptions `json:"servers,omitempty"` RemoteRandom bool `json:"remote_random,omitempty"` Address badoption.Listable[netip.Prefix] `json:"address,omitempty"` - PeerAddress badoption.Addr `json:"peer_address,omitempty"` - PeerAddressIPv6 badoption.Addr `json:"peer_address_ipv6,omitempty"` + PeerAddress *badoption.Addr `json:"peer_address,omitempty"` + PeerAddressIPv6 *badoption.Addr `json:"peer_address_ipv6,omitempty"` Topology string `json:"topology,omitempty" enum:"net30,p2p,subnet"` Username string `json:"username,omitempty"` Password string `json:"password,omitempty"` @@ -81,8 +81,8 @@ type OpenVPNServerEndpointOptions struct { RemotePort uint16 `json:"remote_port,omitempty"` MaxClients int `json:"max_clients,omitempty"` Address badoption.Listable[netip.Prefix] `json:"address"` - PeerAddress badoption.Addr `json:"peer_address,omitempty"` - PeerAddressIPv6 badoption.Addr `json:"peer_address_ipv6,omitempty"` + PeerAddress *badoption.Addr `json:"peer_address,omitempty"` + PeerAddressIPv6 *badoption.Addr `json:"peer_address_ipv6,omitempty"` Topology string `json:"topology,omitempty" enum:"net30,p2p,subnet"` DuplicateCN bool `json:"duplicate_cn,omitempty"` Users []auth.User `json:"users,omitempty"` diff --git a/protocol/openvpn/client.go b/protocol/openvpn/client.go index 42715ef6..03f8e07a 100644 --- a/protocol/openvpn/client.go +++ b/protocol/openvpn/client.go @@ -405,11 +405,11 @@ func buildClientDataChannelOptions(options option.OpenVPNClientEndpointOptions) } func buildClientTunnelOptions(options option.OpenVPNClientEndpointOptions, requirePeerAddress bool) (ovpn.ClientTunnelOptions, error) { - vpnGateway := netip.Addr(options.PeerAddress) + vpnGateway := options.PeerAddress.Build(netip.Addr{}) if vpnGateway.IsValid() && !vpnGateway.Is4() { return ovpn.ClientTunnelOptions{}, E.New("`peer_address` must be an IPv4 address") } - vpnGatewayIPv6 := netip.Addr(options.PeerAddressIPv6) + vpnGatewayIPv6 := options.PeerAddressIPv6.Build(netip.Addr{}) if vpnGatewayIPv6.IsValid() && !vpnGatewayIPv6.Is6() { return ovpn.ClientTunnelOptions{}, E.New("`peer_address_ipv6` must be an IPv6 address") } diff --git a/protocol/openvpn/server.go b/protocol/openvpn/server.go index 9c4ee269..cd8ccc66 100644 --- a/protocol/openvpn/server.go +++ b/protocol/openvpn/server.go @@ -297,7 +297,7 @@ func buildServerOptions(options option.OpenVPNServerEndpointOptions) (ovpn.Serve if options.TLS == nil { return ovpn.ServerOptions{}, E.New("missing `tls` options") } - if len(options.StaticKey) > 0 || options.StaticKeyPath != "" || options.KeyDirection != "" || options.Cipher != "" || options.Remote != "" || options.RemotePort != 0 || netip.Addr(options.PeerAddress).IsValid() || netip.Addr(options.PeerAddressIPv6).IsValid() { + if len(options.StaticKey) > 0 || options.StaticKeyPath != "" || options.KeyDirection != "" || options.Cipher != "" || options.Remote != "" || options.RemotePort != 0 || options.PeerAddress.Build(netip.Addr{}).IsValid() || options.PeerAddressIPv6.Build(netip.Addr{}).IsValid() { return ovpn.ServerOptions{}, E.New("static-key server options require `mode: static_key`") } tlsOptions, keyDirection, err := buildServerTLSOptions(*options.TLS) @@ -367,11 +367,11 @@ func buildStaticKeyServerOptions(options option.OpenVPNServerEndpointOptions, pr if err != nil { return ovpn.ServerOptions{}, err } - vpnGateway := netip.Addr(options.PeerAddress) + vpnGateway := options.PeerAddress.Build(netip.Addr{}) if vpnGateway.IsValid() && !vpnGateway.Is4() { return ovpn.ServerOptions{}, E.New("`peer_address` must be an IPv4 address") } - vpnGatewayIPv6 := netip.Addr(options.PeerAddressIPv6) + vpnGatewayIPv6 := options.PeerAddressIPv6.Build(netip.Addr{}) if vpnGatewayIPv6.IsValid() && !vpnGatewayIPv6.Is6() { return ovpn.ServerOptions{}, E.New("`peer_address_ipv6` must be an IPv6 address") } diff --git a/test/openvpn_test.go b/test/openvpn_test.go index 80f27e58..950db0cb 100644 --- a/test/openvpn_test.go +++ b/test/openvpn_test.go @@ -197,7 +197,7 @@ func TestOpenVPNStaticKeyClientDataPath(t *testing.T) { Mode: ovpn.ModeStaticKey, Network: N.NetworkTCP, Address: []netip.Prefix{netip.MustParsePrefix(clientTunnelAddress + "/30")}, - PeerAddress: badoption.Addr(netip.MustParseAddr(peerTunnelAddress)), + PeerAddress: common.Ptr(badoption.Addr(netip.MustParseAddr(peerTunnelAddress))), Topology: "p2p", StaticKeyPath: staticKeyPath, KeyDirection: "client", @@ -286,7 +286,7 @@ func runOpenVPNStaticKeySelfToSelf(t *testing.T, protocol string) { RemotePort: clientOpenVPNPort, MaxClients: 1, Address: []netip.Prefix{netip.MustParsePrefix(serverTunnelAddress + "/30")}, - PeerAddress: badoption.Addr(netip.MustParseAddr(clientTunnelAddress)), + PeerAddress: common.Ptr(badoption.Addr(netip.MustParseAddr(clientTunnelAddress))), Topology: "p2p", StaticKeyPath: staticKeyPath, KeyDirection: "server", @@ -302,7 +302,7 @@ func runOpenVPNStaticKeySelfToSelf(t *testing.T, protocol string) { Mode: ovpn.ModeStaticKey, Network: protocol, Address: []netip.Prefix{netip.MustParsePrefix(clientTunnelAddress + "/30")}, - PeerAddress: badoption.Addr(netip.MustParseAddr(serverTunnelAddress)), + PeerAddress: common.Ptr(badoption.Addr(netip.MustParseAddr(serverTunnelAddress))), Topology: "p2p", StaticKeyPath: staticKeyPath, KeyDirection: "client",