2020-11-10 21:53:46 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-01-01 02:26:39 +00:00
|
|
|
"context"
|
2020-11-10 21:53:46 -05:00
|
|
|
"net/http"
|
2020-12-17 04:23:25 +00:00
|
|
|
_ "net/http/pprof"
|
2021-12-11 00:32:32 -05:00
|
|
|
"regexp"
|
2022-01-01 02:26:39 +00:00
|
|
|
"runtime"
|
2020-12-17 04:23:25 +00:00
|
|
|
"sync"
|
2021-02-12 00:26:31 +00:00
|
|
|
"time"
|
2021-08-07 19:46:35 +00:00
|
|
|
|
2021-02-13 02:48:11 +00:00
|
|
|
"github.com/microcosm-cc/bluemonday"
|
2022-01-01 02:26:39 +00:00
|
|
|
"gitlab.com/khanzf/fedilogue/shared"
|
2020-11-10 21:53:46 -05:00
|
|
|
)
|
|
|
|
|
2020-11-17 18:28:59 -05:00
|
|
|
// Current instances
|
2022-01-01 02:26:39 +00:00
|
|
|
var runninginstances map[string]shared.RunningInstance
|
2020-11-17 18:28:59 -05:00
|
|
|
var ri_mutex = &sync.Mutex{}
|
2020-11-10 21:53:46 -05:00
|
|
|
|
2020-11-24 20:36:47 -05:00
|
|
|
func startpprof() {
|
2021-02-11 21:05:01 +00:00
|
|
|
logInfo("Starting http/pprof on :7777")
|
2022-01-13 22:55:35 -05:00
|
|
|
logFatal(http.ListenAndServe("127.0.0.1:7777", nil))
|
2020-11-24 20:36:47 -05:00
|
|
|
}
|
2020-11-10 21:53:46 -05:00
|
|
|
|
2021-08-07 19:46:35 +00:00
|
|
|
func statusReportHandler() {
|
2021-02-12 00:26:31 +00:00
|
|
|
for {
|
2021-08-07 19:46:35 +00:00
|
|
|
StatusReport()
|
|
|
|
time.Sleep(time.Second * 60)
|
|
|
|
}
|
|
|
|
}
|
2021-02-12 00:26:31 +00:00
|
|
|
|
2021-09-29 06:43:06 +00:00
|
|
|
/* Tests:
|
|
|
|
- TestStatusReport_empty_run
|
|
|
|
- TestStatusReport_full_content
|
|
|
|
*/
|
2021-08-07 19:46:35 +00:00
|
|
|
func StatusReport() {
|
|
|
|
running := 0
|
|
|
|
keepalive := 0
|
|
|
|
unsupported := 0
|
|
|
|
|
|
|
|
mastodon := 0
|
|
|
|
pleroma := 0
|
2023-07-14 02:42:07 +00:00
|
|
|
misskey := 0
|
2021-08-07 19:46:35 +00:00
|
|
|
other := 0
|
|
|
|
ri_mutex.Lock()
|
2023-07-14 02:42:07 +00:00
|
|
|
for i, o := range runninginstances {
|
|
|
|
logDebug("Software ", o.Software, " Status: ", o.Status, " instance ", i)
|
2021-08-07 19:46:35 +00:00
|
|
|
if o.Status == 200 {
|
|
|
|
running = running + 1
|
|
|
|
} else if o.Status == 607 { // Keepalive
|
|
|
|
keepalive = keepalive + 1
|
|
|
|
} else if o.Status == 605 { // Unsupported instance
|
|
|
|
unsupported = unsupported + 1
|
|
|
|
}
|
2021-02-12 00:26:31 +00:00
|
|
|
|
2021-08-07 19:46:35 +00:00
|
|
|
if o.Software == "mastodon" && o.Status == 200 {
|
|
|
|
mastodon = mastodon + 1
|
|
|
|
} else if o.Software == "pleroma" && o.Status == 200 {
|
|
|
|
pleroma = pleroma + 1
|
2023-07-14 02:42:07 +00:00
|
|
|
} else if o.Software == "misskey" && o.Status == 200 {
|
|
|
|
misskey = misskey + 1
|
2021-08-07 19:46:35 +00:00
|
|
|
} else if o.Status == 200 {
|
|
|
|
other = other + 1
|
2021-02-12 00:26:31 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-07 19:46:35 +00:00
|
|
|
ri_mutex.Unlock()
|
2023-07-14 02:42:07 +00:00
|
|
|
logInfo("Running:", running, " Keepalive:", keepalive, " Unsupported:", unsupported, " Ma:", mastodon, ",P:", pleroma, ",Mi:", misskey, ",O:", other)
|
2021-02-12 00:26:31 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 20:36:47 -05:00
|
|
|
func main() {
|
2020-11-10 21:53:46 -05:00
|
|
|
// Initial Setup
|
2021-01-14 19:51:42 +00:00
|
|
|
logInit()
|
2022-01-01 02:26:39 +00:00
|
|
|
runninginstances = make(map[string]shared.RunningInstance)
|
2020-11-10 21:53:46 -05:00
|
|
|
|
2020-12-03 17:26:38 -05:00
|
|
|
getSettings()
|
2020-11-24 20:36:47 -05:00
|
|
|
go startpprof()
|
2020-11-16 00:10:33 -05:00
|
|
|
|
2020-12-18 06:06:32 +00:00
|
|
|
pool = getDbPool()
|
2020-12-03 17:26:38 -05:00
|
|
|
|
2020-12-06 18:05:17 -05:00
|
|
|
p = bluemonday.NewPolicy()
|
2021-01-14 19:51:42 +00:00
|
|
|
spaceReg = regexp.MustCompile(`[\s\t\.]+`)
|
2021-01-14 20:43:20 +00:00
|
|
|
removeHTMLReg = regexp.MustCompile(`<\/?\s*br\s*>`)
|
2021-01-10 05:31:51 +00:00
|
|
|
re = regexp.MustCompile("^https?://([^/]*)/(.*)$")
|
2021-01-25 20:28:13 -05:00
|
|
|
matchurl = regexp.MustCompile("http?s://[\\w\\-]+\\.[\\w\\-]+\\S*")
|
2021-12-16 04:22:48 +00:00
|
|
|
staggeredStartChan = make(chan bool)
|
2020-12-03 17:26:38 -05:00
|
|
|
|
2021-12-11 00:32:32 -05:00
|
|
|
// Start instances located in database
|
|
|
|
rows, err := pool.Query(context.Background(), "SELECT endpoint FROM instances")
|
|
|
|
if err != nil {
|
|
|
|
logErr("Unable to select from instances")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2022-01-01 02:26:39 +00:00
|
|
|
go staggeredStart()
|
2021-12-16 04:22:48 +00:00
|
|
|
go statusReportHandler()
|
|
|
|
|
2021-12-11 00:32:32 -05:00
|
|
|
for rows.Next() {
|
|
|
|
var endpoint string
|
|
|
|
err = rows.Scan(&endpoint)
|
|
|
|
if err != nil {
|
2021-12-11 00:53:43 -05:00
|
|
|
logErr("Unable to iterate database, exiting.")
|
2021-12-11 00:32:32 -05:00
|
|
|
return
|
|
|
|
}
|
2022-01-01 02:26:39 +00:00
|
|
|
o, exists := GetRunner(endpoint)
|
|
|
|
if o.Banned == true {
|
|
|
|
continue // Banned instance
|
|
|
|
}
|
2021-12-11 00:32:32 -05:00
|
|
|
if exists == false {
|
2020-12-22 20:36:37 +00:00
|
|
|
go StartInstance(endpoint)
|
2020-12-03 17:26:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-22 20:36:37 +00:00
|
|
|
go startctl()
|
2022-01-09 21:16:34 -05:00
|
|
|
go webmain()
|
2020-12-14 23:09:51 +00:00
|
|
|
|
|
|
|
runtime.Goexit()
|
2020-11-10 21:53:46 -05:00
|
|
|
}
|