feat(client): reload plugin certificates on frpc reload (#5576)

The https2http and https2https plugins read their certificate and key
once when the proxy starts, so a certificate that an external tool renews
on disk is only picked up after the proxy itself is recreated. frpc
reload rebuilt proxies only when their configuration changed, and a
rebuild closes the work connections that are already in use.

Let the plugin own the refresh. The HTTPS plugin server keeps the active
keypair and the last refresh error in one immutable snapshot published
through an atomic pointer, so a TLS handshake only loads that pointer and
never touches the disk. Reload rereads both files, and when their bytes
match the published pair it returns without parsing or replacing
anything; otherwise it parses the pair and swaps the snapshot in. A read
or parse failure keeps the previous certificate serving and records the
error next to it.

frpc reload collects the reloadable plugins of the live proxies and
applies the new configuration inside the same reload transaction that
also serializes control handoff, so a reload that races a reconnect
still refreshes the plugins of the published control. The certificate
files are read only after that transaction has been released. The config
store API keeps applying configuration without refreshing certificates,
because only an explicit reload may reread files. Proxy status reports a
refresh failure next to the still running proxy instead of dropping its
remote address.

No configuration field is added, and frps and the wire protocol are
unchanged. Without a reload nothing changes: certificates are still read
once at startup, generated self-signed certificates still never reload,
and a proxy whose configuration changed still goes through the existing
rebuild path.
This commit is contained in:
fatedier
2026-10-19 17:16:55 +08:00
committed by GitHub
parent 585fdc5b83
commit 00e4fc4dd3
14 changed files with 854 additions and 8 deletions
+148
View File
@@ -0,0 +1,148 @@
// Copyright 2026 The frp Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build !frps
package proxy
import (
"context"
"errors"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/require"
plugin "github.com/fatedier/frp/pkg/plugin/client"
"github.com/fatedier/frp/pkg/util/xlog"
)
type reloadError struct {
err error
}
type testReloadPlugin struct {
calls atomic.Int32
state atomic.Pointer[reloadError]
reload func() error
}
func (p *testReloadPlugin) Name() string { return "test" }
func (p *testReloadPlugin) Handle(context.Context, *plugin.ConnectionInfo) {}
func (p *testReloadPlugin) Close() error { return nil }
func (p *testReloadPlugin) Reload() error {
p.calls.Add(1)
err := p.reload()
p.state.Store(&reloadError{err: err})
return err
}
func (p *testReloadPlugin) ReloadError() error {
if s := p.state.Load(); s != nil {
return s.err
}
return nil
}
func reloadFixture(p *testReloadPlugin) (*Manager, *Wrapper, *BaseProxy) {
base := &BaseProxy{proxyPlugin: p}
pw := &Wrapper{
WorkingStatus: WorkingStatus{Name: "same", Phase: ProxyPhaseRunning},
pxy: &GeneralTCPProxy{BaseProxy: base},
xl: xlog.New(),
}
return &Manager{proxies: map[string]*Wrapper{"same": pw}}, pw, base
}
func TestPluginSnapshotSkipsReplacedAndStopped(t *testing.T) {
p := &testReloadPlugin{reload: func() error { return nil }}
pm, pw, base := reloadFixture(p)
run := pm.PreparePluginReload()
q := &testReloadPlugin{reload: func() error { return nil }}
pw.mu.Lock()
base.proxyPlugin = q
pw.mu.Unlock()
run()
require.Zero(t, p.calls.Load())
require.Zero(t, q.calls.Load())
run = pm.PreparePluginReload()
pw.mu.Lock()
pw.Phase = ProxyPhaseClosed
pw.mu.Unlock()
run()
require.Zero(t, q.calls.Load())
}
func TestPluginErrorOwnershipAndLockIsolation(t *testing.T) {
started, release, done := make(chan struct{}), make(chan struct{}), make(chan struct{})
p := &testReloadPlugin{reload: func() error {
close(started)
<-release
return errors.New("bad certificate")
}}
pm, pw, base := reloadFixture(p)
other := &testReloadPlugin{reload: func() error { return nil }}
pm2, _, _ := reloadFixture(other)
run := pm.PreparePluginReload()
releaseReload := sync.OnceFunc(func() { close(release) })
t.Cleanup(func() {
releaseReload()
select {
case <-done:
case <-time.After(5 * time.Second):
t.Error("reload did not finish")
}
})
go func() {
run()
close(done)
}()
select {
case <-started:
case <-time.After(5 * time.Second):
t.Fatal("not started")
}
require.True(t, pm.mu.TryLock())
pm.mu.Unlock()
require.True(t, pw.mu.TryLock())
pw.mu.Unlock()
require.Empty(t, pw.GetStatus().Err)
require.Zero(t, other.calls.Load(), "same name in another manager must be isolated")
pm2.PreparePluginReload()()
require.EqualValues(t, 1, other.calls.Load())
q := &testReloadPlugin{reload: func() error { return nil }}
pw.mu.Lock()
base.proxyPlugin = q
pw.mu.Unlock()
releaseReload()
select {
case <-done:
case <-time.After(5 * time.Second):
t.Fatal("not done")
}
require.Error(t, p.ReloadError())
require.Empty(t, pw.GetStatus().Err, "retired plugin errors must not attach to its replacement")
q.state.Store(&reloadError{err: errors.New("new plugin error")})
require.Contains(t, pw.GetStatus().Err, "new plugin error")
require.Empty(t, pw.Err, "plugin state must not overwrite lifecycle errors")
pw.mu.Lock()
pw.Err = "lifecycle"
pw.mu.Unlock()
require.Contains(t, pw.GetStatus().Err, "lifecycle; reload plugin")
require.NoError(t, q.Reload())
require.Equal(t, "lifecycle", pw.GetStatus().Err)
}
+3
View File
@@ -120,6 +120,9 @@ func (pxy *BaseProxy) Run() error {
return nil
}
// getPlugin is accessed while the owning Wrapper holds its lifecycle lock.
func (pxy *BaseProxy) getPlugin() plugin.Plugin { return pxy.proxyPlugin }
func (pxy *BaseProxy) Close() {
if pxy.proxyPlugin != nil {
pxy.proxyPlugin.Close()
+35
View File
@@ -139,6 +139,41 @@ func (pm *Manager) GetProxyStatus(name string) (*WorkingStatus, bool) {
return nil, false
}
// PreparePluginReload snapshots live plugin instances before applying config.
// Invoke the returned function only after config application succeeds and all
// configuration locks have been released. Replaced instances are skipped.
func (pm *Manager) PreparePluginReload() func() {
type target struct {
pw *Wrapper
p reloadablePlugin
}
pm.mu.RLock()
targets := make([]target, 0, len(pm.proxies))
for _, pw := range pm.proxies {
pw.mu.RLock()
if pw.Phase == ProxyPhaseRunning {
if p := pw.reloadablePluginLocked(); p != nil {
targets = append(targets, target{pw: pw, p: p})
}
}
pw.mu.RUnlock()
}
pm.mu.RUnlock()
return func() {
for _, t := range targets {
t.pw.mu.RLock()
current := t.pw.Phase == ProxyPhaseRunning && t.pw.reloadablePluginLocked() == t.p
t.pw.mu.RUnlock()
if !current {
continue
}
if err := t.p.Reload(); err != nil {
t.pw.xl.Warnf("reload plugin %s: %v", t.p.Name(), err)
}
}
}
}
func (pm *Manager) UpdateAll(proxyCfgs []v1.ProxyConfigurer) {
xl := xlog.FromContextSafe(pm.ctx)
proxyCfgsMap := lo.KeyBy(proxyCfgs, func(c v1.ProxyConfigurer) string {
+30
View File
@@ -30,6 +30,7 @@ import (
v1 "github.com/fatedier/frp/pkg/config/v1"
"github.com/fatedier/frp/pkg/msg"
"github.com/fatedier/frp/pkg/naming"
plugin "github.com/fatedier/frp/pkg/plugin/client"
"github.com/fatedier/frp/pkg/transport"
"github.com/fatedier/frp/pkg/util/xlog"
"github.com/fatedier/frp/pkg/vnet"
@@ -277,6 +278,25 @@ func (pw *Wrapper) InWorkConn(workConn net.Conn, m *msg.StartWorkConn) {
}
}
// reloadablePlugin owns its reload serialization and error snapshot. ReloadError
// must not block on I/O. Reload must be safe during Close, and retain usable
// resources on failure. Existing plugins need not implement this capability.
type reloadablePlugin interface {
plugin.Plugin
Reload() error
ReloadError() error
}
// Caller holds pw.mu. Concrete reloadable plugins are pointer instances.
func (pw *Wrapper) reloadablePluginLocked() reloadablePlugin {
owner, ok := pw.pxy.(interface{ getPlugin() plugin.Plugin })
if !ok {
return nil
}
p, _ := owner.getPlugin().(reloadablePlugin)
return p
}
func (pw *Wrapper) GetStatus() *WorkingStatus {
pw.mu.RLock()
defer pw.mu.RUnlock()
@@ -288,5 +308,15 @@ func (pw *Wrapper) GetStatus() *WorkingStatus {
Cfg: pw.Cfg,
RemoteAddr: pw.RemoteAddr,
}
if pw.Phase == ProxyPhaseRunning {
if p := pw.reloadablePluginLocked(); p != nil {
if err := p.ReloadError(); err != nil {
if ps.Err != "" {
ps.Err += "; "
}
ps.Err += fmt.Sprintf("reload plugin %s: %v", p.Name(), err)
}
}
}
return ps
}