echoip/main.go

72 lines
2.3 KiB
Go
Raw Normal View History

2015-09-17 20:57:27 +02:00
package main
import (
2016-10-09 16:18:38 +02:00
"net/http"
2015-09-17 20:57:27 +02:00
flags "github.com/jessevdk/go-flags"
"os"
2016-08-13 20:32:19 +02:00
"github.com/Sirupsen/logrus"
2017-03-14 18:17:15 +01:00
"github.com/mpolden/ipd/api"
2015-09-17 20:57:27 +02:00
)
func main() {
var opts struct {
2016-04-17 11:28:47 +02:00
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:""`
2015-09-21 18:03:52 +02:00
Listen string `short:"l" long:"listen" description:"Listening address" value-name:"ADDR" default:":8080"`
2016-04-15 20:14:16 +02:00
ReverseLookup bool `short:"r" long:"reverse-lookup" description:"Perform reverse hostname lookups"`
2016-04-17 11:09:56 +02:00
PortLookup bool `short:"p" long:"port-lookup" description:"Enable port lookup"`
2016-10-09 16:21:07 +02:00
Template string `short:"t" long:"template" description:"Path to template" default:"index.html" value-name:"FILE"`
IPHeader string `short:"H" long:"trusted-header" description:"Header to trust for remote IP, if present (e.g. X-Real-IP)" value-name:"NAME"`
2016-08-13 20:32:19 +02:00
LogLevel string `short:"L" long:"log-level" description:"Log level to use" default:"info" choice:"debug" choice:"info" choice:"warn" choice:"error" choice:"fatal" choice:"panic"`
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
}
2016-08-13 20:32:19 +02:00
log := logrus.New()
level, err := logrus.ParseLevel(opts.LogLevel)
if err != nil {
log.Fatal(err)
}
log.Level = level
2016-04-17 11:09:56 +02:00
oracle := api.NewOracle()
2016-04-15 20:14:16 +02:00
if opts.ReverseLookup {
log.Println("Enabling reverse lookup")
2016-04-17 11:09:56 +02:00
oracle.EnableLookupAddr()
2016-04-15 20:14:16 +02:00
}
2016-04-17 11:09:56 +02:00
if opts.PortLookup {
log.Println("Enabling port lookup")
oracle.EnableLookupPort()
2016-04-15 20:52:15 +02:00
}
2016-04-17 11:28:47 +02:00
if opts.CountryDBPath != "" {
log.Printf("Enabling country lookup (using database: %s)", opts.CountryDBPath)
if err := oracle.EnableLookupCountry(opts.CountryDBPath); err != nil {
log.Fatal(err)
}
}
if opts.CityDBPath != "" {
log.Printf("Enabling city lookup (using database: %s)", opts.CityDBPath)
if err := oracle.EnableLookupCity(opts.CityDBPath); err != nil {
2015-09-17 20:57:27 +02:00
log.Fatal(err)
}
}
if opts.IPHeader != "" {
log.Printf("Trusting header %s to contain correct remote IP", opts.IPHeader)
}
2016-04-17 11:09:56 +02:00
2016-08-13 20:32:19 +02:00
api := api.New(oracle, log)
2016-04-15 20:14:16 +02:00
api.Template = opts.Template
api.IPHeader = opts.IPHeader
2015-09-17 20:57:27 +02:00
2015-09-18 17:13:14 +02:00
log.Printf("Listening on %s", opts.Listen)
2016-10-09 16:18:38 +02:00
if err := http.ListenAndServe(opts.Listen, api.Router()); err != nil {
2015-09-17 20:57:27 +02:00
log.Fatal(err)
}
}