config.go 856 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package http
  2. import (
  3. "github.com/xtls/xray-core/common"
  4. "github.com/xtls/xray-core/common/dice"
  5. "github.com/xtls/xray-core/transport/internet"
  6. )
  7. func (c *Config) getHosts() []string {
  8. if len(c.Host) == 0 {
  9. return []string{""}
  10. }
  11. return c.Host
  12. }
  13. func (c *Config) isValidHost(host string) bool {
  14. if len(c.Host) == 0 {
  15. return true
  16. }
  17. hosts := c.getHosts()
  18. for _, h := range hosts {
  19. if internet.IsValidHTTPHost(host, h) {
  20. return true
  21. }
  22. }
  23. return false
  24. }
  25. func (c *Config) getRandomHost() string {
  26. hosts := c.getHosts()
  27. return hosts[dice.Roll(len(hosts))]
  28. }
  29. func (c *Config) getNormalizedPath() string {
  30. if c.Path == "" {
  31. return "/"
  32. }
  33. if c.Path[0] != '/' {
  34. return "/" + c.Path
  35. }
  36. return c.Path
  37. }
  38. func init() {
  39. common.Must(internet.RegisterProtocolConfigCreator(protocolName, func() interface{} {
  40. return new(Config)
  41. }))
  42. }