outbound.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package json
  2. import (
  3. "encoding/json"
  4. "github.com/v2ray/v2ray-core/common/log"
  5. v2net "github.com/v2ray/v2ray-core/common/net"
  6. v2netjson "github.com/v2ray/v2ray-core/common/net/json"
  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. Destination v2net.Destination
  15. Users []*vmessjson.ConfigUser
  16. }
  17. func (t *ConfigTarget) UnmarshalJSON(data []byte) error {
  18. type RawConfigTarget struct {
  19. Address *v2netjson.Host `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. if rawConfig.Address == nil {
  33. log.Error("Address is not set in VMess outbound config.")
  34. return proxyconfig.BadConfiguration
  35. }
  36. t.Destination = v2net.TCPDestination(rawConfig.Address.Address(), rawConfig.Port)
  37. return nil
  38. }
  39. type Outbound struct {
  40. TargetList []*ConfigTarget `json:"vnext"`
  41. }
  42. func (this *Outbound) UnmarshalJSON(data []byte) error {
  43. type RawOutbound struct {
  44. TargetList []*ConfigTarget `json:"vnext"`
  45. }
  46. rawOutbound := &RawOutbound{}
  47. err := json.Unmarshal(data, rawOutbound)
  48. if err != nil {
  49. return err
  50. }
  51. if len(rawOutbound.TargetList) == 0 {
  52. log.Error("0 VMess receiver configured.")
  53. return proxyconfig.BadConfiguration
  54. }
  55. this.TargetList = rawOutbound.TargetList
  56. return nil
  57. }
  58. func (o *Outbound) Receivers() []*outbound.Receiver {
  59. targets := make([]*outbound.Receiver, 0, 2*len(o.TargetList))
  60. for _, rawTarget := range o.TargetList {
  61. users := make([]vmess.User, 0, len(rawTarget.Users))
  62. for _, rawUser := range rawTarget.Users {
  63. users = append(users, rawUser)
  64. }
  65. targets = append(targets, &outbound.Receiver{
  66. Destination: rawTarget.Destination,
  67. Accounts: users,
  68. })
  69. }
  70. return targets
  71. }
  72. func init() {
  73. jsonconfig.RegisterOutboundConnectionConfig("vmess", func() interface{} {
  74. return new(Outbound)
  75. })
  76. }