shadowsocks.go 3.3 KB

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