hops

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: Apache-2.0 Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package hops 记录 Gately 自身转发到本机的连接,用于识别管理界面的访问路径。

通过域名远程管理时,请求可能依次经过协议分流、端口转发、Web 服务才到达管理界面。四层转发(端口转发、 协议分流)连接目标后,如果目标在本机,就以拨出连接的本地地址登记这一跳;下一跳(Web 服务监听或管理界面) 看到的 TCP 对端正是这个地址,据此逐跳向外追溯,直到访客或本机的其他程序。

Web 服务反向代理到管理界面时复用后端连接池,同一条连接会承载不同访客的请求,不能按连接登记; 改为在发往管理界面的请求上附带进程内签名的请求头(Header),说明经过的监听与站点和访客连接的对端。 签名密钥每次启动随机生成且不出进程,外部伪造的请求头无法通过校验。 站点反代到本实例的另一个 Web 服务监听器时同样附带请求头,下一层把它串在自己这一跳的 Prev 中继续传递。

Index

Constants

View Source
const Header = "X-Gately-Hop"

Header 为 Web 服务发往本实例管理界面的请求上附带的访问路径信息。

View Source
const MaxChain = 8

MaxChain 限制请求头中串联的 Web 服务跳数,防止站点互相反代形成环时请求头无限增长

Variables

This section is empty.

Functions

func Encode

func Encode(h Hop) string

Encode 把 Web 服务这一跳编码为带签名的请求头值。

func Normalize

func Normalize(addr string) string

Normalize 把 ip:port 规范为统一形式(IPv4 映射地址还原为 IPv4),无法解析时原样返回。

func RawPeer

func RawPeer(c net.Conn) string

RawPeer 返回连接最底层的 TCP 对端:依次拆开 TLS、PROXY 协议等包装(NetConn、Raw 方法), PROXY 协议改写过的对端地址不是真正发起连接的一方,不能用于追溯。

func Track

func Track(in, out net.Conn, h Hop) func()

Track 登记一条四层转发:in 为接入的连接,out 为连到目标的连接。目标不在本机时不登记。 返回的函数在连接结束时调用,撤销登记。

Types

type Hop

type Hop struct {
	// Module 为 portforward、protoroute 或 webservice
	Module string `json:"module"`
	// Rule 为端口转发或协议分流的规则 key,Web 服务的监听 key
	Rule string `json:"rule"`
	// Route 为协议分流命中的路由名称(默认目标为空)
	Route string `json:"route,omitempty"`
	// Site 为 Web 服务的站点 key
	Site string `json:"site,omitempty"`
	// Listen 为这一跳接入连接的本地地址
	Listen string `json:"listen"`
	// Peer 为这一跳接入连接的 TCP 对端,据此继续向外追溯
	Peer string `json:"peer"`
	// Client 为 Web 服务识别出的访客地址(经可信代理时为真实地址),可为空
	Client string `json:"client,omitempty"`
	// Prev 为这一跳之前、同一进程内更外层的 Web 服务跳(443 站点 → 本机 444 监听器 → 管理界面),由内向外排列;
	// 此时本跳的 Peer 是外层反代拨出的连接,继续追溯要从最外一层的 Peer 开始
	Prev []Hop `json:"prev,omitempty"`
	// ToPort 为发往本实例另一个 Web 服务监听器时目标的端口:下一层只在它等于自己接入连接的本地端口时
	// 才把这一跳串进 Prev,访客把别处拿到的请求头带到其他监听上不会被采信
	ToPort uint16 `json:"toPort,omitempty"`
}

Hop 是访问路径上由 Gately 自身转发的一跳。

func Decode

func Decode(v string) (Hop, bool)

Decode 校验并解出请求头中的一跳,签名不符时返回 false。

func Lookup

func Lookup(peer string) (Hop, bool)

Lookup 按下一跳看到的 TCP 对端查找登记的转发。

func (Hop) Chain

func (h Hop) Chain() []Hop

Chain 返回本跳与更外层的 Web 服务跳,由内向外排列。

Jump to

Keyboard shortcuts

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