-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnostr-bestrelays.sh
executable file
·109 lines (90 loc) · 3.38 KB
/
nostr-bestrelays.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
echo " "
echo " "
echo " ┌┐┌┌─┐┌─┐┌┬┐┬─┐ ";
echo " ││││ │└─┐ │ ├┬┘ ";
echo " ┘└┘└─┘└─┘ ┴ ┴└─ ";
echo " ┌┐ ┌─┐┌─┐┌┬┐ ┬─┐┌─┐┬ ┌─┐┬ ┬┌─┐";
echo " ├┴┐├┤ └─┐ │ ├┬┘├┤ │ ├─┤└┬┘└─┐";
echo " └─┘└─┘└─┘ ┴ ┴└─└─┘┴─┘┴ ┴ ┴ └─┘";
echo " "
echo " 📡 https://github.com/gourcetools/nostr-bestrelays "
echo " Ping and find best nostr relays for you "
echo " "
# Delete a potentially old relays-list.txt
rm -f relays-list.txt
# Download a list of nostr relays from nostr.watch
echo " == 🌐 Downloading online relays list from nostr.watch "
curl -s https://api.nostr.watch/v1/online | jq -r '.[]' > relays.txt
echo " "
# Check if file is here
if [ -s relays.txt ]
then
echo " == ✅ Succes. "
else
echo " == ❌ Download failed, restarting. "
echo " == ❌ Download failed, restarting.. "
echo " == ❌ Download failed, restarting... "
./nostr-bestrelays.sh
fi
# Remove spaces and wss:// from relays so we can ping urls
cut -c 7- relays.txt > urllist.txt && rm -f relays.txt
# Delete any relays that have a / , cuz it breaks the ping command...
sed -i '/\//d' urllist.txt
# urllist.txt is ready for pinging
echo " "
echo " ======================================================= "
echo " "
# Ping the urls in urllist.txt and sort them by ping
cat urllist.txt | while read LINE
do
echo " == 🏓 Pinging $LINE "
# Timeout 0.250 second. If it takes more than that, we dont want this relay.
# Output a list starting with pings in sorted.txt
timeout 0.250 ping -c 1 "$LINE" | tail -n 1 | awk '{print $4}' | cut -d '/' -f 2 | echo $(cat) + "$LINE" >> pinged.txt
sed -n '$s/\(.*\)+.*/\1/p' pinged.txt
done
echo " "
echo " "
echo " ✅ Done pinging all relays. "
echo " "
# Delete urllist.txt.txt we dont need it anymore.
rm -f urllist.txt
#Delete relays that errored or returned no ping before sorting
while read line; do
if [[ $line =~ ^[0-9] ]]; then
echo "$line" >> ok.txt
fi
done < pinged.txt
mv ok.txt pinged.txt
#Sort relays by ping
sort -n pinged.txt > sorted.txt
# Delete pinged.txt, we dont need it anymore.
rm pinged.txt
# Remove pings data from sorted relay list.
while read line; do
echo ${line} | sed 's/.*+//'>>URLrelays.txt
done <sorted.txt
# Delete sorted.txt, we dont need it anymore.
rm -f sorted.txt
# Remove first character wich is a dot left from the pinging... it works.
sed -i 's/^.//' URLrelays.txt
# Keep the first 10 lines
head -n 10 URLrelays.txt > relays-list.txt
# Delete URLrelays.txt we dont need it anymore.
rm -f URLrelays.txt
# Add back wss:// to relays list
sed -i 's/^/wss:\/\//' relays-list.txt
# All done :)
echo "================================================== "
echo "👇 👇 👇 👇 Best relays for you 👇 👇 👇 👇 "
echo " "
cat ./relays-list.txt
echo " "
echo "👆 👆 👆 👆 Best relays for you 👆 👆 👆 👆 "
echo "================================================== "
echo " "
echo " =========================================="
echo " 💾 Saved 10 best relays in: "
echo " 📁 => ./relays-list.txt "
echo " 🙏 Thank you for using nostr-bestrelays. "
echo " =========================================="