2020-11-25 14:01:53 +03:00
|
|
|
package conf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
2020-12-04 04:36:16 +03:00
|
|
|
"github.com/xtls/xray-core/common/protocol"
|
|
|
|
"github.com/xtls/xray-core/common/serial"
|
|
|
|
"github.com/xtls/xray-core/proxy/http"
|
2020-11-25 14:01:53 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type HTTPAccount struct {
|
|
|
|
Username string `json:"user"`
|
|
|
|
Password string `json:"pass"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *HTTPAccount) Build() *http.Account {
|
|
|
|
return &http.Account{
|
|
|
|
Username: v.Username,
|
|
|
|
Password: v.Password,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type HTTPServerConfig struct {
|
|
|
|
Timeout uint32 `json:"timeout"`
|
|
|
|
Accounts []*HTTPAccount `json:"accounts"`
|
|
|
|
Transparent bool `json:"allowTransparent"`
|
|
|
|
UserLevel uint32 `json:"userLevel"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *HTTPServerConfig) Build() (proto.Message, error) {
|
|
|
|
config := &http.ServerConfig{
|
|
|
|
Timeout: c.Timeout,
|
|
|
|
AllowTransparent: c.Transparent,
|
|
|
|
UserLevel: c.UserLevel,
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.Accounts) > 0 {
|
|
|
|
config.Accounts = make(map[string]string)
|
|
|
|
for _, account := range c.Accounts {
|
|
|
|
config.Accounts[account.Username] = account.Password
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type HTTPRemoteConfig struct {
|
|
|
|
Address *Address `json:"address"`
|
|
|
|
Port uint16 `json:"port"`
|
|
|
|
Users []json.RawMessage `json:"users"`
|
|
|
|
}
|
2021-10-19 19:57:14 +03:00
|
|
|
|
2020-11-25 14:01:53 +03:00
|
|
|
type HTTPClientConfig struct {
|
|
|
|
Servers []*HTTPRemoteConfig `json:"servers"`
|
2022-12-15 14:15:43 +03:00
|
|
|
Headers map[string]string `json:"headers"`
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *HTTPClientConfig) Build() (proto.Message, error) {
|
|
|
|
config := new(http.ClientConfig)
|
|
|
|
config.Server = make([]*protocol.ServerEndpoint, len(v.Servers))
|
|
|
|
for idx, serverConfig := range v.Servers {
|
|
|
|
server := &protocol.ServerEndpoint{
|
|
|
|
Address: serverConfig.Address.Build(),
|
|
|
|
Port: uint32(serverConfig.Port),
|
|
|
|
}
|
|
|
|
for _, rawUser := range serverConfig.Users {
|
|
|
|
user := new(protocol.User)
|
|
|
|
if err := json.Unmarshal(rawUser, user); err != nil {
|
|
|
|
return nil, newError("failed to parse HTTP user").Base(err).AtError()
|
|
|
|
}
|
|
|
|
account := new(HTTPAccount)
|
|
|
|
if err := json.Unmarshal(rawUser, account); err != nil {
|
|
|
|
return nil, newError("failed to parse HTTP account").Base(err).AtError()
|
|
|
|
}
|
|
|
|
user.Account = serial.ToTypedMessage(account.Build())
|
|
|
|
server.User = append(server.User, user)
|
|
|
|
}
|
|
|
|
config.Server[idx] = server
|
|
|
|
}
|
2022-12-15 14:15:43 +03:00
|
|
|
config.Header = make([]*http.Header, 0, 32)
|
|
|
|
for key, value := range v.Headers {
|
|
|
|
config.Header = append(config.Header, &http.Header{
|
|
|
|
Key: key,
|
|
|
|
Value: value,
|
|
|
|
})
|
|
|
|
}
|
2020-11-25 14:01:53 +03:00
|
|
|
return config, nil
|
|
|
|
}
|