2022-02-14 13:05:02 +05:30
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-02-22 19:43:23 +05:30
|
|
|
"context"
|
2022-02-14 13:05:02 +05:30
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2022-02-22 19:43:23 +05:30
|
|
|
"os"
|
2022-02-14 13:05:02 +05:30
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
2022-02-23 16:53:14 +05:30
|
|
|
"github.com/gofiber/fiber/v2/middleware/cors"
|
2022-02-14 13:05:02 +05:30
|
|
|
"github.com/gofiber/fiber/v2/middleware/etag"
|
2023-01-27 08:19:21 +05:30
|
|
|
"github.com/google/go-github/v50/github"
|
2022-02-22 19:43:23 +05:30
|
|
|
"golang.org/x/oauth2"
|
2022-02-14 13:05:02 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
var monitored_instances = []Instance{}
|
|
|
|
|
|
|
|
var number_re = regexp.MustCompile(`(?m)(\d+)`)
|
|
|
|
|
|
|
|
type Instance struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
ApiUrl string `json:"api_url"`
|
|
|
|
Locations string `json:"locations"`
|
2022-02-22 19:43:23 +05:30
|
|
|
Version string `json:"version"`
|
|
|
|
UpToDate bool `json:"up_to_date"`
|
2022-02-14 13:05:02 +05:30
|
|
|
Cdn bool `json:"cdn"`
|
|
|
|
Registered int `json:"registered"`
|
|
|
|
LastChecked int64 `json:"last_checked"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func monitorInstances() {
|
2022-02-22 19:43:23 +05:30
|
|
|
ctx := context.Background()
|
|
|
|
var tc *http.Client
|
|
|
|
if os.Getenv("GITHUB_TOKEN") != "" {
|
|
|
|
ts := oauth2.StaticTokenSource(
|
|
|
|
&oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")},
|
|
|
|
)
|
|
|
|
tc = oauth2.NewClient(ctx, ts)
|
|
|
|
}
|
|
|
|
gh_client := github.NewClient(tc)
|
2022-02-14 13:05:02 +05:30
|
|
|
// do forever
|
|
|
|
for {
|
|
|
|
// send a request to get markdown from GitHub
|
|
|
|
resp, err := http.Get("https://raw.githubusercontent.com/wiki/TeamPiped/Piped-Frontend/Instances.md")
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-02-22 19:43:23 +05:30
|
|
|
// Find Latest Commit from GitHub
|
|
|
|
var latest string
|
|
|
|
{
|
|
|
|
commits, _, err := gh_client.Repositories.ListCommits(ctx, "TeamPiped", "Piped-Backend", &github.CommitsListOptions{
|
|
|
|
ListOptions: github.ListOptions{
|
|
|
|
PerPage: 1,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
time.Sleep(time.Second * 5)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
latest = commits[0].GetSHA()
|
|
|
|
}
|
|
|
|
|
2022-02-14 13:05:02 +05:30
|
|
|
if resp.StatusCode == 200 {
|
|
|
|
|
|
|
|
// parse the response
|
|
|
|
buf := new(strings.Builder)
|
|
|
|
_, err := io.Copy(buf, resp.Body)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
lines := strings.Split(buf.String(), "\n")
|
|
|
|
|
|
|
|
instances := []Instance{}
|
|
|
|
|
|
|
|
skipped := 0
|
|
|
|
for _, line := range lines {
|
|
|
|
split := strings.Split(line, "|")
|
|
|
|
if len(split) >= 5 {
|
|
|
|
if skipped < 2 {
|
|
|
|
skipped++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ApiUrl := strings.TrimSpace(split[1])
|
|
|
|
resp, err := http.Get(ApiUrl + "/healthcheck")
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
LastChecked := time.Now().Unix()
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
resp, err = http.Get(ApiUrl + "/registered/badge")
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
registered, err := strconv.ParseInt(number_re.FindString(resp.Request.URL.Path), 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
continue
|
|
|
|
}
|
2022-02-22 19:43:23 +05:30
|
|
|
resp, err = http.Get(ApiUrl + "/version")
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
buf := new(strings.Builder)
|
|
|
|
_, err = io.Copy(buf, resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
version := strings.TrimSpace(buf.String())
|
|
|
|
version_split := strings.Split(version, "-")
|
|
|
|
hash := version_split[len(version_split)-1]
|
2022-02-14 13:05:02 +05:30
|
|
|
|
|
|
|
instances = append(instances, Instance{
|
|
|
|
Name: strings.TrimSpace(split[0]),
|
|
|
|
ApiUrl: ApiUrl,
|
|
|
|
Locations: strings.TrimSpace(split[2]),
|
|
|
|
Cdn: strings.TrimSpace(split[3]) == "Yes",
|
|
|
|
Registered: int(registered),
|
|
|
|
LastChecked: LastChecked,
|
2022-02-22 19:43:23 +05:30
|
|
|
Version: version,
|
|
|
|
UpToDate: strings.Contains(latest, hash),
|
2022-02-14 13:05:02 +05:30
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update the global instances variable
|
|
|
|
monitored_instances = instances
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
time.Sleep(time.Minute)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
go monitorInstances()
|
|
|
|
|
2022-02-22 19:43:23 +05:30
|
|
|
app := fiber.New()
|
2022-02-23 16:53:14 +05:30
|
|
|
app.Use(cors.New())
|
2022-02-14 13:05:02 +05:30
|
|
|
app.Use(etag.New())
|
|
|
|
|
|
|
|
app.Get("/", func(c *fiber.Ctx) error {
|
|
|
|
return c.JSON(monitored_instances)
|
|
|
|
})
|
|
|
|
|
|
|
|
app.Listen(":3000")
|
|
|
|
}
|