outbound.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. if rawConfig.Address.IsIP() {
  37. t.Destination = v2net.TCPDestination(v2net.IPAddress(rawConfig.Address.IP()), rawConfig.Port)
  38. } else {
  39. t.Destination = v2net.TCPDestination(v2net.DomainAddress(rawConfig.Address.Domain()), rawConfig.Port)
  40. }
  41. return nil
  42. }
  43. type Outbound struct {
  44. TargetList []*ConfigTarget `json:"vnext"`
  45. }
  46. func (this *Outbound) UnmarshalJSON(data []byte) error {
  47. type RawOutbound struct {
  48. TargetList []*ConfigTarget `json:"vnext"`
  49. }
  50. rawOutbound := &RawOutbound{}
  51. err := json.Unmarshal(data, rawOutbound)
  52. if err != nil {
  53. return err
  54. }
  55. if len(rawOutbound.TargetList) == 0 {
  56. log.Error("0 VMess receiver configured.")
  57. return proxyconfig.BadConfiguration
  58. }
  59. this.TargetList = rawOutbound.TargetList
  60. return nil
  61. }
  62. func (o *Outbound) Receivers() []*outbound.Receiver {
  63. targets := make([]*outbound.Receiver, 0, 2*len(o.TargetList))
  64. for _, rawTarget := range o.TargetList {
  65. users := make([]vmess.User, 0, len(rawTarget.Users))
  66. for _, rawUser := range rawTarget.Users {
  67. users = append(users, rawUser)
  68. }
  69. targets = append(targets, &outbound.Receiver{
  70. Destination: rawTarget.Destination,
  71. Accounts: users,
  72. })
  73. }
  74. return targets
  75. }
  76. func init() {
  77. jsonconfig.RegisterOutboundConnectionConfig("vmess", func() interface{} {
  78. return new(Outbound)
  79. })
  80. }