Commit eafbc1d7 authored by David Fifield's avatar David Fifield
Browse files

Use the new TOR_PT_PROXY interface from #12125.

parent 8cd16980
Loading
Loading
Loading
Loading
+6 −11
Changes for meek-client/meek-client.go: 6 added lines, 11 removed lines.
Original line number Original line Diff line number Diff line
@@ -389,8 +389,8 @@ func main() {


	// We make a copy of DefaultTransport because we want the default Dial
	// We make a copy of DefaultTransport because we want the default Dial
	// and TLSHandshakeTimeout settings. But we want to disable the default
	// and TLSHandshakeTimeout settings. But we want to disable the default
	// ProxyFromEnvironment setting. Proxy is overridden below if proxyURL
	// ProxyFromEnvironment setting. Proxy is overridden below if
	// is set.
	// options.ProxyURL is set.
	httpTransport = *http.DefaultTransport.(*http.Transport)
	httpTransport = *http.DefaultTransport.(*http.Transport)
	httpTransport.Proxy = nil
	httpTransport.Proxy = nil


@@ -398,27 +398,22 @@ func main() {
	if err != nil {
	if err != nil {
		log.Fatalf("error in ClientSetup: %s", err)
		log.Fatalf("error in ClientSetup: %s", err)
	}
	}
	ptProxyURL, err := PtGetProxyURL()
	if err != nil {
		PtProxyError(err.Error())
		log.Fatalf("can't get managed proxy configuration: %s", err)
	}


	// Command-line proxy overrides managed configuration.
	// Command-line proxy overrides managed configuration.
	if options.ProxyURL == nil {
	if options.ProxyURL == nil {
		options.ProxyURL = ptProxyURL
		options.ProxyURL = ptInfo.ProxyURL
	}
	}
	// Check whether we support this kind of proxy.
	// Check whether we support this kind of proxy.
	if options.ProxyURL != nil {
	if options.ProxyURL != nil {
		err = checkProxyURL(options.ProxyURL)
		err = checkProxyURL(options.ProxyURL)
		if err != nil {
		if err != nil {
			PtProxyError(err.Error())
			pt.ProxyError(err.Error())
			log.Fatal(fmt.Sprintf("proxy error: %s", err))
			log.Fatal(fmt.Sprintf("proxy error: %s", err))
		}
		}
		log.Printf("using proxy %s", options.ProxyURL.String())
		log.Printf("using proxy %s", options.ProxyURL.String())
		httpTransport.Proxy = http.ProxyURL(options.ProxyURL)
		httpTransport.Proxy = http.ProxyURL(options.ProxyURL)
		if ptProxyURL != nil {
		if ptInfo.ProxyURL != nil {
			PtProxyDone()
			pt.ProxyDone()
		}
		}
	}
	}


meek-client/proxy.go

deleted100644 → 0
+0 −52
Changes for meek-client/proxy.go: 0 added lines, 52 removed lines.
Original line number Original line Diff line number Diff line
package main

import (
	"fmt"
	"net/url"
	"os"
)

import "git.torproject.org/pluggable-transports/goptlib.git"

// The code in this file has to do with configuring an upstream proxy, whether
// through the command line or the managed interface of proposal 232
// (TOR_PT_PROXY).
//
// https://gitweb.torproject.org/torspec.git/tree/proposals/232-pluggable-transports-through-proxy.txt

// Get the upstream proxy URL. Returns nil if no proxy is requested. The
// function ensures that the Scheme and Host fields are set; i.e., that the URL
// is absolute. This function reads the environment variable TOR_PT_PROXY.
//
// This function doesn't check that the scheme is one of Tor's supported proxy
// schemes; that is, one of "http", "socks5", or "socks4a". The caller must be
// able to handle any returned scheme (which may be by calling PtProxyError if
// it doesn't know how to handle the scheme).
func PtGetProxyURL() (*url.URL, error) {
	rawurl := os.Getenv("TOR_PT_PROXY")
	if rawurl == "" {
		return nil, nil
	}
	u, err := url.Parse(rawurl)
	if err != nil {
		return nil, err
	}
	if u.Scheme == "" {
		return nil, fmt.Errorf("missing scheme")
	}
	if u.Host == "" {
		return nil, fmt.Errorf("missing host")
	}
	return u, nil
}

// Emit a PROXY-ERROR line with explanation text.
func PtProxyError(msg string) {
	fmt.Fprintf(pt.Stdout, "PROXY-ERROR %s\n", msg)
}

// Emit a PROXY DONE line. Call this after parsing the return value of
// PtGetProxyURL.
func PtProxyDone() {
	fmt.Fprintf(pt.Stdout, "PROXY DONE\n")
}

meek-client/proxy_test.go

deleted100644 → 0
+0 −81
Changes for meek-client/proxy_test.go: 0 added lines, 81 removed lines.
Original line number Original line Diff line number Diff line
package main

import (
	"os"
	"testing"
)

func TestGetProxyURL(t *testing.T) {
	badTests := [...]string{
		"bogus",
		"http:",
		"://127.0.0.1",
		"//127.0.0.1",
		"http:127.0.0.1",
		"://[::1]",
		"//[::1]",
		"http:[::1]",
		"://localhost",
		"//localhost",
		"http:localhost",
	}
	goodTests := [...]struct {
		input, expected string
	}{
		{"http://127.0.0.1", "http://127.0.0.1"},
		{"http://127.0.0.1:8080", "http://127.0.0.1:8080"},
		{"http://127.0.0.1:8080/", "http://127.0.0.1:8080/"},
		{"http://127.0.0.1:8080/path", "http://127.0.0.1:8080/path"},
		{"http://[::1]", "http://[::1]"},
		{"http://[::1]:8080", "http://[::1]:8080"},
		{"http://[::1]:8080/", "http://[::1]:8080/"},
		{"http://[::1]:8080/path", "http://[::1]:8080/path"},
		{"http://localhost", "http://localhost"},
		{"http://localhost:8080", "http://localhost:8080"},
		{"http://localhost:8080/", "http://localhost:8080/"},
		{"http://localhost:8080/path", "http://localhost:8080/path"},
		{"http://user@localhost:8080", "http://user@localhost:8080"},
		{"http://user:password@localhost:8080", "http://user:password@localhost:8080"},
		{"socks5://localhost:1080", "socks5://localhost:1080"},
		{"socks4a://localhost:1080", "socks4a://localhost:1080"},
		{"unknown://localhost/whatever", "unknown://localhost/whatever"},
	}
	/*
		No port: reject; or infer from scheme?
			http://localhost
			socks4a://localhost
			socks5://localhost
		Port without host: probably reject?
			http://:8080
			socks4a://:1080
			socks5://:1080
	*/

	os.Clearenv()
	u, err := PtGetProxyURL()
	if err != nil {
		t.Errorf("empty environment unexpectedly returned an error: %s", err)
	}
	if u != nil {
		t.Errorf("empty environment returned %q", u)
	}

	for _, input := range badTests {
		os.Setenv("TOR_PT_PROXY", input)
		u, err = PtGetProxyURL()
		if err == nil {
			t.Errorf("TOR_PT_PROXY=%q unexpectedly succeeded and returned %q", input, u)
		}
	}

	for _, test := range goodTests {
		os.Setenv("TOR_PT_PROXY", test.input)
		u, err := PtGetProxyURL()
		if err != nil {
			t.Errorf("TOR_PT_PROXY=%q unexpectedly returned an error: %s", test.input, err)
		}
		if u == nil || u.String() != test.expected {
			t.Errorf("TOR_PT_PROXY=%q → %q (expected %q)", test.input, u, test.expected)
		}
	}
}