-
Notifications
You must be signed in to change notification settings - Fork 135
/
telize.go
83 lines (70 loc) · 1.66 KB
/
telize.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* Telize 4.0.0
* Copyright (c) 2013-2023, Frederic Cambus
* https://www.telize.com
*
* Created: 2013-08-15
* Last Updated: 2023-11-28
*
* Telize is released under the BSD 2-Clause license.
* See LICENSE file for details.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
package main
import (
"flag"
"fmt"
"github.com/go-chi/chi/v5"
"github.com/oschwald/maxminddb-golang"
"log"
"net/http"
"os"
)
var asn *maxminddb.Reader
var city *maxminddb.Reader
func main() {
var err error
host := flag.String("host", "127.0.0.1", "Set the server host")
port := flag.String("port", "8080", "Set the server port")
version := flag.Bool("version", false, "Display version")
flag.Usage = func() {
fmt.Println("\nUSAGE:")
flag.PrintDefaults()
}
flag.Parse()
if *version {
fmt.Println("Telize 4.0.0")
os.Exit(0)
}
asn, err = maxminddb.Open("/var/db/GeoIP/GeoLite2-ASN.mmdb")
if err != nil {
log.Fatal(err)
}
defer asn.Close()
city, err = maxminddb.Open("/var/db/GeoIP/GeoLite2-City.mmdb")
if err != nil {
log.Fatal(err)
}
defer city.Close()
address := *host + ":" + *port
r := chi.NewRouter()
r.Get("/ip", ip)
r.Get("/jsonip", jsonip)
r.Get("/geoip", location)
r.Get("/geoip/", location)
r.Get("/geoip/{ip}", location)
r.Get("/location", location)
r.Get("/location/", location)
r.Get("/location/{ip}", location)
r.Head("/ip", ip)
r.Head("/jsonip", jsonip)
r.Head("/geoip", location)
r.Head("/geoip/", location)
r.Head("/geoip/{ip}", location)
r.Head("/location", location)
r.Head("/location/", location)
r.Head("/location/{ip}", location)
fmt.Print("Listening on: http://", address+"\n")
log.Fatal(http.ListenAndServe(address, r))
}