grpcSimple

package
v1.2.5 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 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 之所以叫Tun,只不过是为了兼容xray/v2ray,技术上叫啥都行

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 fallback:

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

test http1.1 fallback:

curl -v --http1.1 -k 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) HasOwnDefaultRejectBehavior added in v1.2.4

func (*ClientConn) HasOwnDefaultRejectBehavior() bool

implements netLayer.RejectConn, return true

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) Write

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

type Config

type Config struct {
	ServiceName string
	Host        string
}

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
	http2.Server
	netLayer.ConnList

	Headers *httpLayer.HeaderPreset
	// 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, newSubConnFunc func(net.Conn), fallbackFunc func(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) HasOwnDefaultRejectBehavior added in v1.2.4

func (*ServerConn) HasOwnDefaultRejectBehavior() bool

implements netLayer.RejectConn, return true

func (*ServerConn) LocalAddr

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

func (*ServerConn) Read

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

func (*ServerConn) Reject added in v1.2.4

func (sc *ServerConn) Reject()

implements netLayer.RejectConn, 模仿nginx响应,参考 httpLayer.SetNginx400Response

func (*ServerConn) RemoteAddr

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

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