outbound.go 2.1 KB

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