2015-02-11 04:40:44 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2015-02-24 00:06:28 +00:00
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2015-02-11 04:40:44 +00:00
|
|
|
"io/ioutil"
|
2015-02-24 00:06:28 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2015-02-11 04:40:44 +00:00
|
|
|
"os/exec"
|
|
|
|
"regexp"
|
2015-02-24 00:06:28 +00:00
|
|
|
"strings"
|
2015-02-11 04:40:44 +00:00
|
|
|
"text/template"
|
2015-02-24 00:06:28 +00:00
|
|
|
"time"
|
2015-02-18 05:08:26 +00:00
|
|
|
|
|
|
|
"github.com/thebaer/geo"
|
|
|
|
"github.com/thebaer/tildes/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
locDataJSON = "/home/bear/public_html/where.json"
|
2015-02-11 04:40:44 +00:00
|
|
|
)
|
|
|
|
|
2015-02-18 22:04:48 +00:00
|
|
|
var (
|
2015-02-19 00:12:39 +00:00
|
|
|
hashSecret = ""
|
2015-02-18 22:04:48 +00:00
|
|
|
)
|
|
|
|
|
2015-02-11 04:40:44 +00:00
|
|
|
func main() {
|
|
|
|
// Get arguments
|
|
|
|
outFilePtr := flag.String("f", "where", "Outputted HTML filename (without .html)")
|
2015-02-18 05:08:26 +00:00
|
|
|
geocodeAPIKeyPtr := flag.String("k", "", "Google Geocoding API key")
|
2015-02-19 00:12:39 +00:00
|
|
|
hashSecretPtr := flag.String("s", "", "Secret for hashing usernames")
|
2015-02-11 04:40:44 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2015-02-18 22:04:48 +00:00
|
|
|
// Set globals
|
2015-02-19 00:12:39 +00:00
|
|
|
hashSecret = *hashSecretPtr
|
2015-02-18 22:04:48 +00:00
|
|
|
|
2015-02-11 04:40:44 +00:00
|
|
|
// Get online users with `who`
|
|
|
|
users := who()
|
|
|
|
|
|
|
|
// Fetch user locations based on IP address
|
|
|
|
for i := range users {
|
|
|
|
getGeo(&users[i])
|
2015-02-18 05:08:26 +00:00
|
|
|
getFuzzyCoords(&users[i], *geocodeAPIKeyPtr)
|
2015-02-11 04:40:44 +00:00
|
|
|
}
|
|
|
|
|
2015-02-18 05:08:26 +00:00
|
|
|
// Write user coord data
|
|
|
|
cacheUserLocations(&users)
|
|
|
|
|
2015-02-11 04:40:44 +00:00
|
|
|
// Generate page
|
|
|
|
generate(users, *outFilePtr)
|
|
|
|
}
|
|
|
|
|
2015-02-18 01:48:51 +00:00
|
|
|
type user struct {
|
2015-02-24 00:06:28 +00:00
|
|
|
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
|
2015-02-18 05:08:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type publicUser struct {
|
2015-02-24 00:06:28 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Region string `json:"region"`
|
|
|
|
Country string `json:"country"`
|
|
|
|
Latitude float64 `json:"lat"`
|
2015-02-18 05:08:26 +00:00
|
|
|
Longitude float64 `json:"lng"`
|
2015-02-11 04:40:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var ipRegex = regexp.MustCompile("(([0-9]{1,3}[.-]){3}[0-9]{1,3})")
|
|
|
|
|
2015-02-18 01:48:51 +00:00
|
|
|
func who() []user {
|
2015-02-11 04:40:44 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-02-18 01:48:51 +00:00
|
|
|
users := make([]user, len(ips))
|
2015-02-11 04:40:44 +00:00
|
|
|
i := 0
|
|
|
|
for ip, name := range ips {
|
2015-02-19 00:11:24 +00:00
|
|
|
users[i] = user{Name: name, IP: ip, Public: true, Anonymous: false}
|
|
|
|
|
2015-02-24 00:06:28 +00:00
|
|
|
// Get user permissions, marking if they're not opted-in with a
|
2015-02-19 00:11:24 +00:00
|
|
|
// `.here` file in their $HOME dir.
|
2016-01-05 21:49:42 +00:00
|
|
|
f, err := os.Stat("/home/" + name + "/.here")
|
|
|
|
if os.IsNotExist(err) {
|
2015-02-19 00:11:24 +00:00
|
|
|
users[i].Public = false
|
2016-01-05 21:49:42 +00:00
|
|
|
} 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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-19 00:11:24 +00:00
|
|
|
}
|
|
|
|
if _, err := os.Stat("/home/" + name + "/.somewhere"); err == nil {
|
|
|
|
users[i].Public = true
|
|
|
|
users[i].Anonymous = true
|
|
|
|
}
|
|
|
|
|
2015-02-11 04:40:44 +00:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
return users
|
|
|
|
}
|
|
|
|
|
2015-02-11 05:09:10 +00:00
|
|
|
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 ""
|
|
|
|
}
|
|
|
|
|
2015-02-18 01:48:51 +00:00
|
|
|
func getGeo(u *user) {
|
2015-02-11 04:40:44 +00:00
|
|
|
fmt.Printf("Fetching %s location...\n", u.Name)
|
|
|
|
|
2015-02-12 04:40:52 +00:00
|
|
|
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)
|
|
|
|
}
|
2015-02-11 04:40:44 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2015-02-11 05:09:10 +00:00
|
|
|
u.CurrentTime = getTimeInZone(dat["time_zone"].(string))
|
2015-02-19 00:11:24 +00:00
|
|
|
if u.Public {
|
|
|
|
u.Region = region
|
|
|
|
u.Country = country
|
|
|
|
}
|
2015-02-12 04:40:52 +00:00
|
|
|
}
|
2015-02-11 04:40:44 +00:00
|
|
|
}
|
|
|
|
|
2015-02-18 22:04:48 +00:00
|
|
|
func computeHmac256(message string) string {
|
2015-02-24 00:06:28 +00:00
|
|
|
key := []byte(hashSecret)
|
|
|
|
h := hmac.New(sha256.New, key)
|
|
|
|
h.Write([]byte(message))
|
|
|
|
return base64.StdEncoding.EncodeToString(h.Sum(nil))
|
2015-02-18 22:04:48 +00:00
|
|
|
}
|
|
|
|
|
2015-02-18 05:08:26 +00:00
|
|
|
func getFuzzyCoords(u *user, apiKey string) {
|
2015-02-19 00:11:24 +00:00
|
|
|
if !u.Public {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-18 05:08:26 +00:00
|
|
|
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]
|
2015-02-19 00:11:24 +00:00
|
|
|
|
|
|
|
// 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 = ""
|
|
|
|
}
|
2015-02-18 05:08:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write user data
|
|
|
|
json, _ := json.Marshal(res)
|
|
|
|
store.WriteData(locDataJSON, json)
|
|
|
|
}
|
|
|
|
|
2015-02-11 04:40:44 +00:00
|
|
|
func prettyLocation(region, country string) string {
|
|
|
|
if region != "" {
|
|
|
|
return fmt.Sprintf("%s, %s", region, country)
|
|
|
|
}
|
|
|
|
return country
|
|
|
|
}
|
|
|
|
|
2015-02-18 01:48:51 +00:00
|
|
|
type page struct {
|
2015-02-24 00:06:28 +00:00
|
|
|
Users []user
|
|
|
|
Updated string
|
2015-02-11 04:40:44 +00:00
|
|
|
UpdatedForHumans string
|
|
|
|
}
|
|
|
|
|
2015-02-18 01:48:51 +00:00
|
|
|
func generate(users []user, outputFile string) {
|
2015-02-11 04:40:44 +00:00
|
|
|
fmt.Println("Generating page.")
|
|
|
|
|
|
|
|
f, err := os.Create(os.Getenv("HOME") + "/public_html/" + strings.ToLower(outputFile) + ".html")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-02-24 00:06:28 +00:00
|
|
|
|
2015-02-11 04:40:44 +00:00
|
|
|
defer f.Close()
|
|
|
|
|
2015-02-24 00:06:28 +00:00
|
|
|
funcMap := template.FuncMap{
|
2015-02-11 04:40:44 +00:00
|
|
|
"Location": prettyLocation,
|
|
|
|
}
|
2015-02-24 00:06:28 +00:00
|
|
|
|
2015-02-11 04:40:44 +00:00
|
|
|
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
|
2015-02-18 01:48:51 +00:00
|
|
|
p := &page{Users: users, UpdatedForHumans: updatedReadable, Updated: updated}
|
|
|
|
template.ExecuteTemplate(w, "where", p)
|
2015-02-11 04:40:44 +00:00
|
|
|
w.Flush()
|
|
|
|
|
|
|
|
fmt.Println("DONE!")
|
|
|
|
}
|