From 01ec91df04b079246434952a5d8f34c6d4e3914b Mon Sep 17 00:00:00 2001 From: "kali kaneko (leap communications)" Date: Tue, 31 Aug 2021 21:32:55 +0200 Subject: [feat] make api configurable --- README.rst | 3 ++- gateways.go | 15 +++++---------- main.go | 17 ++++++++++++++++- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/README.rst b/README.rst index 06ccbf1..f5b2896 100644 --- a/README.rst +++ b/README.rst @@ -26,7 +26,8 @@ You can use ``geoipupdate`` to download MaxMind's City database:: Usage ----------------------- - +-api + domain for the api. Default: black.riseup.net -geodb path to the GeoLite2-City database (default is "/var/lib/GeoIP/GeoLite2-City.mmdb") -port diff --git a/gateways.go b/gateways.go index 05220c3..c4597a0 100644 --- a/gateways.go +++ b/gateways.go @@ -21,16 +21,10 @@ import ( "net/http" ) -const ( - // yes, I am cheating. The config file is also exposed on the top-level - // domain, which is served behind a letsencrypt certificate. this saves passing - // the certificate for the ca etc. - eipAPI = "https://black.riseup.net/1/config/eip-service.json" -) - type bonafide struct { client *http.Client eip *eipService + eipAPI string } type eipService struct { @@ -55,9 +49,10 @@ type coordinates struct { Longitude float64 } -func newBonafide() *bonafide { +func newBonafide(apiDomain string) *bonafide { client := &http.Client{} - return &bonafide{client, nil} + eipAPI := apiDomain + "/3/config/eip-service.json" + return &bonafide{client, nil, eipAPI} } func (b *bonafide) getGateways() ([]gateway, error) { @@ -71,7 +66,7 @@ func (b *bonafide) getGateways() ([]gateway, error) { } func (b *bonafide) fetchEipJSON() error { - resp, err := b.client.Post(eipAPI, "", nil) + resp, err := b.client.Post(b.eipAPI, "", nil) if err != nil { return err } diff --git a/main.go b/main.go index 811e737..0c21c7c 100644 --- a/main.go +++ b/main.go @@ -36,6 +36,13 @@ import ( "github.com/tidwall/cities" ) +const ( + // The config file is also exposed on the top-level + // domain, which is served behind a letsencrypt certificate. this saves passing + // the certificate for the ca etc. + apiForRiseup = "https://black.riseup.net" +) + func floatToString(num float64) string { return strconv.FormatFloat(num, 'f', 6, 64) } @@ -213,6 +220,7 @@ func main() { var port = flag.Int("port", 9001, "port where the service listens on") var metricsPort = flag.Int("metricsPort", 9002, "port where the metrics server listens on") var dbpath = flag.String("geodb", "/var/lib/GeoIP/GeoLite2-City.mmdb", "path to the GeoLite2-City database") + var api = flag.String("api", "", "API to fetch eip-service.json from (default: black.riseup.net)") var notls = flag.Bool("notls", false, "disable TLS on the service") var key = flag.String("server_key", "", "path to the key file for TLS") var crt = flag.String("server_crt", "", "path to the cert file for TLS") @@ -234,6 +242,13 @@ func main() { } } + var configuredAPI string + if *api == "" { + configuredAPI = apiForRiseup + } else { + configuredAPI = "https://" + *api + } + db, err := geoip2.Open(*dbpath) if err != nil { log.Fatal(err) @@ -244,7 +259,7 @@ func main() { geoipdb := geodb{db, forbidden, nil, nil, nil, &earth} log.Println("Seeding gateway list...") - bonafide := newBonafide() + bonafide := newBonafide(configuredAPI) bonafide.getGateways() geoipdb.geolocateGateways(bonafide) -- cgit v1.2.3