grpcSimple

package
v1.2.1-beta.1 Latest Latest
Warning

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

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

Documentation

Overview

Package grpcSimple implements grpc tunnel without importing google.golang.org/grpc.

Reference

https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md

https://github.com/Dreamacro/clash/blob/master/transport/gun/gun.go, which is under MIT license

我们可以通过 grpc的文档 以及clash的 gun.go的代码看到,grpc实际上是 基于包的,而不是基于流的,与ws类似。

本包 在 clash的客户端实现 的 基础上 继续用 golang的 http2包 实现了 grpc 的 基本服务端,并改进了 原代码。

Advantages

grpcSimple包 比grpc包 小很多,替代grpc包的话,可以减小 4MB 左右的可执行文件大小。但是目前不支持 multiMode。

grpcSimple包 是很棒 很有用的 实现,而且支持 grpc的 path 的回落。

grpc虽然是定义 serviceName的,但是实际上和其他http请求一样,是用的一个path,

path就是 /serviceName/Tun

Fallback

grpcSimple can fallback to h2c.

about h2c

https://pkg.go.dev/golang.org/x/net/http2/h2c#example-NewHandler

https://github.com/thrawn01/h2c-golang-example

test h2c:

curl -k -v --http2-prior-knowledge https://localhost:4434/sfd

curl -k -v --http2-prior-knowledge -X POST -F 'asdf=1234'  https://localhost:4434/sfd

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Creator

	Config
	// contains filtered or unexported fields
}

implements advLayer.MuxClient

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 struct {
	// contains filtered or unexported fields
}

implements net.Conn

func (*ClientConn) Close

func (c *ClientConn) Close() error

func (*ClientConn) LocalAddr

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

func (*ClientConn) Read

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

func (*ClientConn) RemoteAddr

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

func (*ClientConn) SetDeadline

func (c *ClientConn) SetDeadline(t time.Time) error

func (*ClientConn) SetReadDeadline

func (c *ClientConn) SetReadDeadline(t time.Time) error

func (*ClientConn) SetWriteDeadline

func (c *ClientConn) SetWriteDeadline(t time.Time) error

func (*ClientConn) Write

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

type Config

type Config struct {
	ServiceName string
	Host        string

	FallbackToH1 bool //默认会回落到h2, 如果指定 FallbackToH1, 则会回落到 http/1.1
}

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 Server

type Server struct {
	Creator

	Config

	Headers *httpLayer.HeaderPreset

	http2.Server
	// contains filtered or unexported fields
}

implements advLayer.MuxServer

func (*Server) GetPath

func (s *Server) GetPath() string

func (*Server) StartHandle

func (s *Server) StartHandle(underlay net.Conn, newSubConnChan chan net.Conn, fallbackConnChan chan httpLayer.FallbackMeta)

func (*Server) Stop

func (s *Server) Stop()

type ServerConn

type ServerConn struct {
	io.Closer
	Writer http.ResponseWriter
	// contains filtered or unexported fields
}

func (*ServerConn) Close

func (sc *ServerConn) Close() error

func (*ServerConn) LocalAddr

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

func (*ServerConn) Read

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

func (*ServerConn) RemoteAddr

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

func (*ServerConn) SetDeadline

func (c *ServerConn) SetDeadline(t time.Time) error

func (*ServerConn) SetReadDeadline

func (c *ServerConn) SetReadDeadline(t time.Time) error

func (*ServerConn) SetWriteDeadline

func (c *ServerConn) SetWriteDeadline(t time.Time) error

func (*ServerConn) Write

func (sc *ServerConn) Write(b []byte) (n int, err error)

Jump to

Keyboard shortcuts

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