http.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package conf
  2. import (
  3. "encoding/json"
  4. "github.com/xtls/xray-core/common/errors"
  5. "github.com/xtls/xray-core/common/protocol"
  6. "github.com/xtls/xray-core/common/serial"
  7. "github.com/xtls/xray-core/proxy/http"
  8. "google.golang.org/protobuf/proto"
  9. )
  10. type HTTPAccount struct {
  11. Username string `json:"user"`
  12. Password string `json:"pass"`
  13. }
  14. func (v *HTTPAccount) Build() *http.Account {
  15. return &http.Account{
  16. Username: v.Username,
  17. Password: v.Password,
  18. }
  19. }
  20. type HTTPServerConfig struct {
  21. Accounts []*HTTPAccount `json:"accounts"`
  22. Transparent bool `json:"allowTransparent"`
  23. UserLevel uint32 `json:"userLevel"`
  24. }
  25. func (c *HTTPServerConfig) Build() (proto.Message, error) {
  26. config := &http.ServerConfig{
  27. AllowTransparent: c.Transparent,
  28. UserLevel: c.UserLevel,
  29. }
  30. if len(c.Accounts) > 0 {
  31. config.Accounts = make(map[string]string)
  32. for _, account := range c.Accounts {
  33. config.Accounts[account.Username] = account.Password
  34. }
  35. }
  36. return config, nil
  37. }
  38. type HTTPRemoteConfig struct {
  39. Address *Address `json:"address"`
  40. Port uint16 `json:"port"`
  41. Users []json.RawMessage `json:"users"`
  42. }
  43. type HTTPClientConfig struct {
  44. Address *Address `json:"address"`
  45. Port uint16 `json:"port"`
  46. Level uint32 `json:"level"`
  47. Email string `json:"email"`
  48. Username string `json:"user"`
  49. Password string `json:"pass"`
  50. Servers []*HTTPRemoteConfig `json:"servers"`
  51. Headers map[string]string `json:"headers"`
  52. }
  53. func (v *HTTPClientConfig) Build() (proto.Message, error) {
  54. config := new(http.ClientConfig)
  55. if v.Address != nil {
  56. v.Servers = []*HTTPRemoteConfig{
  57. {
  58. Address: v.Address,
  59. Port: v.Port,
  60. },
  61. }
  62. if len(v.Username) > 0 {
  63. v.Servers[0].Users = []json.RawMessage{{}}
  64. }
  65. }
  66. if len(v.Servers) != 1 {
  67. return nil, errors.New(`HTTP settings: "servers" should have one and only one member. Multiple endpoints in "servers" should use multiple HTTP outbounds and routing balancer instead`)
  68. }
  69. for _, serverConfig := range v.Servers {
  70. if len(serverConfig.Users) > 1 {
  71. return nil, errors.New(`HTTP servers: "users" should have one member at most. Multiple members in "users" should use multiple HTTP outbounds and routing balancer instead`)
  72. }
  73. server := &protocol.ServerEndpoint{
  74. Address: serverConfig.Address.Build(),
  75. Port: uint32(serverConfig.Port),
  76. }
  77. for _, rawUser := range serverConfig.Users {
  78. user := new(protocol.User)
  79. if v.Address != nil {
  80. user.Level = v.Level
  81. user.Email = v.Email
  82. } else {
  83. if err := json.Unmarshal(rawUser, user); err != nil {
  84. return nil, errors.New("failed to parse HTTP user").Base(err).AtError()
  85. }
  86. }
  87. account := new(HTTPAccount)
  88. if v.Address != nil {
  89. account.Username = v.Username
  90. account.Password = v.Password
  91. } else {
  92. if err := json.Unmarshal(rawUser, account); err != nil {
  93. return nil, errors.New("failed to parse HTTP account").Base(err).AtError()
  94. }
  95. }
  96. user.Account = serial.ToTypedMessage(account.Build())
  97. server.User = user
  98. break
  99. }
  100. config.Server = server
  101. break
  102. }
  103. config.Header = make([]*http.Header, 0, 32)
  104. for key, value := range v.Headers {
  105. config.Header = append(config.Header, &http.Header{
  106. Key: key,
  107. Value: value,
  108. })
  109. }
  110. return config, nil
  111. }