Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions infra/conf/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type HTTPRemoteConfig struct {

type HTTPClientConfig struct {
Servers []*HTTPRemoteConfig `json:"servers"`
Headers map[string]string `json:"headers"`
}

func (v *HTTPClientConfig) Build() (proto.Message, error) {
Expand All @@ -77,5 +78,12 @@ func (v *HTTPClientConfig) Build() (proto.Message, error) {
}
config.Server[idx] = server
}
config.Header = make([]*http.Header, 0, 32)
for key, value := range v.Headers {
config.Header = append(config.Header, &http.Header{
Key: key,
Value: value,
})
}
return config, nil
}
51 changes: 49 additions & 2 deletions proxy/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package http

import (
"bufio"
"bytes"
"context"
"encoding/base64"
"io"
"net/http"
"net/url"
"sync"
"text/template"

"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf"
Expand All @@ -30,6 +32,7 @@ import (
type Client struct {
serverPicker protocol.ServerPicker
policyManager policy.Manager
header []*Header
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

headers

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to xray.transport.internet.websocket.Config and xray.transport.internet.http.Config, the Header property is always in singular form. But in xray.infra.conf.* they are plural form. So I think it should be plural only in config file but singular in other codes.

}

type h2Conn struct {
Expand Down Expand Up @@ -60,6 +63,7 @@ func NewClient(ctx context.Context, config *ClientConfig) (*Client, error) {
return &Client{
serverPicker: protocol.NewRoundRobinServerPicker(serverList),
policyManager: v.GetFeature(policy.ManagerType()).(policy.Manager),
header: config.Header,
}, nil
}

Expand Down Expand Up @@ -88,12 +92,17 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter
buf.ReleaseMulti(mbuf)
defer bytespool.Free(firstPayload)

header, err := fillRequestHeader(ctx, c.header)
if err != nil {
return newError("failed to fill out header").Base(err)
}

if err := retry.ExponentialBackoff(5, 100).On(func() error {
server := c.serverPicker.PickServer()
dest := server.Destination()
user = server.PickUser()

netConn, err := setUpHTTPTunnel(ctx, dest, targetAddr, user, dialer, firstPayload)
netConn, err := setUpHTTPTunnel(ctx, dest, targetAddr, user, dialer, header, firstPayload)
if netConn != nil {
if _, ok := netConn.(*http2Conn); !ok {
if _, err := netConn.Write(firstPayload); err != nil {
Expand Down Expand Up @@ -139,8 +148,42 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter
return nil
}

// fillRequestHeader will fill out the template of the headers
func fillRequestHeader(ctx context.Context, header []*Header) ([]*Header, error) {
if len(header) == 0 {
return header, nil
}

inbound := session.InboundFromContext(ctx)
outbound := session.OutboundFromContext(ctx)

data := struct {
Source net.Destination
Target net.Destination
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems not used in your examples. Is target needed? I feel the receiving end would have it anyway

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's not needed in my case. I just want to make it more extendable for other usages.

}{
Source: inbound.Source,
Target: outbound.Target,
}

filled := make([]*Header, len(header))
for i, h := range header {
tmpl, err := template.New(h.Key).Parse(h.Value)
if err != nil {
return nil, err
}
var buf bytes.Buffer

if err = tmpl.Execute(&buf, data); err != nil {
return nil, err
}
filled[i] = &Header{Key: h.Key, Value: buf.String()}
}

return filled, nil
}

// setUpHTTPTunnel will create a socket tunnel via HTTP CONNECT method
func setUpHTTPTunnel(ctx context.Context, dest net.Destination, target string, user *protocol.MemoryUser, dialer internet.Dialer, firstPayload []byte) (net.Conn, error) {
func setUpHTTPTunnel(ctx context.Context, dest net.Destination, target string, user *protocol.MemoryUser, dialer internet.Dialer, header []*Header, firstPayload []byte) (net.Conn, error) {
req := &http.Request{
Method: http.MethodConnect,
URL: &url.URL{Host: target},
Expand All @@ -154,6 +197,10 @@ func setUpHTTPTunnel(ctx context.Context, dest net.Destination, target string, u
req.Header.Set("Proxy-Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(auth)))
}

for _, h := range header {
req.Header.Set(h.Key, h.Value)
}

connectHTTP1 := func(rawConn net.Conn) (net.Conn, error) {
req.Header.Set("Proxy-Connection", "Keep-Alive")

Expand Down
135 changes: 109 additions & 26 deletions proxy/http/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions proxy/http/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ message ServerConfig {
uint32 user_level = 4;
}

message Header {
string key = 1;
string value = 2;
}

// ClientConfig is the protobuf config for HTTP proxy client.
message ClientConfig {
// Sever is a list of HTTP server addresses.
repeated xray.common.protocol.ServerEndpoint server = 1;
repeated Header header = 2;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, we can use
repeated xray.transport.internet.headers.http.Header header

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

xray.transport.internet.headers.http.Header is designed to pick a random value for a key.
I referenced from xray.transport.internet.websocket.Config to make the current solution.

}