-
Notifications
You must be signed in to change notification settings - Fork 35
/
index.php
executable file
·190 lines (165 loc) · 5.69 KB
/
index.php
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
$TRACKER=urlencode("http://localhost:80/ipmagnet/"); //Remember to include the trailing slash here!
$db = new PDO("sqlite:ipmagnet.db3");
$enableInterval=false;
$trackerInterval=300;
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
function is_sha1($string){
return preg_match('/^[A-Fa-f0-9]{40}$/', $string);
}
//BitTorrent clients will submit the info_hash parameter when requesting the magnet link
if(isset($_GET["info_hash"])){
//prepare the insert query
$query="INSERT INTO hits (hash, timestamp, addr, agent) VALUES (:hash, :timestamp, :addr, :agent)";
$stmt=$db->prepare($query);
if($stmt===FALSE){
exit("d14:failure reason16:Database failuree");
}
//gather all supplied ip addresses
$addrs=htmlentities($_SERVER["REMOTE_ADDR"], ENT_QUOTES);
if(isset($_GET["ipv4"])&&$_GET["ipv4"]!=$_SERVER["REMOTE_ADDR"]){
$addrs.=", ".htmlentities($_GET["ipv4"], ENT_QUOTES);
}
if(isset($_GET["ipv6"])&&$_GET["ipv6"]!=$_SERVER["REMOTE_ADDR"]){
$addrs.=", ".htmlentities($_GET["ipv6"], ENT_QUOTES);
}
if(isset($_GET["ip"])&&$_GET["ip"]!=$_SERVER["REMOTE_ADDR"]){
$addrs.=", ".htmlentities($_GET["ip"], ENT_QUOTES);
}
//insert the hit into the database
if(!($stmt->execute(
array(
":hash" => htmlentities(bin2hex($_GET["info_hash"]), ENT_QUOTES),
":timestamp" => time(),
":addr" => $addrs,
":agent" => htmlentities($_SERVER["HTTP_USER_AGENT"],ENT_QUOTES)
)
))){
//failed to insert.
}
$stmt->closeCursor();
//print the ips as "failure reason" to be displayed by some clients
$resp="IP: ".$addrs;
$resp="14:failure reason".strlen($resp).":".$resp;
if($enableInterval){
$resp.="8:intervali".$trackerInterval."e";
}
exit("d".$resp."e");
}
$returnValue["message"]="ok";
$returnValue["code"]=0;
//if a hash was supplied and it's a valid sha1 hash, use it
if(isset($_GET["hash"]) && is_sha1($_GET["hash"])){
$HASH=strtolower($_GET["hash"]);
}
//else, generate a new one
else{
$HASH=SHA1(mt_rand());
header("Location: ?hash=".$HASH);
exit();
}
$returnValue["hash"]=$HASH;
if(isset($_GET["clear"])){
//delete all hits for a hash from the database
$query="DELETE FROM hits WHERE hash=:hash";
$stmt=$db->prepare($query);
if($stmt===FALSE){
$returnValue["message"]="Failed to prepare query.";
$returnValue["code"]=2;
exit(json_encode($returnValue));
}
$stmt->execute(
array(
":hash"=>$HASH
)
);
$stmt->closeCursor();
}
//get all currently stored hits for a hash
$query="SELECT * FROM hits WHERE hash=:hash";
$stmt=$db->prepare($query);
if($stmt===FALSE){
$returnValue["message"]="Failed to prepare query.";
$returnValue["code"]=3;
exit(json_encode($returnValue));
}
$stmt->execute(
array(
":hash"=>$HASH
)
);
//print the response as JSON data if called from the ajax interface
if(isset($_GET["ajax"])){
//fetch all hits into one array
$returnValue["hits"]=$stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->closeCursor();
//set content-type and CORS headers
header("Content-Type: application/json");
header("Access-Control-Allow-Origin: *");
exit(json_encode($returnValue));
}
?>
<?php echo '<?xml version="1.1" encoding="UTF-8" ?>'; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>ipMagnet</title>
<link rel="icon" href="static/favicon.png" type="image/x-icon" />
<link rel="stylesheet" type="text/css" href="static/ipmagnet.css" />
<script type="text/javascript" src="static/ajax.js"></script>
<script type="text/javascript" src="static/ipmagnet.js"></script>
<meta name="robots" content="noindex,nofollow" />
<noscript>
<meta http-equiv="refresh" content="60; url=?hash=<?php print($HASH); ?>" />
</noscript>
</head>
<body onload="ipmagnet.init('<?php print($HASH); ?>');">
<div id="title-wrap">
<h1>ipMagnet</h1>
</div>
<div id="center-wrap">
<div id="content-main">
<div id="mission-statement">
ipMagnet allows you to see which IP address your BitTorrent Client is handing out to its peers and trackers!
</div>
Add this <a href="magnet:?xt=urn:btih:<?php print($HASH); ?>&dn=ipMagnet+Tracking+Link&tr=<?php print($TRACKER); ?>">Magnet link</a> to your
downloads and watch this page.<br/>
FYI, the address you've accessed this page with is <span id="remote-ip"><?php print($_SERVER["REMOTE_ADDR"]); ?></span>
<div id="current-connections">
<div id="app-links">
<a href="?hash=<?php print($HASH); ?>" class="app-link" id="update-link">Update</a>
<a href="?clear&hash=<?php print($HASH); ?>" class="app-link" id="clear-link">Clear my Data</a>
</div>
<table id="conn-table">
<tr>
<th>Timestamp</th>
<th>IP address(es)</th>
<th>User Agent</th>
</tr>
<?php
$row=$stmt->fetch(PDO::FETCH_ASSOC);
while($row!==FALSE){
print("<tr>");
print("<td>".date("d.m.Y H:i:s (e)",$row["timestamp"])."</td>");
print("<td>".$row["addr"]."</td>");
print("<td>".$row["agent"]."</td>");
print("</tr>");
$row=$stmt->fetch(PDO::FETCH_ASSOC);
}
$stmt->closeCursor();
?>
</table>
</div>
</div>
<div id="footer-text">
<span id="status-line">Status: <span id="status-text">Using plain HTML</span></span>
<span id="meta-footer">
Proudly run without ads or web tracking. Set up your own with the
<a href="https://github.com/cbdevnet/ipmagnet">[source]</a>
<a href="http://www.kopimi.com/kopimi/"><img src="static/kopimi.png" alt="kopimi"/></a>
<a href="http://wtfpl.net/"><img src="static/wtfpl.png" alt="wtfpl"/></a>
</span>
</div>
</div>
</body>
</html>