Skip to content

Commit

Permalink
Add FilterSpam option
Browse files Browse the repository at this point in the history
  • Loading branch information
Jipok committed Feb 19, 2024
1 parent 3361b44 commit ac95251
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ SSH.Enabled = false # Default true
# Used if target not specifies in some [[Domains]] section
# And for direct access via IP address in manual or self-signed mode
DefaultTarget = "8080"
# If true, will filter spam to stdout from http/https servers.
# Messages like`http: TLS handshake error ...` occur due to
# bots/crawlers checking all public addresses.
FilterSpam = true
# If true will drop privileges if started from root.
# Will not be able to save state(tokens) between restarts.
DropPrivileges = false
Expand Down
30 changes: 30 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"log"
"strings"
)

type filteredLogger struct {
Logger *log.Logger
}

func (fl *filteredLogger) Write(p []byte) (n int, err error) {
msg := string(p)

// https://github.com/golang/go/issues/26918
if strings.HasPrefix(msg, "http: TLS handshake error") {
return len(p), nil
}

return fl.Logger.Writer().Write(p)
}

// Less spam from bots/crawlers
func newServerErrorLog() *log.Logger {
if cfg.FilterSpam {
return log.New(&filteredLogger{log.Default()}, "", 0)
} else {
return log.Default()
}
}
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Config struct {
Cert string
Key string
}
FilterSpam bool
DropPrivileges bool
Listen string // Interface to listen
DefaultTarget string
Expand Down Expand Up @@ -101,6 +102,7 @@ func main() {
cfg.SSH.AuthorizedKeys = "~/.ssh/authorized_keys"
cfg.Certificate.Type = "self-signed"
cfg.DefaultTarget = "8080"
cfg.FilterSpam = true // Less spam like `http: TLS handshake error...`
cfg.DropPrivileges = false // Drop privileges if started from root
cfg.Listen = "0.0.0.0"
cfg.RedirectHTTP = true // Start server on 80 port that will redirect all to 443 port
Expand Down
9 changes: 8 additions & 1 deletion ssl-proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,13 @@ func startWebServer() {
http.Redirect(w, r, "https://"+r.Host+r.RequestURI, http.StatusMovedPermanently)
}
go func() {
err := http.ListenAndServe(cfg.Listen+":80", http.HandlerFunc(redirectTLS))
// TODO MaxHeaderBytes and timeouts to read/write/idle
httpServer := http.Server{
Addr: cfg.Listen + ":80",
Handler: http.HandlerFunc(redirectTLS),
ErrorLog: newServerErrorLog(),
}
err := httpServer.ListenAndServe()
if err != nil {
log.Fatal("HTTP redirection server failure", err)
}
Expand All @@ -159,6 +165,7 @@ func startWebServer() {
Addr: address,
TLSConfig: m.TLSConfig(),
Handler: mux,
ErrorLog: newServerErrorLog(),
}
err = s.ListenAndServeTLS("", "")
} else {
Expand Down

0 comments on commit ac95251

Please sign in to comment.