109 lines
2 KiB
Go
109 lines
2 KiB
Go
// peertube-instance-index-filter
|
|
// Copyright (C) 2025 Arns Udovič <zordsdavini@arns.lt>
|
|
//
|
|
// 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.
|
|
//
|
|
// 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.
|
|
//
|
|
// 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"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func main() {
|
|
var command string
|
|
var host string
|
|
|
|
flag.StringVar(&command, "command", "", "Command to execute")
|
|
flag.StringVar(&host, "host", "", "Host for command")
|
|
|
|
flag.Parse()
|
|
fmt.Println(command, host)
|
|
|
|
switch command {
|
|
case "index":
|
|
index(host)
|
|
case "reject":
|
|
reject(host)
|
|
case "collect":
|
|
collect()
|
|
default:
|
|
serve()
|
|
}
|
|
}
|
|
|
|
func index(url string) {
|
|
db := connectDB()
|
|
defer db.Close()
|
|
|
|
exists, err := indexExists(db, url)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if !exists {
|
|
addIndex(db, url)
|
|
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() {
|
|
// TODO: implement
|
|
}
|
|
|
|
func serve() {
|
|
r := gin.Default()
|
|
|
|
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")
|
|
})
|
|
|
|
r.Run(":8080")
|
|
}
|
|
|
|
func formatHost(host string) string {
|
|
host = strings.Trim(host, " ")
|
|
|
|
u, _ := url.Parse(host)
|
|
|
|
if u.Host == "" {
|
|
return host
|
|
}
|
|
|
|
return u.Host
|
|
}
|