config.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 (config VNextConfig) ToVNextServer(network string) VNextServer {
  37. users := make([]user.User, 0, len(config.Users))
  38. for _, user := range config.Users {
  39. vuser, err := user.ToUser()
  40. if err != nil {
  41. panic(log.Error("Failed to convert %v to User.", user))
  42. }
  43. users = append(users, vuser)
  44. }
  45. ip := net.ParseIP(config.Address)
  46. if ip == nil {
  47. panic(log.Error("Unable to parse VNext IP: %s", config.Address))
  48. }
  49. address := v2net.IPAddress(ip, config.Port)
  50. var dest v2net.Destination
  51. if network == "tcp" {
  52. dest = v2net.NewTCPDestination(address)
  53. } else {
  54. dest = v2net.NewUDPDestination(address)
  55. }
  56. return VNextServer{
  57. Destination: dest,
  58. Users: users,
  59. }
  60. }
  61. type VMessOutboundConfig struct {
  62. VNextList []VNextConfig `json:"vnext"`
  63. }
  64. func init() {
  65. json.RegisterConfigType("vmess", config.TypeInbound, func() interface{} {
  66. return new(VMessInboundConfig)
  67. })
  68. json.RegisterConfigType("vmess", config.TypeOutbound, func() interface{} {
  69. return new(VMessOutboundConfig)
  70. })
  71. }