sing-box/transport/v2raygrpclite/client.go

110 lines
2.8 KiB
Go
Raw Normal View History

2022-08-27 21:05:15 +08:00
package v2raygrpclite
import (
"context"
"io"
"net"
"net/http"
"net/url"
"time"
2022-08-27 21:05:15 +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-27 21:05:15 +08:00
"github.com/sagernet/sing-box/option"
2023-03-30 19:06:57 +08:00
"github.com/sagernet/sing-box/transport/v2rayhttp"
2022-08-27 21:05:15 +08:00
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
"golang.org/x/net/http2"
2022-08-27 21:05:15 +08:00
)
var _ adapter.V2RayClientTransport = (*Client)(nil)
var defaultClientHeader = http.Header{
"Content-Type": []string{"application/grpc"},
"User-Agent": []string{"grpc-go/1.48.0"},
"TE": []string{"trailers"},
}
type Client struct {
ctx context.Context
dialer N.Dialer
serverAddr M.Socksaddr
2023-04-04 21:09:08 +08:00
transport *http2.Transport
2022-08-27 21:05:15 +08:00
options option.V2RayGRPCOptions
url *url.URL
}
2022-09-09 18:45:10 +08:00
func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, options option.V2RayGRPCOptions, tlsConfig tls.Config) adapter.V2RayClientTransport {
var host string
if tlsConfig != nil && tlsConfig.ServerName() != "" {
host = M.ParseSocksaddrHostPort(tlsConfig.ServerName(), serverAddr.Port).String()
} else {
host = serverAddr.String()
}
2023-04-04 21:09:08 +08:00
client := &Client{
ctx: ctx,
dialer: dialer,
serverAddr: serverAddr,
options: options,
2023-04-04 21:09:08 +08:00
transport: &http2.Transport{
ReadIdleTimeout: time.Duration(options.IdleTimeout),
PingTimeout: time.Duration(options.PingTimeout),
DisableCompression: true,
},
2022-08-27 21:05:15 +08:00
url: &url.URL{
2023-04-08 17:44:11 +08:00
Scheme: "https",
Host: host,
2023-04-08 17:44:11 +08:00
Path: "/" + options.ServiceName + "/Tun",
RawPath: "/" + url.PathEscape(options.ServiceName) + "/Tun",
2022-08-27 21:05:15 +08:00
},
}
2023-04-04 21:09:08 +08:00
if tlsConfig == nil {
client.transport.DialTLSContext = func(ctx context.Context, network, addr string, cfg *tls.STDConfig) (net.Conn, error) {
return dialer.DialContext(ctx, network, M.ParseSocksaddr(addr))
}
} else {
if len(tlsConfig.NextProtos()) == 0 {
tlsConfig.SetNextProtos([]string{http2.NextProtoTLS})
}
client.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)
}
}
return client
2022-08-27 21:05:15 +08:00
}
func (c *Client) DialContext(ctx context.Context) (net.Conn, error) {
pipeInReader, pipeInWriter := io.Pipe()
request := &http.Request{
2023-04-04 21:09:08 +08:00
Method: http.MethodPost,
Body: pipeInReader,
URL: c.url,
Header: defaultClientHeader,
2022-08-27 21:05:15 +08:00
}
request = request.WithContext(ctx)
conn := newLateGunConn(pipeInWriter)
go func() {
response, err := c.transport.RoundTrip(request)
if err == nil {
conn.setup(response.Body, nil)
} else {
conn.setup(nil, err)
}
}()
return conn, nil
}
2023-03-30 19:06:57 +08:00
func (c *Client) Close() error {
2023-04-04 21:09:08 +08:00
if c.transport != nil {
v2rayhttp.CloseIdleConnections(c.transport)
}
2023-03-30 19:06:57 +08:00
return nil
}