outbound.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package json
  2. import (
  3. "encoding/json"
  4. "net"
  5. "github.com/v2ray/v2ray-core/common/log"
  6. v2net "github.com/v2ray/v2ray-core/common/net"
  7. proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config"
  8. jsonconfig "github.com/v2ray/v2ray-core/proxy/common/config/json"
  9. "github.com/v2ray/v2ray-core/proxy/vmess"
  10. vmessjson "github.com/v2ray/v2ray-core/proxy/vmess/json"
  11. "github.com/v2ray/v2ray-core/proxy/vmess/outbound"
  12. )
  13. type ConfigTarget struct {
  14. Address v2net.Address
  15. Users []*vmessjson.ConfigUser
  16. }
  17. func (t *ConfigTarget) UnmarshalJSON(data []byte) error {
  18. type RawConfigTarget struct {
  19. Address string `json:"address"`
  20. Port v2net.Port `json:"port"`
  21. Users []*vmessjson.ConfigUser `json:"users"`
  22. }
  23. var rawConfig RawConfigTarget
  24. if err := json.Unmarshal(data, &rawConfig); err != nil {
  25. return err
  26. }
  27. if len(rawConfig.Users) == 0 {
  28. log.Error("0 user configured for VMess outbound.")
  29. return proxyconfig.BadConfiguration
  30. }
  31. t.Users = rawConfig.Users
  32. ip := net.ParseIP(rawConfig.Address)
  33. if ip == nil {
  34. log.Error("Unable to parse IP: %s", rawConfig.Address)
  35. return proxyconfig.BadConfiguration
  36. }
  37. t.Address = v2net.IPAddress(ip, rawConfig.Port)
  38. return nil
  39. }
  40. type Outbound struct {
  41. TargetList []*ConfigTarget `json:"vnext"`
  42. }
  43. func (this *Outbound) UnmarshalJSON(data []byte) error {
  44. type RawOutbound struct {
  45. TargetList []*ConfigTarget `json:"vnext"`
  46. }
  47. rawOutbound := &RawOutbound{}
  48. err := json.Unmarshal(data, rawOutbound)
  49. if err != nil {
  50. return err
  51. }
  52. if len(rawOutbound.TargetList) == 0 {
  53. log.Error("0 VMess receiver configured.")
  54. return proxyconfig.BadConfiguration
  55. }
  56. this.TargetList = rawOutbound.TargetList
  57. return nil
  58. }
  59. func (o *Outbound) Receivers() []*outbound.Receiver {
  60. targets := make([]*outbound.Receiver, 0, 2*len(o.TargetList))
  61. for _, rawTarget := range o.TargetList {
  62. users := make([]vmess.User, 0, len(rawTarget.Users))
  63. for _, rawUser := range rawTarget.Users {
  64. users = append(users, rawUser)
  65. }
  66. targets = append(targets, &outbound.Receiver{
  67. Address: rawTarget.Address,
  68. Accounts: users,
  69. })
  70. }
  71. return targets
  72. }
  73. func init() {
  74. jsonconfig.RegisterOutboundConnectionConfig("vmess", func() interface{} {
  75. return new(Outbound)
  76. })
  77. }