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 ¶
- Variables
- func DialNewSubConn(path string, clientconn ClientConn, addr *netLayer.Addr, isMulti bool) (net.Conn, error)
- func RegisterStreamServer(s grpc.ServiceRegistrar, srv StreamServer)
- type Client
- type ClientConn
- type Conn
- type Creator
- func (Creator) CanHandleHeaders() bool
- func (Creator) GetDefaultAlpn() (alpn string, mustUse bool)
- func (Creator) IsMux() bool
- func (Creator) IsSuper() bool
- func (Creator) NewClientFromConf(conf *advLayer.Conf) (advLayer.Client, error)
- func (Creator) NewServerFromConf(conf *advLayer.Conf) (advLayer.Server, error)
- func (Creator) PackageID() string
- func (Creator) ProtocolName() string
- type Hunk
- type MultiConn
- func (c *MultiConn) Close() error
- func (c *MultiConn) LocalAddr() net.Addr
- func (c *MultiConn) Read(b []byte) (n int, err error)
- func (c *MultiConn) ReadBuffers() (bs [][]byte, err error)
- func (c *MultiConn) RemoteAddr() net.Addr
- func (*MultiConn) SetDeadline(time.Time) error
- func (*MultiConn) SetReadDeadline(time.Time) error
- func (*MultiConn) SetWriteDeadline(time.Time) error
- func (c *MultiConn) WillReadBuffersBenifit() bool
- func (c *MultiConn) Write(b []byte) (n int, err error)
- func (c *MultiConn) WriteBuffers(bs [][]byte) (num int64, err error)
- type MultiHunk
- type Server
- type StreamClient
- type StreamConn
- type StreamMultiConn
- type StreamServer
- type Stream_TunClient
- type Stream_TunMultiClient
- type Stream_TunMultiServer
- type Stream_TunServer
- type UnimplementedStreamServer
- type UnsafeStreamServer
Constants ¶
This section is empty.
Variables ¶
var File_stream_proto protoreflect.FileDescriptor
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
type ClientConn ¶
type ClientConn *grpc.ClientConn
func ClientHandshake ¶
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) RemoteAddr ¶
type Creator ¶
type Creator struct{}
func (Creator) CanHandleHeaders ¶ added in v1.2.0
func (Creator) GetDefaultAlpn ¶
func (Creator) NewClientFromConf ¶
func (Creator) NewServerFromConf ¶
func (Creator) ProtocolName ¶ added in v1.2.0
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) ProtoMessage ¶
func (*Hunk) ProtoMessage()
func (*Hunk) ProtoReflect ¶
func (x *Hunk) ProtoReflect() protoreflect.Message
type MultiConn ¶
type MultiConn struct {
// contains filtered or unexported fields
}
实现 net.Conn 和 utils.MultiReader
func (*MultiConn) ReadBuffers ¶
func (*MultiConn) RemoteAddr ¶
func (*MultiConn) WillReadBuffersBenifit ¶
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) ProtoMessage ¶
func (*MultiHunk) ProtoMessage()
func (*MultiHunk) ProtoReflect ¶
func (x *MultiHunk) ProtoReflect() protoreflect.Message
type Server ¶
type Server struct { Creator UnimplementedStreamServer // contains filtered or unexported fields }
Server实现 grpc生成的 StreamServer 接口,用于不断处理一个客户端传来的新需求. implements advLayer.MuxServer
func (*Server) StartHandle ¶
func (s *Server) StartHandle(conn net.Conn, newConnChan chan net.Conn, _ chan httpLayer.FallbackMeta)
StartHandle方法 被用于 手动给 grpc提供新连接.
在本作中 我们不使用 grpc的listen的方法。这样更加灵活. 非阻塞. 不支持回落(若想要回落功能,请看grpcSimple)。
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 ¶
StreamConn 接口 是 stream_grpc.pb.go 中 自动生成的 Stream_TunServer 接口和 Stream_TunClient接口 的共有部分, 我们提出来.
type StreamMultiConn ¶
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_TunMultiClient ¶
type Stream_TunMultiServer ¶
type Stream_TunServer ¶
type UnimplementedStreamServer ¶
type UnimplementedStreamServer struct { }
UnimplementedStreamServer must be embedded to have forward compatible implementations.
func (UnimplementedStreamServer) Tun ¶
func (UnimplementedStreamServer) Tun(Stream_TunServer) error
func (UnimplementedStreamServer) TunMulti ¶
func (UnimplementedStreamServer) TunMulti(Stream_TunMultiServer) error
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.