mirror of
https://github.com/fatedier/frp.git
synced 2026-10-19 23:33:27 +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.
138 lines
3.5 KiB
Go
138 lines
3.5 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 httpsserver
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/tls"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/samber/lo"
|
|
|
|
"github.com/fatedier/frp/pkg/transport"
|
|
httppkg "github.com/fatedier/frp/pkg/util/http"
|
|
)
|
|
|
|
// snapshot is immutable after publication. Errors retain the last usable pair.
|
|
type snapshot struct {
|
|
cert *tls.Certificate
|
|
certPEM, keyPEM []byte
|
|
err error
|
|
}
|
|
|
|
type Server struct {
|
|
*http.Server
|
|
|
|
crtPath, keyPath string
|
|
reloadMu sync.Mutex
|
|
state atomic.Pointer[snapshot]
|
|
}
|
|
|
|
func New(handler http.Handler, crtPath, keyPath string, enableHTTP2 *bool) (*Server, error) {
|
|
server := &Server{crtPath: crtPath, keyPath: keyPath}
|
|
var tlsConfig *tls.Config
|
|
var err error
|
|
if crtPath == "" || keyPath == "" {
|
|
tlsConfig, err = transport.NewServerTLSConfig(crtPath, keyPath, "")
|
|
} else {
|
|
err = server.Reload()
|
|
tlsConfig = &tls.Config{GetCertificate: func(*tls.ClientHelloInfo) (*tls.Certificate, error) {
|
|
return server.state.Load().cert, nil
|
|
}}
|
|
}
|
|
if err != nil {
|
|
return nil, fmt.Errorf("gen TLS config error: %v", err)
|
|
}
|
|
|
|
server.Server = &http.Server{
|
|
Handler: withMisdirectedRequestCheck(handler),
|
|
ReadHeaderTimeout: 60 * time.Second,
|
|
TLSConfig: tlsConfig,
|
|
}
|
|
if !lo.FromPtr(enableHTTP2) {
|
|
server.TLSNextProto = make(map[string]func(*http.Server, *tls.Conn, http.Handler))
|
|
}
|
|
return server, nil
|
|
}
|
|
|
|
func withMisdirectedRequestCheck(handler http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.TLS != nil {
|
|
tlsServerName, _ := httppkg.CanonicalHost(r.TLS.ServerName)
|
|
host, _ := httppkg.CanonicalHost(r.Host)
|
|
if tlsServerName != "" && tlsServerName != host {
|
|
w.WriteHeader(http.StatusMisdirectedRequest)
|
|
return
|
|
}
|
|
}
|
|
handler.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
|
|
func (s *Server) Reload() (err error) {
|
|
if s.crtPath == "" || s.keyPath == "" {
|
|
return nil
|
|
}
|
|
s.reloadMu.Lock()
|
|
defer s.reloadMu.Unlock()
|
|
old := s.state.Load()
|
|
// Publish the error before releasing reloadMu; status never waits for I/O.
|
|
defer func() {
|
|
if err != nil {
|
|
next := snapshot{err: err}
|
|
if old != nil {
|
|
next.cert, next.certPEM, next.keyPEM = old.cert, old.certPEM, old.keyPEM
|
|
}
|
|
s.state.Store(&next)
|
|
}
|
|
}()
|
|
certPEM, err := os.ReadFile(s.crtPath)
|
|
if err != nil {
|
|
return fmt.Errorf("read certificate: %w", err)
|
|
}
|
|
keyPEM, err := os.ReadFile(s.keyPath)
|
|
if err != nil {
|
|
return fmt.Errorf("read private key: %w", err)
|
|
}
|
|
if old != nil && old.cert != nil && bytes.Equal(certPEM, old.certPEM) && bytes.Equal(keyPEM, old.keyPEM) {
|
|
if old.err != nil {
|
|
next := *old
|
|
next.err = nil
|
|
s.state.Store(&next)
|
|
}
|
|
return nil
|
|
}
|
|
cert, err := tls.X509KeyPair(certPEM, keyPEM)
|
|
if err != nil {
|
|
return fmt.Errorf("parse certificate pair: %w", err)
|
|
}
|
|
s.state.Store(&snapshot{cert: &cert, certPEM: certPEM, keyPEM: keyPEM})
|
|
return nil
|
|
}
|
|
|
|
func (s *Server) ReloadError() error {
|
|
if v := s.state.Load(); v != nil {
|
|
return v.err
|
|
}
|
|
return nil
|
|
}
|