diff --git a/infra/conf/transport_sockopt.go b/infra/conf/transport_sockopt.go index 7001cd4d17b1..454128c20dc3 100644 --- a/infra/conf/transport_sockopt.go +++ b/infra/conf/transport_sockopt.go @@ -10,7 +10,7 @@ import ( ) type CustomSockoptConfig struct { - Syetem string `json:"system"` + System string `json:"system"` Network string `json:"network"` Level string `json:"level"` Opt string `json:"opt"` @@ -124,7 +124,7 @@ func (c *SocketConfig) Build() (*internet.SocketConfig, error) { for _, copt := range c.CustomSockopt { customSockopt := &internet.CustomSockopt{ - System: copt.Syetem, + System: copt.System, Network: copt.Network, Level: copt.Level, Opt: copt.Opt, diff --git a/transport/internet/sockopt_linux.go b/transport/internet/sockopt_linux.go index bf100350e0c7..36c4decfd237 100644 --- a/transport/internet/sockopt_linux.go +++ b/transport/internet/sockopt_linux.go @@ -169,41 +169,42 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) return errors.New("failed to set TCP_MAXSEG", err) } } - if len(config.CustomSockopt) > 0 { - for _, custom := range config.CustomSockopt { - if custom.System != "" && custom.System != runtime.GOOS { - errors.LogDebug(context.Background(), "CustomSockopt system not match: ", "want ", custom.System, " got ", runtime.GOOS) - continue - } - // Skip unwanted network type - // network might be tcp4 or tcp6 - // use HasPrefix so that "tcp" can match tcp4/6 with "tcp" if user want to control all tcp (udp is also the same) - // if it is empty, strings.HasPrefix will always return true to make it apply for all networks - if !strings.HasPrefix(network, custom.Network) { - continue - } - level := 0x6 // default TCP - var opt int - if len(custom.Opt) == 0 { - return errors.New("No opt!") - } else { - opt, _ = strconv.Atoi(custom.Opt) - } - if custom.Level != "" { - level, _ = strconv.Atoi(custom.Level) + } + + if len(config.CustomSockopt) > 0 { + for _, custom := range config.CustomSockopt { + if custom.System != "" && custom.System != runtime.GOOS { + errors.LogDebug(context.Background(), "CustomSockopt system not match: ", "want ", custom.System, " got ", runtime.GOOS) + continue + } + // Skip unwanted network type + // network might be tcp4 or tcp6 + // use HasPrefix so that "tcp" can match tcp4/6 with "tcp" if user want to control all tcp (udp is also the same) + // if it is empty, strings.HasPrefix will always return true to make it apply for all networks + if !strings.HasPrefix(network, custom.Network) { + continue + } + level := 0x6 // default TCP + var opt int + if len(custom.Opt) == 0 { + return errors.New("No opt!") + } else { + opt, _ = strconv.Atoi(custom.Opt) + } + if custom.Level != "" { + level, _ = strconv.Atoi(custom.Level) + } + if custom.Type == "int" { + value, _ := strconv.Atoi(custom.Value) + if err := syscall.SetsockoptInt(int(fd), level, opt, value); err != nil { + return errors.New("failed to set CustomSockoptInt", opt, value, err) } - if custom.Type == "int" { - value, _ := strconv.Atoi(custom.Value) - if err := syscall.SetsockoptInt(int(fd), level, opt, value); err != nil { - return errors.New("failed to set CustomSockoptInt", opt, value, err) - } - } else if custom.Type == "str" { - if err := syscall.SetsockoptString(int(fd), level, opt, custom.Value); err != nil { - return errors.New("failed to set CustomSockoptString", opt, custom.Value, err) - } - } else { - return errors.New("unknown CustomSockopt type:", custom.Type) + } else if custom.Type == "str" { + if err := syscall.SetsockoptString(int(fd), level, opt, custom.Value); err != nil { + return errors.New("failed to set CustomSockoptString", opt, custom.Value, err) } + } else { + return errors.New("unknown CustomSockopt type:", custom.Type) } } }