-
Notifications
You must be signed in to change notification settings - Fork 88
Support multiple IP addresses for http_x_forwarded_for
#45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
package main | ||
|
||
import ( | ||
gonx "../.." | ||
"flag" | ||
"fmt" | ||
gonx "github.com/satyrius/gonx" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you change it from relative path? It was very convenient for development. |
||
"io" | ||
"os" | ||
"strings" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
package main | ||
|
||
import ( | ||
gonx "../.." | ||
"flag" | ||
"fmt" | ||
gonx "github.com/satyrius/gonx" | ||
"io" | ||
"os" | ||
"strings" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
package main | ||
|
||
import ( | ||
gonx "../.." | ||
"flag" | ||
"fmt" | ||
gonx "github.com/satyrius/gonx" | ||
"io" | ||
"os" | ||
"strings" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,11 @@ type Parser struct { | |
regexp *regexp.Regexp | ||
} | ||
|
||
func getSpecialNginxRegexes() map[string]string { | ||
return map[string]string{ | ||
"http_x_forwarded_for": `[^, ]*(?:, ?[^, ]+)*`} | ||
} | ||
|
||
// NewParser returns a new Parser, use given log format to create its internal | ||
// strings parsing regexp. | ||
func NewParser(format string) *Parser { | ||
|
@@ -32,10 +37,21 @@ func NewParser(format string) *Parser { | |
) | ||
} | ||
|
||
// Second replace each fileds to regexp grouping | ||
quotedFormat := regexp.QuoteMeta(preparedFormat + " ") | ||
re := regexp.MustCompile(`\\\$([A-Za-z0-9_]+)(?:\\\$[A-Za-z0-9_])*(\\?([^$\\A-Za-z0-9_]))`).ReplaceAllString( | ||
quotedFormat, "(?P<$1>[^$3]*)$2") | ||
formatRegex := regexp.MustCompile(`([^$ ]*)\$([a-z_]+)([^$ ]*)([ ]?)`) | ||
specialNginxRegexes := getSpecialNginxRegexes() | ||
fields := formatRegex.FindAllStringSubmatch(preparedFormat+" ", -1) | ||
re := formatRegex.ReplaceAllString(preparedFormat+" ", "$2$4") | ||
for _, field := range fields { | ||
terminateChar := field[3] | ||
if len([]rune(terminateChar)) == 0 { | ||
terminateChar = field[4] | ||
} | ||
if specialRegex, found := specialNginxRegexes[field[2]]; found { | ||
re = strings.Replace(re, field[2]+field[4], regexp.QuoteMeta(field[1])+"(?P<"+field[2]+">"+specialRegex+")"+regexp.QuoteMeta(field[3])+field[4], 1) | ||
} else { | ||
re = strings.Replace(re, field[2]+field[4], regexp.QuoteMeta(field[1])+"(?P<"+field[2]+">[^"+terminateChar+"]*)"+regexp.QuoteMeta(field[3]+field[4]), 1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like code duplication, please, make it more readable. |
||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the patch above, please, consider adding more comments around to explain what's going on. |
||
|
||
// Finally remove placeholder | ||
re = regexp.MustCompile(fmt.Sprintf(".%s", placeholder)).ReplaceAllString(re, "") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sort versions alphabetically plz