Skip to content

Commit

Permalink
new module
Browse files Browse the repository at this point in the history
  • Loading branch information
Thiago Tognoli committed Oct 28, 2024
1 parent c119a65 commit 19462bb
Show file tree
Hide file tree
Showing 37 changed files with 128 additions and 47 deletions.
40 changes: 25 additions & 15 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
run:
timeout: 3m
skip-files: []
skip-dirs: []
# skip-files: []
# skip-dirs: [geoip2]

linters-settings:
govet:
Expand All @@ -25,21 +25,31 @@ linters-settings:
- FIXME
gofumpt:
extra-rules: true
depguard:
rules:
Main:
allow:
- $gostd
- github.com/thiagotognoli/traefikgeoip
#- github.com/IncSW/geoip2

linters:
enable-all: true
disable:
- deadcode # deprecated
- exhaustivestruct # deprecated
- golint # deprecated
- ifshort # deprecated
- interfacer # deprecated
- maligned # deprecated
- nosnakecase # deprecated
- scopelint # deprecated
- scopelint # deprecated
- structcheck # deprecated
- varcheck # deprecated
#- deadcode # deprecated
#- exhaustivestruct # deprecated
#- golint # deprecated
#- ifshort # deprecated
#- interfacer # deprecated
#- maligned # deprecated
#- nosnakecase # deprecated
#- scopelint # deprecated
#- scopelint # deprecated
#- structcheck # deprecated
#- varcheck # deprecated
#- ifshort #deprecated
- perfsprint
- canonicalheader
- sqlclosecheck # not relevant (SQL)
- rowserrcheck # not relevant (SQL)
- execinquery # not relevant (SQL)
Expand All @@ -53,9 +63,8 @@ linters:
- wsl
- exhaustive
- exhaustruct
- goerr113
- err113
- wrapcheck
- ifshort
- noctx
- lll
- gomnd
Expand All @@ -67,6 +76,7 @@ issues:
max-per-linter: 0
max-same-issues: 0
exclude: []
exclude-dirs: [geoip2]
exclude-rules:
- path: middleware.go
linters:
Expand Down
2 changes: 1 addition & 1 deletion .traefik.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ summary: 'Getting data from MaxMind GeoIP databases and pass it downstream via H

type: middleware

import: github.com/traefik-plugins/traefikgeoip2
import: github.com/thiagotognoli/traefikgeoip

testData:
dbPath: 'GeoLite2-Country.mmdb'
Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ The following snippet should be added to `values.yaml`:
experimental:
plugins:
geoip2:
moduleName: github.com/traefik-plugins/traefikgeoip2
moduleName: github.com/thiagotognoli/traefikgeoip
version: v0.22.0
deployment:
additionalVolumes:
Expand All @@ -47,7 +47,7 @@ deployment:
- "/bin/sh"
- "-ce"
- |
wget -P /tmp https://raw.githubusercontent.com/traefik-plugins/traefikgeoip2/main/geolite2.tgz
wget -P /tmp https://raw.githubusercontent.com/thiagotognoli/traefikgeoip/main/geolite2.tgz
tar --directory /tmp/geoip2 -xvzf /tmp/geolite2.tgz
additionalVolumeMounts:
- name: geoip2
Expand Down Expand Up @@ -79,6 +79,13 @@ PreferXForwardedForHeader | Should `X-Forwarded-For` header be used to extract I

## Development

Install Go, golangci-lint, yaegi and just

```sh
brew install go golangci-lint just
go install github.com/traefik/yaegi/cmd/yaegi@latest
```

To run linter and tests execute this command

```sh
Expand Down
6 changes: 4 additions & 2 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
"mmdb",
"modulename",
"traefikgeoip",
"xvzf"
"xvzf",
"golangci",
"yaegi"
],
"flagWords": [],
"language": "en",
"version": "0.2"
}
}
File renamed without changes.
File renamed without changes.
33 changes: 33 additions & 0 deletions vendor/github.com/IncSW/geoip2/README.md → geoip2/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[Forked from](https://github.com/IncSW/geoip2) to support yaegi with new features

[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
[![Go Report Card](https://goreportcard.com/badge/github.com/IncSW/geoip2?style=flat-square)](https://goreportcard.com/report/github.com/IncSW/geoip2)
[![Go Doc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat-square)](https://pkg.go.dev/github.com/IncSW/geoip2?tab=doc)
Expand Down Expand Up @@ -58,6 +60,37 @@ connection_type-24 3883234 305 ns/op 32 B/op 2 allocs/o
connection_type_parallel-24 34284831 32.1 ns/op 32 B/op 2 allocs/op
```

## Supported databases types

### Country
- GeoIP2-Country
- GeoLite2-Country
- DBIP-Country
- DBIP-Country-Lite

### City
- GeoIP2-City
- GeoLite2-City
- GeoIP2-Enterprise
- DBIP-City-Lite

### ISP
- GeoIP2-ISP

### ASN
- GeoLite2-ASN
- DBIP-ASN-Lite
- DBIP-ASN-Lite (compat=GeoLite2-ASN)

### Connection Type
- GeoIP2-Connection-Type

### Anonymous IP
- GeoIP2-Anonymous-IP

### Domain
- GeoIP2-Domain

## License

[MIT License](LICENSE).
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func readAnonymousIPMap(result *AnonymousIP, buffer []byte, mapSize uint, offset
return 0, err
}
default:
return 0, errors.New("unknown isp key: " + string(key))
return 0, errors.New("unknown anonymous ip key: " + string(key))
}
}
return offset, nil
Expand Down
2 changes: 1 addition & 1 deletion vendor/github.com/IncSW/geoip2/asn.go → geoip2/asn.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func readASNMap(result *ASN, buffer []byte, mapSize uint, offset uint) (uint, er
return 0, err
}
default:
return 0, errors.New("unknown isp key: " + string(key))
return 0, errors.New("unknown asn key: " + string(key))
}
}
return offset, nil
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions vendor/github.com/IncSW/geoip2/isp.go → geoip2/isp.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ func readISPMap(result *ISP, buffer []byte, mapSize uint, offset uint) (uint, er
if err != nil {
return 0, err
}
case "mobile_country_code":
result.MobileCountryCode, offset, err = readString(buffer, offset)
if err != nil {
return 0, err
}
case "mobile_network_code":
result.MobileNetworkCode, offset, err = readString(buffer, offset)
if err != nil {
return 0, err
}
default:
return 0, errors.New("unknown isp key: " + string(key))
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ func NewASNReader(buffer []byte) (*ASNReader, error) {
if err != nil {
return nil, err
}
if reader.metadata.DatabaseType != "GeoLite2-ASN" {
if reader.metadata.DatabaseType != "GeoLite2-ASN" &&
reader.metadata.DatabaseType != "DBIP-ASN-Lite" &&
reader.metadata.DatabaseType != "DBIP-ASN-Lite (compat=GeoLite2-ASN)" {
return nil, errors.New("wrong MaxMind DB ASN type: " + reader.metadata.DatabaseType)
}
return &ASNReader{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ func NewCityReader(buffer []byte) (*CityReader, error) {
}
if reader.metadata.DatabaseType != "GeoIP2-City" &&
reader.metadata.DatabaseType != "GeoLite2-City" &&
reader.metadata.DatabaseType != "GeoIP2-Enterprise" {
reader.metadata.DatabaseType != "GeoIP2-Enterprise" &&
reader.metadata.DatabaseType != "DBIP-City-Lite" {
return nil, errors.New("wrong MaxMind DB City type: " + reader.metadata.DatabaseType)
}
return &CityReader{
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ func NewCountryReader(buffer []byte) (*CountryReader, error) {
return nil, err
}
if reader.metadata.DatabaseType != "GeoIP2-Country" &&
reader.metadata.DatabaseType != "GeoLite2-Country" {
reader.metadata.DatabaseType != "GeoLite2-Country" &&
reader.metadata.DatabaseType != "DBIP-Country" &&
reader.metadata.DatabaseType != "DBIP-Country-Lite" {
return nil, errors.New("wrong MaxMind DB Country type: " + reader.metadata.DatabaseType)
}
return &CountryReader{
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions vendor/github.com/IncSW/geoip2/traits.go → geoip2/traits.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ func readTraitsMap(traits *Traits, buffer []byte, mapSize uint, offset uint) (ui
if err != nil {
return 0, err
}
case "mobile_country_code":
traits.MobileCountryCode, offset, err = readString(buffer, offset)
if err != nil {
return 0, err
}
case "mobile_network_code":
traits.MobileNetworkCode, offset, err = readString(buffer, offset)
if err != nil {
return 0, err
}
default:
return 0, errors.New("unknown traits key: " + string(key))
}
Expand Down
5 changes: 5 additions & 0 deletions vendor/github.com/IncSW/geoip2/types.go → geoip2/types.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package geoip2 is a module for GeoIP
package geoip2

const (
Expand Down Expand Up @@ -72,6 +73,8 @@ type Traits struct {
AutonomousSystemOrganization string // Enterprise
AutonomousSystemNumber uint32 // Enterprise
IsLegitimateProxy bool // Enterprise
MobileCountryCode string // Enterprise
MobileNetworkCode string // Enterprise
IsAnonymousProxy bool
IsSatelliteProvider bool
}
Expand Down Expand Up @@ -101,6 +104,8 @@ type ISP struct {
AutonomousSystemOrganization string
ISP string
Organization string
MobileCountryCode string
MobileNetworkCode string
}

type ConnectionType struct {
Expand Down
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
module github.com/traefik-plugins/traefikgeoip2
module github.com/thiagotognoli/traefikgeoip

go 1.19

require github.com/IncSW/geoip2 v0.1.1
go 1.23
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/IncSW/geoip2 v0.1.1 h1:afzzYF7n9JbdcPy8aiBSgBJuXi4mTWXZ3z6V3o6Vg34=
github.com/IncSW/geoip2 v0.1.1/go.mod h1:adcasR40vXiUBjtzdaTTKL/6wSf+fgO4M8Gve/XzPUk=
#github.com/IncSW/geoip2 v0.1.1 h1:afzzYF7n9JbdcPy8aiBSgBJuXi4mTWXZ3z6V3o6Vg34=
#github.com/IncSW/geoip2 v0.1.1/go.mod h1:adcasR40vXiUBjtzdaTTKL/6wSf+fgO4M8Gve/XzPUk=
6 changes: 5 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ test-yaegi: && _clean-yaegi
set -euox
TMP=$(mktemp -d yaegi.XXXXXX -p /tmp)
WRK="${TMP}/go/src/github.com/traefik-plugins"
WRK="${TMP}/go/src/github.com/thiagotognoli"
mkdir -p ${WRK}
ln -s `pwd` "${WRK}"
cd "${WRK}/$(basename `pwd`)"
env GOPATH="${TMP}/go" yaegi test -v .
# WRKINCSW="${TMP}/go/src/github.com/IncSW"
# mkdir -p ${WRKINCSW}
# ln -s `pwd`/vendor/github.com/IncSW/geoip2 "${WRKINCSW}"
# export GOFLAGS=-mod=vendor

# lint and test
test: _prepare lint test-go test-yaegi
Expand Down
6 changes: 3 additions & 3 deletions middleware.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Package traefikgeoip2 is a Traefik plugin for Maxmind GeoIP2.
package traefikgeoip2
// Package traefikgeoip is a Traefik plugin for Maxmind GeoIP2.
package traefikgeoip

import (
"context"
Expand All @@ -9,7 +9,7 @@ import (
"os"
"strings"

"github.com/IncSW/geoip2"
"github.com/thiagotognoli/traefikgeoip/geoip2"
)

var lookup LookupGeoIP2
Expand Down
14 changes: 7 additions & 7 deletions middleware_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package traefikgeoip2_test
package traefikgeoip_test

import (
"context"
Expand All @@ -7,7 +7,7 @@ import (
"net/http/httptest"
"testing"

mw "github.com/traefik-plugins/traefikgeoip2"
mw "github.com/thiagotognoli/traefikgeoip"
)

const (
Expand Down Expand Up @@ -41,7 +41,7 @@ func TestGeoIPBasic(t *testing.T) {
mwCfg.DBPath = "./GeoLite2-City.mmdb"

called := false
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { called = true })
next := http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) { called = true })

mw.ResetLookup()
instance, err := mw.New(context.TODO(), next, mwCfg, "traefik-geoip2")
Expand All @@ -66,7 +66,7 @@ func TestMissingGeoIPDB(t *testing.T) {
mwCfg.DBPath = "./missing"

called := false
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { called = true })
next := http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) { called = true })

mw.ResetLookup()
instance, err := mw.New(context.TODO(), next, mwCfg, "traefik-geoip2")
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestGeoIPFromRemoteAddr(t *testing.T) {
mwCfg := mw.CreateConfig()
mwCfg.DBPath = "./GeoLite2-City.mmdb"

next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {})
next := http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {})
mw.ResetLookup()
instance, _ := mw.New(context.TODO(), next, mwCfg, "traefik-geoip2")

Expand Down Expand Up @@ -129,7 +129,7 @@ func TestGeoIPFromXForwardedFor(t *testing.T) {
mwCfg.DBPath = "./GeoLite2-City.mmdb"
mwCfg.PreferXForwardedForHeader = true

next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {})
next := http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {})
mw.ResetLookup()
instance, _ := mw.New(context.TODO(), next, mwCfg, "traefik-geoip2")

Expand Down Expand Up @@ -165,7 +165,7 @@ func TestGeoIPCountryDBFromRemoteAddr(t *testing.T) {
mwCfg := mw.CreateConfig()
mwCfg.DBPath = "./GeoLite2-Country.mmdb"

next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {})
next := http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {})
mw.ResetLookup()
instance, _ := mw.New(context.TODO(), next, mwCfg, "traefik-geoip2")

Expand Down
4 changes: 2 additions & 2 deletions types.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package traefikgeoip2
package traefikgeoip

import (
"fmt"
"net"

"github.com/IncSW/geoip2"
"github.com/thiagotognoli/traefikgeoip/geoip2"
)

// Unknown constant for undefined data.
Expand Down
Loading

0 comments on commit 19462bb

Please sign in to comment.