-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
141 lines (118 loc) · 3.21 KB
/
main.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"embed"
_ "embed"
"flag"
"github.com/johnpili/ip-reveal/models"
"github.com/johnpili/ip-reveal/page"
"github.com/julienschmidt/httprouter"
"log"
"net/http"
"os"
"strconv"
"time"
)
// region Configurations / Settings
var (
configuration models.Config
//go:embed views/*
views embed.FS
//go:embed countries.json
countriesJson []byte
countries map[string]string
)
//endregion
func main() {
pid := os.Getpid()
err := os.WriteFile("application.pid", []byte(strconv.Itoa(pid)), 0666) // Used to kill this program
if err != nil {
log.Print(err)
}
var configLocation string
flag.StringVar(&configLocation, "config", "config.yml", "Set the location of configuration file")
flag.Parse()
err = loadConfiguration(configLocation, &configuration)
if err != nil {
log.Fatal(err)
}
err = loadCountries(countriesJson, &countries)
if err != nil {
log.Fatal(err)
}
router := httprouter.New()
router.HandlerFunc(http.MethodGet, "/", indexHandler)
router.HandlerFunc(http.MethodGet, "/json", jsonHandler)
router.HandlerFunc(http.MethodGet, "/text", textHandler)
router.HandlerFunc(http.MethodGet, "/txt", textHandler)
router.HandlerFunc(http.MethodGet, "/ip", textHandler)
port := strconv.Itoa(configuration.HTTP.Port)
httpServer := &http.Server{
Addr: ":" + port,
Handler: router,
ReadTimeout: 120 * time.Second,
WriteTimeout: 120 * time.Second,
}
if configuration.HTTP.IsTLS {
log.Printf("Server running at https://localhost:%s%s/\n", port, configuration.HTTP.BasePath)
log.Fatal(httpServer.ListenAndServeTLS(configuration.HTTP.ServerCert, configuration.HTTP.ServerKey))
return
}
log.Printf("Server running at http://localhost:%s%s/\n", port, configuration.HTTP.BasePath)
log.Fatal(httpServer.ListenAndServe())
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
ipInfo := getIPDetails(r)
country, ok := countries[ipInfo.IPCountry]
if !ok {
country = "Unknown"
}
p := page.New()
p.Title = "IP Reveal"
p.SetData(map[string]string{
"IP": ipInfo.IP,
"UserAgent": ipInfo.UserAgent,
"IPCountry": ipInfo.IPCountry,
"Country": country,
})
renderPage(w, r, p, configuration.HTTP.BasePath, "views/base.html", "views/index.html")
}
func jsonHandler(w http.ResponseWriter, r *http.Request) {
ipInfo := getIPDetails(r)
respondWithJSON(w, ipInfo)
}
func textHandler(w http.ResponseWriter, r *http.Request) {
ipInfo := getIPDetails(r)
respondWithPlainText(w, []byte(ipInfo.IP))
}
func getIPDetails(r *http.Request) models.IPInfo {
ip := ""
if len(configuration.Extraction.HeaderKey) > 0 {
ip = r.Header.Get(configuration.Extraction.HeaderKey) // Extract IP from header because we are using reverse proxy example X-Real-Ip
}
if len(ip) == 0 { // Fallback
ip = extractIPAddress(r.RemoteAddr)
}
ipInfo := models.IPInfo{
IP: ip,
UserAgent: r.Header.Get("User-Agent"),
IPCountry: r.Header.Get("CF-IPCountry"),
}
if configuration.Extraction.DebugHeader {
log.Print(r.Header)
}
return ipInfo
}
func extractIPAddress(ip string) string {
if len(ip) > 0 {
for i := len(ip); i >= 0; i-- {
offset := len(ip)
if (i + 1) <= len(ip) {
offset = i + 1
}
if ip[i:offset] == ":" {
return ip[:i]
}
}
}
return ip
}