Hunter0x7c7
2022-08-11 b8230139fb40edea387617b6accd8371e37eda58
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package v4
 
import (
    "encoding/json"
 
    "github.com/golang/protobuf/proto"
 
    "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"
)
 
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 *cfgcommon.Address `json:"address"`
    Port    uint16             `json:"port"`
    Users   []json.RawMessage  `json:"users"`
}
 
type HTTPClientConfig struct {
    Servers []*HTTPRemoteConfig `json:"servers"`
}
 
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
    }
    return config, nil
}