http.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package v4
  2. import (
  3. "encoding/json"
  4. "github.com/golang/protobuf/proto"
  5. "github.com/v2fly/v2ray-core/v4/common/protocol"
  6. "github.com/v2fly/v2ray-core/v4/common/serial"
  7. "github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon"
  8. "github.com/v2fly/v2ray-core/v4/proxy/http"
  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. Timeout uint32 `json:"timeout"`
  22. Accounts []*HTTPAccount `json:"accounts"`
  23. Transparent bool `json:"allowTransparent"`
  24. UserLevel uint32 `json:"userLevel"`
  25. }
  26. func (c *HTTPServerConfig) Build() (proto.Message, error) {
  27. config := &http.ServerConfig{
  28. Timeout: c.Timeout,
  29. AllowTransparent: c.Transparent,
  30. UserLevel: c.UserLevel,
  31. }
  32. if len(c.Accounts) > 0 {
  33. config.Accounts = make(map[string]string)
  34. for _, account := range c.Accounts {
  35. config.Accounts[account.Username] = account.Password
  36. }
  37. }
  38. return config, nil
  39. }
  40. type HTTPRemoteConfig struct {
  41. Address *cfgcommon.Address `json:"address"`
  42. Port uint16 `json:"port"`
  43. Users []json.RawMessage `json:"users"`
  44. }
  45. type HTTPClientConfig struct {
  46. Servers []*HTTPRemoteConfig `json:"servers"`
  47. }
  48. func (v *HTTPClientConfig) Build() (proto.Message, error) {
  49. config := new(http.ClientConfig)
  50. config.Server = make([]*protocol.ServerEndpoint, len(v.Servers))
  51. for idx, serverConfig := range v.Servers {
  52. server := &protocol.ServerEndpoint{
  53. Address: serverConfig.Address.Build(),
  54. Port: uint32(serverConfig.Port),
  55. }
  56. for _, rawUser := range serverConfig.Users {
  57. user := new(protocol.User)
  58. if err := json.Unmarshal(rawUser, user); err != nil {
  59. return nil, newError("failed to parse HTTP user").Base(err).AtError()
  60. }
  61. account := new(HTTPAccount)
  62. if err := json.Unmarshal(rawUser, account); err != nil {
  63. return nil, newError("failed to parse HTTP account").Base(err).AtError()
  64. }
  65. user.Account = serial.ToTypedMessage(account.Build())
  66. server.User = append(server.User, user)
  67. }
  68. config.Server[idx] = server
  69. }
  70. return config, nil
  71. }