sing-box/transport/v2rayhttp/client.go

158 lines
3.8 KiB
Go
Raw Normal View History

2022-08-22 22:19:25 +08:00
package v2rayhttp
import (
"context"
"io"
"math/rand"
"net"
"net/http"
"net/url"
"time"
2022-08-22 22:19:25 +08:00
"github.com/sagernet/sing-box/adapter"
2022-09-09 18:45:10 +08:00
"github.com/sagernet/sing-box/common/tls"
2022-08-22 22:19:25 +08: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"
2023-04-12 15:14:55 +08:00
sHTTP "github.com/sagernet/sing/protocol/http"
"golang.org/x/net/http2"
2022-08-22 22:19:25 +08:00
)
var _ adapter.V2RayClientTransport = (*Client)(nil)
type Client struct {
2022-08-22 22:51:08 +08:00
ctx context.Context
dialer N.Dialer
serverAddr M.Socksaddr
transport http.RoundTripper
2022-08-22 22:51:08 +08:00
http2 bool
url *url.URL
host []string
method string
headers http.Header
2022-08-22 22:19:25 +08:00
}
2023-04-12 15:14:55 +08:00
func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, options option.V2RayHTTPOptions, tlsConfig tls.Config) (adapter.V2RayClientTransport, error) {
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 {
2023-04-04 21:09:08 +08:00
if len(tlsConfig.NextProtos()) == 0 {
tlsConfig.SetNextProtos([]string{http2.NextProtoTLS})
}
transport = &http2.Transport{
ReadIdleTimeout: time.Duration(options.IdleTimeout),
PingTimeout: time.Duration(options.PingTimeout),
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 22:19:25 +08:00
client := &Client{
2022-08-22 22:51:08 +08:00
ctx: ctx,
dialer: dialer,
serverAddr: serverAddr,
host: options.Host,
method: options.Method,
2023-10-21 12:00:00 +08:00
headers: options.Headers.Build(),
transport: transport,
2022-09-09 18:45:10 +08:00
http2: tlsConfig != nil,
}
2022-08-22 22:19:25 +08: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
2023-04-12 15:14:55 +08:00
err := sHTTP.URLSetPath(&uri, options.Path)
if err != nil {
2023-11-09 16:59:44 +08:00
return nil, E.Cause(err, "parse path")
2022-08-22 22:19:25 +08:00
}
client.url = &uri
2023-04-12 15:14:55 +08:00
return client, nil
2022-08-22 22:19:25 +08:00
}
func (c *Client) DialContext(ctx context.Context) (net.Conn, error) {
2022-08-22 22:51:08 +08:00
if !c.http2 {
2022-08-26 08:41:45 +08:00
return c.dialHTTP(ctx)
2022-08-22 22:51:08 +08:00
} else {
2022-08-26 08:41:45 +08:00
return c.dialHTTP2(ctx)
2022-08-22 22:51:08 +08:00
}
}
2022-08-26 08:41:45 +08:00
func (c *Client) dialHTTP(ctx context.Context) (net.Conn, error) {
conn, err := c.dialer.DialContext(ctx, N.NetworkTCP, c.serverAddr)
2022-08-22 22:51:08 +08:00
if err != nil {
return nil, err
}
2022-08-22 22:51:08 +08:00
request := &http.Request{
Method: c.method,
URL: c.url,
Header: c.headers.Clone(),
2022-08-22 22:51:08 +08:00
}
switch hostLen := len(c.host); hostLen {
case 0:
request.Host = c.serverAddr.AddrString()
2022-08-22 22:51:08 +08:00
case 1:
request.Host = c.host[0]
default:
request.Host = c.host[rand.Intn(hostLen)]
}
return NewHTTP1Conn(conn, request), nil
2022-08-22 22:51:08 +08:00
}
2022-08-26 08:41:45 +08:00
func (c *Client) dialHTTP2(ctx context.Context) (net.Conn, error) {
2022-08-22 22:19:25 +08:00
pipeInReader, pipeInWriter := io.Pipe()
request := &http.Request{
Method: c.method,
Body: pipeInReader,
URL: c.url,
Header: c.headers.Clone(),
2022-08-22 22:19:25 +08:00
}
2022-08-26 08:41:45 +08:00
request = request.WithContext(ctx)
2022-08-22 22:19:25 +08:00
switch hostLen := len(c.host); hostLen {
case 0:
// https://github.com/v2fly/v2ray-core/blob/master/transport/internet/http/config.go#L13
request.Host = "www.example.com"
2022-08-22 22:19:25 +08:00
case 1:
request.Host = c.host[0]
default:
request.Host = c.host[rand.Intn(hostLen)]
}
2023-04-21 17:49:43 +08:00
conn := NewLateHTTPConn(pipeInWriter)
go func() {
response, err := c.transport.RoundTrip(request)
if err != nil {
2023-04-21 17:49:43 +08:00
conn.Setup(nil, err)
} else if response.StatusCode != 200 {
response.Body.Close()
2023-04-21 17:49:43 +08:00
conn.Setup(nil, E.New("unexpected status: ", response.StatusCode, " ", response.Status))
} else {
2023-04-21 17:49:43 +08:00
conn.Setup(response.Body, nil)
}
}()
return conn, nil
2022-08-22 22:19:25 +08:00
}
2023-03-30 19:06:57 +08:00
func (c *Client) Close() error {
CloseIdleConnections(c.transport)
return nil
}