shadowsocks.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package v4
  2. import (
  3. "github.com/golang/protobuf/proto"
  4. "github.com/v2fly/v2ray-core/v4/common/protocol"
  5. "github.com/v2fly/v2ray-core/v4/common/serial"
  6. "github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon"
  7. "github.com/v2fly/v2ray-core/v4/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. NetworkList *cfgcommon.NetworkList `json:"network"`
  16. IVCheck bool `json:"ivCheck"`
  17. }
  18. func (v *ShadowsocksServerConfig) Build() (proto.Message, error) {
  19. config := new(shadowsocks.ServerConfig)
  20. config.UdpEnabled = v.UDP
  21. config.Network = v.NetworkList.Build()
  22. if v.Password == "" {
  23. return nil, newError("Shadowsocks password is not specified.")
  24. }
  25. account := &shadowsocks.Account{
  26. Password: v.Password,
  27. IvCheck: v.IVCheck,
  28. }
  29. account.CipherType = shadowsocks.CipherFromString(v.Cipher)
  30. if account.CipherType == shadowsocks.CipherType_UNKNOWN {
  31. return nil, newError("unknown cipher method: ", v.Cipher)
  32. }
  33. config.User = &protocol.User{
  34. Email: v.Email,
  35. Level: uint32(v.Level),
  36. Account: serial.ToTypedMessage(account),
  37. }
  38. return config, nil
  39. }
  40. type ShadowsocksServerTarget struct {
  41. Address *cfgcommon.Address `json:"address"`
  42. Port uint16 `json:"port"`
  43. Cipher string `json:"method"`
  44. Password string `json:"password"`
  45. Email string `json:"email"`
  46. Ota bool `json:"ota"`
  47. Level byte `json:"level"`
  48. IVCheck bool `json:"ivCheck"`
  49. }
  50. type ShadowsocksClientConfig struct {
  51. Servers []*ShadowsocksServerTarget `json:"servers"`
  52. }
  53. func (v *ShadowsocksClientConfig) Build() (proto.Message, error) {
  54. config := new(shadowsocks.ClientConfig)
  55. if len(v.Servers) == 0 {
  56. return nil, newError("0 Shadowsocks server configured.")
  57. }
  58. serverSpecs := make([]*protocol.ServerEndpoint, len(v.Servers))
  59. for idx, server := range v.Servers {
  60. if server.Address == nil {
  61. return nil, newError("Shadowsocks server address is not set.")
  62. }
  63. if server.Port == 0 {
  64. return nil, newError("Invalid Shadowsocks port.")
  65. }
  66. if server.Password == "" {
  67. return nil, newError("Shadowsocks password is not specified.")
  68. }
  69. account := &shadowsocks.Account{
  70. Password: server.Password,
  71. }
  72. account.CipherType = shadowsocks.CipherFromString(server.Cipher)
  73. if account.CipherType == shadowsocks.CipherType_UNKNOWN {
  74. return nil, newError("unknown cipher method: ", server.Cipher)
  75. }
  76. account.IvCheck = server.IVCheck
  77. ss := &protocol.ServerEndpoint{
  78. Address: server.Address.Build(),
  79. Port: uint32(server.Port),
  80. User: []*protocol.User{
  81. {
  82. Level: uint32(server.Level),
  83. Email: server.Email,
  84. Account: serial.ToTypedMessage(account),
  85. },
  86. },
  87. }
  88. serverSpecs[idx] = ss
  89. }
  90. config.Server = serverSpecs
  91. return config, nil
  92. }