user detection

master
nate smith 2019-09-06 16:17:17 -05:00
parent a4fe3bab21
commit 69b832c1ab
1 changed files with 57 additions and 8 deletions

65
main.go
View File

@ -15,6 +15,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
"os"
"os/exec" "os/exec"
"strings" "strings"
"time" "time"
@ -67,17 +68,64 @@ func news() []NewsEntry {
return []NewsEntry{} return []NewsEntry{}
} }
func userCount() int { func userCount(users []User) int {
// TODO // TODO
return 0 return 0
} }
func users() []User { func systemUsers() map[string]bool {
// TODO systemUsers := map[string]bool{
return []User{} "ubuntu": true,
"ttadmin": true,
"root": true,
}
envSystemUsers := os.Getenv("SYSTEM_USERS")
if envSystemUsers != "" {
for _, username := range strings.Split(envSystemUsers, ",") {
systemUsers[username] = true
}
}
return systemUsers
} }
func liveUserCount() int { func getUsers() (users []User) {
// For the purposes of this program, we discover users via:
// - presence in /home/
// - absence in systemUsers list (sourced from source code and potentially augmented by an environment variable)
// We formally used passwd parsing. This is definitely more "correct" and I'm
// not opposed to going back to that; going back to parsing /home is mainly to
// get this new version going.
homeDir := os.Getenv("HOMES_DIR")
if homeDir == "" {
homeDir = "/home"
}
out, err := exec.Command("ls", homeDir).Output()
scanner := bufio.NewScanner(bytes.NewReader(out))
if err != nil {
log.Fatalf("could not run who %s", err)
}
systemUsers := systemUsers()
for scanner.Scan() {
username := scanner.Text()
if !systemUsers[username] {
user := User{
Username: username,
// TODO other fields
}
users = append(users, user)
}
}
return users
}
func liveUserCount(users []User) int {
// TODO // TODO
return 0 return 0
} }
@ -109,6 +157,7 @@ func uptime() string {
} }
func tdp() TildeData { func tdp() TildeData {
users := getUsers()
return TildeData{ return TildeData{
Name: "tilde.town", Name: "tilde.town",
URL: "https://tilde.town", URL: "https://tilde.town",
@ -116,9 +165,9 @@ func tdp() TildeData {
WantUsers: true, WantUsers: true,
AdminEmail: "root@tilde.town", AdminEmail: "root@tilde.town",
Description: description, Description: description,
UserCount: userCount(), UserCount: len(users),
Users: users(), Users: users,
LiveUserCount: liveUserCount(), LiveUserCount: liveUserCount(users),
ActiveUserCount: activeUserCount(), ActiveUserCount: activeUserCount(),
Uptime: uptime(), Uptime: uptime(),
News: news(), News: news(),