2023-01-07 21:03:51 +05:30
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ProjectSegfault/publapi/pages"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
2023-01-07 22:24:04 +05:30
|
|
|
log "github.com/sirupsen/logrus"
|
2023-01-18 21:05:07 +05:30
|
|
|
"github.com/spf13/viper"
|
2023-01-17 19:35:28 +05:30
|
|
|
"os"
|
|
|
|
"os/exec"
|
2023-01-07 22:46:35 +05:30
|
|
|
"runtime"
|
2023-01-08 00:13:20 +05:30
|
|
|
"strings"
|
2023-01-07 21:03:51 +05:30
|
|
|
)
|
|
|
|
|
2023-01-17 19:35:28 +05:30
|
|
|
type Userstruct struct {
|
2023-01-18 22:36:57 +05:30
|
|
|
Status int `json:"status"`
|
|
|
|
Online int `json:"online"`
|
|
|
|
Total int `json:"total"`
|
|
|
|
Users []Userinfo `json:"users"`
|
2023-01-17 19:35:28 +05:30
|
|
|
}
|
|
|
|
type Userinfo struct {
|
2023-01-18 22:36:57 +05:30
|
|
|
Name string `json:"name"`
|
|
|
|
FullName string `json:"fullName"`
|
|
|
|
Loc string `json:"loc"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
Desc string `json:"desc"`
|
|
|
|
Website string `json:"website"`
|
|
|
|
Capsule string `json:"capsule"`
|
|
|
|
Online bool `json:"online"`
|
2023-01-17 19:35:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func Dedup(input string) string {
|
|
|
|
unique := []string{}
|
|
|
|
|
|
|
|
words := strings.Split(input, " ")
|
|
|
|
for _, word := range words {
|
|
|
|
// If we alredy have this word, skip.
|
|
|
|
if contains(unique, word) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
unique = append(unique, word)
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(unique, " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
func contains(strs []string, str string) bool {
|
|
|
|
for _, s := range strs {
|
|
|
|
if s == str {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-01-18 21:14:36 +05:30
|
|
|
func userdata(username, usersonline string) Userinfo {
|
2023-01-18 21:05:07 +05:30
|
|
|
filename := "/home/" + username + "/meta-info.env"
|
|
|
|
_, error := os.Stat(filename)
|
|
|
|
if error != nil {
|
|
|
|
if os.IsNotExist(error) {
|
|
|
|
log.Error(username + " does not have a meta-info.env")
|
|
|
|
var user Userinfo
|
2023-01-18 21:14:36 +05:30
|
|
|
if strings.Contains(usersonline, " "+username) == true {
|
2023-01-18 22:20:51 +05:30
|
|
|
user.Online = true
|
2023-01-18 21:14:36 +05:30
|
|
|
} else {
|
2023-01-18 22:20:51 +05:30
|
|
|
user.Online = false
|
2023-01-18 21:14:36 +05:30
|
|
|
}
|
2023-01-18 21:05:07 +05:30
|
|
|
return user
|
|
|
|
}
|
|
|
|
}
|
|
|
|
viper.SetConfigFile(filename)
|
|
|
|
viper.ReadInConfig()
|
2023-01-17 19:35:28 +05:30
|
|
|
var user Userinfo
|
2023-01-18 22:20:51 +05:30
|
|
|
user.Name = viper.GetString("USERNAME")
|
|
|
|
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")
|
|
|
|
user.Loc = viper.GetString("LOCATION")
|
2023-01-18 21:14:36 +05:30
|
|
|
if strings.Contains(usersonline, " "+username) == true {
|
2023-01-18 22:20:51 +05:30
|
|
|
user.Online = true
|
2023-01-18 21:14:36 +05:30
|
|
|
} else {
|
2023-01-18 22:20:51 +05:30
|
|
|
user.Online = false
|
2023-01-18 21:14:36 +05:30
|
|
|
}
|
2023-01-17 19:35:28 +05:30
|
|
|
return user
|
|
|
|
}
|
|
|
|
|
2023-01-07 21:32:06 +05:30
|
|
|
// publapi is a simple API for Project Segfault's public shared server (pubnix).
|
2023-01-07 21:03:51 +05:30
|
|
|
func main() {
|
|
|
|
app := fiber.New()
|
|
|
|
|
|
|
|
app.Get("/", func(c *fiber.Ctx) error {
|
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
"message": "welcome to publapi",
|
|
|
|
"status": c.Response().StatusCode(),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-01-18 21:16:44 +05:30
|
|
|
app.Get("/users", func(c *fiber.Ctx) error {
|
2023-01-07 22:46:35 +05:30
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return c.JSON(fiber.Map{
|
2023-01-18 21:16:44 +05:30
|
|
|
"message": "/users is not supported on Windows",
|
2023-01-07 22:46:35 +05:30
|
|
|
"status": c.Response().StatusCode(),
|
|
|
|
})
|
|
|
|
}
|
2023-01-07 21:03:51 +05:30
|
|
|
// Get the number of users online
|
2023-01-17 19:35:28 +05:30
|
|
|
usersonline, err := exec.Command("bash", "-c", "/usr/bin/users").Output()
|
2023-01-18 22:43:23 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return c.SendStatus(fiber.StatusInternalServerError)
|
|
|
|
}
|
2023-01-17 19:35:28 +05:30
|
|
|
usersonlinestr := string(usersonline)
|
|
|
|
usersonlinededup := Dedup(usersonlinestr)
|
|
|
|
outputa := int(strings.Count(usersonlinededup, " "))
|
2023-01-18 21:26:37 +05:30
|
|
|
var output int
|
2023-01-17 19:35:28 +05:30
|
|
|
output = outputa + 1
|
|
|
|
users, err2 := exec.Command("bash", "-c", "/usr/bin/ls /home").Output()
|
|
|
|
if err2 != nil {
|
|
|
|
log.Error(err2)
|
2023-01-18 22:43:23 +05:30
|
|
|
return c.SendStatus(fiber.StatusInternalServerError)
|
2023-01-17 19:35:28 +05:30
|
|
|
}
|
|
|
|
userstr := string(users)
|
|
|
|
userstr2 := strings.TrimSuffix(userstr, "\n")
|
|
|
|
usersarr := strings.Split(userstr2, "\n")
|
|
|
|
//var userinfoarr []interface{}
|
|
|
|
var userinfostruct []Userinfo
|
|
|
|
for i := 0; i < len(usersarr); i++ {
|
|
|
|
uname := string(usersarr[i])
|
2023-01-18 21:14:36 +05:30
|
|
|
userinfostruct = append(userinfostruct, userdata(uname, usersonlinededup))
|
2023-01-17 19:35:28 +05:30
|
|
|
}
|
2023-01-18 21:05:07 +05:30
|
|
|
log.Info(userinfostruct)
|
2023-01-17 19:35:28 +05:30
|
|
|
data := Userstruct{
|
|
|
|
Status: c.Response().StatusCode(),
|
|
|
|
Online: output,
|
|
|
|
Users: userinfostruct,
|
2023-01-18 21:14:36 +05:30
|
|
|
Total: int(strings.Count(userstr, "\n")),
|
2023-01-17 19:35:28 +05:30
|
|
|
}
|
|
|
|
return c.JSON(data)
|
2023-01-07 21:03:51 +05:30
|
|
|
})
|
|
|
|
|
|
|
|
app.Post("/signup", pages.SignupPage)
|
|
|
|
|
2023-01-07 22:15:42 +05:30
|
|
|
app.Listen(GetPort())
|
2023-01-07 21:03:51 +05:30
|
|
|
}
|
2023-01-07 22:14:25 +05:30
|
|
|
|
|
|
|
// GetPort returns the port to listen on
|
|
|
|
func GetPort() string {
|
|
|
|
port := os.Getenv("PUBLAPI_PORT")
|
|
|
|
if port == "" {
|
|
|
|
port = "3000"
|
|
|
|
}
|
|
|
|
return ":" + port
|
|
|
|
}
|