config.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package vmess
  2. import (
  3. "net"
  4. "strings"
  5. "github.com/v2ray/v2ray-core/common/log"
  6. v2net "github.com/v2ray/v2ray-core/common/net"
  7. "github.com/v2ray/v2ray-core/config"
  8. "github.com/v2ray/v2ray-core/config/json"
  9. "github.com/v2ray/v2ray-core/proxy/vmess/protocol/user"
  10. )
  11. // VMessUser is an authenticated user account in VMess configuration.
  12. type VMessUser struct {
  13. Id string `json:"id"`
  14. Email string `json:"email"`
  15. }
  16. func (u *VMessUser) ToUser() (user.User, error) {
  17. id, err := user.NewID(u.Id)
  18. return user.User{
  19. Id: id,
  20. }, err
  21. }
  22. // VMessInboundConfig is
  23. type VMessInboundConfig struct {
  24. AllowedClients []VMessUser `json:"clients"`
  25. UDPEnabled bool `json:"udp"`
  26. }
  27. type VNextConfig struct {
  28. Address string `json:"address"`
  29. Port uint16 `json:"port"`
  30. Users []VMessUser `json:"users"`
  31. Network string `json:"network"`
  32. }
  33. func (config VNextConfig) HasNetwork(network string) bool {
  34. return strings.Contains(config.Network, network)
  35. }
  36. func (c VNextConfig) ToVNextServer(network string) (*VNextServer, error) {
  37. users := make([]user.User, 0, len(c.Users))
  38. for _, user := range c.Users {
  39. vuser, err := user.ToUser()
  40. if err != nil {
  41. log.Error("Failed to convert %v to User.", user)
  42. return nil, config.BadConfiguration
  43. }
  44. users = append(users, vuser)
  45. }
  46. ip := net.ParseIP(c.Address)
  47. if ip == nil {
  48. log.Error("Unable to parse VNext IP: %s", c.Address)
  49. return nil, config.BadConfiguration
  50. }
  51. address := v2net.IPAddress(ip, c.Port)
  52. var dest v2net.Destination
  53. if network == "tcp" {
  54. dest = v2net.NewTCPDestination(address)
  55. } else {
  56. dest = v2net.NewUDPDestination(address)
  57. }
  58. return &VNextServer{
  59. Destination: dest,
  60. Users: users,
  61. }, nil
  62. }
  63. type VMessOutboundConfig struct {
  64. VNextList []VNextConfig `json:"vnext"`
  65. }
  66. func init() {
  67. json.RegisterConfigType("vmess", config.TypeInbound, func() interface{} {
  68. return new(VMessInboundConfig)
  69. })
  70. json.RegisterConfigType("vmess", config.TypeOutbound, func() interface{} {
  71. return new(VMessOutboundConfig)
  72. })
  73. }