Skip to content

Commit 2b9ff6f

Browse files
committed
Refactor rule system
1 parent 1921ceb commit 2b9ff6f

3 files changed

Lines changed: 34 additions & 29 deletions

File tree

index.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
Autoloader::register();
2121

2222
// Check IP, deny access if not allowed
23-
if(!(empty(Config::ACCESS_IP) OR $_SERVER['REMOTE_ADDR'] == "127.0.0.1" OR $_SERVER['REMOTE_ADDR'] == "::1" OR $_SERVER['REMOTE_ADDR'] == Config::ACCESS_IP)){
23+
if(!(empty(Config::ACCESS_IP) || $_SERVER['REMOTE_ADDR'] == "127.0.0.1" || $_SERVER['REMOTE_ADDR'] == "::1" || $_SERVER['REMOTE_ADDR'] == Config::ACCESS_IP)){
2424
header('Location: login.html');
2525
exit;
2626
}
2727

2828
// Cronjob Rule Run
29-
if(isset($_GET['job']) AND $_GET['job'] === substr(hash('sha256', Config::PASSWORD."ebe8d532"),0,24)){
29+
if((isset($_GET['job']) && $_GET['job'] === substr(hash('sha256', Config::PASSWORD."ebe8d532"),0,24)) || (isset($argc) && $argv[1] === substr(hash('sha256', Config::PASSWORD."ebe8d532"),0,24))){
3030
require_once 'src/Utility.php';
3131
$bitcoind = new jsonRPCClient('http://'.Config::RPC_USER.':'.Config::RPC_PASSWORD.'@'.Config::RPC_IP.'/');
3232
Rule::run();
@@ -39,16 +39,16 @@
3939
$passToken = hash('sha256', Config::PASSWORD."ibe81rn6");
4040

4141
// Active Session
42-
if(isset($_SESSION['login']) AND $_SESSION['login'] === TRUE){
42+
if(isset($_SESSION['login']) && $_SESSION['login'] === TRUE){
4343
// Nothing needs to be done
4444

4545
// Login Cookie available
46-
}elseif(isset($_COOKIE["Login"]) AND $_COOKIE["Login"] == $passToken){
46+
}elseif(isset($_COOKIE["Login"]) && $_COOKIE["Login"] == $passToken){
4747
$_SESSION['login'] = TRUE;
4848
$_SESSION["csfrToken"] = hash('sha256', random_bytes(20));
4949

5050
// Login
51-
}elseif(!isset($_SESSION['login']) AND isset($_POST['password']) AND $_POST['password'] == Config::PASSWORD){
51+
}elseif(!isset($_SESSION['login']) && isset($_POST['password']) && $_POST['password'] == Config::PASSWORD){
5252
$passHashed = hash('sha256', Config::PASSWORD);
5353
$_SESSION['login'] = TRUE;
5454
$_SESSION["csfrToken"] = hash('sha256', random_bytes(20));
@@ -73,7 +73,7 @@
7373

7474
// Content
7575
// Main Page
76-
if(empty($_GET) OR $_GET['p'] == "main") {
76+
if(empty($_GET) || $_GET['p'] == "main") {
7777
try{
7878
$content = createMainContent();
7979
}catch(\Exception $e) {
@@ -85,7 +85,7 @@
8585
}elseif($_GET['p'] == "peers") {
8686

8787
// Check if command
88-
if(isset($_GET['c']) AND $_GET['t'] == $_SESSION["csfrToken"]){
88+
if(isset($_GET['c']) && $_GET['t'] == $_SESSION["csfrToken"]){
8989
// Ban Command
9090
if($_GET['c'] == "ban"){
9191
$err = 0;
@@ -161,7 +161,7 @@
161161

162162
$hosterList = json_decode(file_get_contents('data/hoster.json'),true);
163163

164-
if(isset($_GET['c']) AND $_GET['t'] == $_SESSION["csfrToken"]){
164+
if(isset($_GET['c']) && $_GET['t'] == $_SESSION["csfrToken"]){
165165
// Remove Hoster Command
166166
if($_GET['c'] == "remove"){
167167
if(preg_match("/^[0-9a-zA-Z-,\. ]{3,40}$/", $_GET['n'])) {
@@ -204,7 +204,7 @@
204204
}elseif($_GET['p'] == "banlist") {
205205

206206
// Check if commands needs to be run
207-
if(isset($_GET['c']) AND $_GET['t'] == $_SESSION["csfrToken"]){
207+
if(isset($_GET['c']) && $_GET['t'] == $_SESSION["csfrToken"]){
208208
if($_GET['c'] == "unban"){
209209
if(preg_match("/^([0-9a-z:\.]{7,39}\/[0-9]{1,3})$/", $_GET['ip'], $match)) {
210210
$ip = $match[1];
@@ -232,7 +232,7 @@
232232
$i = 0;
233233
foreach($banlist as $ban){
234234
$timestamp = strtotime($ban[2]);
235-
if(checkIpBanList($ban[0]) AND $timestamp !== FALSE){
235+
if(checkIpBanList($ban[0]) && $timestamp !== FALSE){
236236
$result = $bitcoind->setban($ban[0], "add", $timestamp, true);
237237
$i++;
238238
}
@@ -257,7 +257,7 @@
257257

258258
$editID = NULL;
259259
// Check if commands needs to be run
260-
if(isset($_GET['c']) AND $_GET['t'] == $_SESSION["csfrToken"]){
260+
if(isset($_GET['c']) && $_GET['t'] == $_SESSION["csfrToken"]){
261261
// Save new or edited rule
262262
if($_GET['c'] == "save"){
263263
$rule = new Rule();
@@ -286,7 +286,7 @@
286286
}
287287
// Delete single rule or all
288288
}elseif($_GET['c'] == "delete"){
289-
if(isset($_GET['id']) AND ctype_digit($_GET['id'])){
289+
if(isset($_GET['id']) && ctype_digit($_GET['id'])){
290290
$reponse = Rule::deleteByID($_GET['id']);
291291
if($reponse){
292292
$message = "Rule succesfully deleted";
@@ -329,7 +329,7 @@
329329
// Memory Pool Page
330330
}elseif($_GET['p'] == "mempool") {
331331

332-
if(isset($_GET['e']) AND ctype_digit($_GET['id'])){
332+
if(isset($_GET['e']) && ctype_digit($_GET['id'])){
333333
$end = $_GET['e'];
334334
}else{
335335
$end = Config::DISPLAY_TXS;
@@ -358,10 +358,10 @@
358358
// Settings Page
359359
}elseif($_GET['p'] == "settings") {
360360
$geoPeers = Config::PEERS_GEO;
361-
if(isset($_GET['c']) AND $_GET['t'] == $_SESSION["csfrToken"]){
362-
if(isset($_GET['c']) AND $_GET['c'] == "geosave"){
361+
if(isset($_GET['c']) && $_GET['t'] == $_SESSION["csfrToken"]){
362+
if(isset($_GET['c']) && $_GET['c'] == "geosave"){
363363
// Check if Geo Peer Tracing was changed
364-
if(isset($_POST['geopeers']) AND $_POST['geopeers'] == "on"){
364+
if(isset($_POST['geopeers']) && $_POST['geopeers'] == "on"){
365365
$geoPeers = "true";
366366
}else{
367367
$geoPeers = "false";

src/Rule.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public static function run() {
9595
$data = self::getData();
9696

9797
if(empty($data['peers'])){
98-
return false;
98+
return true;
9999
}
100100

101101
$logging = "";
@@ -159,22 +159,22 @@ public static function run() {
159159
case "ban":
160160
try{
161161
$msg1 = $bitcoind->setban($peer->ip, "add", intval($rule->bantime));
162-
$logging .= $logTime.": Banned (".$rule->trigger."): ".$peer->ip." (".$peer->client.") for ".$rule->bantime." s - (".$rule->id.")\r\n";
162+
$logging .= $logTime." - ID ".$rule->id.": Banned (".$rule->trigger."): ".$peer->ip." (".$peer->client.") for ".$rule->bantime." s\r\n";
163163
}catch(Exception $e) {
164-
$logging .= $logTime.": Error banning ".$peer->ip." for ".$rule->bantime." s (".$rule->id.")\r\n";
164+
$logging .= $logTime." - ID ".$rule->id.": Error banning ".$peer->ip." (".$peer->client.") for ".$rule->bantime." s\r\n";
165165
}
166166

167167
break;
168168
case "disconnect":
169169
try{
170170
$bitcoind->disconnectnode($peer->ip);
171-
$logging .= $logTime.": Disconnected (".$rule->trigger."): ".$peer->ip." (".$peer->client.") - (".$rule->id.")\r\n";
171+
$logging .= $logTime." - ID ".$rule->id.": Disconnected (".$rule->trigger."): ".$peer->ip." (".$peer->client.")\r\n";
172172
}catch(Exception $e) {
173-
$logging .= $logTime.": Error disconnecting ".$peer->ip." (".$rule->id.")\r\n";
173+
$logging .= $logTime." - ID ".$rule->id.": Error disconnecting ".$peer->ip." (".$peer->client.")\r\n";
174174
}
175175
break;
176176
case "notice":
177-
$logging .= $logTime.": Notice (".$rule->trigger."): ".$peer->ip." (".$peer->client.") - (".$rule->id.")\r\n";
177+
$logging .= $logTime." - ID ".$rule->id.": Notice (".$rule->trigger."): ".$peer->ip." (".$peer->client.")\r\n";
178178
break;
179179
}
180180
$rule->uses++;
@@ -194,8 +194,7 @@ public static function run() {
194194
private static function getData(){
195195

196196
$node = new Node();
197-
198-
$data['peers'] = getPeerData(false);
197+
$data['peers'] = getPeerData(false)['peers'];
199198
$data['rules'] = self::getRules();
200199
$data['global']['connections'] = $node->toConn;
201200
$data['global']['traffic'] = $node->tTotal;

views/rules.phtml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,18 @@ In the last fourth row you need to speficy the global trigger event if you check
219219
<li><a class="close-link"><i class="fa fa-close"></i></a>
220220
<li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
221221
<li><a><i class="fa fa-question-circle-o" data-toggle="tooltip" data-html="true" data-placement="left" title="
222-
If you use a linux system you can set up a cron job to run the rules automatically and periodically.
222+
If you use a linux based system you can set up a cron job to run the rules automatically.
223223
"></i></a>
224224
</li>
225225
</ul>
226226
<div class="clearfix"></div>
227227
</div>
228-
Use the following URL/command to run rules as cron job. You probably need to change the folder in the URL. The URL changes after a password change.
229-
<br />
230-
<input value="/usr/bin/curl --silent localhost<?=getcwd()?>/?job=<?= $content['jobToken'] ?> &>/dev/null" size="30" id="cron" />
228+
Use the following command / url to run rules. The token changes after a password change.
229+
<br /><br />
230+
<strong>Bash</strong><br />
231+
<input value="/usr/bin/php <?=getcwd()?>/index.php <?= $content['jobToken'] ?> &>/dev/null" size="50" id="cron" />
232+
<br /><br /><strong>Url</strong><br />
233+
<input value="<?=$_SERVER['HTTP_HOST'] . str_replace('index.php', '', $_SERVER['PHP_SELF'])?>?job=<?= $content['jobToken'] ?>" size="50" id="cronurl" />
231234
</div>
232235
<div class="clearfix"></div>
233236
</div>
@@ -308,7 +311,10 @@ In the last fourth row you need to speficy the global trigger event if you check
308311
{
309312
text: 'Export',
310313
extend: "csv",
311-
className: "btn-sm btn-dark"
314+
className: "btn-sm btn-dark",
315+
exportOptions: {
316+
columns: [0,3,4,5]
317+
}
312318
},
313319
],
314320
responsive: true

0 commit comments

Comments
 (0)