config.go 1.9 KB

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