socket.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package socketcfg
  2. import (
  3. "strings"
  4. "github.com/v2fly/v2ray-core/v5/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. BindToDevice string `json:"bindToDevice"`
  15. RxBufSize uint64 `json:"rxBufSize"`
  16. TxBufSize uint64 `json:"txBufSize"`
  17. ForceBufSize bool `json:"forceBufSize"`
  18. }
  19. // Build implements Buildable.
  20. func (c *SocketConfig) Build() (*internet.SocketConfig, error) {
  21. var tfoSettings internet.SocketConfig_TCPFastOpenState
  22. if c.TFO != nil {
  23. if *c.TFO {
  24. tfoSettings = internet.SocketConfig_Enable
  25. } else {
  26. tfoSettings = internet.SocketConfig_Disable
  27. }
  28. }
  29. tfoQueueLength := c.TFOQueueLength
  30. if tfoQueueLength == 0 {
  31. tfoQueueLength = 4096
  32. }
  33. var tproxy internet.SocketConfig_TProxyMode
  34. switch strings.ToLower(c.TProxy) {
  35. case "tproxy":
  36. tproxy = internet.SocketConfig_TProxy
  37. case "redirect":
  38. tproxy = internet.SocketConfig_Redirect
  39. default:
  40. tproxy = internet.SocketConfig_Off
  41. }
  42. return &internet.SocketConfig{
  43. Mark: c.Mark,
  44. Tfo: tfoSettings,
  45. TfoQueueLength: tfoQueueLength,
  46. Tproxy: tproxy,
  47. AcceptProxyProtocol: c.AcceptProxyProtocol,
  48. TcpKeepAliveInterval: c.TCPKeepAliveInterval,
  49. TcpKeepAliveIdle: c.TCPKeepAliveIdle,
  50. RxBufSize: int64(c.RxBufSize),
  51. TxBufSize: int64(c.TxBufSize),
  52. ForceBufSize: c.ForceBufSize,
  53. BindToDevice: c.BindToDevice,
  54. }, nil
  55. }