github.com/xmplusdev/xray-core@v1.8.10/infra/conf/http.go (about)

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