shadowsocks.go 3.5 KB

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