shadowsocks.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package conf
  2. import (
  3. "strings"
  4. "v2ray.com/core/common/protocol"
  5. "v2ray.com/core/common/serial"
  6. "v2ray.com/core/proxy/shadowsocks"
  7. )
  8. type ShadowsocksServerConfig struct {
  9. Cipher string `json:"method"`
  10. Password string `json:"password"`
  11. UDP bool `json:"udp"`
  12. Level byte `json:"level"`
  13. Email string `json:"email"`
  14. OTA *bool `json:"ota"`
  15. }
  16. func (v *ShadowsocksServerConfig) Build() (*serial.TypedMessage, error) {
  17. config := new(shadowsocks.ServerConfig)
  18. config.UdpEnabled = v.UDP
  19. if len(v.Password) == 0 {
  20. return nil, newError("Shadowsocks password is not specified.")
  21. }
  22. account := &shadowsocks.Account{
  23. Password: v.Password,
  24. Ota: shadowsocks.Account_Auto,
  25. }
  26. if v.OTA != nil {
  27. if *v.OTA {
  28. account.Ota = shadowsocks.Account_Enabled
  29. } else {
  30. account.Ota = shadowsocks.Account_Disabled
  31. }
  32. }
  33. cipher := strings.ToLower(v.Cipher)
  34. switch cipher {
  35. case "aes-256-cfb":
  36. account.CipherType = shadowsocks.CipherType_AES_256_CFB
  37. case "aes-128-cfb":
  38. account.CipherType = shadowsocks.CipherType_AES_128_CFB
  39. case "chacha20":
  40. account.CipherType = shadowsocks.CipherType_CHACHA20
  41. case "chacha20-ietf":
  42. account.CipherType = shadowsocks.CipherType_CHACHA20_IETF
  43. default:
  44. return nil, newError("unknown cipher method: " + cipher)
  45. }
  46. config.User = &protocol.User{
  47. Email: v.Email,
  48. Level: uint32(v.Level),
  49. Account: serial.ToTypedMessage(account),
  50. }
  51. return serial.ToTypedMessage(config), nil
  52. }
  53. type ShadowsocksServerTarget struct {
  54. Address *Address `json:"address"`
  55. Port uint16 `json:"port"`
  56. Cipher string `json:"method"`
  57. Password string `json:"password"`
  58. Email string `json:"email"`
  59. Ota bool `json:"ota"`
  60. }
  61. type ShadowsocksClientConfig struct {
  62. Servers []*ShadowsocksServerTarget `json:"servers"`
  63. }
  64. func (v *ShadowsocksClientConfig) Build() (*serial.TypedMessage, error) {
  65. config := new(shadowsocks.ClientConfig)
  66. if len(v.Servers) == 0 {
  67. return nil, newError("0 Shadowsocks server configured.")
  68. }
  69. serverSpecs := make([]*protocol.ServerEndpoint, len(v.Servers))
  70. for idx, server := range v.Servers {
  71. if server.Address == nil {
  72. return nil, newError("Shadowsocks server address is not set.")
  73. }
  74. if server.Port == 0 {
  75. return nil, newError("Invalid Shadowsocks port.")
  76. }
  77. if len(server.Password) == 0 {
  78. return nil, newError("Shadowsocks password is not specified.")
  79. }
  80. account := &shadowsocks.Account{
  81. Password: server.Password,
  82. Ota: shadowsocks.Account_Enabled,
  83. }
  84. if !server.Ota {
  85. account.Ota = shadowsocks.Account_Disabled
  86. }
  87. cipher := strings.ToLower(server.Cipher)
  88. switch cipher {
  89. case "aes-256-cfb":
  90. account.CipherType = shadowsocks.CipherType_AES_256_CFB
  91. case "aes-128-cfb":
  92. account.CipherType = shadowsocks.CipherType_AES_128_CFB
  93. case "chacha20":
  94. account.CipherType = shadowsocks.CipherType_CHACHA20
  95. case "chacha20-ietf":
  96. account.CipherType = shadowsocks.CipherType_CHACHA20_IETF
  97. default:
  98. return nil, newError("unknown cipher method: " + cipher)
  99. }
  100. ss := &protocol.ServerEndpoint{
  101. Address: server.Address.Build(),
  102. Port: uint32(server.Port),
  103. User: []*protocol.User{
  104. {
  105. Email: server.Email,
  106. Account: serial.ToTypedMessage(account),
  107. },
  108. },
  109. }
  110. serverSpecs[idx] = ss
  111. }
  112. config.Server = serverSpecs
  113. return serial.ToTypedMessage(config), nil
  114. }