grpc

package
v1.2.4-beta.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 22, 2022 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package grpc implements methods for grpc tunnel.

ProtoBuf

从stream.proto 生成 stream.pb.go 和 stream_grpc.pb.go:

protoc --go_out=. --go_opt=paths=source_relative \
	--go-grpc_out=. --go-grpc_opt=paths=source_relative \
	stream.proto

stream.pb.go 可以无视, 是数据编码; 主要观察 stream_grpc.pb.go .

Naming of gun

这里参考v2ray/xray, 它们的 Tun的意思应该是 network tunnel, 没啥的. GunService 看不懂, 可能是 "gprc tunnel"的简写吧。但是就算简写也要是 gTun。毕竟gun谐音不好。

查看一下v2ray的合并grpc功能的pr, https://github.com/v2fly/v2ray-core/pull/757

是来自 Qv2ray/gun 这个库;总之也一样,那个库的起名还是很怪。

因为我们使用自定义ServiceName的方法,所以 GunService的名字无所谓,可以随便改.

直接把 GunService名称改成了 “Stream”,不影响的。反正我们自定义内部真实名称.

实测 是不影响的, 一样兼容xray/v2ray的 grpc, 因为 我们自定义实际的 ServiceDesc.

Client Methods

建立新客户端连接的调用过程:

先用 GetEstablishedConnFor 看看有没有已存在的 clientConn

没有 已存在的 时,自己先拨号tcp,然后拨号tls,然后把tls连接 传递给 ClientHandshake, 生成一个 clientConn

然后把获取到的 clientConn传递给 DialNewSubConn, 获取可用的一条 grpc 连接

Index

Constants

This section is empty.

Variables

View Source
var File_stream_proto protoreflect.FileDescriptor
View Source
var Stream_ServiceDesc = grpc.ServiceDesc{
	ServiceName: "v2ray_simple.grpc.Stream",
	HandlerType: (*StreamServer)(nil),
	Methods:     []grpc.MethodDesc{},
	Streams: []grpc.StreamDesc{
		{
			StreamName:    "Tun",
			Handler:       _Stream_Tun_Handler,
			ServerStreams: true,
			ClientStreams: true,
		},
		{
			StreamName:    "TunMulti",
			Handler:       _Stream_TunMulti_Handler,
			ServerStreams: true,
			ClientStreams: true,
		},
	},
	Metadata: "stream.proto",
}

Stream_ServiceDesc is the grpc.ServiceDesc for Stream service. It's only intended for direct use with grpc.RegisterService, and not to be introspected or modified (even as a copy)

Functions

func DialNewSubConn

func DialNewSubConn(path string, clientconn ClientConn, addr *netLayer.Addr, isMulti bool) (net.Conn, error)

在一个已存在的grpc连接中 进行新的子连接申请

func RegisterStreamServer

func RegisterStreamServer(s grpc.ServiceRegistrar, srv StreamServer)

Types

type Client

type Client struct {
	Creator
	ServerAddr netLayer.Addr
	Path       string
	// contains filtered or unexported fields
}

implements advLayer.MuxClient

func NewClient

func NewClient(addr netLayer.Addr, path string, ismulti bool) (*Client, error)

func (*Client) DialSubConn

func (c *Client) DialSubConn(underlay any) (net.Conn, error)

func (*Client) GetCommonConn

func (c *Client) GetCommonConn(underlay net.Conn) (any, error)

func (*Client) GetPath

func (c *Client) GetPath() string

func (*Client) IsEarly

func (c *Client) IsEarly() bool

type ClientConn

type ClientConn *grpc.ClientConn

func ClientHandshake

func ClientHandshake(underlay net.Conn, addr *netLayer.Addr) (ClientConn, error)

ClientHandshake 在客户端被调用, 将一个普通连接升级为 grpc连接。 该 underlay一般为 tls连接。 addr为实际的远程地址,我们不从 underlay里获取addr,避免转换.

func GetEstablishedConnFor

func GetEstablishedConnFor(addr *netLayer.Addr) ClientConn

获取与 某grpc服务器的 已存在的grpc连接

type Conn

type Conn struct {
	netLayer.EasyDeadline
	// contains filtered or unexported fields
}

Conn implements net.Conn

func (*Conn) Close

func (c *Conn) Close() error

func (*Conn) LocalAddr

func (c *Conn) LocalAddr() net.Addr

func (*Conn) Read

func (c *Conn) Read(b []byte) (n int, err error)

func (*Conn) RemoteAddr

func (c *Conn) RemoteAddr() net.Addr

func (*Conn) Write

func (c *Conn) Write(b []byte) (n int, err error)

type Creator

type Creator struct{}

func (Creator) CanHandleHeaders added in v1.2.0

func (Creator) CanHandleHeaders() bool

func (Creator) GetDefaultAlpn

func (Creator) GetDefaultAlpn() (alpn string, mustUse bool)

func (Creator) IsMux added in v1.2.0

func (Creator) IsMux() bool

func (Creator) IsSuper added in v1.2.0

func (Creator) IsSuper() bool

func (Creator) NewClientFromConf

func (Creator) NewClientFromConf(conf *advLayer.Conf) (advLayer.Client, error)

func (Creator) NewServerFromConf

func (Creator) NewServerFromConf(conf *advLayer.Conf) (advLayer.Server, error)

func (Creator) PackageID

func (Creator) PackageID() string

func (Creator) ProtocolName added in v1.2.0

func (Creator) ProtocolName() string

type Hunk

type Hunk struct {
	Data []byte `protobuf:"bytes,1,opt,name=data,proto3" json:"data,omitempty"`
	// contains filtered or unexported fields
}

func (*Hunk) Descriptor deprecated

func (*Hunk) Descriptor() ([]byte, []int)

Deprecated: Use Hunk.ProtoReflect.Descriptor instead.

func (*Hunk) GetData

func (x *Hunk) GetData() []byte

func (*Hunk) ProtoMessage

func (*Hunk) ProtoMessage()

func (*Hunk) ProtoReflect

func (x *Hunk) ProtoReflect() protoreflect.Message

func (*Hunk) Reset

func (x *Hunk) Reset()

func (*Hunk) String

func (x *Hunk) String() string

type MultiConn

type MultiConn struct {
	// contains filtered or unexported fields
}

实现 net.Conn 和 utils.MultiReader

func (*MultiConn) Close

func (c *MultiConn) Close() error

func (*MultiConn) LocalAddr

func (c *MultiConn) LocalAddr() net.Addr

func (*MultiConn) Read

func (c *MultiConn) Read(b []byte) (n int, err error)

func (*MultiConn) ReadBuffers

func (c *MultiConn) ReadBuffers() (bs [][]byte, err error)

func (*MultiConn) RemoteAddr

func (c *MultiConn) RemoteAddr() net.Addr

func (*MultiConn) SetDeadline

func (*MultiConn) SetDeadline(time.Time) error

func (*MultiConn) SetReadDeadline

func (*MultiConn) SetReadDeadline(time.Time) error

func (*MultiConn) SetWriteDeadline

func (*MultiConn) SetWriteDeadline(time.Time) error

func (*MultiConn) WillReadBuffersBenifit

func (c *MultiConn) WillReadBuffersBenifit() bool

func (*MultiConn) Write

func (c *MultiConn) Write(b []byte) (n int, err error)

func (*MultiConn) WriteBuffers

func (c *MultiConn) WriteBuffers(bs [][]byte) (num int64, err error)

type MultiHunk

type MultiHunk struct {
	Data [][]byte `protobuf:"bytes,1,rep,name=data,proto3" json:"data,omitempty"`
	// contains filtered or unexported fields
}

func (*MultiHunk) Descriptor deprecated

func (*MultiHunk) Descriptor() ([]byte, []int)

Deprecated: Use MultiHunk.ProtoReflect.Descriptor instead.

func (*MultiHunk) GetData

func (x *MultiHunk) GetData() [][]byte

func (*MultiHunk) ProtoMessage

func (*MultiHunk) ProtoMessage()

func (*MultiHunk) ProtoReflect

func (x *MultiHunk) ProtoReflect() protoreflect.Message

func (*MultiHunk) Reset

func (x *MultiHunk) Reset()

func (*MultiHunk) String

func (x *MultiHunk) String() string

type Server

type Server struct {
	Creator

	UnimplementedStreamServer
	// contains filtered or unexported fields
}

Server实现 grpc生成的 StreamServer 接口,用于不断处理一个客户端传来的新需求. implements advLayer.MuxServer

func NewServer

func NewServer(serviceName string) *Server

NewServer 以 自定义 serviceName 来创建一个新 Server

func (*Server) GetPath

func (s *Server) GetPath() string

func (*Server) StartHandle

func (s *Server) StartHandle(conn net.Conn, newConnChan chan net.Conn, _ chan httpLayer.FallbackMeta)
StartHandle方法 被用于 手动给 grpc提供新连接.

在本作中 我们不使用 grpc的listen的方法。这样更加灵活. 非阻塞. 不支持回落(若想要回落功能,请看grpcSimple)。

func (*Server) Stop

func (s *Server) Stop()

func (*Server) Tun

func (s *Server) Tun(stream_TunServer Stream_TunServer) error

该 Tun方法会被 grpc包调用, stream_TunServer就是获取到的新连接; 实际上就是在 handle_grpcRawConn 后, 每一条客户端发来的子连接 都会调用一次 s.Tun .

我们把该 stream_TunServer 包装成 net.Conn 并传入 NewConnChan。 该方法是自动被grpc包调用的, 不用我们主动调用.

func (*Server) TunMulti

func (s *Server) TunMulti(stream_TunMultiServer Stream_TunMultiServer) error

type StreamClient

type StreamClient interface {
	Tun(ctx context.Context, opts ...grpc.CallOption) (Stream_TunClient, error)
	TunMulti(ctx context.Context, opts ...grpc.CallOption) (Stream_TunMultiClient, error)
}

StreamClient is the client API for Stream service.

For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.

func NewStreamClient

func NewStreamClient(cc grpc.ClientConnInterface) StreamClient

type StreamConn

type StreamConn interface {
	Context() context.Context
	Send(*Hunk) error
	Recv() (*Hunk, error)
}

StreamConn 接口 是 stream_grpc.pb.go 中 自动生成的 Stream_TunServer 接口和 Stream_TunClient接口 的共有部分, 我们提出来.

type StreamMultiConn

type StreamMultiConn interface {
	Context() context.Context
	Send(*MultiHunk) error
	Recv() (*MultiHunk, error)
}

type StreamServer

type StreamServer interface {
	Tun(Stream_TunServer) error
	TunMulti(Stream_TunMultiServer) error
	// contains filtered or unexported methods
}

StreamServer is the server API for Stream service. All implementations must embed UnimplementedStreamServer for forward compatibility

type Stream_TunClient

type Stream_TunClient interface {
	Send(*Hunk) error
	Recv() (*Hunk, error)
	grpc.ClientStream
}

type Stream_TunMultiClient

type Stream_TunMultiClient interface {
	Send(*MultiHunk) error
	Recv() (*MultiHunk, error)
	grpc.ClientStream
}

type Stream_TunMultiServer

type Stream_TunMultiServer interface {
	Send(*MultiHunk) error
	Recv() (*MultiHunk, error)
	grpc.ServerStream
}

type Stream_TunServer

type Stream_TunServer interface {
	Send(*Hunk) error
	Recv() (*Hunk, error)
	grpc.ServerStream
}

type UnimplementedStreamServer

type UnimplementedStreamServer struct {
}

UnimplementedStreamServer must be embedded to have forward compatible implementations.

func (UnimplementedStreamServer) Tun

func (UnimplementedStreamServer) TunMulti

type UnsafeStreamServer

type UnsafeStreamServer interface {
	// contains filtered or unexported methods
}

UnsafeStreamServer may be embedded to opt out of forward compatibility for this service. Use of this interface is not recommended, as added methods to StreamServer will result in compilation errors.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL