github.com/eagleql/xray-core@v1.4.4/infra/conf/http.go (about) 1 package conf 2 3 import ( 4 "encoding/json" 5 6 "github.com/eagleql/xray-core/common/protocol" 7 "github.com/eagleql/xray-core/common/serial" 8 "github.com/eagleql/xray-core/proxy/http" 9 "github.com/golang/protobuf/proto" 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 type HTTPClientConfig struct { 54 Servers []*HTTPRemoteConfig `json:"servers"` 55 } 56 57 func (v *HTTPClientConfig) Build() (proto.Message, error) { 58 config := new(http.ClientConfig) 59 config.Server = make([]*protocol.ServerEndpoint, len(v.Servers)) 60 for idx, serverConfig := range v.Servers { 61 server := &protocol.ServerEndpoint{ 62 Address: serverConfig.Address.Build(), 63 Port: uint32(serverConfig.Port), 64 } 65 for _, rawUser := range serverConfig.Users { 66 user := new(protocol.User) 67 if err := json.Unmarshal(rawUser, user); err != nil { 68 return nil, newError("failed to parse HTTP user").Base(err).AtError() 69 } 70 account := new(HTTPAccount) 71 if err := json.Unmarshal(rawUser, account); err != nil { 72 return nil, newError("failed to parse HTTP account").Base(err).AtError() 73 } 74 user.Account = serial.ToTypedMessage(account.Build()) 75 server.User = append(server.User, user) 76 } 77 config.Server[idx] = server 78 } 79 return config, nil 80 }