3 Commits

Author SHA1 Message Date
db658f5a68 feat(logs): change logs to structured json
All checks were successful
release / docker-image (push) Successful in 1m2s
release / goreleaser (push) Successful in 10m0s
2026-02-11 10:48:35 +03:00
61a6fcee03 feat(docs): update readme 2026-01-25 13:23:57 +03:00
ad31687111 feat(config): add config example in config.dist.toml 2026-01-25 13:19:50 +03:00
3 changed files with 102 additions and 34 deletions

View File

@@ -1,32 +1,51 @@
# Trackers
Опрашивает ссылки со списками torrent-трекеров,
соединяет их в один список. Поддерживает кеширование,
http, file источники.
A torrent tracker aggregator that polls multiple sources and serves a unified,
deduplicated list of tracker URLs via HTTP API.
Учебный проект.
**Educational project written in Go 1.25.5**
## Запуск
## Features
- Polls tracker sources at configurable intervals
- Supports HTTP/HTTPS and local file sources
- Deduplicates and validates tracker URLs
- Persistent disk caching for resilience
- HTTP proxy support via environment variables
- Request logging for monitoring
- Single binary with minimal dependencies
## Usage
```shell
trackers --config config.toml
trackers -config config.toml
```
Получение списка трекеров:
Get the aggregated tracker list:
```shell
curl http://127.0.0.1:8080/list
```
## Конфигурация
## Configuration
Источники для трекеров описываются в toml-конфиге:
See [config.dist.toml](config.dist.toml) for a complete configuration example with comments.
```toml
port = 8080
Copy the example file and customize:
sources = [
"https://example.com",
"file:///home/user/local-file.txt",
]
```
```shell
cp config.dist.toml config.toml
# Edit config.toml with your sources
```
## Proxy Support
HTTP requests respect standard proxy environment variables:
```shell
export HTTP_PROXY=http://proxy.example.com:8080
export HTTPS_PROXY=http://proxy.example.com:8080
trackers -config config.toml
```
See [config.dist.toml](config.dist.toml) for details.

44
config.dist.toml Normal file
View File

@@ -0,0 +1,44 @@
# Trackers Configuration Example
# Copy this file to config.toml and adjust values for your setup
# HTTP server port
# Default: 8080
port = 8080
# Directory for caching tracker lists
# Used to persist tracker data between restarts and handle source failures
# Default: "cache"
cache_dir = "cache"
# Interval between polling each source
# Valid units: s (seconds), m (minutes), h (hours)
# Examples: "30s", "5m", "1h", "90m"
# Default: "60m"
poll_interval = "60m"
# List of tracker sources to aggregate
# Supported schemes:
# - http:// / https:// - Remote HTTP endpoints
# - file:// - Local files (e.g., file:///path/to/trackers.txt)
#
# Each source should return tracker URLs, one per line
# Blank lines and duplicates are automatically filtered
sources = [
"https://example.com/trackers/all.txt",
"https://another-source.org/trackers.txt",
# "file:///etc/trackers/local.txt",
]
# Proxy Configuration
# ==================
# HTTP requests automatically respect standard proxy environment variables:
#
# HTTP_PROXY - Proxy for HTTP requests (e.g., http://proxy.example.com:8080)
# HTTPS_PROXY - Proxy for HTTPS requests (e.g., http://proxy.example.com:8080)
# NO_PROXY - Comma-separated list of hosts to bypass proxy (e.g., localhost,127.0.0.1)
#
# Example usage:
# export HTTP_PROXY=http://proxy.example.com:8080
# export HTTPS_PROXY=http://proxy.example.com:8080
# export NO_PROXY=localhost,127.0.0.1,.local
# ./trackers -config config.toml

41
main.go
View File

@@ -8,7 +8,7 @@ import (
"flag"
"fmt"
"io"
"log"
"log/slog"
"net/http"
"net/url"
"os"
@@ -111,13 +111,17 @@ func main() {
configPath := flag.String("config", "config.toml", "path to config file")
flag.Parse()
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, nil)))
cfg, interval, err := loadConfig(*configPath)
if err != nil {
log.Fatalf("config error: %v", err)
slog.Error("config load failed", "error", err)
os.Exit(1)
}
if err := os.MkdirAll(cfg.CacheDir, 0o755); err != nil {
log.Fatalf("cache dir: %v", err)
slog.Error("cache dir create failed", "error", err)
os.Exit(1)
}
agg := NewAggregator()
@@ -133,7 +137,7 @@ func main() {
for _, source := range cfg.Sources {
cached, err := loadCachedLinks(cfg.CacheDir, source)
if err != nil {
log.Printf("load cache for %s: %v", source, err)
slog.Error("load cache failed", "source", source, "error", err)
}
if len(cached) > 0 {
agg.Update(source, cached)
@@ -149,7 +153,7 @@ func main() {
mux := http.NewServeMux()
mux.HandleFunc("/list", func(w http.ResponseWriter, r *http.Request) {
log.Printf("request /list from %s [%s %s]", r.RemoteAddr, r.Method, r.UserAgent())
slog.Info("request", "path", "/list", "remote", r.RemoteAddr, "method", r.Method, "user_agent", r.UserAgent())
links := agg.List()
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
for i, link := range links {
@@ -158,7 +162,7 @@ func main() {
}
_, _ = w.Write([]byte(link))
}
log.Printf("response /list to %s: %d links", r.RemoteAddr, len(links))
slog.Info("response", "path", "/list", "remote", r.RemoteAddr, "links", len(links))
})
server := &http.Server{
@@ -173,15 +177,16 @@ func main() {
<-ctx.Done()
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
log.Printf("server shutdown")
slog.Info("server shutting down")
if err := server.Shutdown(shutdownCtx); err != nil {
log.Printf("server shutdown error: %v", err)
slog.Error("server shutdown failed", "error", err)
}
}()
log.Printf("listening on :%d", cfg.Port)
slog.Info("listening", "port", cfg.Port)
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("server error: %v", err)
slog.Error("server listen failed", "error", err)
os.Exit(1)
}
}
@@ -194,7 +199,7 @@ func pollSource(ctx context.Context, source string, interval time.Duration, cach
for {
select {
case <-ctx.Done():
log.Printf("poller shutdown")
slog.Info("poller stopped", "source", source)
return
case <-ticker.C:
runOnce(ctx, source, cacheDir, agg, client)
@@ -205,22 +210,22 @@ func pollSource(ctx context.Context, source string, interval time.Duration, cach
func runOnce(ctx context.Context, source string, cacheDir string, agg *Aggregator, client *http.Client) {
links, err := fetchSource(ctx, source, client)
if err != nil {
log.Printf("poll %s: failed - %v", source, err)
slog.Error("poll failed", "source", source, "error", err)
return
}
if len(links) == 0 {
log.Printf("poll %s: success - 0 links", source)
slog.Info("poll success", "source", source, "links", 0)
agg.Update(source, nil)
if err := writeCache(cacheDir, source, nil); err != nil {
log.Printf("write cache %s: %v", source, err)
slog.Error("write cache failed", "source", source, "error", err)
}
return
}
log.Printf("poll %s: success - %d links", source, len(links))
slog.Info("poll success", "source", source, "links", len(links))
agg.Update(source, links)
if err := writeCache(cacheDir, source, links); err != nil {
log.Printf("write cache %s: %v", source, err)
slog.Error("write cache failed", "source", source, "error", err)
}
}
@@ -244,11 +249,11 @@ func fetchSource(ctx context.Context, source string, client *http.Client) ([]str
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return nil, fmt.Errorf("unexpected status: %s", resp.Status)
}
log.Printf("fetch %s: HTTP %d", source, resp.StatusCode)
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("read body: %w", err)
}
slog.Info("fetch complete", "source", source, "status", resp.StatusCode, "bytes", len(body))
return normalizeLinks(string(body)), nil
case "file":
path := u.Path
@@ -259,7 +264,7 @@ func fetchSource(ctx context.Context, source string, client *http.Client) ([]str
if err != nil {
return nil, fmt.Errorf("read file: %w", err)
}
log.Printf("fetch %s: file read %d bytes", source, len(data))
slog.Info("fetch complete", "source", source, "bytes", len(data))
return normalizeLinks(string(data)), nil
default:
return nil, fmt.Errorf("unsupported source scheme: %s", u.Scheme)