echoip/cmd/ipd/main.go

56 lines
1.8 KiB
Go
Raw Normal View History

2015-09-17 20:57:27 +02:00
package main
import (
2018-03-18 23:12:00 +01:00
"log"
2015-09-17 20:57:27 +02:00
flags "github.com/jessevdk/go-flags"
"os"
2018-02-10 13:24:32 +01:00
"github.com/mpolden/ipd/http"
2018-02-10 14:35:12 +01:00
"github.com/mpolden/ipd/iputil"
2018-08-14 21:00:46 +02:00
"github.com/mpolden/ipd/iputil/geo"
2015-09-17 20:57:27 +02:00
)
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:""`
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"`
2015-09-17 20:57:27 +02:00
}
_, err := flags.ParseArgs(&opts, os.Args)
if err != nil {
2016-04-15 20:14:16 +02:00
os.Exit(1)
2015-09-17 20:57:27 +02:00
}
2018-03-18 23:12:00 +01:00
log := log.New(os.Stderr, "ipd: ", 0)
2018-08-14 21:00:46 +02:00
r, err := geo.Open(opts.CountryDBPath, opts.CityDBPath)
2018-02-11 11:19:50 +01:00
if err != nil {
log.Fatal(err)
2018-02-10 14:35:12 +01:00
}
2018-02-11 11:19:50 +01:00
2018-08-14 21:00:46 +02:00
server := http.New(r)
2018-02-11 11:19:50 +01:00
server.Template = opts.Template
server.IPHeaders = opts.IPHeaders
2016-04-15 20:14:16 +02:00
if opts.ReverseLookup {
log.Println("Enabling reverse lookup")
2018-02-11 11:19:50 +01:00
server.LookupAddr = iputil.LookupAddr
2016-04-15 20:14:16 +02:00
}
2016-04-17 11:09:56 +02:00
if opts.PortLookup {
log.Println("Enabling port lookup")
2018-02-11 11:19:50 +01:00
server.LookupPort = iputil.LookupPort
2015-09-17 20:57:27 +02:00
}
if len(opts.IPHeaders) > 0 {
log.Printf("Trusting header(s) %+v to contain correct remote IP", opts.IPHeaders)
}
2016-04-17 11:09:56 +02:00
2017-11-29 19:24:18 +01:00
log.Printf("Listening on http://%s", opts.Listen)
2018-02-10 13:24:32 +01:00
if err := server.ListenAndServe(opts.Listen); err != nil {
2015-09-17 20:57:27 +02:00
log.Fatal(err)
}
}