mirror of
https://github.com/FrankerFaceZ/FrankerFaceZ.git
synced 2025-08-02 16:08:31 +00:00
Reject over capacity before finishing TLS handshake
This commit is contained in:
parent
26cddf5271
commit
a0b3e049d0
2 changed files with 28 additions and 12 deletions
|
@ -81,7 +81,8 @@ func main() {
|
||||||
Addr: conf.SSLListenAddr,
|
Addr: conf.SSLListenAddr,
|
||||||
Handler: http.DefaultServeMux,
|
Handler: http.DefaultServeMux,
|
||||||
TLSConfig: &tls.Config{
|
TLSConfig: &tls.Config{
|
||||||
GetCertificate: reloader.GetCertificateFunc(),
|
GetCertificate: reloader.GetCertificateFunc(),
|
||||||
|
GetConfigForClient: server.TLSEarlyReject,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
go func() {
|
go func() {
|
||||||
|
|
|
@ -2,6 +2,7 @@ package server // import "github.com/FrankerFaceZ/FrankerFaceZ/socketserver/serv
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"crypto/tls"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -239,6 +240,29 @@ var BannerHTML []byte
|
||||||
// StopAcceptingConnectionsCh is closed while the server is shutting down.
|
// StopAcceptingConnectionsCh is closed while the server is shutting down.
|
||||||
var StopAcceptingConnectionsCh = make(chan struct{})
|
var StopAcceptingConnectionsCh = make(chan struct{})
|
||||||
|
|
||||||
|
func shouldRejectConnection() bool {
|
||||||
|
memFreeKB := atomic.LoadUint64(&Statistics.SysMemFreeKB)
|
||||||
|
if memFreeKB > 0 && memFreeKB < Configuration.MinMemoryKBytes {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
curClients := atomic.LoadUint64(&Statistics.CurrentClientCount)
|
||||||
|
if Configuration.MaxClientCount != 0 && curClients >= Configuration.MaxClientCount {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
var errEarlyTLSReject = errors.New("over capacity")
|
||||||
|
|
||||||
|
func TLSEarlyReject(*tls.ClientHelloInfo) (*tls.Config, error) {
|
||||||
|
if shouldRejectConnection() {
|
||||||
|
return nil, errEarlyTLSReject
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
// HTTPHandleRootURL is the http.HandleFunc for requests on `/`.
|
// HTTPHandleRootURL is the http.HandleFunc for requests on `/`.
|
||||||
// It either uses the SocketUpgrader or writes out the BannerHTML.
|
// It either uses the SocketUpgrader or writes out the BannerHTML.
|
||||||
func HTTPHandleRootURL(w http.ResponseWriter, r *http.Request) {
|
func HTTPHandleRootURL(w http.ResponseWriter, r *http.Request) {
|
||||||
|
@ -251,21 +275,12 @@ func HTTPHandleRootURL(w http.ResponseWriter, r *http.Request) {
|
||||||
if strings.Contains(strings.ToLower(r.Header.Get("Connection")), "upgrade") {
|
if strings.Contains(strings.ToLower(r.Header.Get("Connection")), "upgrade") {
|
||||||
updateSysMem()
|
updateSysMem()
|
||||||
|
|
||||||
if Statistics.SysMemFreeKB > 0 && Statistics.SysMemFreeKB < Configuration.MinMemoryKBytes {
|
if shouldRejectConnection() {
|
||||||
w.WriteHeader(503)
|
w.WriteHeader(503)
|
||||||
fmt.Fprint(w, "error: low memory")
|
fmt.Fprint(w, "connection rejected: over capacity")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if Configuration.MaxClientCount != 0 {
|
|
||||||
curClients := atomic.LoadUint64(&Statistics.CurrentClientCount)
|
|
||||||
if curClients >= Configuration.MaxClientCount {
|
|
||||||
w.WriteHeader(503)
|
|
||||||
fmt.Fprint(w, "error: client limit reached")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
conn, err := SocketUpgrader.Upgrade(w, r, nil)
|
conn, err := SocketUpgrader.Upgrade(w, r, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(w, "error: %v", err)
|
fmt.Fprintf(w, "error: %v", err)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue