socket.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. MPTCP *bool `json:"mptcp"`
  19. }
  20. // Build implements Buildable.
  21. func (c *SocketConfig) Build() (*internet.SocketConfig, error) {
  22. var tfoSettings internet.SocketConfig_TCPFastOpenState
  23. if c.TFO != nil {
  24. if *c.TFO {
  25. tfoSettings = internet.SocketConfig_Enable
  26. } else {
  27. tfoSettings = internet.SocketConfig_Disable
  28. }
  29. }
  30. tfoQueueLength := c.TFOQueueLength
  31. if tfoQueueLength == 0 {
  32. tfoQueueLength = 4096
  33. }
  34. var tproxy internet.SocketConfig_TProxyMode
  35. switch strings.ToLower(c.TProxy) {
  36. case "tproxy":
  37. tproxy = internet.SocketConfig_TProxy
  38. case "redirect":
  39. tproxy = internet.SocketConfig_Redirect
  40. default:
  41. tproxy = internet.SocketConfig_Off
  42. }
  43. var mptcpSettings internet.MPTCPState
  44. if c.MPTCP != nil {
  45. if *c.MPTCP {
  46. mptcpSettings = internet.MPTCPState_Enable
  47. } else {
  48. mptcpSettings = internet.MPTCPState_Disable
  49. }
  50. }
  51. return &internet.SocketConfig{
  52. Mark: c.Mark,
  53. Tfo: tfoSettings,
  54. TfoQueueLength: tfoQueueLength,
  55. Tproxy: tproxy,
  56. AcceptProxyProtocol: c.AcceptProxyProtocol,
  57. TcpKeepAliveInterval: c.TCPKeepAliveInterval,
  58. TcpKeepAliveIdle: c.TCPKeepAliveIdle,
  59. RxBufSize: int64(c.RxBufSize),
  60. TxBufSize: int64(c.TxBufSize),
  61. ForceBufSize: c.ForceBufSize,
  62. BindToDevice: c.BindToDevice,
  63. Mptcp: mptcpSettings,
  64. }, nil
  65. }