add path option support for grpc
This commit is contained in:
parent
d7b56871a9
commit
818463260f
@ -21,7 +21,7 @@ func init() {
|
||||
}
|
||||
|
||||
type grpcDialer struct {
|
||||
clients map[string]pb.GostTunelClient
|
||||
clients map[string]pb.GostTunelClientX
|
||||
clientMutex sync.Mutex
|
||||
md metadata
|
||||
options dialer.Options
|
||||
@ -34,7 +34,7 @@ func NewDialer(opts ...dialer.Option) dialer.Dialer {
|
||||
}
|
||||
|
||||
return &grpcDialer{
|
||||
clients: make(map[string]pb.GostTunelClient),
|
||||
clients: make(map[string]pb.GostTunelClientX),
|
||||
options: options,
|
||||
}
|
||||
}
|
||||
@ -95,11 +95,11 @@ func (d *grpcDialer) Dial(ctx context.Context, addr string, opts ...dialer.DialO
|
||||
d.options.Logger.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
client = pb.NewGostTunelClient(cc)
|
||||
client = pb.NewGostTunelClientX(cc)
|
||||
d.clients[addr] = client
|
||||
}
|
||||
|
||||
cli, err := client.Tunnel(ctx)
|
||||
cli, err := client.TunnelX(ctx, d.md.path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -8,16 +8,19 @@ import (
|
||||
type metadata struct {
|
||||
insecure bool
|
||||
host string
|
||||
path string
|
||||
}
|
||||
|
||||
func (d *grpcDialer) parseMetadata(md mdata.Metadata) (err error) {
|
||||
const (
|
||||
insecure = "grpcInsecure"
|
||||
host = "host"
|
||||
path = "path"
|
||||
)
|
||||
|
||||
d.md.insecure = mdx.GetBool(md, insecure)
|
||||
d.md.host = mdx.GetString(md, host)
|
||||
d.md.path = mdx.GetString(md, path)
|
||||
|
||||
return
|
||||
}
|
||||
|
72
internal/util/grpc/proto/gost_grpcx.go
Normal file
72
internal/util/grpc/proto/gost_grpcx.go
Normal file
@ -0,0 +1,72 @@
|
||||
package proto
|
||||
|
||||
import (
|
||||
context "context"
|
||||
"strings"
|
||||
|
||||
grpc "google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type GostTunelClientX interface {
|
||||
TunnelX(ctx context.Context, method string, opts ...grpc.CallOption) (GostTunel_TunnelClient, error)
|
||||
}
|
||||
type gostTunelClientX struct {
|
||||
cc grpc.ClientConnInterface
|
||||
}
|
||||
|
||||
func NewGostTunelClientX(cc grpc.ClientConnInterface) GostTunelClientX {
|
||||
return &gostTunelClientX{
|
||||
cc: cc,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *gostTunelClientX) TunnelX(ctx context.Context, method string, opts ...grpc.CallOption) (GostTunel_TunnelClient, error) {
|
||||
sd := ServerDesc(method)
|
||||
method = "/" + sd.ServiceName + "/" + sd.Streams[0].StreamName
|
||||
stream, err := c.cc.NewStream(ctx, &sd.Streams[0], method, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
x := &gostTunelTunnelClient{stream}
|
||||
return x, nil
|
||||
}
|
||||
|
||||
func RegisterGostTunelServerX(s grpc.ServiceRegistrar, srv GostTunelServer, method string) {
|
||||
sd := ServerDesc(method)
|
||||
s.RegisterService(&sd, srv)
|
||||
}
|
||||
|
||||
func ServerDesc(method string) grpc.ServiceDesc {
|
||||
serviceName, streamName := parsingMethod(method)
|
||||
|
||||
return grpc.ServiceDesc{
|
||||
ServiceName: serviceName,
|
||||
HandlerType: GostTunel_ServiceDesc.HandlerType,
|
||||
Methods: GostTunel_ServiceDesc.Methods,
|
||||
Streams: []grpc.StreamDesc{
|
||||
{
|
||||
StreamName: streamName,
|
||||
Handler: GostTunel_ServiceDesc.Streams[0].Handler,
|
||||
ServerStreams: GostTunel_ServiceDesc.Streams[0].ServerStreams,
|
||||
ClientStreams: GostTunel_ServiceDesc.Streams[0].ClientStreams,
|
||||
},
|
||||
},
|
||||
Metadata: GostTunel_ServiceDesc.Metadata,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func parsingMethod(method string) (string, string) {
|
||||
serviceName := GostTunel_ServiceDesc.ServiceName
|
||||
streamName := GostTunel_ServiceDesc.Streams[0].StreamName
|
||||
v := strings.SplitN(strings.Trim(method, "/"), "/", 2)
|
||||
if len(v) == 1 && v[0] != "" {
|
||||
serviceName = v[0]
|
||||
}
|
||||
if len(v) == 2 {
|
||||
serviceName = v[0]
|
||||
streamName = strings.Replace(v[1], "/", "-", -1)
|
||||
}
|
||||
|
||||
return serviceName, streamName
|
||||
}
|
@ -66,11 +66,11 @@ func (l *grpcListener) Init(md md.Metadata) (err error) {
|
||||
l.cqueue = make(chan net.Conn, l.md.backlog)
|
||||
l.errChan = make(chan error, 1)
|
||||
|
||||
pb.RegisterGostTunelServer(l.server, &server{
|
||||
pb.RegisterGostTunelServerX(l.server, &server{
|
||||
cqueue: l.cqueue,
|
||||
localAddr: l.addr,
|
||||
logger: l.options.Logger,
|
||||
})
|
||||
}, l.md.path)
|
||||
|
||||
go func() {
|
||||
err := l.server.Serve(ln)
|
||||
|
@ -12,12 +12,14 @@ const (
|
||||
type metadata struct {
|
||||
backlog int
|
||||
insecure bool
|
||||
path string
|
||||
}
|
||||
|
||||
func (l *grpcListener) parseMetadata(md mdata.Metadata) (err error) {
|
||||
const (
|
||||
backlog = "backlog"
|
||||
insecure = "grpcInsecure"
|
||||
path = "path"
|
||||
)
|
||||
|
||||
l.md.backlog = mdx.GetInt(md, backlog)
|
||||
@ -26,5 +28,6 @@ func (l *grpcListener) parseMetadata(md mdata.Metadata) (err error) {
|
||||
}
|
||||
|
||||
l.md.insecure = mdx.GetBool(md, insecure)
|
||||
l.md.path = mdx.GetString(md, path)
|
||||
return
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user