shadowsocks.go 3.2 KB

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