-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Add custom header support for HTTP proxy #1443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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" | ||
|
|
@@ -30,6 +32,7 @@ import ( | |
| type Client struct { | ||
| serverPicker protocol.ServerPicker | ||
| policyManager policy.Manager | ||
| header []*Header | ||
| } | ||
|
|
||
| type h2Conn struct { | ||
|
|
@@ -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 | ||
| } | ||
|
|
||
|
|
@@ -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 { | ||
|
|
@@ -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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}, | ||
|
|
@@ -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") | ||
|
|
||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe, we can use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
headers
There was a problem hiding this comment.
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.Configandxray.transport.internet.http.Config, theHeaderproperty is always in singular form. But inxray.infra.conf.*they are plural form. So I think it should be plural only in config file but singular in other codes.