2018-03-01 07:16:52 -05:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
gotls "crypto/tls"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"golang.org/x/net/http2"
|
|
|
|
"v2ray.com/core/common"
|
2018-04-16 08:57:13 -04:00
|
|
|
"v2ray.com/core/common/buf"
|
2018-03-01 07:16:52 -05:00
|
|
|
"v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/transport/internet"
|
|
|
|
"v2ray.com/core/transport/internet/tls"
|
2018-04-16 08:57:13 -04:00
|
|
|
"v2ray.com/core/transport/pipe"
|
2018-03-01 07:16:52 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-03-21 18:28:28 -04:00
|
|
|
globalDialerMap map[net.Destination]*http.Client
|
2018-03-01 07:16:52 -05:00
|
|
|
globalDailerAccess sync.Mutex
|
|
|
|
)
|
|
|
|
|
|
|
|
func getHTTPClient(ctx context.Context, dest net.Destination) (*http.Client, error) {
|
|
|
|
globalDailerAccess.Lock()
|
|
|
|
defer globalDailerAccess.Unlock()
|
|
|
|
|
2018-03-21 18:28:28 -04:00
|
|
|
if globalDialerMap == nil {
|
|
|
|
globalDialerMap = make(map[net.Destination]*http.Client)
|
|
|
|
}
|
|
|
|
|
2018-03-01 07:16:52 -05:00
|
|
|
if client, found := globalDialerMap[dest]; found {
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
config := tls.ConfigFromContext(ctx)
|
|
|
|
if config == nil {
|
|
|
|
return nil, newError("TLS must be enabled for http transport.").AtWarning()
|
|
|
|
}
|
|
|
|
|
|
|
|
transport := &http2.Transport{
|
|
|
|
DialTLS: func(network string, addr string, tlsConfig *gotls.Config) (net.Conn, error) {
|
|
|
|
rawHost, rawPort, err := net.SplitHostPort(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(rawPort) == 0 {
|
|
|
|
rawPort = "443"
|
|
|
|
}
|
|
|
|
port, err := net.PortFromString(rawPort)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
address := net.ParseAddress(rawHost)
|
|
|
|
|
|
|
|
pconn, err := internet.DialSystem(context.Background(), nil, net.TCPDestination(address, port))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return gotls.Client(pconn, tlsConfig), nil
|
|
|
|
},
|
|
|
|
TLSClientConfig: config.GetTLSConfig(tls.WithDestination(dest), tls.WithNextProto("h2")),
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
}
|
|
|
|
|
|
|
|
globalDialerMap[dest] = client
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dial dials a new TCP connection to the given destination.
|
|
|
|
func Dial(ctx context.Context, dest net.Destination) (internet.Connection, error) {
|
2018-03-01 08:22:33 -05:00
|
|
|
rawSettings := internet.TransportSettingsFromContext(ctx)
|
|
|
|
httpSettings, ok := rawSettings.(*Config)
|
|
|
|
if !ok {
|
|
|
|
return nil, newError("HTTP config is not set.").AtError()
|
|
|
|
}
|
|
|
|
|
2018-03-01 07:16:52 -05:00
|
|
|
client, err := getHTTPClient(ctx, dest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-16 08:57:13 -04:00
|
|
|
preader, pwriter := pipe.New(pipe.WithSizeLimit(20 * 1024))
|
2018-04-20 18:54:53 -04:00
|
|
|
breader := &buf.BufferedReader{Reader: preader}
|
2018-03-01 07:16:52 -05:00
|
|
|
request := &http.Request{
|
|
|
|
Method: "PUT",
|
2018-03-01 08:22:33 -05:00
|
|
|
Host: httpSettings.getRandomHost(),
|
2018-04-20 18:54:53 -04:00
|
|
|
Body: breader,
|
2018-03-01 07:16:52 -05:00
|
|
|
URL: &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: dest.NetAddr(),
|
2018-03-01 08:22:33 -05:00
|
|
|
Path: httpSettings.getNormalizedPath(),
|
2018-03-01 07:16:52 -05:00
|
|
|
},
|
|
|
|
Proto: "HTTP/2",
|
|
|
|
ProtoMajor: 2,
|
|
|
|
ProtoMinor: 0,
|
2018-04-20 10:10:32 -04:00
|
|
|
Header: make(http.Header),
|
2018-03-01 07:16:52 -05:00
|
|
|
}
|
2018-04-20 10:10:32 -04:00
|
|
|
// Disable any compression method from server.
|
|
|
|
request.Header.Set("Accept-Encoding", "identity")
|
|
|
|
|
2018-03-01 07:16:52 -05:00
|
|
|
response, err := client.Do(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to dial to ", dest).Base(err).AtWarning()
|
|
|
|
}
|
|
|
|
if response.StatusCode != 200 {
|
|
|
|
return nil, newError("unexpected status", response.StatusCode).AtWarning()
|
|
|
|
}
|
|
|
|
|
2018-04-16 08:57:13 -04:00
|
|
|
bwriter := buf.NewBufferedWriter(pwriter)
|
|
|
|
common.Must(bwriter.SetBuffered(false))
|
2018-04-20 15:30:58 -04:00
|
|
|
return net.NewConnection(
|
|
|
|
net.ConnectionOutput(response.Body),
|
|
|
|
net.ConnectionInput(bwriter),
|
|
|
|
net.ConnectionOnClose(common.NewChainedClosable(breader, bwriter, response.Body)),
|
|
|
|
), nil
|
2018-03-01 07:16:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
common.Must(internet.RegisterTransportDialer(internet.TransportProtocol_HTTP, Dial))
|
|
|
|
}
|