2023-01-19 20:42:46 +05:30
|
|
|
package pages
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"regexp"
|
|
|
|
"runtime"
|
2023-07-14 15:54:20 +05:30
|
|
|
"sort"
|
2023-01-23 20:30:41 +05:30
|
|
|
"strconv"
|
2023-01-19 20:42:46 +05:30
|
|
|
"strings"
|
2023-07-11 02:53:29 +05:30
|
|
|
|
2024-09-28 23:41:58 +05:30
|
|
|
log "log/slog"
|
|
|
|
|
2023-07-11 02:53:29 +05:30
|
|
|
"github.com/ProjectSegfault/publapi/utils"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/spf13/viper"
|
2023-01-19 20:42:46 +05:30
|
|
|
)
|
|
|
|
|
2024-09-29 00:20:57 +05:30
|
|
|
type UserStruct struct {
|
2023-01-19 20:42:46 +05:30
|
|
|
Status int `json:"status"`
|
|
|
|
Online int `json:"online"`
|
|
|
|
Total int `json:"total"`
|
2024-09-29 00:20:57 +05:30
|
|
|
Users []UserInfo `json:"users"`
|
2023-01-19 20:42:46 +05:30
|
|
|
}
|
|
|
|
|
2024-09-29 00:20:57 +05:30
|
|
|
type UserInfo struct {
|
2023-01-21 16:55:08 +05:30
|
|
|
Name string `json:"name"`
|
|
|
|
FullName string `json:"fullName"`
|
|
|
|
Desc string `json:"desc"`
|
|
|
|
Online bool `json:"online"`
|
2023-01-23 19:30:34 +05:30
|
|
|
Op bool `json:"op"`
|
2023-01-23 20:30:41 +05:30
|
|
|
Created int `json:"created"`
|
2023-01-21 16:55:08 +05:30
|
|
|
Email string `json:"email"`
|
|
|
|
Matrix string `json:"matrix"`
|
|
|
|
Fediverse string `json:"fediverse"`
|
|
|
|
Website string `json:"website"`
|
|
|
|
Capsule string `json:"capsule"`
|
|
|
|
Loc string `json:"loc"`
|
2023-01-19 20:42:46 +05:30
|
|
|
}
|
|
|
|
|
2024-09-29 00:20:57 +05:30
|
|
|
type ByAdminAndName []UserInfo
|
2023-07-14 15:54:20 +05:30
|
|
|
|
|
|
|
func (a ByAdminAndName) Len() int { return len(a) }
|
|
|
|
func (a ByAdminAndName) Less(i, j int) bool {
|
|
|
|
// If one person is an admin and the other is not, place the admin first
|
|
|
|
if a[i].Op && !a[j].Op {
|
|
|
|
return true
|
|
|
|
} else if !a[i].Op && a[j].Op {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// If both persons are admins or both are not, sort by name
|
|
|
|
return a[i].Name < a[j].Name
|
|
|
|
}
|
|
|
|
func (a ByAdminAndName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
|
2024-09-28 23:41:58 +05:30
|
|
|
func UserError(message string, username string, err error) {
|
2024-09-29 15:59:59 +05:30
|
|
|
log.Error("message", message, log.Any("err", err), "username", username)
|
2024-09-28 23:41:58 +05:30
|
|
|
}
|
|
|
|
|
2024-09-29 00:20:57 +05:30
|
|
|
func userData(username, usersonline, ops string) UserInfo {
|
2023-01-19 20:42:46 +05:30
|
|
|
regex := "(^| )" + username + "($| )"
|
|
|
|
isonline, err := regexp.MatchString(string(regex), string(usersonline))
|
|
|
|
if err != nil {
|
2024-09-28 23:41:58 +05:30
|
|
|
UserError("couldn't get online status for user", username, err)
|
2023-01-19 20:42:46 +05:30
|
|
|
}
|
2023-01-23 20:23:05 +05:30
|
|
|
isop, operr := regexp.MatchString(string(regex), string(ops))
|
|
|
|
if operr != nil {
|
2024-09-28 23:41:58 +05:30
|
|
|
UserError("couldn't get op status for user", username, operr)
|
2023-01-23 20:23:05 +05:30
|
|
|
}
|
|
|
|
cmd := "/usr/bin/stat -c %W /home/" + username
|
|
|
|
crd, crerr := exec.Command("bash", "-c", cmd).Output()
|
|
|
|
if crerr != nil {
|
2024-09-28 23:41:58 +05:30
|
|
|
UserError("couldn't stat user", username, crerr)
|
2023-01-23 20:23:05 +05:30
|
|
|
}
|
2024-09-29 18:05:33 +05:30
|
|
|
crdstr := strings.TrimSuffix(string(crd), "\n")
|
2023-02-26 20:46:38 +05:30
|
|
|
filename := "/home/" + username + "/meta-info.toml"
|
2024-10-06 19:54:46 +05:30
|
|
|
filestat, error := os.Stat(filename)
|
2023-01-19 20:42:46 +05:30
|
|
|
if error != nil {
|
|
|
|
if os.IsNotExist(error) {
|
2024-09-29 15:59:59 +05:30
|
|
|
UserError("user doesn't have a meta-info.toml", username, error)
|
2024-09-29 00:20:57 +05:30
|
|
|
var user UserInfo
|
2023-01-19 20:42:46 +05:30
|
|
|
user.Name = username
|
2023-01-23 20:30:41 +05:30
|
|
|
user.Created, _ = strconv.Atoi(crdstr)
|
2024-09-28 23:41:58 +05:30
|
|
|
if isonline {
|
2023-01-19 20:42:46 +05:30
|
|
|
user.Online = true
|
|
|
|
} else {
|
|
|
|
user.Online = false
|
|
|
|
}
|
2024-09-28 23:41:58 +05:30
|
|
|
if isop {
|
2023-01-23 19:30:34 +05:30
|
|
|
user.Op = true
|
|
|
|
} else {
|
|
|
|
user.Op = false
|
|
|
|
}
|
2023-01-19 20:42:46 +05:30
|
|
|
return user
|
|
|
|
}
|
|
|
|
}
|
2024-10-06 19:54:46 +05:30
|
|
|
// Check file size of meta-info.toml. If it's over 100 kB, bail
|
|
|
|
if filestat.Size() > 100_000 {
|
|
|
|
log.Error("user's meta-info.toml is above the 100 kB file size limit", "user", username)
|
|
|
|
user := UserInfo{
|
|
|
|
Name: username,
|
|
|
|
Online: false,
|
|
|
|
Op: false,
|
|
|
|
}
|
|
|
|
user.Created, _ = strconv.Atoi(crdstr)
|
|
|
|
if isonline {
|
|
|
|
user.Online = true
|
|
|
|
}
|
|
|
|
if isop {
|
|
|
|
user.Op = true
|
|
|
|
}
|
|
|
|
return user
|
|
|
|
}
|
2023-01-19 20:42:46 +05:30
|
|
|
viper.SetConfigFile(filename)
|
2024-09-28 21:28:17 +05:30
|
|
|
if err := viper.ReadInConfig(); err != nil {
|
2024-09-29 15:59:59 +05:30
|
|
|
log.Error("message", "couldn't read a users meta-info.toml file.", "error", log.Any("err", err), "user", username)
|
2024-09-29 00:20:57 +05:30
|
|
|
user := UserInfo{
|
2024-09-28 21:28:17 +05:30
|
|
|
Name: username,
|
|
|
|
}
|
|
|
|
user.Created, _ = strconv.Atoi(crdstr)
|
|
|
|
if isonline {
|
|
|
|
user.Online = true
|
|
|
|
} else {
|
|
|
|
user.Online = false
|
|
|
|
}
|
|
|
|
if isop {
|
|
|
|
user.Op = true
|
|
|
|
} else {
|
|
|
|
user.Op = false
|
|
|
|
}
|
|
|
|
return user
|
|
|
|
}
|
2024-09-29 00:20:57 +05:30
|
|
|
user := UserInfo{
|
|
|
|
Name: username,
|
|
|
|
FullName: viper.GetString("fullname"),
|
|
|
|
Capsule: viper.GetString("gemini"),
|
|
|
|
Website: viper.GetString("website"),
|
|
|
|
Desc: viper.GetString("description"),
|
|
|
|
Email: viper.GetString("email"),
|
|
|
|
Matrix: viper.GetString("matrix"),
|
|
|
|
Fediverse: viper.GetString("fediverse"),
|
|
|
|
Loc: viper.GetString("location"),
|
|
|
|
Op: false,
|
|
|
|
Online: false,
|
|
|
|
}
|
2023-01-23 20:30:41 +05:30
|
|
|
user.Created, _ = strconv.Atoi(crdstr)
|
2024-09-28 23:41:58 +05:30
|
|
|
if isop {
|
2023-01-23 19:30:34 +05:30
|
|
|
user.Op = true
|
|
|
|
}
|
2024-09-28 23:41:58 +05:30
|
|
|
if isonline {
|
2023-01-19 20:42:46 +05:30
|
|
|
user.Online = true
|
|
|
|
}
|
|
|
|
return user
|
|
|
|
}
|
|
|
|
|
|
|
|
func UsersPage(c *fiber.Ctx) error {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
"message": "/users is not supported on Windows",
|
|
|
|
"status": c.Response().StatusCode(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
// Get the number of users online
|
2024-09-29 00:20:57 +05:30
|
|
|
usersOnline, err := exec.Command("bash", "-c", "/usr/bin/users").Output()
|
2023-01-19 20:42:46 +05:30
|
|
|
if err != nil {
|
2024-09-28 23:41:58 +05:30
|
|
|
log.Error("error", log.Any("error", err))
|
2023-01-19 20:42:46 +05:30
|
|
|
return c.SendStatus(fiber.StatusInternalServerError)
|
|
|
|
}
|
2024-09-29 00:20:57 +05:30
|
|
|
usersOnlineStr := string(usersOnline)
|
|
|
|
usersOnlineDedup := utils.Dedup(usersOnlineStr)
|
2023-01-19 20:42:46 +05:30
|
|
|
var output int
|
2024-09-29 00:20:57 +05:30
|
|
|
if strings.Contains(usersOnlineDedup, " ") {
|
|
|
|
outputa := int(strings.Count(usersOnlineDedup, " "))
|
2023-01-21 17:13:29 +05:30
|
|
|
output = outputa + 1
|
2024-09-29 00:20:57 +05:30
|
|
|
} else if usersOnlineDedup == "" {
|
2023-01-21 17:13:29 +05:30
|
|
|
output = 0
|
|
|
|
} else {
|
|
|
|
output = 1
|
|
|
|
}
|
2023-01-23 19:30:34 +05:30
|
|
|
// Get OPs
|
|
|
|
ops, opserr := exec.Command("bash", "-c", "/usr/bin/members sudo").Output()
|
|
|
|
if opserr != nil {
|
2024-09-28 23:50:51 +05:30
|
|
|
log.Error("couldn't get ops", "error", log.Any("err", opserr))
|
2023-01-23 19:30:34 +05:30
|
|
|
return c.SendStatus(fiber.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
opstr := string(ops)
|
|
|
|
// Get all users
|
2023-01-19 20:42:46 +05:30
|
|
|
users, err2 := exec.Command("bash", "-c", "/usr/bin/ls /home").Output()
|
|
|
|
if err2 != nil {
|
2024-09-28 23:41:58 +05:30
|
|
|
log.Error("couldn't get all users", "error", log.Any("err", err2))
|
2023-01-19 20:42:46 +05:30
|
|
|
return c.SendStatus(fiber.StatusInternalServerError)
|
|
|
|
}
|
2024-09-29 15:59:59 +05:30
|
|
|
userstr := strings.TrimSuffix(string(users), "\n")
|
|
|
|
usersarr := strings.Split(userstr, "\n")
|
2023-01-23 19:30:34 +05:30
|
|
|
// Fill user info
|
2024-09-29 15:59:59 +05:30
|
|
|
var userInfoArray []UserInfo
|
2023-01-19 20:42:46 +05:30
|
|
|
for i := 0; i < len(usersarr); i++ {
|
|
|
|
uname := string(usersarr[i])
|
2024-09-29 15:59:59 +05:30
|
|
|
userInfoArray = append(
|
|
|
|
userInfoArray,
|
2024-09-29 00:20:57 +05:30
|
|
|
userData(
|
2023-07-11 02:53:29 +05:30
|
|
|
uname,
|
2024-09-29 00:20:57 +05:30
|
|
|
strings.TrimSuffix(usersOnlineDedup, "\n"),
|
2023-07-11 02:53:29 +05:30
|
|
|
strings.TrimSuffix(opstr, "\n"),
|
|
|
|
),
|
|
|
|
)
|
2023-01-19 20:42:46 +05:30
|
|
|
}
|
2024-09-29 15:59:59 +05:30
|
|
|
sort.Sort(ByAdminAndName(userInfoArray))
|
2024-09-29 00:20:57 +05:30
|
|
|
data := UserStruct{
|
2023-01-19 20:42:46 +05:30
|
|
|
Status: c.Response().StatusCode(),
|
|
|
|
Online: output,
|
2024-09-29 15:59:59 +05:30
|
|
|
Users: userInfoArray,
|
2023-01-19 20:42:46 +05:30
|
|
|
Total: int(strings.Count(userstr, "\n")),
|
|
|
|
}
|
|
|
|
return c.JSON(data)
|
|
|
|
}
|