diff --git a/main.go b/main.go index 37c6a27..a2bf4da 100644 --- a/main.go +++ b/main.go @@ -40,6 +40,7 @@ var proxyUpstreamURL = flag.String( ) var proxyUser = flag.String("proxy.user", "", "HTTP proxy auth user") var proxyPass = flag.String("proxy.pass", "", "HTTP proxy auth password") +var proxyCountry = flag.String("proxy.country", "", "HTTP proxy country targeting") var proxyMapPort = FlagArray( "proxy.port-map", `Explicitly map source port to destination port (separated by comma - "8443:443,18443:8443")`, @@ -84,7 +85,7 @@ func main() { apiServer := api.NewServer(*proxyAPIAddr, sm, domainTracer) go apiServer.ListenAndServe() - dialerUpstream := proxy.NewDialerHTTPConnect(proxy.DialerDirect, dialerUpstreamURL.Host, *proxyUser, *proxyPass) + dialerUpstream := proxy.NewDialerHTTPConnect(proxy.DialerDirect, dialerUpstreamURL.Host, *proxyUser, *proxyPass, *proxyCountry) var dialer netproxy.Dialer if len(*filterHostnames) > 0 || len(*filterZones) > 0 { diff --git a/proxy/dialer_http_connect.go b/proxy/dialer_http_connect.go index fb9df6b..e64fd16 100644 --- a/proxy/dialer_http_connect.go +++ b/proxy/dialer_http_connect.go @@ -31,25 +31,26 @@ import ( // NewDialerHTTPConnect returns a new Dialer that dials through the provided // proxy server's network and address. -func NewDialerHTTPConnect(forwardDialer netproxy.Dialer, forwardAddress, user, pass string) *dialerHTTPConnect { +func NewDialerHTTPConnect(forwardDialer netproxy.Dialer, forwardAddress, user, pass, country string) *dialerHTTPConnect { return &dialerHTTPConnect{ forwardDialer: forwardDialer, forwardAddress: forwardAddress, user: user, pass: pass, + country: country, } } type dialerHTTPConnect struct { - forwardDialer netproxy.Dialer - forwardAddress string - user, pass string + forwardDialer netproxy.Dialer + forwardAddress string + user, pass, country string } // Connection wraps net.Conn to provide extra method for establishing CONNECT session. type Connection struct { net.Conn - user, pass string + user, pass, country string } // Dial makes actual connection to specified address through intermediate HTTP proxy @@ -57,9 +58,10 @@ func (dialer *dialerHTTPConnect) Dial(network, address string) (net.Conn, error) conn, err := dialer.forwardDialer.Dial(network, dialer.forwardAddress) return &Connection{ - Conn: conn, - user: dialer.user, - pass: dialer.pass, + Conn: conn, + user: dialer.user, + pass: dialer.pass, + country: dialer.country, }, err } @@ -73,8 +75,12 @@ func (c *Connection) ConnectTo(conn io.ReadWriter, address string, userID string Header: make(http.Header), } - if len(userID) > 0 { - req.Header.Add("Forwarded", "UserID="+userID) + if userID != "" { + req.Header.Add("User-Id", userID) + } + + if c.country != "" { + req.Header.Add("Country", c.country) } if len(c.user) > 0 && len(c.pass) > 0 { diff --git a/proxy/server_test.go b/proxy/server_test.go index 3779437..880e46a 100644 --- a/proxy/server_test.go +++ b/proxy/server_test.go @@ -36,7 +36,7 @@ func Test_Server_ServeHTTP(t *testing.T) { upstreamAddr := upstreamServer.run() defer upstreamServer.stop() - upstreamDialer := NewDialerHTTPConnect(DialerDirect, upstreamAddr, "", "") + upstreamDialer := NewDialerHTTPConnect(DialerDirect, upstreamAddr, "", "", "") req, _ := http.NewRequest("GET", "http://domain.com", nil) @@ -63,7 +63,7 @@ func Test_Server_AuthHeaderAdded(t *testing.T) { upstreamAddr := upstreamServer.run() defer upstreamServer.stop() - upstreamDialer := NewDialerHTTPConnect(DialerDirect, upstreamAddr, "uuuu", "1234") + upstreamDialer := NewDialerHTTPConnect(DialerDirect, upstreamAddr, "uuuu", "1234", "") req, _ := http.NewRequest("GET", "http://domain.com", nil)