1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/transport/internet/grpc/dial.go

123 lines
3.6 KiB
Go
Raw Normal View History

//go:build !confonly
// +build !confonly
package grpc
import (
"context"
gonet "net"
"sync"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/backoff"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials"
2024-09-06 11:07:27 -04:00
"google.golang.org/grpc/credentials/insecure"
core "github.com/v2fly/v2ray-core/v5"
"github.com/v2fly/v2ray-core/v5/common"
"github.com/v2fly/v2ray-core/v5/common/net"
"github.com/v2fly/v2ray-core/v5/common/session"
"github.com/v2fly/v2ray-core/v5/transport/internet"
"github.com/v2fly/v2ray-core/v5/transport/internet/grpc/encoding"
"github.com/v2fly/v2ray-core/v5/transport/internet/tls"
)
func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (internet.Connection, error) {
newError("creating connection to ", dest).WriteToLog(session.ExportIDToError(ctx))
conn, err := dialgRPC(ctx, dest, streamSettings)
if err != nil {
return nil, newError("failed to dial Grpc").Base(err)
}
return internet.Connection(conn), nil
}
func init() {
common.Must(internet.RegisterTransportDialer(protocolName, Dial))
}
2021-07-01 13:58:13 -04:00
type dialerCanceller func()
var (
globalDialerMap map[net.Destination]*grpc.ClientConn
globalDialerAccess sync.Mutex
)
func dialgRPC(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (net.Conn, error) {
grpcSettings := streamSettings.ProtocolSettings.(*Config)
config := tls.ConfigFromStreamSettings(streamSettings)
2024-09-06 11:07:27 -04:00
transportCredentials := insecure.NewCredentials()
if config != nil {
transportCredentials = credentials.NewTLS(config.GetTLSConfig(tls.WithDestination(dest)))
}
2024-09-06 11:07:27 -04:00
dialOption := grpc.WithTransportCredentials(transportCredentials)
conn, canceller, err := getGrpcClient(ctx, dest, dialOption, streamSettings)
if err != nil {
return nil, newError("Cannot dial grpc").Base(err)
}
client := encoding.NewGunServiceClient(conn)
gunService, err := client.(encoding.GunServiceClientX).TunCustomName(ctx, grpcSettings.ServiceName)
if err != nil {
2021-07-01 13:58:13 -04:00
canceller()
return nil, newError("Cannot dial grpc").Base(err)
}
return encoding.NewGunConn(gunService, nil), nil
}
func getGrpcClient(ctx context.Context, dest net.Destination, dialOption grpc.DialOption, streamSettings *internet.MemoryStreamConfig) (*grpc.ClientConn, dialerCanceller, error) {
globalDialerAccess.Lock()
defer globalDialerAccess.Unlock()
if globalDialerMap == nil {
globalDialerMap = make(map[net.Destination]*grpc.ClientConn)
}
2021-07-01 13:58:13 -04:00
canceller := func() {
globalDialerAccess.Lock()
defer globalDialerAccess.Unlock()
delete(globalDialerMap, dest)
}
2021-05-09 10:16:42 -04:00
// TODO Should support chain proxy to the same destination
if client, found := globalDialerMap[dest]; found && client.GetState() != connectivity.Shutdown {
2021-07-01 13:58:13 -04:00
return client, canceller, nil
}
conn, err := grpc.Dial(
dest.Address.String()+":"+dest.Port.String(),
dialOption,
grpc.WithConnectParams(grpc.ConnectParams{
Backoff: backoff.Config{
BaseDelay: 500 * time.Millisecond,
Multiplier: 1.5,
Jitter: 0.2,
2021-03-16 08:49:23 -04:00
MaxDelay: 19 * time.Second,
},
MinConnectTimeout: 5 * time.Second,
}),
2021-05-09 10:13:18 -04:00
grpc.WithContextDialer(func(ctxGrpc context.Context, s string) (gonet.Conn, error) {
rawHost, rawPort, err := net.SplitHostPort(s)
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)
detachedContext := core.ToBackgroundDetachedContext(ctx)
return internet.DialSystem(detachedContext, net.TCPDestination(address, port), streamSettings.SocketSettings)
}),
)
globalDialerMap[dest] = conn
2021-07-01 13:58:13 -04:00
return conn, canceller, err
}