Просмотр исходного кода

re-organized code to allow user agent selection in additional cases

Michael Goldberger 9 лет назад
Родитель
Сommit
c0eb519924
6 измененных файлов с 33 добавлено и 21 удалено
  1. 3 0
      psiphon/TCPConn.go
  2. 20 0
      psiphon/common/userAgentPicker.go
  3. 2 0
      psiphon/meekConn.go
  4. 1 0
      psiphon/notice.go
  5. 1 1
      psiphon/serverApi.go
  6. 6 20
      psiphon/tunnel.go

+ 3 - 0
psiphon/TCPConn.go

@@ -176,6 +176,9 @@ func proxiedTcpDial(
 	dialer := func(network, addr string) (net.Conn, error) {
 		return tcpDial(addr, config, dialResult)
 	}
+
+	config.UpstreamProxyCustomHeaders, _ = common.UserAgentIfUnset(config.UpstreamProxyCustomHeaders)
+
 	upstreamDialer := upstreamproxy.NewProxyDialFunc(
 		&upstreamproxy.UpstreamProxyConfig{
 			ForwardDialFunc: dialer,

+ 20 - 0
psiphon/common/userAgentPicker.go

@@ -20,6 +20,7 @@
 package common
 
 import (
+	"net/http"
 	"sync/atomic"
 )
 
@@ -36,3 +37,22 @@ func PickUserAgent() string {
 	}
 	return ""
 }
+
+func UserAgentIfUnset(h http.Header) (http.Header, bool) {
+	internalUserAgent := false
+	if _, ok := h["User-Agent"]; !ok {
+		if h == nil {
+			h = make(map[string][]string)
+		}
+
+		if FlipCoin() {
+			h.Set("User-Agent", PickUserAgent())
+		} else {
+			h.Set("User-Agent", "")
+		}
+
+		internalUserAgent = true
+	}
+
+	return h, internalUserAgent
+}

+ 2 - 0
psiphon/meekConn.go

@@ -584,6 +584,8 @@ func (meek *MeekConn) roundTrip(sendPayload []byte) (io.ReadCloser, error) {
 
 		request.Header.Set("Content-Type", "application/octet-stream")
 
+		meek.additionalHeaders, _ = common.UserAgentIfUnset(meek.additionalHeaders)
+
 		// Set additional headers to the HTTP request using the same method we use for adding
 		// custom headers to HTTP proxy requests
 		for name, value := range meek.additionalHeaders {

+ 1 - 0
psiphon/notice.go

@@ -352,6 +352,7 @@ func NoticeConnectedTunnelDialStats(ipAddress string, tunnelDialStats *TunnelDia
 		"meekSNIServerName", tunnelDialStats.MeekSNIServerName,
 		"meekHostHeader", tunnelDialStats.MeekHostHeader,
 		"meekTransformedHostName", tunnelDialStats.MeekTransformedHostName,
+		"internalUserAgent", tunnelDialStats.InternalUserAgent,
 		"userAgent", tunnelDialStats.UserAgent)
 }
 

+ 1 - 1
psiphon/serverApi.go

@@ -834,7 +834,7 @@ func (serverContext *ServerContext) getBaseParams() requestJSONObject {
 		}
 		params["meek_transformed_host_name"] = transformedHostName
 
-		if tunnel.dialStats.HasUserAgent {
+		if tunnel.dialStats.InternalUserAgent {
 			params["user_agent"] = tunnel.dialStats.UserAgent
 		}
 	}

+ 6 - 20
psiphon/tunnel.go

@@ -103,7 +103,7 @@ type TunnelDialStats struct {
 	MeekSNIServerName              string
 	MeekHostHeader                 string
 	MeekTransformedHostName        bool
-	HasUserAgent                   bool
+	InternalUserAgent              bool
 	UserAgent                      string
 }
 
@@ -623,22 +623,8 @@ func dialSsh(
 		resolvedIPAddress.Store(IPAddress)
 	}
 
-	// If "User-Agent" is not already present in the custom headers,
-	// flip a coin, either pick a user agent or omit the header entirely.
-	hasUserAgent := false
-	if _, ok := config.UpstreamProxyCustomHeaders["User-Agent"]; !ok {
-		if config.UpstreamProxyCustomHeaders == nil {
-			config.UpstreamProxyCustomHeaders = make(map[string][]string)
-		}
-
-		if common.FlipCoin() {
-			config.UpstreamProxyCustomHeaders.Set("User-Agent", common.PickUserAgent())
-		} else {
-			config.UpstreamProxyCustomHeaders.Set("User-Agent", "")
-		}
-
-		hasUserAgent = true
-	}
+	var internalUserAgent bool
+	config.UpstreamProxyCustomHeaders, internalUserAgent = common.UserAgentIfUnset(config.UpstreamProxyCustomHeaders)
 
 	// Create the base transport: meek or direct connection
 	dialConfig := &DialConfig{
@@ -773,8 +759,8 @@ func dialSsh(
 	if dialConfig.UpstreamProxyUrl != "" || meekConfig != nil {
 		dialStats = &TunnelDialStats{}
 
-		if hasUserAgent {
-			dialStats.HasUserAgent = true
+		if internalUserAgent {
+			dialStats.InternalUserAgent = true
 			dialStats.UserAgent = dialConfig.UpstreamProxyCustomHeaders.Get("User-Agent")
 		}
 
@@ -788,7 +774,7 @@ func dialSsh(
 
 			dialStats.UpstreamProxyCustomHeaderNames = make([]string, 0)
 			for name, _ := range dialConfig.UpstreamProxyCustomHeaders {
-				if hasUserAgent && name == "User-Agent" {
+				if internalUserAgent && name == "User-Agent" {
 					continue
 				}
 				dialStats.UpstreamProxyCustomHeaderNames = append(dialStats.UpstreamProxyCustomHeaderNames, name)