aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Polden <mpolden@mpolden.no>2020-09-05 11:40:26 +0200
committerMartin Polden <mpolden@mpolden.no>2020-09-05 11:41:18 +0200
commit0caa5873b9d2dd8e32e116fcc01ae52de5dba3c4 (patch)
treef5cf56fe292ada68a9f465de4eefb3f67d94619d
parent702c1f1e23f34d5aaff90ae6a13ea070d05c79ed (diff)
cmd: Remove go-flags
-rw-r--r--README.md33
-rw-r--r--cmd/echoip/main.go77
-rw-r--r--go.mod5
-rw-r--r--go.sum2
-rw-r--r--http/cache_test.go16
5 files changed, 78 insertions, 55 deletions
diff --git a/README.md b/README.md
index 855babc..a74b6e4 100644
--- a/README.md
+++ b/README.md
@@ -111,20 +111,21 @@ Hub](https://hub.docker.com/r/mpolden/echoip), which can be downloaded with:
```
$ echoip -h
-Usage:
- echoip [OPTIONS]
-
-Application Options:
- -f, --country-db=FILE Path to GeoIP country database
- -c, --city-db=FILE Path to GeoIP city database
- -a, --asn-db=FILE Path to GeoIP ASN database
- -l, --listen=ADDR Listening address (default: :8080)
- -r, --reverse-lookup Perform reverse hostname lookups
- -p, --port-lookup Enable port lookup
- -t, --template=FILE Path to template (default: index.html)
- -H, --trusted-header=NAME Header to trust for remote IP, if present (e.g. X-Real-IP)
- -C, --cache-size=SIZE Size of response cache. Set to 0 to disable
-
-Help Options:
- -h, --help Show this help message
+Usage of echoip:
+ -C int
+ Size of response cache. Set to 0 to disable
+ -H value
+ Header to trust for remote IP, if present (e.g. X-Real-IP)
+ -a string
+ Path to GeoIP ASN database
+ -c string
+ Path to GeoIP city database
+ -f string
+ Path to GeoIP country database
+ -l string
+ Listening address (default ":8080")
+ -p Enable port lookup
+ -r Perform reverse hostname lookups
+ -t string
+ Path to template (default "index.html")
```
diff --git a/cmd/echoip/main.go b/cmd/echoip/main.go
index 7e6d6cf..ef0965f 100644
--- a/cmd/echoip/main.go
+++ b/cmd/echoip/main.go
@@ -1,10 +1,9 @@
package main
import (
+ "flag"
"log"
- flags "github.com/jessevdk/go-flags"
-
"os"
"github.com/mpolden/echoip/http"
@@ -12,54 +11,66 @@ import (
"github.com/mpolden/echoip/iputil/geo"
)
-func main() {
- var opts struct {
- CountryDBPath string `short:"f" long:"country-db" description:"Path to GeoIP country database" value-name:"FILE" default:""`
- CityDBPath string `short:"c" long:"city-db" description:"Path to GeoIP city database" value-name:"FILE" default:""`
- ASNDBPath string `short:"a" long:"asn-db" description:"Path to GeoIP ASN database" value-name:"FILE" default:""`
- Listen string `short:"l" long:"listen" description:"Listening address" value-name:"ADDR" default:":8080"`
- ReverseLookup bool `short:"r" long:"reverse-lookup" description:"Perform reverse hostname lookups"`
- PortLookup bool `short:"p" long:"port-lookup" description:"Enable port lookup"`
- Template string `short:"t" long:"template" description:"Path to template" default:"index.html" value-name:"FILE"`
- IPHeaders []string `short:"H" long:"trusted-header" description:"Header to trust for remote IP, if present (e.g. X-Real-IP)" value-name:"NAME"`
- CacheCapacity int `short:"C" long:"cache-size" description:"Size of response cache. Set to 0 to disable" value-name:"SIZE"`
- }
- _, err := flags.ParseArgs(&opts, os.Args)
- if err != nil {
- os.Exit(1)
+type multiValueFlag []string
+
+func (f *multiValueFlag) String() string {
+ vs := ""
+ for i, v := range *f {
+ vs += v
+ if i < len(*f)-1 {
+ vs += ", "
+ }
}
+ return vs
+}
+
+func (f *multiValueFlag) Set(v string) error {
+ *f = append(*f, v)
+ return nil
+}
+
+func main() {
+ countryFile := flag.String("f", "", "Path to GeoIP country database")
+ cityFile := flag.String("c", "", "Path to GeoIP city database")
+ asnFile := flag.String("a", "", "Path to GeoIP ASN database")
+ listen := flag.String("l", ":8080", "Listening address")
+ reverseLookup := flag.Bool("r", false, "Perform reverse hostname lookups")
+ portLookup := flag.Bool("p", false, "Enable port lookup")
+ template := flag.String("t", "index.html", "Path to template")
+ cacheSize := flag.Int("C", 0, "Size of response cache. Set to 0 to disable")
+ var headers multiValueFlag
+ flag.Var(&headers, "H", "Header to trust for remote IP, if present (e.g. X-Real-IP)")
+ flag.Parse()
log := log.New(os.Stderr, "echoip: ", 0)
- r, err := geo.Open(opts.CountryDBPath, opts.CityDBPath, opts.ASNDBPath)
+ r, err := geo.Open(*countryFile, *cityFile, *asnFile)
if err != nil {
log.Fatal(err)
}
- cache := http.NewCache(opts.CacheCapacity)
+ cache := http.NewCache(*cacheSize)
server := http.New(r, cache)
- server.IPHeaders = opts.IPHeaders
- if _, err := os.Stat(opts.Template); err == nil {
- server.Template = opts.Template
+ server.IPHeaders = headers
+ if _, err := os.Stat(*template); err == nil {
+ server.Template = *template
} else {
- log.Printf("Not configuring default handler: Template not found: %s", opts.Template)
+ log.Printf("Not configuring default handler: Template not found: %s", *template)
}
- if opts.ReverseLookup {
+ if *reverseLookup {
log.Println("Enabling reverse lookup")
server.LookupAddr = iputil.LookupAddr
}
- if opts.PortLookup {
+ if *portLookup {
log.Println("Enabling port lookup")
server.LookupPort = iputil.LookupPort
}
- if len(opts.IPHeaders) > 0 {
- log.Printf("Trusting header(s) %+v to contain correct remote IP", opts.IPHeaders)
+ if len(headers) > 0 {
+ log.Printf("Trusting remote IP from header(s): %s", headers.String())
}
-
- listen := opts.Listen
- if listen == ":8080" {
- listen = "0.0.0.0:8080"
+ if *cacheSize > 0 {
+ log.Printf("Cache capacity set to %d", *cacheSize)
}
- log.Printf("Listening on http://%s", listen)
- if err := server.ListenAndServe(opts.Listen); err != nil {
+ log.Printf("Listening on http://%s", *listen)
+ if err := server.ListenAndServe(*listen); err != nil {
log.Fatal(err)
}
}
diff --git a/go.mod b/go.mod
index 68390e9..cca3101 100644
--- a/go.mod
+++ b/go.mod
@@ -2,7 +2,4 @@ module github.com/mpolden/echoip
go 1.13
-require (
- github.com/jessevdk/go-flags v1.4.0
- github.com/oschwald/geoip2-golang v1.4.0
-)
+require github.com/oschwald/geoip2-golang v1.4.0
diff --git a/go.sum b/go.sum
index ab9ccf5..36bcb8c 100644
--- a/go.sum
+++ b/go.sum
@@ -1,7 +1,5 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
-github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA=
-github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/oschwald/geoip2-golang v1.4.0 h1:5RlrjCgRyIGDz/mBmPfnAF4h8k0IAcRv9PvrpOfz+Ug=
github.com/oschwald/geoip2-golang v1.4.0/go.mod h1:8QwxJvRImBH+Zl6Aa6MaIcs5YdlZSTKtzmPGzQqi9ng=
github.com/oschwald/maxminddb-golang v1.6.0 h1:KAJSjdHQ8Kv45nFIbtoLGrGWqHFajOIm7skTyz/+Dls=
diff --git a/http/cache_test.go b/http/cache_test.go
index c16c068..dd16e2b 100644
--- a/http/cache_test.go
+++ b/http/cache_test.go
@@ -4,8 +4,24 @@ import (
"fmt"
"net"
"testing"
+ "unsafe"
)
+func TestCache(t *testing.T) {
+
+ c := NewCache(10)
+
+ for i := 0; i < 100; i++ {
+ ip := net.ParseIP(fmt.Sprintf("192.0.2.%d", i))
+ r := &Response{IP: ip}
+ fmt.Println(unsafe.Sizeof(r))
+ c.Set(ip, r)
+ }
+
+ fmt.Println(len(c.entries))
+ fmt.Println(len(c.keys))
+}
+
func TestCacheCapacity(t *testing.T) {
var tests = []struct {
addCount, capacity, size int