1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 10:08:15 -05:00
v2fly/infra/conf/v4/http.go

84 lines
2.2 KiB
Go
Raw Normal View History

package v4
2019-02-10 13:04:11 -05:00
import (
2019-07-23 21:15:05 -04:00
"encoding/json"
2019-02-10 13:04:11 -05:00
"github.com/golang/protobuf/proto"
2021-02-16 15:31:50 -05:00
"github.com/v2fly/v2ray-core/v5/common/protocol"
"github.com/v2fly/v2ray-core/v5/common/serial"
"github.com/v2fly/v2ray-core/v5/infra/conf/cfgcommon"
"github.com/v2fly/v2ray-core/v5/proxy/http"
2019-02-10 13:04:11 -05:00
)
type HTTPAccount struct {
2019-02-10 13:04:11 -05:00
Username string `json:"user"`
Password string `json:"pass"`
}
func (v *HTTPAccount) Build() *http.Account {
2019-07-23 21:15:05 -04:00
return &http.Account{
Username: v.Username,
Password: v.Password,
}
}
type HTTPServerConfig struct {
2019-02-10 13:04:11 -05:00
Timeout uint32 `json:"timeout"`
Accounts []*HTTPAccount `json:"accounts"`
2019-02-10 13:04:11 -05:00
Transparent bool `json:"allowTransparent"`
UserLevel uint32 `json:"userLevel"`
}
func (c *HTTPServerConfig) Build() (proto.Message, error) {
2019-02-10 13:04:11 -05:00
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
}
2019-07-23 21:15:05 -04:00
type HTTPRemoteConfig struct {
Address *cfgcommon.Address `json:"address"`
Port uint16 `json:"port"`
Users []json.RawMessage `json:"users"`
2019-07-23 21:15:05 -04:00
}
2021-05-19 17:28:52 -04:00
type HTTPClientConfig struct {
Servers []*HTTPRemoteConfig `json:"servers"`
2019-07-23 21:15:05 -04:00
}
func (v *HTTPClientConfig) Build() (proto.Message, error) {
2019-07-23 21:15:05 -04:00
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)
2019-07-23 21:15:05 -04:00
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
}
return config, nil
}