outbound.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 RawConfigTarget struct {
  12. Address string `json:"address"`
  13. Port v2net.Port `json:"port"`
  14. Users []*ConfigUser `json:"users"`
  15. }
  16. type ConfigTarget struct {
  17. Address v2net.Address
  18. Users []*ConfigUser
  19. }
  20. func (t *ConfigTarget) UnmarshalJSON(data []byte) error {
  21. var rawConfig RawConfigTarget
  22. if err := json.Unmarshal(data, &rawConfig); err != nil {
  23. return err
  24. }
  25. t.Users = rawConfig.Users
  26. ip := net.ParseIP(rawConfig.Address)
  27. if ip == nil {
  28. log.Error("Unable to parse IP: %s", rawConfig.Address)
  29. return proxyconfig.BadConfiguration
  30. }
  31. t.Address = v2net.IPAddress(ip, rawConfig.Port)
  32. return nil
  33. }
  34. type Outbound struct {
  35. TargetList []*ConfigTarget `json:"vnext"`
  36. }
  37. func (o *Outbound) Targets() []*vmessconfig.OutboundTarget {
  38. targets := make([]*vmessconfig.OutboundTarget, 0, 2*len(o.TargetList))
  39. for _, rawTarget := range o.TargetList {
  40. users := make([]vmessconfig.User, 0, len(rawTarget.Users))
  41. for _, rawUser := range rawTarget.Users {
  42. users = append(users, rawUser)
  43. }
  44. targets = append(targets, &vmessconfig.OutboundTarget{
  45. Destination: v2net.NewTCPDestination(rawTarget.Address),
  46. Accounts: users,
  47. })
  48. }
  49. return targets
  50. }
  51. func init() {
  52. jsonconfig.RegisterOutboundConnectionConfig("vmess", func() interface{} {
  53. return new(Outbound)
  54. })
  55. }