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