log real client ip behind a HTTPS gateway (#38)

* log real client ip behind a HTTPS gateway

* fix tab/space indent

* better compatible for apache/nginx log default format

* add  config option
This commit is contained in:
qyb
2019-03-16 05:36:52 +08:00
committed by Star Brilliant
parent 871604f577
commit 2df81db465
3 changed files with 15 additions and 1 deletions

View File

@@ -40,6 +40,7 @@ type config struct {
TCPOnly bool `toml:"tcp_only"`
Verbose bool `toml:"verbose"`
DebugHTTPHeaders []string `toml:"debug_http_headers"`
LogGuessedIP bool `toml:"log_guessed_client_ip"`
}
func loadConfig(path string) (*config, error) {

View File

@@ -38,3 +38,7 @@ tcp_only = false
# Enable logging
verbose = false
# Enable log IP from HTTPS-reverse proxy header: X-Forwarded-For or X-Real-IP
# Note: http uri/useragent log cannot be controlled by this config
log_guessed_client_ip = false

View File

@@ -30,6 +30,7 @@ import (
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"strconv"
"strings"
@@ -94,7 +95,15 @@ func (s *Server) parseRequestIETF(ctx context.Context, w http.ResponseWriter, r
} else {
questionType = strconv.FormatUint(uint64(question.Qtype), 10)
}
fmt.Printf("%s - - [%s] \"%s %s %s\"\n", r.RemoteAddr, time.Now().Format("02/Jan/2006:15:04:05 -0700"), questionName, questionClass, questionType)
var clientip net.IP = nil
if s.conf.LogGuessedIP {
clientip = s.findClientIP(r)
}
if clientip != nil {
fmt.Printf("%s - - [%s] \"%s %s %s\"\n", clientip, time.Now().Format("02/Jan/2006:15:04:05 -0700"), questionName, questionClass, questionType)
} else {
fmt.Printf("%s - - [%s] \"%s %s %s\"\n", r.RemoteAddr, time.Now().Format("02/Jan/2006:15:04:05 -0700"), questionName, questionClass, questionType)
}
}
transactionID := msg.Id