Merge pull request #137 from dirtbags/extract_xforwardedfor_headers

Extract and use X-Forwarded-For headers in mothd logging
This commit is contained in:
int00h5525 2020-03-05 17:59:01 -06:00 committed by GitHub
commit 97bd51ba59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 1 deletions

View File

@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Include basic metadata in mothballs - Include basic metadata in mothballs
- add_script_stream convenience function allows easy script addition to puzzle - add_script_stream convenience function allows easy script addition to puzzle
- Autobuild Docker images to test buildability - Autobuild Docker images to test buildability
- Extract and use X-Forwarded-For headers in mothd logging
### Fixed ### Fixed
- Handle cases where non-legacy puzzles don't have an `author` attribute - Handle cases where non-legacy puzzles don't have an `author` attribute
- Handle YAML-formatted file and script lists as expected - Handle YAML-formatted file and script lists as expected

View File

@ -339,10 +339,22 @@ func (ctx *Instance) ServeHTTP(wOrig http.ResponseWriter, r *http.Request) {
w: wOrig, w: wOrig,
statusCode: new(int), statusCode: new(int),
} }
clientIP := r.RemoteAddr
if (ctx.UseXForwarded) {
forwardedIP := r.Header.Get("X-Forwarded-For")
forwardedIP = strings.Split(forwardedIP, ", ")[0]
if forwardedIP != "" {
clientIP = forwardedIP
}
}
ctx.mux.ServeHTTP(w, r) ctx.mux.ServeHTTP(w, r)
log.Printf( log.Printf(
"%s %s %s %d\n", "%s %s %s %d\n",
r.RemoteAddr, clientIP,
r.Method, r.Method,
r.URL, r.URL,
*w.statusCode, *w.statusCode,

View File

@ -25,6 +25,7 @@ type Instance struct {
StateDir string StateDir string
ThemeDir string ThemeDir string
AttemptInterval time.Duration AttemptInterval time.Duration
UseXForwarded bool
Runtime RuntimeConfig Runtime RuntimeConfig

View File

@ -52,6 +52,12 @@ func main() {
20*time.Second, 20*time.Second,
"Time between maintenance tasks", "Time between maintenance tasks",
) )
flag.BoolVar(
&ctx.UseXForwarded,
"x-forwarded-for",
false,
"Emit IPs from the X-Forwarded-For header in logs, when available, instead of the source IP. Use this when running behind a load-balancer or proxy",
)
listen := flag.String( listen := flag.String(
"listen", "listen",
":8080", ":8080",