outbound.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. "github.com/v2ray/v2ray-core/proxy/internal"
  8. proxyconfig "github.com/v2ray/v2ray-core/proxy/internal/config"
  9. jsonconfig "github.com/v2ray/v2ray-core/proxy/internal/config/json"
  10. "github.com/v2ray/v2ray-core/proxy/vmess"
  11. vmessjson "github.com/v2ray/v2ray-core/proxy/vmess/json"
  12. "github.com/v2ray/v2ray-core/proxy/vmess/outbound"
  13. )
  14. type ConfigTarget struct {
  15. Destination v2net.Destination
  16. Users []*vmessjson.ConfigUser
  17. }
  18. func (t *ConfigTarget) UnmarshalJSON(data []byte) error {
  19. type RawConfigTarget struct {
  20. Address *v2netjson.Host `json:"address"`
  21. Port v2net.Port `json:"port"`
  22. Users []*vmessjson.ConfigUser `json:"users"`
  23. }
  24. var rawConfig RawConfigTarget
  25. if err := json.Unmarshal(data, &rawConfig); err != nil {
  26. return err
  27. }
  28. if len(rawConfig.Users) == 0 {
  29. log.Error("0 user configured for VMess outbound.")
  30. return internal.ErrorBadConfiguration
  31. }
  32. t.Users = rawConfig.Users
  33. if rawConfig.Address == nil {
  34. log.Error("Address is not set in VMess outbound config.")
  35. return internal.ErrorBadConfiguration
  36. }
  37. t.Destination = v2net.TCPDestination(rawConfig.Address.Address(), 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 internal.ErrorBadConfiguration
  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. Destination: rawTarget.Destination,
  68. Accounts: users,
  69. })
  70. }
  71. return targets
  72. }
  73. func init() {
  74. proxyconfig.RegisterOutboundConnectionConfig("vmess", jsonconfig.JsonConfigLoader(func() interface{} {
  75. return new(Outbound)
  76. }))
  77. }