sing-box/transport/v2raygrpc/client.go

120 lines
3.3 KiB
Go
Raw Normal View History

2022-08-22 10:53:47 +00:00
package v2raygrpc
import (
"context"
"net"
"sync"
"time"
"github.com/sagernet/sing-box/adapter"
2022-09-09 10:45:10 +00:00
"github.com/sagernet/sing-box/common/tls"
2022-08-22 12:20:56 +00:00
"github.com/sagernet/sing-box/option"
2022-08-22 10:53:47 +00:00
"github.com/sagernet/sing/common"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
2022-11-09 03:49:01 +00:00
"golang.org/x/net/http2"
2022-08-22 10:53:47 +00:00
"google.golang.org/grpc"
"google.golang.org/grpc/backoff"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/keepalive"
2022-08-22 10:53:47 +00:00
)
var _ adapter.V2RayClientTransport = (*Client)(nil)
type Client struct {
ctx context.Context
dialer N.Dialer
serverAddr string
serviceName string
dialOptions []grpc.DialOption
conn *grpc.ClientConn
connAccess sync.Mutex
}
2022-09-09 10:45:10 +00:00
func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, options option.V2RayGRPCOptions, tlsConfig tls.Config) (adapter.V2RayClientTransport, error) {
2022-08-22 10:53:47 +00:00
var dialOptions []grpc.DialOption
if tlsConfig != nil {
2023-04-04 13:09:08 +00:00
if len(tlsConfig.NextProtos()) == 0 {
tlsConfig.SetNextProtos([]string{http2.NextProtoTLS})
}
dialOptions = append(dialOptions, grpc.WithTransportCredentials(NewTLSTransportCredentials(tlsConfig)))
2022-08-22 10:53:47 +00:00
} else {
dialOptions = append(dialOptions, grpc.WithTransportCredentials(insecure.NewCredentials()))
}
if options.IdleTimeout > 0 {
dialOptions = append(dialOptions, grpc.WithKeepaliveParams(keepalive.ClientParameters{
Time: time.Duration(options.IdleTimeout),
Timeout: time.Duration(options.PingTimeout),
PermitWithoutStream: options.PermitWithoutStream,
}))
}
2022-08-22 10:53:47 +00:00
dialOptions = append(dialOptions, grpc.WithConnectParams(grpc.ConnectParams{
Backoff: backoff.Config{
BaseDelay: 500 * time.Millisecond,
Multiplier: 1.5,
Jitter: 0.2,
MaxDelay: 19 * time.Second,
},
MinConnectTimeout: 5 * time.Second,
}))
dialOptions = append(dialOptions, grpc.WithContextDialer(func(ctx context.Context, server string) (net.Conn, error) {
return dialer.DialContext(ctx, N.NetworkTCP, M.ParseSocksaddr(server))
}))
dialOptions = append(dialOptions, grpc.WithReturnConnectionError())
return &Client{
ctx: ctx,
dialer: dialer,
serverAddr: serverAddr.String(),
2022-08-22 12:20:56 +00:00
serviceName: options.ServiceName,
2022-08-22 10:53:47 +00:00
dialOptions: dialOptions,
2022-09-09 10:45:10 +00:00
}, nil
2022-08-22 10:53:47 +00:00
}
func (c *Client) connect() (*grpc.ClientConn, error) {
conn := c.conn
if conn != nil && conn.GetState() != connectivity.Shutdown {
return conn, nil
}
c.connAccess.Lock()
defer c.connAccess.Unlock()
conn = c.conn
if conn != nil && conn.GetState() != connectivity.Shutdown {
return conn, nil
}
2024-04-06 15:03:24 +00:00
//nolint:staticcheck
//goland:noinspection GoDeprecation
2022-08-22 10:53:47 +00:00
conn, err := grpc.DialContext(c.ctx, c.serverAddr, c.dialOptions...)
if err != nil {
return nil, err
}
c.conn = conn
return conn, nil
}
func (c *Client) DialContext(ctx context.Context) (net.Conn, error) {
clientConn, err := c.connect()
if err != nil {
return nil, err
}
client := NewGunServiceClient(clientConn).(GunServiceCustomNameClient)
2023-04-10 05:00:57 +00:00
ctx, cancel := common.ContextWithCancelCause(ctx)
2022-08-22 10:53:47 +00:00
stream, err := client.TunCustomName(ctx, c.serviceName)
if err != nil {
2023-04-10 05:00:57 +00:00
cancel(err)
2022-08-22 10:53:47 +00:00
return nil, err
}
2024-10-21 15:38:34 +00:00
return NewGRPCConn(stream), nil
2022-08-22 10:53:47 +00:00
}
2024-07-07 07:45:50 +00:00
func (c *Client) Close() error {
c.connAccess.Lock()
defer c.connAccess.Unlock()
if c.conn != nil {
c.conn.Close()
c.conn = nil
}
return nil
}