2022-08-22 22:19:25 +08:00
|
|
|
package v2rayhttp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"math/rand"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2023-03-11 15:49:02 +08:00
|
|
|
"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"
|
2022-09-11 10:22:52 +08:00
|
|
|
|
|
|
|
"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
|
2022-09-11 10:22:52 +08:00
|
|
|
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) {
|
2022-09-11 10:22:52 +08:00
|
|
|
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})
|
|
|
|
}
|
2022-09-11 10:22:52 +08:00
|
|
|
transport = &http2.Transport{
|
2023-03-11 15:49:02 +08:00
|
|
|
ReadIdleTimeout: time.Duration(options.IdleTimeout),
|
|
|
|
PingTimeout: time.Duration(options.PingTimeout),
|
2022-09-11 10:22:52 +08:00
|
|
|
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(),
|
2022-09-11 10:22:52 +08:00
|
|
|
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 {
|
|
|
|
return nil, E.New("failed to set path: " + err.Error())
|
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) {
|
2023-04-07 18:20:07 +08:00
|
|
|
conn, err := c.dialer.DialContext(ctx, N.NetworkTCP, c.serverAddr)
|
2022-08-22 22:51:08 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-07 18:20:07 +08:00
|
|
|
|
2022-08-22 22:51:08 +08:00
|
|
|
request := &http.Request{
|
2023-04-07 18:20:07 +08:00
|
|
|
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:
|
2023-03-25 18:19:56 +08:00
|
|
|
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)]
|
|
|
|
}
|
2023-04-07 18:20:07 +08:00
|
|
|
|
|
|
|
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{
|
2023-04-07 18:20:07 +08:00
|
|
|
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:
|
2023-03-25 18:19:56 +08:00
|
|
|
// 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)
|
2022-09-11 10:22:52 +08:00
|
|
|
go func() {
|
|
|
|
response, err := c.transport.RoundTrip(request)
|
|
|
|
if err != nil {
|
2023-04-21 17:49:43 +08:00
|
|
|
conn.Setup(nil, err)
|
2022-09-11 10:22:52 +08:00
|
|
|
} else if response.StatusCode != 200 {
|
2023-04-21 17:44:05 +08:00
|
|
|
response.Body.Close()
|
2023-04-21 17:49:43 +08:00
|
|
|
conn.Setup(nil, E.New("unexpected status: ", response.StatusCode, " ", response.Status))
|
2022-09-11 10:22:52 +08:00
|
|
|
} else {
|
2023-04-21 17:49:43 +08:00
|
|
|
conn.Setup(response.Body, nil)
|
2022-09-11 10:22:52 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
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
|
|
|
|
}
|