echoip/cmd/echoip/main.go

158 lines
3.4 KiB
Go
Raw Normal View History

2015-09-17 20:57:27 +02:00
package main
import (
"io"
2018-03-18 23:12:00 +01:00
"log"
"strings"
2018-03-18 23:12:00 +01:00
2015-09-17 20:57:27 +02:00
"os"
"github.com/BurntSushi/toml"
"github.com/levelsoftware/echoip/cache"
2023-10-05 17:43:02 +02:00
"github.com/levelsoftware/echoip/http"
"github.com/levelsoftware/echoip/iputil"
"github.com/levelsoftware/echoip/iputil/geo"
"github.com/levelsoftware/echoip/iputil/ipstack"
parser "github.com/levelsoftware/echoip/iputil/paser"
2023-09-23 03:21:58 +02:00
ipstackApi "github.com/qioalice/ipstack"
2015-09-17 20:57:27 +02:00
)
2020-09-05 11:40:26 +02:00
type multiValueFlag []string
func (f *multiValueFlag) String() string {
return strings.Join([]string(*f), ", ")
2020-09-05 11:40:26 +02:00
}
func (f *multiValueFlag) Set(v string) error {
*f = append(*f, v)
return nil
}
2020-12-09 21:08:06 +01:00
func init() {
log.SetPrefix("echoip: ")
log.SetFlags(log.Lshortfile)
}
type IPStack struct {
ApiKey string
UseHttps bool
EnableSecurity bool
}
type GeoIP struct {
CountryFile string
CityFile string
AsnFile string
}
type Config struct {
Listen string
TemplateDir string
RedisUrl string
CacheTtl int
ReverseLookup bool
PortLookup bool
ShowSponsor bool
TrustedHeaders []string
Database string
Profile bool
IPStack IPStack
GeoIP GeoIP
}
2020-09-05 11:40:26 +02:00
func main() {
file, err := os.Open("/etc/echoip/config.toml")
if err != nil {
panic(err)
}
defer file.Close()
var config Config
b, err := io.ReadAll(file)
if err != nil {
panic(err)
}
err = toml.Unmarshal(b, &config)
if err != nil {
panic(err)
2020-12-03 22:28:27 +01:00
}
2015-09-17 20:57:27 +02:00
2023-09-23 02:15:10 +02:00
var parser parser.Parser
if config.Database == "geoip" {
log.Print("Using GeoIP for IP database")
geo, err := geo.Open(
config.GeoIP.CountryFile,
config.GeoIP.CityFile,
config.GeoIP.AsnFile,
)
2023-09-23 02:15:10 +02:00
if err != nil {
log.Fatal(err)
}
parser = &geo
2023-09-23 03:28:49 +02:00
}
if config.Database == "ipstack" {
log.Print("Using GeoIP for IP database")
if config.IPStack.EnableSecurity {
log.Print("Enable Security Module ( Requires Professional Plus account )")
}
enableSecurity := ipstackApi.ParamEnableSecurity(config.IPStack.EnableSecurity)
apiKey := ipstackApi.ParamToken(config.IPStack.ApiKey)
useHttps := ipstackApi.ParamUseHTTPS(config.IPStack.UseHttps)
if config.IPStack.UseHttps {
log.Print("Use IP Stack HTTPS API ( Requires non-free account )")
}
if err := ipstackApi.Init(apiKey, enableSecurity, useHttps); err != nil {
2023-09-23 02:15:10 +02:00
log.Fatal(err)
}
2023-09-23 03:21:58 +02:00
ips := ipstack.IPStack{}
2023-09-23 02:15:10 +02:00
parser = &ips
2023-09-23 03:28:49 +02:00
}
var serverCache cache.Cache
if len(config.RedisUrl) > 0 {
redisCache, err := cache.RedisCache(config.RedisUrl)
serverCache = &redisCache
if err != nil {
log.Fatal(err)
}
} else {
serverCache = &cache.Null{}
}
server := http.New(parser, serverCache, config.CacheTtl, config.Profile)
server.IPHeaders = config.TrustedHeaders
if _, err := os.Stat(config.TemplateDir); err == nil {
server.Template = config.TemplateDir
2018-12-28 15:05:31 +01:00
} else {
log.Printf("Not configuring default handler: Template not found: %s", config.TemplateDir)
2018-12-28 15:05:31 +01:00
}
if config.ReverseLookup {
2016-04-15 20:14:16 +02:00
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
}
if config.PortLookup {
2016-04-17 11:09:56 +02:00
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 config.ShowSponsor {
2020-12-14 19:02:35 +01:00
log.Println("Enabling sponsor logo")
server.Sponsor = config.ShowSponsor
}
if len(config.TrustedHeaders) > 0 {
log.Printf("Trusting remote IP from header(s): %s", config.TrustedHeaders)
2018-12-28 14:51:20 +01:00
}
if config.Profile {
2020-09-05 12:21:02 +02:00
log.Printf("Enabling profiling handlers")
}
log.Printf("Listening on http://%s", config.Listen)
if err := server.ListenAndServe(config.Listen); err != nil {
2015-09-17 20:57:27 +02:00
log.Fatal(err)
}
}