[tor-commits] [meek/master] Use Clone to make copies of http.DefaultTransport.

dcf at torproject.org dcf at torproject.org
Wed Apr 29 19:35:42 UTC 2020


commit bc887de694bc2d2381af099d5c38f0e9efd76c4b
Author: David Fifield <david at bamsoftware.com>
Date:   Wed Apr 29 13:22:07 2020 -0600

    Use Clone to make copies of http.DefaultTransport.
    
    Clone is new in go1.13.
    https://github.com/golang/go/issues/26013
---
 meek-client/meek-client.go |  2 +-
 meek-client/utls.go        | 28 +++-------------------------
 2 files changed, 4 insertions(+), 26 deletions(-)

diff --git a/meek-client/meek-client.go b/meek-client/meek-client.go
index f520a60..3431b53 100644
--- a/meek-client/meek-client.go
+++ b/meek-client/meek-client.go
@@ -82,7 +82,7 @@ const (
 // We use this RoundTripper to make all our requests when neither --helper nor
 // utls is in effect. We use the defaults, except we take control of the Proxy
 // setting (notably, disabling the default ProxyFromEnvironment).
-var httpRoundTripper *http.Transport = http.DefaultTransport.(*http.Transport)
+var httpRoundTripper *http.Transport = http.DefaultTransport.(*http.Transport).Clone()
 
 // We use this RoundTripper when --helper is in effect.
 var helperRoundTripper = &HelperRoundTripper{
diff --git a/meek-client/utls.go b/meek-client/utls.go
index 66b1a93..576eb26 100644
--- a/meek-client/utls.go
+++ b/meek-client/utls.go
@@ -39,7 +39,6 @@ import (
 	"net"
 	"net/http"
 	"net/url"
-	"reflect"
 	"strings"
 	"sync"
 
@@ -48,25 +47,6 @@ import (
 	"golang.org/x/net/proxy"
 )
 
-// Copy the public fields (fields for which CanSet is true) from src to dst.
-// src and dst must be pointers to the same type. We use this to make copies of
-// httpRoundTripper. We cannot use struct assignment, because http.Transport
-// contains private mutexes. The idea of using reflection to copy only the
-// public fields comes from a post by Nick Craig-Wood:
-// https://groups.google.com/d/msg/Golang-Nuts/SDiGYNVE8iY/89hRKTF4BAAJ
-func copyPublicFields(dst, src interface{}) {
-	if reflect.TypeOf(dst) != reflect.TypeOf(src) {
-		panic("unequal types")
-	}
-	dstValue := reflect.ValueOf(dst).Elem()
-	srcValue := reflect.ValueOf(src).Elem()
-	for i := 0; i < dstValue.NumField(); i++ {
-		if dstValue.Field(i).CanSet() {
-			dstValue.Field(i).Set(srcValue.Field(i))
-		}
-	}
-}
-
 // Extract a host:port address from a URL, suitable for passing to net.Dial.
 func addrForDial(url *url.URL) (string, error) {
 	host := url.Hostname()
@@ -264,9 +244,8 @@ func makeRoundTripper(url *url.URL, clientHelloID *utls.ClientHelloID, cfg *utls
 	default:
 		// With http.Transport, copy important default fields from
 		// http.DefaultTransport, such as TLSHandshakeTimeout and
-		// IdleConnTimeout.
-		tr := &http.Transport{}
-		copyPublicFields(tr, httpRoundTripper)
+		// IdleConnTimeout, before overriding DialTLS.
+		tr := httpRoundTripper.Clone()
 		tr.DialTLS = dialTLS
 		return tr, nil
 	}
@@ -310,8 +289,7 @@ func NewUTLSRoundTripper(name string, cfg *utls.Config, proxyURL *url.URL) (http
 
 	// This special-case RoundTripper is used for HTTP requests, which don't
 	// use uTLS but should use the specified proxy.
-	httpRT := &http.Transport{}
-	copyPublicFields(httpRT, httpRoundTripper)
+	httpRT := httpRoundTripper.Clone()
 	httpRT.Proxy = http.ProxyURL(proxyURL)
 
 	return &UTLSRoundTripper{





More information about the tor-commits mailing list