-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
134 lines (111 loc) · 2.5 KB
/
server.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
package main
import (
"github.com/etix/geoip"
"github.com/go-martini/martini"
"github.com/martini-contrib/render"
"log"
"net"
"net/http"
"net/url"
"strings"
)
var (
geo *geoip.GeoIP
)
type result struct {
ReqHostname string
ClientIP string
ClientFR bool
ServerIP string
ServerFR bool
AnyFR bool
}
func getIP(remoteAddr string) string {
r := strings.Split(remoteAddr, ":")
return r[0]
}
func lookupIP(host string) (string, error) {
addr, err := net.LookupHost(host)
if err != nil {
return "", err
}
for _, a := range addr {
// Exclude ipv6 since we do not use the GeoIP Lite with v6 support
if !strings.Contains(a, ":") {
return a, nil
}
}
return addr[0], nil
}
func resultPage(r render.Render, req *http.Request, u *url.URL) {
var err error
data := &result{}
data.ReqHostname = u.Host
data.ServerIP, err = lookupIP(u.Host)
// Use the forwarded IP if any
ip := req.Header.Get("X-Real-IP")
if ip == "" {
ip = req.RemoteAddr
}
data.ClientIP = getIP(ip)
if err != nil {
r.HTML(200, "index", "Adresse invalide")
return
}
if country, _ := geo.GetCountry(data.ClientIP); country == "FR"
|| country == "PM"
|| country == "MF"
|| country == "BL"
|| country == "GP"
|| country == "MQ"
|| country == "GF"
|| country == "RE"
|| country == "YT"
|| country == "NC"
|| country == "PF"
|| country == "WF"
|| country == "TF" {
data.ClientFR = true
}
if country, _ := geo.GetCountry(data.ServerIP); country == "FR"
|| country == "PM"
|| country == "MF"
|| country == "BL"
|| country == "GP"
|| country == "MQ"
|| country == "GF"
|| country == "RE"
|| country == "YT"
|| country == "NC"
|| country == "PF"
|| country == "WF"
|| country == "TF" {
data.ServerFR = true
}
data.AnyFR = data.ClientFR || data.ServerFR
r.HTML(200, "result", data)
}
func main() {
var err error
geo, err = geoip.Open()
if err != nil {
log.Fatal(err)
}
m := martini.Classic()
m.Use(render.Renderer())
m.Use(martini.Static("static"))
m.Get("/", func(r render.Render, req *http.Request) {
query := req.URL.Query()
q := query.Get("q")
if q != "" && !strings.HasPrefix(q, "http://") && !strings.HasPrefix(q, "https://") {
q = "http://" + q
}
u, err := url.Parse(q)
if err != nil || q == "" {
r.HTML(200, "index", nil)
} else {
resultPage(r, req, u)
}
})
m.RunOnAddr("127.0.0.1:8080")
}