shadowsocks.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package conf
  2. import (
  3. "errors"
  4. "strings"
  5. "encoding/json"
  6. "v2ray.com/core/common/loader"
  7. "v2ray.com/core/common/protocol"
  8. "v2ray.com/core/proxy/shadowsocks"
  9. )
  10. type ShadowsocksServerConfig struct {
  11. Cipher string `json:"method"`
  12. Password string `json:"password"`
  13. UDP bool `json:"udp"`
  14. Level byte `json:"level"`
  15. Email string `json:"email"`
  16. }
  17. func (this *ShadowsocksServerConfig) Build() (*loader.TypedSettings, error) {
  18. config := new(shadowsocks.ServerConfig)
  19. config.UdpEnabled = this.UDP
  20. if len(this.Password) == 0 {
  21. return nil, errors.New("Shadowsocks password is not specified.")
  22. }
  23. account := &shadowsocks.Account{
  24. Password: this.Password,
  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. }
  53. type ShadowsocksClientConfig struct {
  54. Servers []*ShadowsocksServerTarget `json:"servers"`
  55. }
  56. func (this *ShadowsocksClientConfig) Build() (*loader.TypedSettings, error) {
  57. config := new(shadowsocks.ClientConfig)
  58. if len(this.Servers) == 0 {
  59. return nil, errors.New("0 Shadowsocks server configured.")
  60. }
  61. serverSpecs := make([]*protocol.ServerEndpoint, len(this.Servers))
  62. for idx, server := range this.Servers {
  63. if server.Address == nil {
  64. return nil, errors.New("Shadowsocks server address is not set.")
  65. }
  66. if server.Port == 0 {
  67. return nil, errors.New("Invalid Shadowsocks port.")
  68. }
  69. if len(server.Password) == 0 {
  70. return nil, errors.New("Shadowsocks password is not specified.")
  71. }
  72. account := &shadowsocks.Account{
  73. Password: server.Password,
  74. }
  75. cipher := strings.ToLower(server.Cipher)
  76. switch cipher {
  77. case "aes-256-cfb":
  78. account.CipherType = shadowsocks.CipherType_AES_256_CFB
  79. case "aes-128-cfb":
  80. account.CipherType = shadowsocks.CipherType_AES_128_CFB
  81. case "chacha20":
  82. account.CipherType = shadowsocks.CipherType_CHACHA20
  83. case "chacha20-ietf":
  84. account.CipherType = shadowsocks.CipherType_CHACHA20_IEFT
  85. default:
  86. return nil, errors.New("Unknown cipher method: " + cipher)
  87. }
  88. ss := &protocol.ServerEndpoint{
  89. Address: server.Address.Build(),
  90. Port: uint32(server.Port),
  91. User: []*protocol.User{
  92. {
  93. Email: server.Email,
  94. Account: loader.NewTypedSettings(account),
  95. },
  96. },
  97. }
  98. }
  99. return loader.NewTypedSettings(config), nil
  100. }