shadowsocks.go 3.3 KB

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