http.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package conf
  2. import (
  3. "encoding/json"
  4. "github.com/golang/protobuf/proto"
  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. )
  9. type HTTPAccount struct {
  10. Username string `json:"user"`
  11. Password string `json:"pass"`
  12. }
  13. func (v *HTTPAccount) Build() *http.Account {
  14. return &http.Account{
  15. Username: v.Username,
  16. Password: v.Password,
  17. }
  18. }
  19. type HTTPServerConfig struct {
  20. Timeout uint32 `json:"timeout"`
  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. Timeout: c.Timeout,
  28. AllowTransparent: c.Transparent,
  29. UserLevel: c.UserLevel,
  30. }
  31. if len(c.Accounts) > 0 {
  32. config.Accounts = make(map[string]string)
  33. for _, account := range c.Accounts {
  34. config.Accounts[account.Username] = account.Password
  35. }
  36. }
  37. return config, nil
  38. }
  39. type HTTPRemoteConfig struct {
  40. Address *Address `json:"address"`
  41. Port uint16 `json:"port"`
  42. Users []json.RawMessage `json:"users"`
  43. }
  44. type HTTPClientConfig struct {
  45. Servers []*HTTPRemoteConfig `json:"servers"`
  46. }
  47. func (v *HTTPClientConfig) Build() (proto.Message, error) {
  48. config := new(http.ClientConfig)
  49. config.Server = make([]*protocol.ServerEndpoint, len(v.Servers))
  50. for idx, serverConfig := range v.Servers {
  51. server := &protocol.ServerEndpoint{
  52. Address: serverConfig.Address.Build(),
  53. Port: uint32(serverConfig.Port),
  54. }
  55. for _, rawUser := range serverConfig.Users {
  56. user := new(protocol.User)
  57. if err := json.Unmarshal(rawUser, user); err != nil {
  58. return nil, newError("failed to parse HTTP user").Base(err).AtError()
  59. }
  60. account := new(HTTPAccount)
  61. if err := json.Unmarshal(rawUser, account); err != nil {
  62. return nil, newError("failed to parse HTTP account").Base(err).AtError()
  63. }
  64. user.Account = serial.ToTypedMessage(account.Build())
  65. server.User = append(server.User, user)
  66. }
  67. config.Server[idx] = server
  68. }
  69. return config, nil
  70. }