2023-01-19 20:42:46 +05:30
|
|
|
package pages
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"regexp"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/ProjectSegfault/publapi/utils"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Userstruct struct {
|
|
|
|
Status int `json:"status"`
|
|
|
|
Online int `json:"online"`
|
|
|
|
Total int `json:"total"`
|
|
|
|
Users []Userinfo `json:"users"`
|
|
|
|
}
|
|
|
|
|
|
|
|
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:23:05 +05:30
|
|
|
Created string `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
|
|
|
}
|
|
|
|
|
2023-01-23 19:30:34 +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 {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
2023-01-23 20:23:05 +05:30
|
|
|
isop, operr := regexp.MatchString(string(regex), string(ops))
|
|
|
|
if operr != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
cmd := "/usr/bin/stat -c %W /home/" + username
|
|
|
|
crd, crerr := exec.Command("bash", "-c", cmd).Output()
|
|
|
|
if crerr != nil {
|
|
|
|
log.Error(crerr)
|
|
|
|
}
|
|
|
|
crdstr := string(crd)
|
|
|
|
filename := "/home/" + username + "/meta-info.env"
|
|
|
|
_, error := os.Stat(filename)
|
2023-01-19 20:42:46 +05:30
|
|
|
if error != nil {
|
|
|
|
if os.IsNotExist(error) {
|
|
|
|
log.Error(username + " does not have a meta-info.env")
|
|
|
|
var user Userinfo
|
|
|
|
user.Name = username
|
2023-01-23 20:23:05 +05:30
|
|
|
user.Created = strings.TrimSuffix(crdstr, "\n")
|
2023-01-19 20:42:46 +05:30
|
|
|
if isonline == true {
|
|
|
|
user.Online = true
|
|
|
|
} else {
|
|
|
|
user.Online = false
|
|
|
|
}
|
2023-01-23 19:30:34 +05:30
|
|
|
if isop == true {
|
|
|
|
user.Op = true
|
|
|
|
} else {
|
|
|
|
user.Op = false
|
|
|
|
}
|
2023-01-19 20:42:46 +05:30
|
|
|
return user
|
|
|
|
}
|
|
|
|
}
|
|
|
|
viper.SetConfigFile(filename)
|
|
|
|
viper.ReadInConfig()
|
|
|
|
var user Userinfo
|
|
|
|
user.Name = username
|
2023-01-23 20:23:05 +05:30
|
|
|
user.Created = strings.TrimSuffix(crdstr, "\n")
|
2023-01-19 20:42:46 +05:30
|
|
|
user.FullName = viper.GetString("FULL_NAME")
|
|
|
|
user.Capsule = viper.GetString("GEMINI_CAPSULE")
|
|
|
|
user.Website = viper.GetString("WEBSITE")
|
|
|
|
user.Desc = viper.GetString("DESCRIPTION")
|
|
|
|
user.Email = viper.GetString("EMAIL")
|
2023-01-21 16:55:08 +05:30
|
|
|
user.Matrix = viper.GetString("MATRIX")
|
|
|
|
user.Fediverse = viper.GetString("FEDIVERSE")
|
2023-01-19 20:42:46 +05:30
|
|
|
user.Loc = viper.GetString("LOCATION")
|
2023-01-23 19:30:34 +05:30
|
|
|
if isop == true {
|
|
|
|
user.Op = true
|
|
|
|
} else {
|
|
|
|
user.Op = false
|
|
|
|
}
|
2023-01-19 20:42:46 +05:30
|
|
|
if isonline == true {
|
|
|
|
user.Online = true
|
|
|
|
} else {
|
|
|
|
user.Online = false
|
|
|
|
}
|
|
|
|
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
|
|
|
|
usersonline, err := exec.Command("bash", "-c", "/usr/bin/users").Output()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return c.SendStatus(fiber.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
usersonlinestr := string(usersonline)
|
|
|
|
usersonlinededup := utils.Dedup(usersonlinestr)
|
|
|
|
var output int
|
2023-01-21 17:13:29 +05:30
|
|
|
if strings.Contains(usersonlinededup, " ") {
|
|
|
|
outputa := int(strings.Count(usersonlinededup, " "))
|
|
|
|
output = outputa + 1
|
|
|
|
} else if usersonlinededup == "" {
|
|
|
|
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 {
|
|
|
|
log.Error(err)
|
|
|
|
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 {
|
|
|
|
log.Error(err2)
|
|
|
|
return c.SendStatus(fiber.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
userstr := string(users)
|
|
|
|
userstr2 := strings.TrimSuffix(userstr, "\n")
|
|
|
|
usersarr := strings.Split(userstr2, "\n")
|
2023-01-23 19:30:34 +05:30
|
|
|
// Fill user info
|
2023-01-19 20:42:46 +05:30
|
|
|
var userinfostruct []Userinfo
|
|
|
|
for i := 0; i < len(usersarr); i++ {
|
|
|
|
uname := string(usersarr[i])
|
2023-01-23 19:30:34 +05:30
|
|
|
userinfostruct = append(userinfostruct, userdata(uname, strings.TrimSuffix(usersonlinededup, "\n"), strings.TrimSuffix(opstr, "\n")))
|
2023-01-19 20:42:46 +05:30
|
|
|
}
|
|
|
|
data := Userstruct{
|
|
|
|
Status: c.Response().StatusCode(),
|
|
|
|
Online: output,
|
|
|
|
Users: userinfostruct,
|
|
|
|
Total: int(strings.Count(userstr, "\n")),
|
|
|
|
}
|
|
|
|
return c.JSON(data)
|
|
|
|
}
|