Skip to content

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
language: go
sudo: false
go:
- 1.1
- 1.2
- 1.2.2
- 1.3
- 1.4
- 1.5
- 1.12
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sort versions alphabetically plz

- 1.13
- tip
go_import_path: github.com/satyrius/gonx
install: make deps
script:
- go test -v -bench .
Expand Down
2 changes: 1 addition & 1 deletion example/common/common.go
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"
Copy link
Owner

Choose a reason for hiding this comment

The 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"
Expand Down
2 changes: 1 addition & 1 deletion example/nginx/nginx.go
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"
Expand Down
2 changes: 1 addition & 1 deletion example/reduce/reduce.go
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"
Expand Down
24 changes: 20 additions & 4 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like code duplication, please, make it more readable.

}
}
Copy link
Owner

Choose a reason for hiding this comment

The 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, "")
Expand Down
26 changes: 14 additions & 12 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func TestParser(t *testing.T) {
Convey("Test Parser", t, func() {
Convey("Parse custom format", func() {
format := "$remote_addr [$time_local] \"$request\" $status"
format := "$remote_addr [$time_local] \"$request\" $status $http_x_forwarded_for"
parser := NewParser(format)

Convey("Ensure parser format is ok", func() {
Expand All @@ -18,29 +18,31 @@ func TestParser(t *testing.T) {

Convey("Test format to regexp translation", func() {
So(parser.regexp.String(), ShouldEqual,
`^(?P<remote_addr>[^ ]*) \[(?P<time_local>[^]]*)\] "(?P<request>[^"]*)" (?P<status>[^ ]*)`)
`^(?P<remote_addr>[^ ]*) \[(?P<time_local>[^]]*)\] "(?P<request>[^"]*)" (?P<status>[^ ]*) (?P<http_x_forwarded_for>[^, ]*(?:, ?[^, ]+)*)`)
})

Convey("ParseString", func() {
line := `89.234.89.123 [08/Nov/2013:13:39:18 +0000] "GET /api/foo/bar HTTP/1.1" 200`
line := `89.234.89.123 [08/Nov/2013:13:39:18 +0000] "GET /api/foo/bar HTTP/1.1" 200 1.2.3.4,5.6.7.8, 9.10.11.12`
expected := NewEntry(Fields{
"remote_addr": "89.234.89.123",
"time_local": "08/Nov/2013:13:39:18 +0000",
"request": "GET /api/foo/bar HTTP/1.1",
"status": "200",
"remote_addr": "89.234.89.123",
"time_local": "08/Nov/2013:13:39:18 +0000",
"request": "GET /api/foo/bar HTTP/1.1",
"status": "200",
"http_x_forwarded_for": "1.2.3.4,5.6.7.8, 9.10.11.12",
})
entry, err := parser.ParseString(line)
So(err, ShouldBeNil)
So(entry, ShouldResemble, expected)
})

Convey("Handle empty values", func() {
line := `89.234.89.123 [08/Nov/2013:13:39:18 +0000] "" 200`
line := `89.234.89.123 [08/Nov/2013:13:39:18 +0000] "" 200 1.2.3.4`
expected := NewEntry(Fields{
"remote_addr": "89.234.89.123",
"time_local": "08/Nov/2013:13:39:18 +0000",
"request": "",
"status": "200",
"remote_addr": "89.234.89.123",
"time_local": "08/Nov/2013:13:39:18 +0000",
"request": "",
"status": "200",
"http_x_forwarded_for": "1.2.3.4",
})
entry, err := parser.ParseString(line)
So(err, ShouldBeNil)
Expand Down