-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwhere.go
309 lines (253 loc) · 6.46 KB
/
where.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package main
import (
"bufio"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"regexp"
"strings"
"text/template"
"time"
"github.com/thebaer/geo"
"github.com/thebaer/tildes/store"
)
const (
locDataJSON = "/home/bear/public_html/where.json"
)
var (
hashSecret = ""
)
func main() {
// Get arguments
outFilePtr := flag.String("f", "where", "Outputted HTML filename (without .html)")
geocodeAPIKeyPtr := flag.String("k", "", "Google Geocoding API key")
hashSecretPtr := flag.String("s", "", "Secret for hashing usernames")
flag.Parse()
// Set globals
hashSecret = *hashSecretPtr
// Get online users with `who`
users := who()
// Fetch user locations based on IP address
for i := range users {
getGeo(&users[i])
getFuzzyCoords(&users[i], *geocodeAPIKeyPtr)
}
// Write user coord data
cacheUserLocations(&users)
// Generate page
generate(users, *outFilePtr)
}
type user struct {
Name string `json:"name"`
IP string `json:"ip"`
Region string `json:"region"`
Country string `json:"country"`
CurrentTime string `json:"current_time"`
Latitude float64 `json:"lat"`
Longitude float64 `json:"lng"`
Public bool
Anonymous bool
}
type publicUser struct {
Name string `json:"name"`
Region string `json:"region"`
Country string `json:"country"`
Latitude float64 `json:"lat"`
Longitude float64 `json:"lng"`
}
var ipRegex = regexp.MustCompile("(([0-9]{1,3}[.-]){3}[0-9]{1,3})")
func who() []user {
fmt.Println("who --ips")
cmd := exec.Command("who", "--ips")
stdout, err := cmd.StdoutPipe()
if err != nil {
fmt.Println(err)
}
if err := cmd.Start(); err != nil {
fmt.Println(err)
}
ips := make(map[string]string)
r := bufio.NewReader(stdout)
scanner := bufio.NewScanner(r)
for scanner.Scan() {
lineParts := strings.Split(scanner.Text(), " ")
name := lineParts[0]
fmt.Println(name)
// Extract IP address
ipMatch := ipRegex.FindAllString(scanner.Text(), 1)
if len(ipMatch) == 0 {
continue
}
// Normalize any host names with dashes
newIp := strings.Replace(ipMatch[0], "-", ".", -1)
ips[newIp] = name
}
users := make([]user, len(ips))
i := 0
for ip, name := range ips {
users[i] = user{Name: name, IP: ip, Public: true, Anonymous: false}
// Get user permissions, marking if they're not opted-in with a
// `.here` file in their $HOME dir.
f, err := os.Stat("/home/" + name + "/.here")
if os.IsNotExist(err) {
users[i].Public = false
} else {
if f.Size() > 0 {
// There's something in the file! Maybe a present?
data, err := ioutil.ReadFile("/home/" + name + "/.here")
if err != nil {
fmt.Printf("Error reading ~/%s/.here: %v\n", name, err)
} else {
fmt.Printf("Read ~/%s/.here: %s\n", name, data)
// Match IP
ipMatch := ipRegex.FindAll(data, 1)
if len(ipMatch) > 0 {
users[i].IP = strings.TrimSpace(string(data))
}
}
}
}
if _, err := os.Stat("/home/" + name + "/.somewhere"); err == nil {
users[i].Public = true
users[i].Anonymous = true
}
i++
}
return users
}
func getTimeInZone(tz string) string {
cmd := exec.Command("date", "+%A %H:%M")
cmd.Env = append(cmd.Env, fmt.Sprintf("TZ=%s", tz))
stdout, err := cmd.StdoutPipe()
if err != nil {
fmt.Println(err)
}
if err := cmd.Start(); err != nil {
fmt.Println(err)
}
r := bufio.NewReader(stdout)
scanner := bufio.NewScanner(r)
for scanner.Scan() {
return scanner.Text()
}
return ""
}
func getGeo(u *user) {
fmt.Printf("Fetching %s location...\n", u.Name)
response, err := http.Get(fmt.Sprintf("https://freegeoip.net/json/%s", u.IP))
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
} else {
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
}
var dat map[string]interface{}
if err := json.Unmarshal(contents, &dat); err != nil {
fmt.Println(err)
return
}
region := dat["region_name"].(string)
country := dat["country_name"].(string)
u.CurrentTime = getTimeInZone(dat["time_zone"].(string))
if u.Public {
u.Region = region
u.Country = country
}
}
}
func computeHmac256(message string) string {
key := []byte(hashSecret)
h := hmac.New(sha256.New, key)
h.Write([]byte(message))
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}
func getFuzzyCoords(u *user, apiKey string) {
if !u.Public {
return
}
fmt.Printf("Fetching %s fuzzy coordinates...\n", u.Name)
loc := prettyLocation(u.Region, u.Country)
addr, err := geo.Geocode(loc, apiKey)
if err != nil {
fmt.Println(err)
return
}
u.Latitude = addr.Lat
u.Longitude = addr.Lng
}
func cacheUserLocations(users *[]user) {
// Read user data
res := &map[string]publicUser{}
if err := json.Unmarshal(store.ReadData(locDataJSON), &res); err != nil {
fmt.Println(err)
}
// Update user data
for i := range *users {
u := (*users)[i]
// Don't save users who are private
if !u.Public {
continue
}
// Hide user's name if they want to remain anonymous
var displayName string
if !u.Anonymous {
displayName = u.Name
}
(*res)[computeHmac256(u.Name)] = publicUser{Name: displayName, Region: u.Region, Country: u.Country, Latitude: u.Latitude, Longitude: u.Longitude}
// Now that we have the info we need, remove it from the page's user list
if u.Anonymous {
(*users)[i].Region = ""
(*users)[i].Country = ""
}
}
// Write user data
json, _ := json.Marshal(res)
store.WriteData(locDataJSON, json)
}
func prettyLocation(region, country string) string {
if region != "" {
return fmt.Sprintf("%s, %s", region, country)
}
return country
}
type page struct {
Users []user
Updated string
UpdatedForHumans string
}
func generate(users []user, outputFile string) {
fmt.Println("Generating page.")
f, err := os.Create(os.Getenv("HOME") + "/public_html/" + strings.ToLower(outputFile) + ".html")
if err != nil {
panic(err)
}
defer f.Close()
funcMap := template.FuncMap{
"Location": prettyLocation,
}
w := bufio.NewWriter(f)
template, err := template.New("").Funcs(funcMap).ParseFiles("../templates/where.html")
if err != nil {
panic(err)
}
// Extra page data
curTime := time.Now().UTC()
updatedReadable := curTime.Format(time.RFC1123)
updated := curTime.Format(time.RFC3339)
// Generate the page
p := &page{Users: users, UpdatedForHumans: updatedReadable, Updated: updated}
template.ExecuteTemplate(w, "where", p)
w.Flush()
fmt.Println("DONE!")
}