-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
There's an [open thread](refraction-networking/utls#16) in uTLS about dial support for http/2 servers. The tldr is that by returning a utls.Connection (versus a tls.Connection ) in the dial function it fails the test specified in [root handler](https://github.com/golang/go/blob/ea1437a8cdf6bb3c2d2447833a5d06dbd75f7ae4/src/net/http/transport.go#L1496) and therefore doesn't keep track of the connection state. This results in the regular connection attempting to request http/2 connection through an http/1 protocol. We work around this through meek's re-implementation of the roundtrip handler that dynamically routes between http/1 and http/2.
- Loading branch information
1 parent
5ab2612
commit c278bc8
Showing
32 changed files
with
2,282 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
proxy_benchmarks/assets/proxies/gomitmproxy-mimic/proxy/transport.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/* | ||
* Copyright (c) 2019 Yawning Angel <yawning at schwanenlied dot me> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package gomitmproxy | ||
|
||
import ( | ||
"crypto/tls" | ||
"errors" | ||
"fmt" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
"sync" | ||
|
||
utls "github.com/refraction-networking/utls" | ||
"golang.org/x/net/http2" | ||
) | ||
|
||
var errProtocolNegotiated = errors.New("meek_lite: protocol negotiated") | ||
|
||
type roundTripper struct { | ||
sync.Mutex | ||
|
||
transport http.RoundTripper | ||
|
||
initConn net.Conn | ||
} | ||
|
||
func (rt *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
// Note: This isn't protected with a lock, since the meeklite ioWorker | ||
// serializes RoundTripper requests. | ||
// | ||
// This also assumes that req.URL.Host will remain constant for the | ||
// lifetime of the roundTripper, which is a valid assumption for meeklite. | ||
if rt.transport == nil { | ||
if err := rt.getTransport(req); err != nil { | ||
return nil, err | ||
} | ||
} | ||
return rt.transport.RoundTrip(req) | ||
} | ||
|
||
func (rt *roundTripper) getTransport(req *http.Request) error { | ||
switch strings.ToLower(req.URL.Scheme) { | ||
case "http": | ||
rt.transport = &http.Transport{Dial: net.Dial} | ||
return nil | ||
case "https": | ||
default: | ||
return fmt.Errorf("meek_lite: invalid URL scheme: '%v'", req.URL.Scheme) | ||
} | ||
|
||
_, err := rt.dialTLS("tcp", getDialTLSAddr(req.URL)) | ||
switch err { | ||
case errProtocolNegotiated: | ||
case nil: | ||
// Should never happen. | ||
panic("meek_lite: dialTLS returned no error when determining transport") | ||
default: | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (rt *roundTripper) dialTLS(network, addr string) (net.Conn, error) { | ||
// Unlike rt.transport, this is protected by a critical section | ||
// since past the initial manual call from getTransport, the HTTP | ||
// client will be the caller. | ||
rt.Lock() | ||
defer rt.Unlock() | ||
|
||
// If we have the connection from when we determined the HTTPS | ||
// transport to use, return that. | ||
if conn := rt.initConn; conn != nil { | ||
rt.initConn = nil | ||
return conn, nil | ||
} | ||
|
||
rawConn, err := net.Dial(network, addr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var host string | ||
if host, _, err = net.SplitHostPort(addr); err != nil { | ||
host = addr | ||
} | ||
|
||
// TODO: Make this configurable. What "works" is host dependent. | ||
// * HelloChrome_Auto - Failures in a stand alone testcase against google.com | ||
// * HelloFirefox_Auto - Fails with the azure bridge, incompatible group. | ||
// * HelloIOS_Auto - Seems to work. | ||
// | ||
// Since HelloChrome_Auto works with azure, that's what'll be used for | ||
// now, since that's what the overwelming vast majority of people will | ||
// use. | ||
conn := utls.UClient(rawConn, &utls.Config{ServerName: host}, utls.HelloChrome_Auto) | ||
if err = conn.Handshake(); err != nil { | ||
conn.Close() | ||
return nil, err | ||
} | ||
|
||
if rt.transport != nil { | ||
return conn, nil | ||
} | ||
|
||
// No http.Transport constructed yet, create one based on the results | ||
// of ALPN. | ||
switch conn.ConnectionState().NegotiatedProtocol { | ||
case http2.NextProtoTLS: | ||
// The remote peer is speaking HTTP 2 + TLS. | ||
rt.transport = &http2.Transport{DialTLS: rt.dialTLSHTTP2} | ||
default: | ||
// Assume the remote peer is speaking HTTP 1.x + TLS. | ||
rt.transport = &http.Transport{DialTLS: rt.dialTLS} | ||
} | ||
|
||
// Stash the connection just established for use servicing the | ||
// actual request (should be near-immediate). | ||
rt.initConn = conn | ||
|
||
return nil, errProtocolNegotiated | ||
} | ||
|
||
func (rt *roundTripper) dialTLSHTTP2(network, addr string, cfg *tls.Config) (net.Conn, error) { | ||
return rt.dialTLS(network, addr) | ||
} | ||
|
||
func getDialTLSAddr(u *url.URL) string { | ||
host, port, err := net.SplitHostPort(u.Host) | ||
if err == nil { | ||
return net.JoinHostPort(host, port) | ||
} | ||
|
||
return net.JoinHostPort(u.Host, u.Scheme) | ||
} | ||
|
||
func newRoundTripper() http.RoundTripper { | ||
return &roundTripper{} | ||
} | ||
|
||
func init() { | ||
// Attempt to increase compatibility, there's an encrypted link | ||
// underneath, and this doesn't (shouldn't) affect the external | ||
// fingerprint. | ||
utls.EnableWeakCiphers() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.