github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/infra/conf/http.go (about)

     1  package conf
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/golang/protobuf/proto"
     7  	"v2ray.com/core/common/protocol"
     8  	"v2ray.com/core/common/serial"
     9  	"v2ray.com/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  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  }