Skip to content

Commit

Permalink
Merge pull request #16 from mysteriumnetwork/feature/country-targeting
Browse files Browse the repository at this point in the history
Country targeting
  • Loading branch information
Waldz authored Nov 22, 2021
2 parents ea4dc4b + 035690b commit 8969164
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")`,
Expand Down Expand Up @@ -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 {
Expand Down
26 changes: 16 additions & 10 deletions proxy/dialer_http_connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,37 @@ 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
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
}

Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions proxy/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand Down

0 comments on commit 8969164

Please sign in to comment.