mirror of
https://gitcode.com/JianFeeeee/webui4frpc.git
synced 2026-09-20 08:57:55 +00:00
- process: spawn 不再重复起 supervise goroutine(Restart 触发旧 supervise close doneCh 后再 close → panic);新增 closeOnce/supervising 守卫;Start 首次起 supervise,重启循环在同一 goroutine 内 - httpapi: status 增加 connState(connected/connecting/failed/not_started/disabled),由 worker 日志探测真实 frps 连接(不再把本地 worker 状态误当远程 frps 状态) - StatusView: 远程卡片主状态改为连接状态徽标;启停按钮标注为控制本地 frpc worker;本地转发区保留 worker 状态显示 - 新增 ClusterView 集群页骨架(M3 LB+健康检查载体)+ App.vue 导航接入 - render: bandwidthLimit 从 proxy 顶层移入 transport 段(frpc 0.61 正确 schema,修复真实连不上的 bug)
180 lines
6.2 KiB
Go
180 lines
6.2 KiB
Go
// Package render generates frpc worker configuration (JSON) from canvas data.
|
|
package render
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"webui4frpc/internal/store"
|
|
)
|
|
|
|
// Proxy is a single proxy to render inside a worker config. It carries enough
|
|
// info to build any supported frpc proxy type.
|
|
type Proxy struct {
|
|
// Name is the display name (the local service name).
|
|
Name string
|
|
// Type is the proxy type: tcp | udp | http | https.
|
|
Type string
|
|
// LocalIP and LocalPort are the backend address.
|
|
LocalIP string
|
|
LocalPort int
|
|
// RemotePort is the exposed port on the frps server (tcp/udp only).
|
|
RemotePort int
|
|
// SubDomain is used by http/https proxies when set.
|
|
SubDomain string
|
|
// CustomDomains is used by http/https proxies when set.
|
|
CustomDomains []string
|
|
|
|
// Advanced transport knobs (M1): proxy-level encryption/compression/limit.
|
|
UseEncryption bool
|
|
UseCompression bool
|
|
BandwidthLimit string
|
|
PoolCount int
|
|
Metadatas map[string]string
|
|
Annotations map[string]string
|
|
|
|
// M2 HTTP/HTTPS routing (http/https only).
|
|
Locations []string // path routing
|
|
HostHeaderRewrite string // rewrite Host header
|
|
HTTPHeaders map[string]string // additional request headers
|
|
BasicAuthUser string
|
|
BasicAuthPassword string
|
|
}
|
|
|
|
// frpcAuth mirrors frpc's auth section.
|
|
type frpcAuth struct {
|
|
Method string `json:"method"`
|
|
Token string `json:"token,omitempty"`
|
|
}
|
|
|
|
// frpcTransport is the transport section used by frpc.
|
|
type frpcTransport struct {
|
|
Protocol string `json:"protocol,omitempty"`
|
|
TLS *frpcTLS `json:"tls,omitempty"`
|
|
PoolCount int `json:"poolCount,omitempty"`
|
|
}
|
|
|
|
// frpcTLS mirrors frpc's transport.tls section.
|
|
type frpcTLS struct {
|
|
Enable bool `json:"enable,omitempty"`
|
|
ServerName string `json:"serverName,omitempty"`
|
|
}
|
|
|
|
// frpcProxyTransport is the per-proxy transport section (frpc >= 0.52)
|
|
// where bandwidthLimit is defined.
|
|
type frpcProxyTransport struct {
|
|
BandwidthLimit string `json:"bandwidthLimit,omitempty"`
|
|
}
|
|
|
|
// frpcBasicAuth mirrors frpc's HTTP basicAuth section.
|
|
type frpcBasicAuth struct {
|
|
User string `json:"user,omitempty"`
|
|
Password string `json:"password,omitempty"`
|
|
}
|
|
|
|
// frpcProxy is the serialized proxy object. Different types use different
|
|
// fields; empty ones are omitted so the JSON is accepted by frpc.
|
|
type frpcProxy struct {
|
|
Name string `json:"name"`
|
|
Type string `json:"type"`
|
|
LocalIP string `json:"localIP,omitempty"`
|
|
LocalPort int `json:"localPort"`
|
|
RemotePort int `json:"remotePort,omitempty"`
|
|
CustomDomains []string `json:"customDomains,omitempty"`
|
|
SubDomain string `json:"subdomain,omitempty"`
|
|
|
|
// M1 advanced transport fields. bandwidthLimit lives in transport.
|
|
UseEncryption bool `json:"useEncryption,omitempty"`
|
|
UseCompression bool `json:"useCompression,omitempty"`
|
|
PoolCount int `json:"poolCount,omitempty"`
|
|
Metadatas map[string]string `json:"metadatas,omitempty"`
|
|
Annotations map[string]string `json:"annotations,omitempty"`
|
|
Transport *frpcProxyTransport `json:"transport,omitempty"`
|
|
|
|
// M2 HTTP/HTTPS routing.
|
|
Locations []string `json:"locations,omitempty"`
|
|
HostHeaderRewrite string `json:"hostHeaderRewrite,omitempty"`
|
|
HTTPHeaders map[string]string `json:"httpHeaders,omitempty"`
|
|
HTTPBasicAuth *frpcBasicAuth `json:"basicAuth,omitempty"`
|
|
}
|
|
|
|
// Config is a complete frpc configuration document.
|
|
type Config struct {
|
|
ServerAddr string `json:"serverAddr"`
|
|
ServerPort int `json:"serverPort"`
|
|
Auth frpcAuth `json:"auth"`
|
|
Transport frpcTransport `json:"transport,omitempty"`
|
|
LoginFailExit bool `json:"loginFailExit"`
|
|
Proxies []frpcProxy `json:"proxies"`
|
|
}
|
|
|
|
// Render builds the worker config JSON for a remote server from its setting
|
|
// plus the list of proxies to forward.
|
|
func Render(remote store.Remote, proxies []Proxy) ([]byte, error) {
|
|
cfg := Config{
|
|
ServerAddr: remote.IP,
|
|
ServerPort: remote.Port,
|
|
Auth: frpcAuth{Method: "token", Token: remote.Token},
|
|
LoginFailExit: false,
|
|
Proxies: make([]frpcProxy, 0, len(proxies)),
|
|
}
|
|
if remote.TransportProtocol != "" || remote.TransportTLS || remote.TransportPool > 0 || remote.TransportTLSServerName != "" {
|
|
cfg.Transport = frpcTransport{
|
|
Protocol: remote.TransportProtocol,
|
|
PoolCount: remote.TransportPool,
|
|
}
|
|
if remote.TransportTLS || remote.TransportTLSServerName != "" {
|
|
cfg.Transport.TLS = &frpcTLS{
|
|
Enable: remote.TransportTLS,
|
|
ServerName: remote.TransportTLSServerName,
|
|
}
|
|
}
|
|
}
|
|
|
|
nameCounts := make(map[string]int)
|
|
for _, p := range proxies {
|
|
frpcP := frpcProxy{
|
|
Name: p.Name,
|
|
Type: p.Type,
|
|
LocalIP: p.LocalIP,
|
|
LocalPort: p.LocalPort,
|
|
SubDomain: p.SubDomain,
|
|
UseEncryption: p.UseEncryption,
|
|
UseCompression: p.UseCompression,
|
|
PoolCount: p.PoolCount,
|
|
Metadatas: p.Metadatas,
|
|
Annotations: p.Annotations,
|
|
Locations: p.Locations,
|
|
HostHeaderRewrite: p.HostHeaderRewrite,
|
|
HTTPHeaders: p.HTTPHeaders,
|
|
}
|
|
if p.BandwidthLimit != "" {
|
|
frpcP.Transport = &frpcProxyTransport{BandwidthLimit: p.BandwidthLimit}
|
|
}
|
|
if p.BasicAuthUser != "" || p.BasicAuthPassword != "" {
|
|
frpcP.HTTPBasicAuth = &frpcBasicAuth{User: p.BasicAuthUser, Password: p.BasicAuthPassword}
|
|
}
|
|
// http/https proxies use customDomains instead of remotePort. The
|
|
// default domain is <name>.local; frps matches it by Host header.
|
|
if p.Type == "http" || p.Type == "https" {
|
|
frpcP.RemotePort = 0
|
|
if frpcP.SubDomain == "" && len(frpcP.CustomDomains) == 0 {
|
|
frpcP.CustomDomains = []string{p.Name + ".local"}
|
|
}
|
|
} else {
|
|
frpcP.RemotePort = p.RemotePort
|
|
}
|
|
if len(p.CustomDomains) > 0 {
|
|
frpcP.CustomDomains = p.CustomDomains
|
|
}
|
|
// Duplicate service name -> suffix with the remote port so frpc accepts
|
|
// multiple proxies for the same local service.
|
|
nameCounts[frpcP.Name]++
|
|
if nameCounts[frpcP.Name] > 1 {
|
|
frpcP.Name = fmt.Sprintf("%s-%d", frpcP.Name, frpcP.RemotePort)
|
|
}
|
|
cfg.Proxies = append(cfg.Proxies, frpcP)
|
|
}
|
|
return json.MarshalIndent(cfg, "", " ")
|
|
}
|