refactor!: review the logic related to the operational flags

This commit is contained in:
Tommaso Gragnato 2024-11-26 01:50:24 +01:00 committed by Tommaso Gragnato
parent b746785568
commit 0f02c8446b
10 changed files with 426 additions and 279 deletions

18
doc/config.example.yml Normal file
View File

@ -0,0 +1,18 @@
databaseURL: "postgres://magnetico:magnetico@localhost:5432/magnetico?sslmode=disable"
indexerAddrs:
- "0.0.0.0:0"
indexerMaxNeighbors: 5000
leechMaxN: 1000
maxRPS: 500
bootstrappingNodes:
- "dht.tgragnato.it:80"
- "dht.tgragnato.it:443"
- "dht.tgragnato.it:1337"
- "dht.tgragnato.it:6969"
- "dht.tgragnato.it:6881"
- "dht.tgragnato.it:25401"
filterNodesCIDRs: []
addr: "[::1]:8080"
cred: ""
runDaemon: false
runWeb: false

22
go.mod
View File

@ -11,8 +11,8 @@ require (
github.com/multiformats/go-multihash v0.2.3
github.com/prometheus/client_golang v1.20.5
github.com/rabbitmq/amqp091-go v1.10.0
github.com/spf13/viper v1.19.0
golang.org/x/crypto v0.29.0
gopkg.in/yaml.v3 v3.0.1
gopkg.in/zeromq/goczmq.v4 v4.1.0
maragu.dev/gomponents v1.0.0
)
@ -21,39 +21,25 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/bits-and-blooms/bitset v1.17.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/klauspost/cpuid/v2 v2.2.9 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/minio/sha256-simd v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.60.1 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sync v0.9.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/text v0.20.0 // indirect
google.golang.org/protobuf v1.35.2 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/blake3 v1.3.0 // indirect
)

44
go.sum
View File

@ -9,18 +9,12 @@ github.com/bits-and-blooms/bloom/v3 v3.7.0 h1:VfknkqV4xI+PsaDIsoHueyxVDZrfvMn56j
github.com/bits-and-blooms/bloom/v3 v3.7.0/go.mod h1:VKlUSvp0lFIYqxJjzdnSsZEw4iHb1kOL2tfHTgyJBHg=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
@ -42,14 +36,10 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dzMM=
github.com/minio/sha256-simd v1.0.1/go.mod h1:Pz6AKMiUdngCLpeTL/RJY1M9rUuPMYujV5xJjtbRSN8=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o=
github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
github.com/multiformats/go-multihash v0.2.3 h1:7Lyc8XfX/IY2jWb/gI7JP+o7JEq9hOa7BFvVU9RSh+U=
@ -58,8 +48,6 @@ github.com/multiformats/go-varint v0.0.7 h1:sWSGR+f/eu5ABZA2ZpYKBILXTTs9JWpdEM/n
github.com/multiformats/go-varint v0.0.7/go.mod h1:r8PUYw/fD/SjBCiKOoDlGF6QawOELpZAu9eioSos/OU=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
@ -75,47 +63,19 @@ github.com/rabbitmq/amqp091-go v1.10.0 h1:STpn5XsHlHGcecLmMFCtg7mqq0RnD+zFr4uzuk
github.com/rabbitmq/amqp091-go v1.10.0/go.mod h1:Hy4jKW5kQART1u+JkDTF9YYOQUHXqMuhrgxOEeS7G4o=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY=
github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI=
github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/twmb/murmur3 v1.1.6 h1:mqrRot1BRxm+Yct+vavLMou2/iJt0tNVTTC0QoIjaZg=
github.com/twmb/murmur3 v1.1.6/go.mod h1:Qq/R7NUyOfr65zD+6Q5IHKsJLwP7exErjN6lyyq3OSQ=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ=
golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=
golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
@ -127,8 +87,6 @@ google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojt
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

235
main.go
View File

@ -1,58 +1,26 @@
package main
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"log"
"net"
"os"
"os/signal"
"path"
"path/filepath"
"reflect"
"regexp"
"strconv"
"strings"
"syscall"
"time"
"github.com/jessevdk/go-flags"
"github.com/spf13/viper"
"tgragnato.it/magnetico/dht"
"tgragnato.it/magnetico/dht/mainline"
"tgragnato.it/magnetico/metadata"
"tgragnato.it/magnetico/opflags"
"tgragnato.it/magnetico/persistence"
"tgragnato.it/magnetico/stats"
"tgragnato.it/magnetico/web"
)
var opFlags struct {
RunDaemon bool
RunWeb bool
DatabaseURL string
IndexerAddrs []string
IndexerMaxNeighbors uint
LeechMaxN int
BootstrappingNodes []string
FilterNodesCIDRs []net.IPNet
Addr string
Credentials map[string][]byte
CredentialsPath string
}
func main() {
// opFlags is the "operational flags"
if parseFlags() != nil {
// Do not print any error messages as jessevdk/go-flags already did.
return
opFlags := opflags.OpFlags{}
if err := opFlags.Parse(); err != nil {
log.Fatalln(err.Error())
}
// Handle Ctrl-C gracefully.
@ -70,7 +38,7 @@ func main() {
}
opFlags.Credentials = make(map[string][]byte)
if err := loadCred(opFlags.CredentialsPath); err != nil {
if err := opFlags.LoadCred(); err != nil {
log.Fatalf("couldn't load credentials %s\n", err.Error())
}
}
@ -95,8 +63,18 @@ func main() {
return
}
trawlingManager := dht.NewManager(opFlags.IndexerAddrs, opFlags.IndexerMaxNeighbors, opFlags.BootstrappingNodes, opFlags.FilterNodesCIDRs)
metadataSink := metadata.NewSink(5*time.Second, opFlags.LeechMaxN, opFlags.FilterNodesCIDRs)
mainline.DefaultThrottleRate = int(opFlags.MaxRPS)
trawlingManager := dht.NewManager(
opFlags.IndexerAddrs,
opFlags.IndexerMaxNeighbors,
opFlags.BootstrappingNodes,
opFlags.FilterNodesIpNets,
)
metadataSink := metadata.NewSink(
5*time.Second,
int(opFlags.LeechMaxN),
opFlags.FilterNodesIpNets,
)
// The Event Loop
for stopped := false; !stopped; {
@ -122,182 +100,3 @@ func main() {
}
}
}
func parseFlags() error {
var cmdF struct {
RunWithConfigFile bool `long:"with-config-file" description:"Run using yaml configuration file."`
ConfigFilePath string `long:"config-file-path" description:"Configuration file path. If not filled in, it will default to config.yml in the same directory of this program."`
DatabaseURL string `long:"database" description:"URL of the database." default:"postgres://magnetico:magnetico@localhost:5432/magnetico?sslmode=disable" mapstructure:"databaseURL"`
IndexerAddrs []string `long:"indexer-addr" description:"Address(es) to be used by indexing DHT nodes." default:"0.0.0.0:0" mapstructure:"indexerAddrs"`
IndexerMaxNeighbors uint `long:"indexer-max-neighbors" description:"Maximum number of neighbors of an indexer." default:"5000" mapstructure:"indexerMaxNeighbors"`
LeechMaxN uint `long:"leech-max-n" description:"Maximum number of leeches." default:"1000" mapstructure:"leechMaxN"`
MaxRPS uint `long:"max-rps" description:"Maximum requests per second." default:"500" mapstructure:"maxRPS"`
BootstrappingNodes []string `long:"bootstrap-node" description:"Host(s) to be used for bootstrapping." default:"dht.tgragnato.it:80" default:"dht.tgragnato.it:443" default:"dht.tgragnato.it:1337" default:"dht.tgragnato.it:6969" default:"dht.tgragnato.it:6881" default:"dht.tgragnato.it:25401" mapstructure:"bootstrappingNodes"`
FilterNodesCIDRs []string `long:"filter-nodes-cidrs" description:"List of CIDRs on which Magnetico can operate. Empty is open mode." default:"" mapstructure:"filterNodesCIDRs"`
Addr string `short:"a" long:"addr" description:"Address (host:port) to serve on" default:"[::1]:8080" mapstructure:"addr"`
Cred string `short:"c" long:"credentials" description:"Path to the credentials file" default:"" mapstructure:"cred"`
RunDaemon bool `short:"d" long:"daemon" description:"Run the crawler without the web interface." mapstructure:"runDaemon"`
RunWeb bool `short:"w" long:"web" description:"Run the web interface without the crawler." mapstructure:"runWeb"`
}
if _, err := flags.Parse(&cmdF); err != nil {
return err
}
if cmdF.RunWithConfigFile {
if cmdF.ConfigFilePath == "" {
execPath, err := os.Executable()
if err != nil {
return err
}
execPath, err = filepath.Abs(execPath)
if err != nil {
return err
}
cmdF.ConfigFilePath = path.Join(filepath.Dir(execPath), "config.yml")
}
configFilePath := cmdF.ConfigFilePath
configFileNameWithSuffix := path.Base(configFilePath)
configFileType := path.Ext(configFileNameWithSuffix)
configFileNameOnly := strings.TrimSuffix(configFileNameWithSuffix, configFileType)
vip := viper.New()
vip.SetConfigName(configFileNameOnly)
vip.SetConfigType(configFileType[1:])
vip.AddConfigPath(filepath.Dir(configFilePath))
err := vip.ReadInConfig()
if err != nil {
return err
}
err = vip.Unmarshal(&cmdF)
if err != nil {
return err
}
_ = vip.MergeConfigMap(vip.AllSettings())
// return nil
}
if cmdF.RunDaemon && !cmdF.RunWeb {
opFlags.RunDaemon = true
opFlags.RunWeb = false
} else if !cmdF.RunDaemon && cmdF.RunWeb {
opFlags.RunDaemon = false
opFlags.RunWeb = true
} else {
opFlags.RunDaemon = true
opFlags.RunWeb = true
}
opFlags.DatabaseURL = cmdF.DatabaseURL
if opFlags.RunWeb {
opFlags.Addr = cmdF.Addr
opFlags.CredentialsPath = cmdF.Cred
opFlags.Credentials = make(map[string][]byte)
if err := loadCred(opFlags.CredentialsPath); err == nil {
web.UpdateCredentials(opFlags.Credentials)
}
}
if opFlags.RunDaemon {
if err := checkAddrs(cmdF.IndexerAddrs); err != nil {
log.Fatalf("Of argument (list) `trawler-ml-addr` %s\n", err.Error())
} else {
opFlags.IndexerAddrs = cmdF.IndexerAddrs
}
opFlags.IndexerMaxNeighbors = cmdF.IndexerMaxNeighbors
opFlags.LeechMaxN = int(cmdF.LeechMaxN)
if opFlags.LeechMaxN > 1000 {
log.Println(
"Beware that on many systems max # of file descriptors per process is limited to 1024. " +
"Setting maximum number of leeches greater than 1k might cause \"too many open files\" errors!",
)
}
mainline.DefaultThrottleRate = int(cmdF.MaxRPS)
opFlags.BootstrappingNodes = cmdF.BootstrappingNodes
opFlags.FilterNodesCIDRs = []net.IPNet{}
for _, cidr := range cmdF.FilterNodesCIDRs {
if cidr == "" {
continue
}
if _, ipnet, err := net.ParseCIDR(cidr); err == nil {
opFlags.FilterNodesCIDRs = append(opFlags.FilterNodesCIDRs, *ipnet)
} else {
log.Fatalf("Error while parsing CIDR %s: %s\n", cidr, err.Error())
}
}
if len(opFlags.FilterNodesCIDRs) != 0 && reflect.DeepEqual(cmdF.BootstrappingNodes, []string{"dht.tgragnato.it:80", "dht.tgragnato.it:443", "dht.tgragnato.it:1337", "dht.tgragnato.it:6969", "dht.tgragnato.it:6881", "dht.tgragnato.it:25401"}) {
log.Fatalln("You should specify your own internal bootstrapping nodes in filter mode.")
}
}
return nil
}
func checkAddrs(addrs []string) error {
for _, addr := range addrs {
// We are using ResolveUDPAddr but it works equally well for checking TCPAddr(esses) as
// well.
_, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return err
}
}
return nil
}
func loadCred(cred string) error {
if cred == "" {
return nil
}
file, err := os.Open(cred)
if err != nil {
return err
}
reader := bufio.NewReader(file)
for lineno := 1; true; lineno++ {
line, err := reader.ReadBytes('\n')
if err != nil {
if err == io.EOF {
break
}
return errors.New("Error while reading line " + strconv.Itoa(lineno) + " " + err.Error())
}
line = line[:len(line)-1] // strip '\n'
/* The following regex checks if the line satisfies the following conditions:
*
* <USERNAME>:<BCRYPT HASH>
*
* where
* <USERNAME> must start with a small-case a-z character, might contain non-consecutive
* underscores in-between, and consists of small-case a-z characters and digits 0-9.
*
* <BCRYPT HASH> is the output of the well-known bcrypt function.
*/
re := regexp.MustCompile(`^[a-z](?:_?[a-z0-9])*:\$2[aby]?\$\d{1,2}\$[./A-Za-z0-9]{53}$`)
if !re.Match(line) {
return fmt.Errorf("on line %d: format should be: <USERNAME>:<BCRYPT HASH>, instead got: %s", lineno, line)
}
tokens := bytes.Split(line, []byte(":"))
opFlags.Credentials[string(tokens[0])] = tokens[1]
}
return nil
}

56
opflags/load_cred.go Normal file
View File

@ -0,0 +1,56 @@
package opflags
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"os"
"regexp"
"strconv"
)
func (o *OpFlags) LoadCred() error {
if o.Cred == "" {
return nil
}
file, err := os.Open(o.Cred)
if err != nil {
return err
}
reader := bufio.NewReader(file)
for lineno := 1; true; lineno++ {
line, err := reader.ReadBytes('\n')
if err != nil {
if err == io.EOF {
break
}
return errors.New("Error while reading line " + strconv.Itoa(lineno) + " " + err.Error())
}
line = line[:len(line)-1] // strip '\n'
/* The following regex checks if the line satisfies the following conditions:
*
* <USERNAME>:<BCRYPT HASH>
*
* where
* <USERNAME> must start with a small-case a-z character, might contain non-consecutive
* underscores in-between, and consists of small-case a-z characters and digits 0-9.
*
* <BCRYPT HASH> is the output of the well-known bcrypt function.
*/
re := regexp.MustCompile(`^[a-z](?:_?[a-z0-9])*:\$2[aby]?\$\d{1,2}\$[./A-Za-z0-9]{53}$`)
if !re.Match(line) {
return fmt.Errorf("on line %d: format should be: <USERNAME>:<BCRYPT HASH>, instead got: %s", lineno, line)
}
tokens := bytes.Split(line, []byte(":"))
o.Credentials[string(tokens[0])] = tokens[1]
}
return nil
}

66
opflags/load_cred_test.go Normal file
View File

@ -0,0 +1,66 @@
package opflags
import (
"os"
"reflect"
"testing"
)
func TestLoadCred(t *testing.T) {
t.Parallel()
tests := []struct {
name string
credContent string
expectedErr bool
expectedMap map[string][]byte
}{
{
name: "Invalid credentials format",
credContent: "user1:invalidhash\n",
expectedErr: true,
expectedMap: nil,
},
{
name: "Empty credentials",
credContent: "",
expectedErr: false,
expectedMap: map[string][]byte{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a temporary file with the test credentials
tmpfile, err := os.CreateTemp("", "testcred")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())
if _, err := tmpfile.Write([]byte(tt.credContent)); err != nil {
t.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}
// Initialize OpFlags with the temporary file path
o := &OpFlags{
Cred: tmpfile.Name(),
Credentials: make(map[string][]byte),
}
// Call LoadCred and check the result
err = o.LoadCred()
if (err != nil) != tt.expectedErr {
t.Errorf("LoadCred() error = %v, expectedErr %v", err, tt.expectedErr)
return
}
if !tt.expectedErr && !reflect.DeepEqual(o.Credentials, tt.expectedMap) {
t.Errorf("LoadCred() credentials = %v, expectedMap %v", o.Credentials, tt.expectedMap)
}
})
}
}

86
opflags/opflags.go Normal file
View File

@ -0,0 +1,86 @@
package opflags
import (
"errors"
"fmt"
"net"
"reflect"
)
type OpFlags struct {
DatabaseURL string `long:"database" description:"URL of the database." default:"postgres://magnetico:magnetico@localhost:5432/magnetico?sslmode=disable" yaml:"databaseURL"`
IndexerAddrs []string `long:"indexer-addr" description:"Address(es) to be used by indexing DHT nodes." default:"0.0.0.0:0" yaml:"indexerAddrs"`
IndexerMaxNeighbors uint `long:"indexer-max-neighbors" description:"Maximum number of neighbors of an indexer." default:"5000" yaml:"indexerMaxNeighbors"`
LeechMaxN uint `long:"leech-max-n" description:"Maximum number of leeches." default:"1000" yaml:"leechMaxN"`
MaxRPS uint `long:"max-rps" description:"Maximum requests per second." default:"500" yaml:"maxRPS"`
BootstrappingNodes []string `long:"bootstrap-node" description:"Host(s) to be used for bootstrapping." default:"dht.tgragnato.it:80" default:"dht.tgragnato.it:443" default:"dht.tgragnato.it:1337" default:"dht.tgragnato.it:6969" default:"dht.tgragnato.it:6881" default:"dht.tgragnato.it:25401" yaml:"bootstrappingNodes"`
FilterNodesCIDRs []string `long:"filter-nodes-cidrs" description:"List of CIDRs on which Magnetico can operate. Empty is open mode." default:"" yaml:"filterNodesCIDRs"`
FilterNodesIpNets []net.IPNet
Addr string `short:"a" long:"addr" description:"Address (host:port) to serve on" default:"[::1]:8080" yaml:"addr"`
Cred string `short:"c" long:"credentials" description:"Path to the credentials file" default:"" yaml:"cred"`
Credentials map[string][]byte
RunDaemon bool `short:"d" long:"daemon" description:"Run the crawler without the web interface." yaml:"runDaemon"`
RunWeb bool `short:"w" long:"web" description:"Run the web interface without the crawler." yaml:"runWeb"`
ConfigFilePath string `long:"config-file-path" description:"Configuration YAML file path. If not filled in, it will default to disabled." default:""`
}
func (o *OpFlags) check() error {
if o.RunWeb {
o.Credentials = make(map[string][]byte)
if err := o.LoadCred(); err != nil {
return err
}
}
if o.RunDaemon {
if err := o.checkAddrs(); err != nil {
return err
}
if o.LeechMaxN > 1000 {
fmt.Println(
"Beware that on many systems max # of file descriptors per process is limited to 1024. " +
"Setting maximum number of leeches greater than 1k might cause \"too many open files\" errors!",
)
}
o.FilterNodesIpNets = []net.IPNet{}
for _, cidr := range o.FilterNodesCIDRs {
if cidr == "" {
continue
}
if _, ipnet, err := net.ParseCIDR(cidr); err == nil {
o.FilterNodesIpNets = append(o.FilterNodesIpNets, *ipnet)
} else {
return fmt.Errorf("error while parsing CIDR %s: %s", cidr, err.Error())
}
}
if len(o.FilterNodesIpNets) != 0 && reflect.DeepEqual(o.BootstrappingNodes, []string{"dht.tgragnato.it:80", "dht.tgragnato.it:443", "dht.tgragnato.it:1337", "dht.tgragnato.it:6969", "dht.tgragnato.it:6881", "dht.tgragnato.it:25401"}) {
return fmt.Errorf("you should specify your own internal bootstrapping nodes in filter mode")
}
}
return nil
}
func (o *OpFlags) checkAddrs() error {
if len(o.IndexerAddrs) == 0 || len(o.IndexerAddrs) == 1 && o.IndexerAddrs[0] == "" {
return errors.New("IndexerAddrs cannot be empty")
}
for _, addr := range o.IndexerAddrs {
// We are using ResolveUDPAddr but it works equally well for checking TCPAddr(esses) as
// well.
_, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return err
}
}
return nil
}

126
opflags/opflags_test.go Normal file
View File

@ -0,0 +1,126 @@
package opflags
import (
"testing"
)
func TestCheckAddrs(t *testing.T) {
t.Parallel()
tests := []struct {
name string
indexerAddrs []string
expectError bool
}{
{
name: "EmptyIndexerAddrs",
indexerAddrs: []string{},
expectError: true,
},
{
name: "SingleEmptyIndexerAddr",
indexerAddrs: []string{""},
expectError: true,
},
{
name: "ValidIndexerAddrs",
indexerAddrs: []string{"127.0.0.1:6881", "192.168.1.1:6881"},
expectError: false,
},
{
name: "InvalidIndexerAddr",
indexerAddrs: []string{"invalid-addr"},
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opFlags := &OpFlags{
IndexerAddrs: tt.indexerAddrs,
}
err := opFlags.checkAddrs()
if (err != nil) != tt.expectError {
t.Errorf("checkAddrs() error = %v, expectError %v", err, tt.expectError)
}
})
}
}
func TestCheck(t *testing.T) {
t.Parallel()
tests := []struct {
name string
opFlags OpFlags
expectError bool
expectPrintOutput bool
}{
{
name: "RunWebWithValidCred",
opFlags: OpFlags{
RunWeb: true,
},
expectError: false,
},
{
name: "RunWebWithInvalidCred",
opFlags: OpFlags{
RunWeb: true,
Cred: "invalid-cred",
},
expectError: true,
},
{
name: "RunDaemonWithValidAddrs",
opFlags: OpFlags{
RunDaemon: true,
IndexerAddrs: []string{"127.0.0.1:6881"},
},
expectError: false,
},
{
name: "RunDaemonWithInvalidAddrs",
opFlags: OpFlags{
RunDaemon: true,
IndexerAddrs: []string{"invalid-addr"},
},
expectError: true,
},
{
name: "RunDaemonWithInvalidCIDR",
opFlags: OpFlags{
RunDaemon: true,
FilterNodesCIDRs: []string{"invalid-cidr"},
},
expectError: true,
},
{
name: "RunDaemonWithValidCIDR",
opFlags: OpFlags{
IndexerAddrs: []string{"0.0.0.0:0"},
RunDaemon: true,
FilterNodesCIDRs: []string{"192.168.1.0/24"},
},
expectError: false,
},
{
name: "RunDaemonWithDefaultBootstrappingNodesInFilterMode",
opFlags: OpFlags{
RunDaemon: true,
FilterNodesCIDRs: []string{"192.168.1.0/24"},
BootstrappingNodes: []string{"dht.tgragnato.it:80", "dht.tgragnato.it:443", "dht.tgragnato.it:1337", "dht.tgragnato.it:6969", "dht.tgragnato.it:6881", "dht.tgragnato.it:25401"},
},
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.opFlags.check()
if (err != nil) != tt.expectError {
t.Errorf("check() error = %v, expectError %v", err, tt.expectError)
}
})
}
}

35
opflags/parsers.go Normal file
View File

@ -0,0 +1,35 @@
package opflags
import (
"os"
"github.com/jessevdk/go-flags"
"gopkg.in/yaml.v3"
)
func (o *OpFlags) Parse() (err error) {
_, err = flags.Parse(o)
if err != nil {
return err
}
err = o.parseYaml()
if err != nil {
return err
}
return o.check()
}
func (o *OpFlags) parseYaml() error {
if o.ConfigFilePath == "" {
return nil
}
data, err := os.ReadFile(o.ConfigFilePath)
if err != nil {
return err
}
return yaml.Unmarshal(data, o)
}

17
opflags/parsers_test.go Normal file
View File

@ -0,0 +1,17 @@
package opflags
import (
"testing"
)
func TestParseYaml(t *testing.T) {
t.Parallel()
o := &OpFlags{
ConfigFilePath: "../doc/config.example.yml",
}
if err := o.parseYaml(); err != nil {
t.Fatal(err)
}
}