From 90d319382cf78f72960fc1a05d22f65e9f4b2649 Mon Sep 17 00:00:00 2001 From: Arnas Udovic Date: Mon, 23 Jun 2025 12:50:46 +0300 Subject: [PATCH] audit dead instances --- README.md | 1 + audit.go | 31 +++++++++++++++++++++++++++++++ docker/Dockerfile | 2 ++ docker/Makefile | 6 ++++++ docker/docker-compose.yaml | 4 ++-- main.go | 23 ++++++++++++++++++++++- 6 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 audit.go diff --git a/README.md b/README.md index a0e72ef..2b1f499 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ App can be called directly and do management in console. - `peertube-instance-index-filter -command index -url https://instances.joinpeertube.org/api/v1/instances/hosts -instance-url https://instances.joinpeertube.org/api/v1/instances` - add url to index of hosts. Later it will be used to collect instances - `peertube-instance-index-filter -command collect` - collect instances from index of hosts urls - `peertube-instance-index-filter -command reject -host www.example.com` - reject instance from index to exclude it from global index +- `peertube-instance-index-filter -command audit -filter dead` - go through not rejected instances and check by filters: dead ## Links diff --git a/audit.go b/audit.go new file mode 100644 index 0000000..6fee428 --- /dev/null +++ b/audit.go @@ -0,0 +1,31 @@ +// peertube-instance-index-filter +// Copyright (C) 2025 Arns Udovič +// +// 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 . + +package main + +import "net/http" + +func doAudit(host string, filter string) bool { + switch filter { + case "dead": + _, err := http.Get("https://" + host) + if err != nil { + return true + } + } + + return false +} diff --git a/docker/Dockerfile b/docker/Dockerfile index 8d80f1f..3651d1f 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,5 +1,7 @@ FROM golang:1.24 +RUN apt-get update && apt-get -y install cron + WORKDIR /usr/src/app ADD ./peertube-instance-index-filter ./ diff --git a/docker/Makefile b/docker/Makefile index e4f05a9..807a97e 100644 --- a/docker/Makefile +++ b/docker/Makefile @@ -13,3 +13,9 @@ add-index: .PHONY: reject reject: docker compose -f docker-compose.yaml exec -it peertube-instance-index-filter peertube-instance-index-filter -command reject -host $(host) -reject-reason $(reason) + +### audit: Audit hosts. Arguments: filter +.PHONY: audit +audit: + docker compose -f docker-compose.yaml exec -it peertube-instance-index-filter peertube-instance-index-filter -command audit -filter $(filter) + diff --git a/docker/docker-compose.yaml b/docker/docker-compose.yaml index dda9087..c3b118b 100644 --- a/docker/docker-compose.yaml +++ b/docker/docker-compose.yaml @@ -2,14 +2,14 @@ version: "3.9" services: peertube-instance-index-filter: - build: ./peertube-instance-index-filter + build: . volumes: - ./instances.db:/usr/src/app/instances.db expose: - 8081 labels: - traefik.enable=true - - traefik.http.routers.peertube_instance_index.rule=Host(`index.tv.arns.lt`) + - traefik.http.routers.peertube_instance_index.rule=Host(`index-tv.arns.lt`) - traefik.http.routers.peertube_instance_index.tls.certresolver=le - traefik.http.routers.peertube_instance_index.entrypoints=websecure restart: always diff --git a/main.go b/main.go index 43aaa5e..772f43f 100644 --- a/main.go +++ b/main.go @@ -49,12 +49,14 @@ func main() { var instanceUrl string var host string var reason string + var filter string - flag.StringVar(&command, "command", "", "Command to execute: index, reject, collect, serve") + flag.StringVar(&command, "command", "", "Command to execute: index, reject, collect, serve, audit") flag.StringVar(&url, "url", "", "Url to index hosts") flag.StringVar(&instanceUrl, "instance-url", "", "Url to fetch instance information") flag.StringVar(&host, "host", "", "Host to reject") flag.StringVar(&reason, "reject-reason", "", "Reject reason (optional)") + flag.StringVar(&filter, "filter", "", "filter for audit") flag.Parse() fmt.Println(command, host) @@ -66,6 +68,8 @@ func main() { reject(host, reason) case "collect": collect() + case "audit": + audit(filter) case "serve": default: serve() @@ -140,6 +144,23 @@ func collect() { } } +func audit(filter string) { + db := connectDB() + defer db.Close() + + hosts, err := getHosts(db, 0, 10000, "", "", "url") + if err != nil { + panic(err) + } + + for _, url := range hosts { + filtered := doAudit(url, filter) + if filtered { + reject(url, filter) + } + } +} + func serve() { db := connectDB() defer db.Close()