shadowtls.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package option
  2. import (
  3. "encoding/json"
  4. E "github.com/sagernet/sing/common/exceptions"
  5. "github.com/sagernet/sing/common/json/badjson"
  6. )
  7. type ShadowTLSInboundOptions struct {
  8. ListenOptions
  9. Version int `json:"version,omitempty"`
  10. Password string `json:"password,omitempty"`
  11. Users []ShadowTLSUser `json:"users,omitempty"`
  12. Handshake ShadowTLSHandshakeOptions `json:"handshake,omitempty"`
  13. HandshakeForServerName *badjson.TypedMap[string, ShadowTLSHandshakeOptions] `json:"handshake_for_server_name,omitempty"`
  14. StrictMode bool `json:"strict_mode,omitempty"`
  15. WildcardSNI WildcardSNI `json:"wildcard_sni,omitempty"`
  16. }
  17. type WildcardSNI int
  18. const (
  19. ShadowTLSWildcardSNIOff WildcardSNI = iota
  20. ShadowTLSWildcardSNIAuthed
  21. ShadowTLSWildcardSNIAll
  22. )
  23. func (w WildcardSNI) MarshalJSON() ([]byte, error) {
  24. return json.Marshal(w.String())
  25. }
  26. func (w WildcardSNI) String() string {
  27. switch w {
  28. case ShadowTLSWildcardSNIOff:
  29. return "off"
  30. case ShadowTLSWildcardSNIAuthed:
  31. return "authed"
  32. case ShadowTLSWildcardSNIAll:
  33. return "all"
  34. default:
  35. panic("unknown wildcard SNI value")
  36. }
  37. }
  38. func (w *WildcardSNI) UnmarshalJSON(bytes []byte) error {
  39. var valueString string
  40. err := json.Unmarshal(bytes, &valueString)
  41. if err != nil {
  42. return err
  43. }
  44. switch valueString {
  45. case "off", "":
  46. *w = ShadowTLSWildcardSNIOff
  47. case "authed":
  48. *w = ShadowTLSWildcardSNIAuthed
  49. case "all":
  50. *w = ShadowTLSWildcardSNIAll
  51. default:
  52. return E.New("unknown wildcard SNI value: ", valueString)
  53. }
  54. return nil
  55. }
  56. type ShadowTLSUser struct {
  57. Name string `json:"name,omitempty"`
  58. Password string `json:"password,omitempty"`
  59. }
  60. type ShadowTLSHandshakeOptions struct {
  61. ServerOptions
  62. DialerOptions
  63. }
  64. type ShadowTLSOutboundOptions struct {
  65. DialerOptions
  66. ServerOptions
  67. Version int `json:"version,omitempty"`
  68. Password string `json:"password,omitempty"`
  69. OutboundTLSOptionsContainer
  70. }