frp

package
v0.0.0-...-65ae77c Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2024 License: MulanPSL-2.0 Imports: 12 Imported by: 0

README

frp

使用frp管理远程端口。

  • 服务端下发指令,每个指令包括全部的反向代理信息。
  • 客户端监听指令,根据下发的反向代理信息启动frpc连接。

目前支持如下的反向代理模式:

  • http:将IP:PORT映射为云端的HTTP服务,需要一个自定义域名,访问时可以直接域名访问。
  • tcp/udp:基本连接,将IP:PORT映射到云端的指定端口,这个端口是由云端的ExternalPortManager统一分配的,访问时,使用云端服务器IP/域名+分配的端口号,进行访问。
  • stcp:特指将本地的/var/run/docker.sock,用stcp的方式映射到云端,本地的服务有密钥,使用visitor连接时,必须密钥一致。visitor会将IP:PORT映射到云端服务容器内部的127.0.0.1的一个端口上,由VisitorPortManager分配端口号。然后可以使用docker部分的服务连接这个端口号,就可以对本地进行Docker操作了。

需要设置端口池范围:

// 设置TCP/UDP外部端口范围,二者共用这个区间
frp.ExternalPortManager.MinPort = xxx
frp.ExternalPortManager.MaxPort = xxx

// (可选) 设置visitor端口范围,仅在有stcp的docker.sock映射时使用
frp.VisitorPortManager.MinPort = 10000
frp.VisitorPortManager.MaxPort = 60000

需要初始化TCP/UDP端口池的使用情况(与数据库中同步):

frp.ExternalPortManager.Init(xxx)

注意事项:

  • frpsfrpc版本为0.33.0
  • github.com/fatedier/frp:版本v0.37.1,因为最新的v0.58.1需要使用Go的1.22特性,包括但不限于cmpslicesmath/rand/v2等。

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ExternalPortManager = &PortManager{
	MinPort: 0,
	MaxPort: 0,
	lock:    sync.RWMutex{},
	status:  map[uint16]bool{},
	pointer: 0,
}

服务器外部端口池

View Source
var VisitorPortManager = PortManager{
	MinPort: 0,
	MaxPort: 0,
	// contains filtered or unexported fields
}

frps的visitor连接使用的端口池单例

Functions

This section is empty.

Types

type ClientConfig

type ClientConfig struct {
	ServerAddr  string // 服务器地址
	ServerPort  uint16 // 服务器端口号
	ServerToken string // (可选) 服务器Token鉴权
}

sse命令的客户端连接配置

func (ClientConfig) Generate

func (cc ClientConfig) Generate() config.ClientCommonConf

type Command

type Command struct {
	Client ClientConfig  // 客户端连接配置
	Proxy  []ProxyConfig // 代理列表
}

sse命令

func (Command) Generate

func (cmd Command) Generate() (config.ClientCommonConf, map[string]config.ProxyConf)

type Instance

type Instance struct {
	LastCmd Command         // 最近的命令
	Svr     *client.Service // frpc服务
}

本地处理类

func (*Instance) Reload

func (ins *Instance) Reload(cmd Command) (err error)

收到新的sse命令

type PortManager

type PortManager struct {
	MinPort uint16 // (需外部设置) 最小端口号(含)
	MaxPort uint16 // (需外部设置) 最大端口号(不含)
	// contains filtered or unexported fields
}

端口管理类

func (*PortManager) Get

func (pm *PortManager) Get() (port uint16, err error)

从端口池取出一个端口号使用

func (*PortManager) InRange

func (pm *PortManager) InRange(port uint16) bool

端口号是否有效

func (*PortManager) Init

func (pm *PortManager) Init(statusMap map[uint16]bool)

初始化端口号使用情况

func (*PortManager) IsUsed

func (pm *PortManager) IsUsed(port uint16) bool

端口号是否被占用

func (*PortManager) Put

func (pm *PortManager) Put(port uint16)

放回一个端口号

type ProxyConfig

type ProxyConfig struct {
	Identifier    string // 服务标识,全局唯一
	Mode          string // 模式,http/tcp/udp/stcp
	LocalIp       string // 本地IP
	LocalPort     uint16 // 本地端口号
	RemotePort    uint16 // 远程端口号,http模式为0
	CustomDomains string // 自定义域名,非http模式为空
	SecretKey     string // 服务密钥,仅stcp模式有效
}

sse命令内容的代理信息

func (ProxyConfig) Generate

func (pc ProxyConfig) Generate() config.ProxyConf

type VisitorClient

type VisitorClient struct {
	Config  VisitorClientConfig // 连接配置信息
	Visitor *client.Service     // visitor服务
	Port    uint16              // visitor服务端口号
}

frps的visitor连接

func NewVisitorClient

func NewVisitorClient(config VisitorClientConfig) (c *VisitorClient)

新建函数

func (*VisitorClient) Close

func (c *VisitorClient) Close()

关闭visitor连接

func (*VisitorClient) Connect

func (c *VisitorClient) Connect() (err error)

visitor连接到frps

type VisitorClientConfig

type VisitorClientConfig struct {
	ServerName      string // 服务器名称
	ServerPort      uint16 // 服务器端口号
	ServerToken     string // 服务器Token
	ClientName      string // 客户端名称(server_name)
	ClientSecretKey string // 客户端密钥(secret_key)
}

frps的visitor连接配置信息

Jump to

Keyboard shortcuts

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