sing-box/transport/v2rayhttp/client.go

164 lines
3.9 KiB
Go
Raw Normal View History

2022-08-22 14:19:25 +00:00
package v2rayhttp
import (
2022-08-22 14:51:08 +00:00
"bufio"
2022-08-22 14:19:25 +00:00
"context"
"io"
"math/rand"
"net"
"net/http"
"net/url"
"strings"
"github.com/sagernet/sing-box/adapter"
2022-09-09 10:45:10 +00:00
"github.com/sagernet/sing-box/common/tls"
2022-08-22 14:19:25 +00:00
"github.com/sagernet/sing-box/option"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
"golang.org/x/net/http2"
2022-08-22 14:19:25 +00:00
)
var _ adapter.V2RayClientTransport = (*Client)(nil)
type Client struct {
2022-08-22 14:51:08 +00:00
ctx context.Context
dialer N.Dialer
serverAddr M.Socksaddr
transport http.RoundTripper
2022-08-22 14:51:08 +00:00
http2 bool
url *url.URL
host []string
method string
headers http.Header
2022-08-22 14:19:25 +00:00
}
2022-09-09 10:45:10 +00:00
func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, options option.V2RayHTTPOptions, tlsConfig tls.Config) adapter.V2RayClientTransport {
var transport http.RoundTripper
if tlsConfig == nil {
transport = &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return dialer.DialContext(ctx, network, M.ParseSocksaddr(addr))
},
}
} else {
tlsConfig.SetNextProtos([]string{http2.NextProtoTLS})
transport = &http2.Transport{
DialTLSContext: func(ctx context.Context, network, addr string, cfg *tls.STDConfig) (net.Conn, error) {
conn, err := dialer.DialContext(ctx, network, M.ParseSocksaddr(addr))
if err != nil {
return nil, err
}
return tls.ClientHandshake(ctx, conn, tlsConfig)
},
}
}
2022-08-22 14:19:25 +00:00
client := &Client{
2022-08-22 14:51:08 +00:00
ctx: ctx,
dialer: dialer,
serverAddr: serverAddr,
host: options.Host,
method: options.Method,
headers: make(http.Header),
transport: transport,
2022-09-09 10:45:10 +00:00
http2: tlsConfig != nil,
}
2022-08-22 14:19:25 +00:00
if client.method == "" {
client.method = "PUT"
}
var uri url.URL
if tlsConfig == nil {
uri.Scheme = "http"
} else {
uri.Scheme = "https"
}
uri.Host = serverAddr.String()
uri.Path = options.Path
if !strings.HasPrefix(uri.Path, "/") {
uri.Path = "/" + uri.Path
}
for key, value := range options.Headers {
client.headers.Set(key, value)
}
client.url = &uri
return client
}
func (c *Client) DialContext(ctx context.Context) (net.Conn, error) {
2022-08-22 14:51:08 +00:00
if !c.http2 {
2022-08-26 00:41:45 +00:00
return c.dialHTTP(ctx)
2022-08-22 14:51:08 +00:00
} else {
2022-08-26 00:41:45 +00:00
return c.dialHTTP2(ctx)
2022-08-22 14:51:08 +00:00
}
}
2022-08-26 00:41:45 +00:00
func (c *Client) dialHTTP(ctx context.Context) (net.Conn, error) {
2022-08-22 14:51:08 +00:00
conn, err := c.dialer.DialContext(c.ctx, N.NetworkTCP, c.serverAddr)
if err != nil {
return nil, err
}
request := &http.Request{
Method: c.method,
URL: c.url,
ProtoMajor: 1,
Proto: "HTTP/1.1",
Header: c.headers.Clone(),
}
2022-08-26 00:41:45 +00:00
request = request.WithContext(ctx)
2022-08-22 14:51:08 +00:00
switch hostLen := len(c.host); hostLen {
case 0:
case 1:
request.Host = c.host[0]
default:
request.Host = c.host[rand.Intn(hostLen)]
}
err = request.Write(conn)
if err != nil {
return nil, err
}
reader := bufio.NewReader(conn)
response, err := http.ReadResponse(reader, request)
if err != nil {
return nil, err
}
if response.StatusCode != 200 {
return nil, E.New("unexpected status: ", response.Status)
}
return conn, nil
}
2022-08-26 00:41:45 +00:00
func (c *Client) dialHTTP2(ctx context.Context) (net.Conn, error) {
2022-08-22 14:19:25 +00:00
pipeInReader, pipeInWriter := io.Pipe()
request := &http.Request{
Method: c.method,
Body: pipeInReader,
URL: c.url,
ProtoMajor: 2,
Proto: "HTTP/2",
Header: c.headers.Clone(),
}
2022-08-26 00:41:45 +00:00
request = request.WithContext(ctx)
2022-08-22 14:19:25 +00:00
switch hostLen := len(c.host); hostLen {
case 0:
case 1:
request.Host = c.host[0]
default:
request.Host = c.host[rand.Intn(hostLen)]
}
// Disable any compression method from server.
request.Header.Set("Accept-Encoding", "identity")
conn := newLateHTTPConn(pipeInWriter)
go func() {
response, err := c.transport.RoundTrip(request)
if err != nil {
conn.setup(nil, err)
} else if response.StatusCode != 200 {
conn.setup(nil, E.New("unexpected status: ", response.StatusCode, " ", response.Status))
} else {
conn.setup(response.Body, nil)
}
}()
return conn, nil
2022-08-22 14:19:25 +00:00
}