cmd: Remove go-flags

This commit is contained in:
Martin Polden 2020-09-05 11:40:26 +02:00
parent 702c1f1e23
commit 0caa5873b9
5 changed files with 78 additions and 55 deletions

View File

@ -111,20 +111,21 @@ Hub](https://hub.docker.com/r/mpolden/echoip), which can be downloaded with:
``` ```
$ echoip -h $ echoip -h
Usage: Usage of echoip:
echoip [OPTIONS] -C int
Size of response cache. Set to 0 to disable
Application Options: -H value
-f, --country-db=FILE Path to GeoIP country database Header to trust for remote IP, if present (e.g. X-Real-IP)
-c, --city-db=FILE Path to GeoIP city database -a string
-a, --asn-db=FILE Path to GeoIP ASN database Path to GeoIP ASN database
-l, --listen=ADDR Listening address (default: :8080) -c string
-r, --reverse-lookup Perform reverse hostname lookups Path to GeoIP city database
-p, --port-lookup Enable port lookup -f string
-t, --template=FILE Path to template (default: index.html) Path to GeoIP country database
-H, --trusted-header=NAME Header to trust for remote IP, if present (e.g. X-Real-IP) -l string
-C, --cache-size=SIZE Size of response cache. Set to 0 to disable Listening address (default ":8080")
-p Enable port lookup
Help Options: -r Perform reverse hostname lookups
-h, --help Show this help message -t string
Path to template (default "index.html")
``` ```

View File

@ -1,10 +1,9 @@
package main package main
import ( import (
"flag"
"log" "log"
flags "github.com/jessevdk/go-flags"
"os" "os"
"github.com/mpolden/echoip/http" "github.com/mpolden/echoip/http"
@ -12,54 +11,66 @@ import (
"github.com/mpolden/echoip/iputil/geo" "github.com/mpolden/echoip/iputil/geo"
) )
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() { func main() {
var opts struct { countryFile := flag.String("f", "", "Path to GeoIP country database")
CountryDBPath string `short:"f" long:"country-db" description:"Path to GeoIP country database" value-name:"FILE" default:""` cityFile := flag.String("c", "", "Path to GeoIP city database")
CityDBPath string `short:"c" long:"city-db" description:"Path to GeoIP city database" value-name:"FILE" default:""` asnFile := flag.String("a", "", "Path to GeoIP ASN database")
ASNDBPath string `short:"a" long:"asn-db" description:"Path to GeoIP ASN database" value-name:"FILE" default:""` listen := flag.String("l", ":8080", "Listening address")
Listen string `short:"l" long:"listen" description:"Listening address" value-name:"ADDR" default:":8080"` reverseLookup := flag.Bool("r", false, "Perform reverse hostname lookups")
ReverseLookup bool `short:"r" long:"reverse-lookup" description:"Perform reverse hostname lookups"` portLookup := flag.Bool("p", false, "Enable port lookup")
PortLookup bool `short:"p" long:"port-lookup" description:"Enable port lookup"` template := flag.String("t", "index.html", "Path to template")
Template string `short:"t" long:"template" description:"Path to template" default:"index.html" value-name:"FILE"` cacheSize := flag.Int("C", 0, "Size of response cache. Set to 0 to disable")
IPHeaders []string `short:"H" long:"trusted-header" description:"Header to trust for remote IP, if present (e.g. X-Real-IP)" value-name:"NAME"` var headers multiValueFlag
CacheCapacity int `short:"C" long:"cache-size" description:"Size of response cache. Set to 0 to disable" value-name:"SIZE"` flag.Var(&headers, "H", "Header to trust for remote IP, if present (e.g. X-Real-IP)")
} flag.Parse()
_, err := flags.ParseArgs(&opts, os.Args)
if err != nil {
os.Exit(1)
}
log := log.New(os.Stderr, "echoip: ", 0) 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 { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
cache := http.NewCache(opts.CacheCapacity) cache := http.NewCache(*cacheSize)
server := http.New(r, cache) server := http.New(r, cache)
server.IPHeaders = opts.IPHeaders server.IPHeaders = headers
if _, err := os.Stat(opts.Template); err == nil { if _, err := os.Stat(*template); err == nil {
server.Template = opts.Template server.Template = *template
} else { } 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") log.Println("Enabling reverse lookup")
server.LookupAddr = iputil.LookupAddr server.LookupAddr = iputil.LookupAddr
} }
if opts.PortLookup { if *portLookup {
log.Println("Enabling port lookup") log.Println("Enabling port lookup")
server.LookupPort = iputil.LookupPort server.LookupPort = iputil.LookupPort
} }
if len(opts.IPHeaders) > 0 { if len(headers) > 0 {
log.Printf("Trusting header(s) %+v to contain correct remote IP", opts.IPHeaders) log.Printf("Trusting remote IP from header(s): %s", headers.String())
} }
if *cacheSize > 0 {
listen := opts.Listen log.Printf("Cache capacity set to %d", *cacheSize)
if listen == ":8080" {
listen = "0.0.0.0:8080"
} }
log.Printf("Listening on http://%s", listen) log.Printf("Listening on http://%s", *listen)
if err := server.ListenAndServe(opts.Listen); err != nil { if err := server.ListenAndServe(*listen); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

5
go.mod
View File

@ -2,7 +2,4 @@ module github.com/mpolden/echoip
go 1.13 go 1.13
require ( require github.com/oschwald/geoip2-golang v1.4.0
github.com/jessevdk/go-flags v1.4.0
github.com/oschwald/geoip2-golang v1.4.0
)

2
go.sum
View File

@ -1,7 +1,5 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= 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/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 h1:5RlrjCgRyIGDz/mBmPfnAF4h8k0IAcRv9PvrpOfz+Ug=
github.com/oschwald/geoip2-golang v1.4.0/go.mod h1:8QwxJvRImBH+Zl6Aa6MaIcs5YdlZSTKtzmPGzQqi9ng= github.com/oschwald/geoip2-golang v1.4.0/go.mod h1:8QwxJvRImBH+Zl6Aa6MaIcs5YdlZSTKtzmPGzQqi9ng=
github.com/oschwald/maxminddb-golang v1.6.0 h1:KAJSjdHQ8Kv45nFIbtoLGrGWqHFajOIm7skTyz/+Dls= github.com/oschwald/maxminddb-golang v1.6.0 h1:KAJSjdHQ8Kv45nFIbtoLGrGWqHFajOIm7skTyz/+Dls=

View File

@ -4,8 +4,24 @@ import (
"fmt" "fmt"
"net" "net"
"testing" "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) { func TestCacheCapacity(t *testing.T) {
var tests = []struct { var tests = []struct {
addCount, capacity, size int addCount, capacity, size int