socket.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package socketcfg
  2. import (
  3. "strings"
  4. "github.com/v2fly/v2ray-core/v4/transport/internet"
  5. )
  6. type SocketConfig struct {
  7. Mark uint32 `json:"mark"`
  8. TFO *bool `json:"tcpFastOpen"`
  9. TProxy string `json:"tproxy"`
  10. AcceptProxyProtocol bool `json:"acceptProxyProtocol"`
  11. TCPKeepAliveInterval int32 `json:"tcpKeepAliveInterval"`
  12. TCPKeepAliveIdle int32 `json:"tcpKeepAliveIdle"`
  13. TFOQueueLength uint32 `json:"tcpFastOpenQueueLength"`
  14. }
  15. // Build implements Buildable.
  16. func (c *SocketConfig) Build() (*internet.SocketConfig, error) {
  17. var tfoSettings internet.SocketConfig_TCPFastOpenState
  18. if c.TFO != nil {
  19. if *c.TFO {
  20. tfoSettings = internet.SocketConfig_Enable
  21. } else {
  22. tfoSettings = internet.SocketConfig_Disable
  23. }
  24. }
  25. tfoQueueLength := c.TFOQueueLength
  26. if tfoQueueLength == 0 {
  27. tfoQueueLength = 4096
  28. }
  29. var tproxy internet.SocketConfig_TProxyMode
  30. switch strings.ToLower(c.TProxy) {
  31. case "tproxy":
  32. tproxy = internet.SocketConfig_TProxy
  33. case "redirect":
  34. tproxy = internet.SocketConfig_Redirect
  35. default:
  36. tproxy = internet.SocketConfig_Off
  37. }
  38. return &internet.SocketConfig{
  39. Mark: c.Mark,
  40. Tfo: tfoSettings,
  41. TfoQueueLength: tfoQueueLength,
  42. Tproxy: tproxy,
  43. AcceptProxyProtocol: c.AcceptProxyProtocol,
  44. TcpKeepAliveInterval: c.TCPKeepAliveInterval,
  45. TcpKeepAliveIdle: c.TCPKeepAliveIdle,
  46. }, nil
  47. }