shadowsocks.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. }