mirror of
https://github.com/fatedier/frp.git
synced 2026-10-19 19:13:32 +08:00
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.
144 lines
3.4 KiB
Go
144 lines
3.4 KiB
Go
// 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 client
|
|
|
|
import (
|
|
"context"
|
|
stdlog "log"
|
|
"net/http"
|
|
"net/http/httputil"
|
|
"time"
|
|
|
|
"github.com/fatedier/golib/pool"
|
|
|
|
v1 "github.com/fatedier/frp/pkg/config/v1"
|
|
"github.com/fatedier/frp/pkg/plugin/client/internal/httpsserver"
|
|
"github.com/fatedier/frp/pkg/util/log"
|
|
netpkg "github.com/fatedier/frp/pkg/util/net"
|
|
)
|
|
|
|
const httpBridgeReadHeaderTimeout = 60 * time.Second
|
|
|
|
func rewriteHTTPPluginRequest(
|
|
req *http.Request,
|
|
scheme string,
|
|
localAddr string,
|
|
hostHeaderRewrite string,
|
|
requestHeaders v1.HeaderOperations,
|
|
) {
|
|
req.URL.Scheme = scheme
|
|
req.URL.Host = localAddr
|
|
if hostHeaderRewrite != "" {
|
|
req.Host = hostHeaderRewrite
|
|
}
|
|
for k, v := range requestHeaders.Set {
|
|
req.Header.Set(k, v)
|
|
}
|
|
}
|
|
|
|
type httpBridgePlugin struct {
|
|
l *Listener
|
|
s *http.Server
|
|
httpsServer *httpsserver.Server
|
|
|
|
useSourceRemoteAddr bool
|
|
}
|
|
|
|
func newHTTPBridgePluginServer(handler http.Handler, useSourceRemoteAddr bool) *httpBridgePlugin {
|
|
listener := NewProxyListener()
|
|
p := &httpBridgePlugin{
|
|
l: listener,
|
|
useSourceRemoteAddr: useSourceRemoteAddr,
|
|
}
|
|
p.s = &http.Server{
|
|
Handler: handler,
|
|
ReadHeaderTimeout: httpBridgeReadHeaderTimeout,
|
|
}
|
|
go func() {
|
|
_ = p.s.Serve(listener)
|
|
}()
|
|
return p
|
|
}
|
|
|
|
func newHTTPSBridgePluginServer(
|
|
handler http.Handler,
|
|
crtPath string,
|
|
keyPath string,
|
|
enableHTTP2 *bool,
|
|
useSourceRemoteAddr bool,
|
|
) (*httpBridgePlugin, error) {
|
|
listener := NewProxyListener()
|
|
server, err := httpsserver.New(handler, crtPath, keyPath, enableHTTP2)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
p := &httpBridgePlugin{
|
|
l: listener,
|
|
s: server.Server,
|
|
httpsServer: server,
|
|
useSourceRemoteAddr: useSourceRemoteAddr,
|
|
}
|
|
go func() {
|
|
_ = p.s.ServeTLS(listener, "", "")
|
|
}()
|
|
return p, nil
|
|
}
|
|
|
|
func newHTTPBridgeReverseProxy(
|
|
rewrite func(*httputil.ProxyRequest),
|
|
transport http.RoundTripper,
|
|
) *httputil.ReverseProxy {
|
|
rp := &httputil.ReverseProxy{
|
|
Rewrite: rewrite,
|
|
BufferPool: pool.NewBuffer(32 * 1024),
|
|
ErrorLog: stdlog.New(log.NewWriteLogger(log.WarnLevel, 2), "", 0),
|
|
}
|
|
if transport != nil {
|
|
rp.Transport = transport
|
|
}
|
|
return rp
|
|
}
|
|
|
|
func (p *httpBridgePlugin) Handle(_ context.Context, connInfo *ConnectionInfo) {
|
|
wrapConn := netpkg.WrapReadWriteCloserToConn(connInfo.Conn, connInfo.UnderlyingConn)
|
|
if p.useSourceRemoteAddr && connInfo.SrcAddr != nil {
|
|
wrapConn.SetRemoteAddr(connInfo.SrcAddr)
|
|
}
|
|
_ = p.l.PutConn(wrapConn)
|
|
}
|
|
|
|
func (p *httpBridgePlugin) Close() error {
|
|
err := p.s.Close()
|
|
_ = p.l.Close()
|
|
return err
|
|
}
|
|
|
|
// Reload refreshes only resources owned by this plugin; HTTP bridges are no-op.
|
|
func (p *httpBridgePlugin) Reload() error {
|
|
if p.httpsServer == nil {
|
|
return nil
|
|
}
|
|
return p.httpsServer.Reload()
|
|
}
|
|
|
|
func (p *httpBridgePlugin) ReloadError() error {
|
|
if p.httpsServer == nil {
|
|
return nil
|
|
}
|
|
return p.httpsServer.ReloadError()
|
|
}
|