shadowsocks.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. }
  25. cipher := strings.ToLower(this.Cipher)
  26. switch cipher {
  27. case "aes-256-cfb":
  28. account.CipherType = shadowsocks.CipherType_AES_256_CFB
  29. case "aes-128-cfb":
  30. account.CipherType = shadowsocks.CipherType_AES_128_CFB
  31. case "chacha20":
  32. account.CipherType = shadowsocks.CipherType_CHACHA20
  33. case "chacha20-ietf":
  34. account.CipherType = shadowsocks.CipherType_CHACHA20_IEFT
  35. default:
  36. return nil, errors.New("Unknown cipher method: " + cipher)
  37. }
  38. config.User = &protocol.User{
  39. Email: this.Email,
  40. Level: uint32(this.Level),
  41. Account: loader.NewTypedSettings(account),
  42. }
  43. return loader.NewTypedSettings(config), nil
  44. }
  45. type ShadowsocksServerTarget struct {
  46. Address *Address `json:"address"`
  47. Port uint16 `json:"port"`
  48. Cipher string `json:"method"`
  49. Password string `json:"password"`
  50. Email string `json:"email"`
  51. }
  52. type ShadowsocksClientConfig struct {
  53. Servers []*ShadowsocksServerTarget `json:"servers"`
  54. }
  55. func (this *ShadowsocksClientConfig) Build() (*loader.TypedSettings, error) {
  56. config := new(shadowsocks.ClientConfig)
  57. if len(this.Servers) == 0 {
  58. return nil, errors.New("0 Shadowsocks server configured.")
  59. }
  60. serverSpecs := make([]*protocol.ServerEndpoint, len(this.Servers))
  61. for idx, server := range this.Servers {
  62. if server.Address == nil {
  63. return nil, errors.New("Shadowsocks server address is not set.")
  64. }
  65. if server.Port == 0 {
  66. return nil, errors.New("Invalid Shadowsocks port.")
  67. }
  68. if len(server.Password) == 0 {
  69. return nil, errors.New("Shadowsocks password is not specified.")
  70. }
  71. account := &shadowsocks.Account{
  72. Password: server.Password,
  73. }
  74. cipher := strings.ToLower(server.Cipher)
  75. switch cipher {
  76. case "aes-256-cfb":
  77. account.CipherType = shadowsocks.CipherType_AES_256_CFB
  78. case "aes-128-cfb":
  79. account.CipherType = shadowsocks.CipherType_AES_128_CFB
  80. case "chacha20":
  81. account.CipherType = shadowsocks.CipherType_CHACHA20
  82. case "chacha20-ietf":
  83. account.CipherType = shadowsocks.CipherType_CHACHA20_IEFT
  84. default:
  85. return nil, errors.New("Unknown cipher method: " + cipher)
  86. }
  87. ss := &protocol.ServerEndpoint{
  88. Address: server.Address.Build(),
  89. Port: uint32(server.Port),
  90. User: []*protocol.User{
  91. {
  92. Email: server.Email,
  93. Account: loader.NewTypedSettings(account),
  94. },
  95. },
  96. }
  97. serverSpecs[idx] = ss
  98. }
  99. return loader.NewTypedSettings(config), nil
  100. }