summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnton Luka Šijanec <anton@sijanec.eu>2023-01-30 17:27:57 +0100
committerAnton Luka Šijanec <anton@sijanec.eu>2023-01-30 17:27:57 +0100
commitc6156989725399a83f4a33384a3c60885ed6f211 (patch)
tree842d3fb0e54e1f5c8ccaf1941dc8caf8a8863c6a
parentfix use after free in torrent structs (diff)
downloadtravnik-c6156989725399a83f4a33384a3c60885ed6f211.tar
travnik-c6156989725399a83f4a33384a3c60885ed6f211.tar.gz
travnik-c6156989725399a83f4a33384a3c60885ed6f211.tar.bz2
travnik-c6156989725399a83f4a33384a3c60885ed6f211.tar.lz
travnik-c6156989725399a83f4a33384a3c60885ed6f211.tar.xz
travnik-c6156989725399a83f4a33384a3c60885ed6f211.tar.zst
travnik-c6156989725399a83f4a33384a3c60885ed6f211.zip
-rw-r--r--src/dht.c27
-rw-r--r--src/main.c4
-rw-r--r--www/index.php108
-rw-r--r--www/info.php78
-rw-r--r--www/list.php17
5 files changed, 140 insertions, 94 deletions
diff --git a/src/dht.c b/src/dht.c
index dc3ff77..c20b6ed 100644
--- a/src/dht.c
+++ b/src/dht.c
@@ -428,7 +428,7 @@ void torrent_print (FILE * s, const struct torrent * t) {
char buf[41];
buf[40] = '\0';
bin2hex(buf, t->hash, 20);
- printf("magnet:?xt=urn:btih:%s%s%s%s%s%s%s%s\n\t**** PEERS ****\n", buf, t->type & announce ? " announce" : "", t->type & peers ? " peers" : "", t->type & info ? " info" : "", t->state & handshake_sent ? " handshake_sent" : "", t->state & handshake_received ? " handshake_received" : "", t->state & extension_sent ? " extension_sent" : "", t->state & extension_received ? " extension_received" : "");
+ printf("magnet:?xt=urn:btih:%s%s%s%s%s%s%s%s ttl=%ld\n\t**** PEERS ****\n", buf, t->type & announce ? " announce" : "", t->type & peers ? " peers" : "", t->type & info ? " info" : "", t->state & handshake_sent ? " handshake_sent" : "", t->state & handshake_received ? " handshake_received" : "", t->state & extension_sent ? " extension_sent" : "", t->state & extension_received ? " extension_received" : "", t->ttl > 0 ? t->ttl-seconds() : -1);
struct peer * p = t->peers;
while (p) {
fprintf(s, "\t");
@@ -559,8 +559,8 @@ void dht_print (FILE * s, const struct dht * d) {
char buf[41];
buf[40] = '\0';
bin2hex(buf, d->id, 20);
- char secret[17*2];
- secret[17*2+1] = '\0';
+ char secret[16*2+1];
+ secret[16*2] = '\0';
bin2hex(secret, d->secret, 16);
fprintf(s, "id=%s socket=%d t=%u p=%u tmax=%u pmax=%u p/t-max=%u runsec=%ld rxp=%u txp=%u rxb=%u txb=%u secret=%s tt=%u tr=%u p=%u\n", buf, d->socket, d->torrents_num, d->peers_num, d->torrents_max, d->peers_max, d->peers_per_torrent_max, seconds()-d->time, d->rxp, d->txp, d->rxb, d->txb, secret, d->tt, d->tr, d->p);
fprintf(s, "**** NODES ****\n");
@@ -587,12 +587,15 @@ void dht_print (FILE * s, const struct dht * d) {
fprintf(s, "\t**** COUNT OF %s BUCKETS: %d\n", i ? "IPv6" : "IPv4", buckets);
}
fprintf(s, "**** COUNT OF NODES: %d\n", nodes);
- printf("**** TORRENTS ****\n");
+ fprintf(s, "**** TORRENTS ****\n");
struct torrent * t = d->torrents;
+ unsigned torrents = 0;
while (t) {
+ torrents++;
torrent_print(s, t);
t = t->next;
}
+ fprintf(s, "**** COUNT OF TORRENTS: %u\n", torrents);
}
/**
@@ -2333,11 +2336,11 @@ void periodic (struct dht * d) {
get_peers(d, &n->addr, t->hash);
n = n->next;
}
- if (sent < K) {
+ if (sent < 1) {
#define RTGP(buckets) {struct bucket * b = d->buckets; \
find(t->hash, &b, NULL); \
struct node * n = b->nodes; \
- while (sent < K && n) { \
+ while (sent < 1 && n) { \
sent++; \
if (!n->unanswered) \
n->last_sent = seconds(); \
@@ -2348,11 +2351,11 @@ void periodic (struct dht * d) {
RTGP(buckets);
RTGP(buckets6);
}
- if (sent < K) {
+ if (sent < 1) {
struct bucket * b = d->buckets;
- while (sent < K && b) {
+ while (sent < 1 && b) {
n = b->nodes;
- while (sent < K && n) {
+ while (sent < 1 && n) {
sent++;
if (!n->unanswered)
n->last_sent = seconds();
@@ -2423,7 +2426,11 @@ void periodic (struct dht * d) {
t = t->next;
}
L(debug, d, "txqp=%u rxrp=%u rxqp=%u txrp=%u", d->txqp, d->rxrp, d->rxqp, d->txrp);
- assert(!(d->txqp > 16384 || d->rxrp > 16384 || d->rxqp > 16384 || d->txrp > 16384));
+#define TOOMUCH 32727
+ if (d->txqp > TOOMUCH || d->rxrp > TOOMUCH || d->rxqp > TOOMUCH || d->txrp > TOOMUCH) {
+ dht_print(stdout, d);
+ raise(SIGABRT);
+ }
d->txqp = d->txrp = d->rxqp = d->rxrp = 0;
}
diff --git a/src/main.c b/src/main.c
index b42c873..4e9c188 100644
--- a/src/main.c
+++ b/src/main.c
@@ -45,10 +45,10 @@ void found_torrent (struct dht * d __attribute__((unused)), const unsigned char
L(debug, d, "%s%s", buf, t ? " stored" : " new");
if (t) {
if (!t->type)
- t->ttl = seconds()+512;
+ t->ttl = seconds()+128;
t->type |= info | peers;
} else {
- if (last_added + 10 > seconds()) {
+ if (last_added + 1 > seconds()) {
L(debug, d, "not adding a torrent this fast");
return;
}
diff --git a/www/index.php b/www/index.php
index ac4bb50..1fd25a2 100644
--- a/www/index.php
+++ b/www/index.php
@@ -1,89 +1,33 @@
<?php
-require_once "vendor/autoload.php";
-use Rhilip\Bencode\TorrentFile;
-use Rhilip\Bencode\ParseException;
-if (empty($_REQUEST["h"])) {
- if ($handle = opendir("..")) {
- echo "<ul>";
- while (false !== ($entry = readdir($handle))) {
- if (preg_match("/torrent$/", $entry)) {
- $h = htmlspecialchars(explode(".", $entry)[0]);
- echo '<li> <a href="?h=' . $h . '">' . $h . '</a>';
- }
- }
- die();
- closedir($handle);
- } else {
- die("ne morem brati direktorija");
- }
-}
-if (!preg_match("/^[a-f0-9A-F]{40}$/", $_REQUEST["h"]))
- die('!preg_match("/^[a-f0-9A-F]{40}$/", $_REQUEST["h"])');
-$t = TorrentFile::load("../".$_REQUEST["h"].".torrent");
+ require_once "vendor/autoload.php";
+ use Rhilip\Bencode\TorrentFile;
+ use Rhilip\Bencode\ParseException;
?>
-<meta name=viewport content='width=device-width, initial-scale=1.0'>
<meta charset=UTF-8 />
-<style>
-table, td, tr, th {
- border: 1px solid gray;
-}
-</style>
-<h1><?= htmlspecialchars($t->getName()) ?></h1>
-<title><?= htmlspecialchars($t->getName()) ?></title>
-<table>
- <tr>
- <th>pridobljeno</th>
- <td><?= date("d. m. Y H:i:s", $t->getCreationDate()) ?></td>
- <tr>
- <th>tip</th>
- <td><?= $t->getProtocol() ?></td>
- <tr>
- <th>datotečni način</th>
- <td><?= $t->getFileMode() ?></td>
- <?php if ($t->isPrivate()) echo "<tr colspan=2><th style=color:red>zaseben</th>"; ?>
- <tr>
- <th colspan=2><a href="<?= htmlspecialchars($t->getMagnetLink()) ?>">magnetna povezava</a></th>
- <tr>
- <th colspan=2><a href=/<?= $_REQUEST["h"] ?>.torrent>torrent datoteka</a></th>
- <tr>
- <th>velikost</th>
- <td><?= number_format($t->getSize()/(1024*1024*1024), 6, ",", "") ?> GiB</td>
- <tr>
- <th>število datotek</th>
- <td><?= $t->getFileCount() ?></td>
- <tr>
- <th>ip naslov vira</th>
- <td><?= htmlspecialchars($t->getRootData()["source"]["ip"]) ?></td>
-<?php if ($t->getRootData()["source"]["v"]) { ?>
- <tr>
- <th>odjemalec vira</th>
- <td><?= htmlspecialchars($t->getRootData()["source"]["v"]) ?></td>
-<?php } ?>
- <tr>
- <th>velikost koščka</th>
- <td><?= $t->getPieceLength()/1024; ?> kB</td>
-<?php if (!empty($t->getSource())) { ?>
- <tr>
- <th>izvor</th>
- <td><?= htmlspecialchars($t->getSource()) ?></td>
-<?php } ?>
-</table>
+<meta name=viewport content='width=device-width, initial-scale=1.0'>
+<h1>število datotek: <?= shell_exec("find .. -name '*.torrent' | wc -l") ?></h1>
+<h2><a href=list.php>seznam</a></h2>
+<h2>iskalnik</h2>
+<form>
+ <input name=i value="<?= !empty($_REQUEST["i"]) ? htmlspecialchars($_REQUEST["i"]) : "" ?>" />
+ <input type=submit value=išči />
+</form>
+<?php if (!empty($_REQUEST["i"])) { ?>
+<h3>rezultati</h3>
+<pre>
+<?php
+$q = "find .. -name '*.torrent' | xargs grep -lie " . escapeshellarg($_REQUEST["i"]);
+echo htmlspecialchars($q);
+?>
+</pre>
+<ul>
<?php
-function p ($k, $v) {
- if (is_array($v)) {
- echo "<li> " . htmlspecialchars($k) . "<ul>";
- foreach ($v as $ke => $va)
- p($ke, $va);
- echo "</ul>";
- } else {
- echo "<li> <b>" . htmlspecialchars($k) . "</b> (" . number_format($v/(1024*1024), 6, ",", "") . " MiB)";
- }
+foreach (explode(PHP_EOL, shell_exec($q)) as $f) {
+ if (trim($f) == "")
+ continue;
+ $t = TorrentFile::load($f);
+ echo "<li> <a href=info.php?h=" . $t->getInfoHash() . ">" . htmlspecialchars($t->getName()) . "</a></li>";
}
-echo "<ul>";
-foreach ($t->getFileTree() as $k => $v)
- p($k, $v);
-echo "</ul>";
?>
-<?php if (preg_match("/Development Server/", $_SERVER["SERVER_SOFTWARE"])) { ?>
-<h1 style=color:red>interna stran, dostop prepovedan</h1>
+</ul>
<?php } ?>
diff --git a/www/info.php b/www/info.php
new file mode 100644
index 0000000..7ec562a
--- /dev/null
+++ b/www/info.php
@@ -0,0 +1,78 @@
+<?php
+require_once "vendor/autoload.php";
+use Rhilip\Bencode\TorrentFile;
+use Rhilip\Bencode\ParseException;
+if (!preg_match("/^[a-f0-9A-F]{40}$/", $_REQUEST["h"]))
+ die('!preg_match("/^[a-f0-9A-F]{40}$/", $_REQUEST["h"])');
+$t = TorrentFile::load("../".$_REQUEST["h"].".torrent");
+?>
+<meta name=viewport content='width=device-width, initial-scale=1.0'>
+<meta charset=UTF-8 />
+<style>
+table, td, tr, th {
+ border: 1px solid gray;
+}
+</style>
+<h1><?= htmlspecialchars($t->getName()) ?></h1>
+<title><?= htmlspecialchars($t->getName()) ?></title>
+<table>
+ <tr>
+ <th>pridobljeno</th>
+ <td><?= date("d. m. Y H:i:s", $t->getCreationDate()) ?></td>
+ <tr>
+ <th>tip</th>
+ <td><?= $t->getProtocol() ?></td>
+ <tr>
+ <th>datotečni način</th>
+ <td><?= $t->getFileMode() ?></td>
+ <?php if ($t->isPrivate()) echo "<tr colspan=2><th style=color:red>zaseben</th>"; ?>
+ <tr>
+ <th colspan=2><a href="<?= htmlspecialchars($t->getMagnetLink()) ?>">magnetna povezava</a></th>
+ <tr>
+ <th colspan=2><a href=/<?= $_REQUEST["h"] ?>.torrent>torrent datoteka</a></th>
+<?php try { ?>
+ <tr>
+ <th>velikost</th>
+ <td><?= number_format($t->getSize()/(1024*1024*1024), 6, ",", "") ?> GiB</td>
+ <tr>
+ <th>število datotek</th>
+ <td><?= $t->getFileCount() ?></td>
+<?php } catch (Exception $a) {} ?>
+ <tr>
+ <th>ip naslov vira</th>
+ <td><?= htmlspecialchars($t->getRootData()["source"]["ip"]) ?></td>
+<?php if ($t->getRootData()["source"]["v"]) { ?>
+ <tr>
+ <th>odjemalec vira</th>
+ <td><?= htmlspecialchars($t->getRootData()["source"]["v"]) ?></td>
+<?php } ?>
+ <tr>
+ <th>velikost koščka</th>
+ <td><?= $t->getPieceLength()/1024; ?> kB</td>
+<?php if (!empty($t->getSource())) { ?>
+ <tr>
+ <th>izvor</th>
+ <td><?= htmlspecialchars($t->getSource()) ?></td>
+<?php } ?>
+</table>
+<?php
+function p ($k, $v) {
+ if (preg_match("/padding.file/i", $k))
+ return;
+ if (is_array($v)) {
+ echo "<li> " . htmlspecialchars($k) . "<ul>";
+ foreach ($v as $ke => $va)
+ p($ke, $va);
+ echo "</ul>";
+ } else {
+ echo "<li> <b>" . htmlspecialchars($k) . "</b> (" . number_format($v/(1024*1024), 6, ",", "") . " MiB)";
+ }
+}
+echo "<ul>";
+foreach ($t->getFileTree() as $k => $v)
+ p($k, $v);
+echo "</ul>";
+?>
+<?php if (preg_match("/Development Server/", $_SERVER["SERVER_SOFTWARE"])) { ?>
+<h1 style=color:red>interna stran, dostop prepovedan</h1>
+<?php } ?>
diff --git a/www/list.php b/www/list.php
new file mode 100644
index 0000000..6dbeb40
--- /dev/null
+++ b/www/list.php
@@ -0,0 +1,17 @@
+<?php
+require_once "vendor/autoload.php";
+use Rhilip\Bencode\TorrentFile;
+use Rhilip\Bencode\ParseException;
+if ($handle = opendir("..")) {
+ echo "<ul>";
+ while (false !== ($entry = readdir($handle))) {
+ if (preg_match("/torrent$/", $entry)) {
+ $h = htmlspecialchars(explode(".", $entry)[0]);
+ echo '<li> <a href="info.php?h=' . $h . '">' . $h . '</a>';
+ }
+ }
+ die();
+ closedir($handle);
+} else {
+ die("ne morem brati direktorija");
+}