peertube-instance-index-filter/main.go

156 lines
3.1 KiB
Go
Raw Normal View History

// peertube-instance-index-filter
// Copyright (C) 2025 Arns Udovič <zordsdavini@arns.lt>
2025-06-20 19:04:57 +03:00
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
2025-06-20 19:04:57 +03:00
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
2025-06-20 19:04:57 +03:00
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"flag"
"fmt"
"net/url"
"strings"
2025-06-20 19:04:57 +03:00
"time"
"github.com/gin-gonic/gin"
)
func main() {
var command string
var host string
2025-06-20 19:04:57 +03:00
var url string
var instanceUrl string
2025-06-20 19:04:57 +03:00
flag.StringVar(&command, "command", "", "Command to execute: index, reject, collect, serve")
flag.StringVar(&host, "host", "", "Host to reject")
flag.StringVar(&url, "url", "", "Url to index hosts")
flag.StringVar(&instanceUrl, "instance-url", "", "Url to fetch instance information")
flag.Parse()
fmt.Println(command, host)
switch command {
case "index":
2025-06-20 19:04:57 +03:00
index(url, instanceUrl)
case "reject":
reject(host)
case "collect":
collect()
default:
serve()
}
}
2025-06-20 19:04:57 +03:00
func index(url string, instanceUrl string) {
db := connectDB()
defer db.Close()
exists, err := indexExists(db, url)
if err != nil {
panic(err)
}
if !exists {
2025-06-20 19:04:57 +03:00
indexHost := IndexHost{
Url: url,
InstanceUrl: instanceUrl,
}
addIndex(db, indexHost)
fmt.Println(url, "added to index")
} else {
fmt.Println(url, "already added")
}
}
func reject(host string) {
host = formatHost(host)
db := connectDB()
defer db.Close()
rejectHost(db, host)
fmt.Println(host, "rejected")
}
func collect() {
2025-06-20 06:31:18 +03:00
db := connectDB()
defer db.Close()
2025-06-20 19:04:57 +03:00
indexHosts := getIndexHosts(db)
for _, indexHost := range indexHosts {
fmt.Println(indexHost.Url)
fmt.Println("==========================================")
2025-06-20 06:31:18 +03:00
start := 0
count := 20
for {
2025-06-20 19:04:57 +03:00
hosts, err := getNewHosts(indexHost.Url, indexHost.LastFetchedAt, start, count)
2025-06-20 06:31:18 +03:00
if err != nil {
panic(err)
}
if len(hosts) == 0 {
break
}
2025-06-20 19:04:57 +03:00
fmt.Println("New hosts:", len(hosts), hosts)
2025-06-20 06:31:18 +03:00
for _, host := range hosts {
2025-06-20 19:04:57 +03:00
fmt.Println(host)
time.Sleep(1 * time.Second)
instance := fetchInstance(indexHost.InstanceUrl, host)
2025-06-20 06:31:18 +03:00
addInstance(db, instance)
}
start += count
}
2025-06-20 19:04:57 +03:00
updateLastFetched(db, indexHost)
2025-06-20 06:31:18 +03:00
}
}
func serve() {
r := gin.Default()
2025-06-20 19:04:57 +03:00
r.GET("/", func(c *gin.Context) {
c.String(200, "index")
})
r.GET("/instances", func(c *gin.Context) {
c.String(200, "pong")
})
r.GET("/instances/hosts", func(c *gin.Context) {
c.String(200, "pong")
})
r.GET("/stats", func(c *gin.Context) {
c.String(200, "pong")
})
2025-06-20 19:04:57 +03:00
r.Run(":8081")
}
func formatHost(host string) string {
host = strings.Trim(host, " ")
u, _ := url.Parse(host)
if u.Host == "" {
return host
}
return u.Host
}