outbound.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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) UnmarshallJSON(data []byte) error {
  42. err := json.Unmarshal(data, this)
  43. if err != nil {
  44. return err
  45. }
  46. if len(this.TargetList) == 0 {
  47. log.Error("0 VMess receiver configured.")
  48. return proxyconfig.BadConfiguration
  49. }
  50. return nil
  51. }
  52. func (o *Outbound) Receivers() []*vmessconfig.Receiver {
  53. targets := make([]*vmessconfig.Receiver, 0, 2*len(o.TargetList))
  54. for _, rawTarget := range o.TargetList {
  55. users := make([]vmessconfig.User, 0, len(rawTarget.Users))
  56. for _, rawUser := range rawTarget.Users {
  57. users = append(users, rawUser)
  58. }
  59. targets = append(targets, &vmessconfig.Receiver{
  60. Address: rawTarget.Address,
  61. Accounts: users,
  62. })
  63. }
  64. return targets
  65. }
  66. func init() {
  67. jsonconfig.RegisterOutboundConnectionConfig("vmess", func() interface{} {
  68. return new(Outbound)
  69. })
  70. }