hysteria2.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package option
  2. import (
  3. "net/url"
  4. C "github.com/sagernet/sing-box/constant"
  5. E "github.com/sagernet/sing/common/exceptions"
  6. "github.com/sagernet/sing/common/json"
  7. "github.com/sagernet/sing/common/json/badjson"
  8. "github.com/sagernet/sing/common/json/badoption"
  9. )
  10. type Hysteria2InboundOptions struct {
  11. ListenOptions
  12. UpMbps int `json:"up_mbps,omitempty"`
  13. DownMbps int `json:"down_mbps,omitempty"`
  14. Obfs *Hysteria2Obfs `json:"obfs,omitempty"`
  15. Users []Hysteria2User `json:"users,omitempty"`
  16. IgnoreClientBandwidth bool `json:"ignore_client_bandwidth,omitempty"`
  17. InboundTLSOptionsContainer
  18. Masquerade *Hysteria2Masquerade `json:"masquerade,omitempty"`
  19. BrutalDebug bool `json:"brutal_debug,omitempty"`
  20. }
  21. type Hysteria2Obfs struct {
  22. Type string `json:"type,omitempty"`
  23. Password string `json:"password,omitempty"`
  24. }
  25. type Hysteria2User struct {
  26. Name string `json:"name,omitempty"`
  27. Password string `json:"password,omitempty"`
  28. }
  29. type _Hysteria2Masquerade struct {
  30. Type string `json:"type,omitempty"`
  31. FileOptions Hysteria2MasqueradeFile `json:"-"`
  32. ProxyOptions Hysteria2MasqueradeProxy `json:"-"`
  33. StringOptions Hysteria2MasqueradeString `json:"-"`
  34. }
  35. type Hysteria2Masquerade _Hysteria2Masquerade
  36. func (m Hysteria2Masquerade) MarshalJSON() ([]byte, error) {
  37. var v any
  38. switch m.Type {
  39. case C.Hysterai2MasqueradeTypeFile:
  40. v = m.FileOptions
  41. case C.Hysterai2MasqueradeTypeProxy:
  42. v = m.ProxyOptions
  43. case C.Hysterai2MasqueradeTypeString:
  44. v = m.StringOptions
  45. default:
  46. return nil, E.New("unknown masquerade type: ", m.Type)
  47. }
  48. return badjson.MarshallObjects((_Hysteria2Masquerade)(m), v)
  49. }
  50. func (m *Hysteria2Masquerade) UnmarshalJSON(bytes []byte) error {
  51. var urlString string
  52. err := json.Unmarshal(bytes, &urlString)
  53. if err == nil {
  54. masqueradeURL, err := url.Parse(urlString)
  55. if err != nil {
  56. return E.Cause(err, "invalid masquerade URL")
  57. }
  58. switch masqueradeURL.Scheme {
  59. case "file":
  60. m.Type = C.Hysterai2MasqueradeTypeFile
  61. m.FileOptions.Directory = masqueradeURL.Path
  62. case "http", "https":
  63. m.Type = C.Hysterai2MasqueradeTypeProxy
  64. m.ProxyOptions.URL = urlString
  65. default:
  66. return E.New("unknown masquerade URL scheme: ", masqueradeURL.Scheme)
  67. }
  68. }
  69. err = json.Unmarshal(bytes, (*_Hysteria2Masquerade)(m))
  70. if err != nil {
  71. return err
  72. }
  73. var v any
  74. switch m.Type {
  75. case C.Hysterai2MasqueradeTypeFile:
  76. v = &m.FileOptions
  77. case C.Hysterai2MasqueradeTypeProxy:
  78. v = &m.ProxyOptions
  79. case C.Hysterai2MasqueradeTypeString:
  80. v = &m.StringOptions
  81. default:
  82. return E.New("unknown masquerade type: ", m.Type)
  83. }
  84. return badjson.UnmarshallExcluded(bytes, (*_Hysteria2Masquerade)(m), v)
  85. }
  86. type Hysteria2MasqueradeFile struct {
  87. Directory string `json:"directory"`
  88. }
  89. type Hysteria2MasqueradeProxy struct {
  90. URL string `json:"url"`
  91. RewriteHost bool `json:"rewrite_host,omitempty"`
  92. }
  93. type Hysteria2MasqueradeString struct {
  94. StatusCode int `json:"status_code,omitempty"`
  95. Headers badoption.HTTPHeader `json:"headers,omitempty"`
  96. Content string `json:"content"`
  97. }
  98. type Hysteria2OutboundOptions struct {
  99. DialerOptions
  100. ServerOptions
  101. UpMbps int `json:"up_mbps,omitempty"`
  102. DownMbps int `json:"down_mbps,omitempty"`
  103. Obfs *Hysteria2Obfs `json:"obfs,omitempty"`
  104. Password string `json:"password,omitempty"`
  105. Network NetworkList `json:"network,omitempty"`
  106. OutboundTLSOptionsContainer
  107. BrutalDebug bool `json:"brutal_debug,omitempty"`
  108. }