From d1c36f7af48b5d19b7f1825d7d084e66b1340f41 Mon Sep 17 00:00:00 2001 From: Arya Kiran Date: Tue, 17 Jan 2023 19:35:28 +0530 Subject: [PATCH] don't depend on wc for online user count; wip meta info in json --- go.mod | 1 + go.sum | 2 + main.go | 116 ++++++++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 104 insertions(+), 15 deletions(-) diff --git a/go.mod b/go.mod index 67daea6..d37e306 100644 --- a/go.mod +++ b/go.mod @@ -19,4 +19,5 @@ require ( go.uber.org/atomic v1.10.0 // indirect go.uber.org/multierr v1.9.0 // indirect golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index f3c8e63..e793cac 100644 --- a/go.sum +++ b/go.sum @@ -245,4 +245,6 @@ gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/main.go b/main.go index ebcc50e..ba3c690 100644 --- a/main.go +++ b/main.go @@ -1,19 +1,90 @@ package main import ( - "os" - "os/exec" - + "fmt" "github.com/ProjectSegfault/publapi/pages" "github.com/gofiber/fiber/v2" - log "github.com/sirupsen/logrus" - + "gopkg.in/yaml.v3" + "io/ioutil" + "os" + "os/exec" "runtime" "strings" ) +type Userstruct struct { + Status int + Online int + Users []Userinfo +} +type Userinfo struct { + name string + fullname string + loc string + email string + desc string + website string + capsule string + online string +} + +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 +} + +func confparse(username, item string) string { + filename := "/home/" + username + "/meta-info.yaml" + file, err := ioutil.ReadFile(filename) + if err != nil { + log.Error(err) + } + parsedData := make(map[interface{}]interface{}) + err2 := yaml.Unmarshal(file, &parsedData) + if err2 != nil { + log.Error(err2) + } + val, err3 := parsedData[item].(string) + if !err3 { + log.Error(err3) + } + return val +} +func userdata(username string) Userinfo { + var user Userinfo + user.name = confparse(username, "name") + user.fullname = confparse(username, "fullname") + user.capsule = confparse(username, "capsule") + user.website = confparse(username, "website") + user.desc = confparse(username, "desc") + user.email = confparse(username, "email") + user.loc = confparse(username, "loc") + //fmt.Println(user) + return user +} + // publapi is a simple API for Project Segfault's public shared server (pubnix). func main() { app := fiber.New() @@ -33,19 +104,34 @@ func main() { }) } // Get the number of users online - out, err := exec.Command("bash", "-c", "/usr/bin/users | /usr/bin/wc -l").Output() - log.Info(string(out)) + usersonline, err := exec.Command("bash", "-c", "/usr/bin/users").Output() + usersonlinestr := string(usersonline) + usersonlinededup := Dedup(usersonlinestr) + outputa := int(strings.Count(usersonlinededup, " ")) + var output int = 0 + output = outputa + 1 if err != nil { log.Error(err) - return c.SendStatus(fiber.StatusInternalServerError) } - - output := string(out) - - return c.JSON(fiber.Map{ - "users": strings.TrimSuffix(output, "\n"), - "status": c.Response().StatusCode(), - }) + users, err2 := exec.Command("bash", "-c", "/usr/bin/ls /home").Output() + if err2 != nil { + log.Error(err2) + } + 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]) + userinfostruct = append(userinfostruct, userdata(uname)) + } + data := Userstruct{ + Status: c.Response().StatusCode(), + Online: output, + Users: userinfostruct, + } + return c.JSON(data) }) app.Post("/signup", pages.SignupPage)