forked from elbereth/dashninja-ctl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dmnctlrpc.script.inc.php
109 lines (94 loc) · 2.99 KB
/
dmnctlrpc.script.inc.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
<?php
/*
This file is part of GoByte Ninja.
https://github.com/gobytecoin/gobyteninja-ctl
GoByte Ninja is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
GoByte Ninja is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GoByte Ninja. If not, see <http://www.gnu.org/licenses/>.
*/
if (!defined('DMN_SCRIPT') || !defined('DMN_CONFIG') || (DMN_SCRIPT !== true) || (DMN_CONFIG !== true)) {
die('Not executable');
}
define('DMN_VERSION_CTLRPC','1.0.0');
xecho('GoByte Ninja Control RPC Client v'.DMN_VERSION_CTLRPC."\n");
if ($argc != 4) {
xecho("Usage: ".basename($argv[0])." uname rpccommand outputfile\n");
die(0);
}
$uname = $argv[1];
$rpccommand = $argv[2];
$outputfile = $argv[3];
$nodepath1 = DMN_PID_PATH.$uname.'/.darkcoin/';
$nodepath2 = DMN_PID_PATH.$uname.'/.gobyte/';
$nodepath3 = DMN_PID_PATH.$uname.'/.gobytecore/';
if (is_dir($nodepath1)) {
$nodepath = $nodepath1;
}
elseif (is_dir($nodepath2)) {
$nodepath = $nodepath2;
}
elseif (is_dir($nodepath3)) {
$nodepath = $nodepath3;
}
else {
xecho("Directory $nodepath3 not found.\n");
die(1);
}
if (file_exists($outputfile)) {
xecho("Output file already exists. Aborting.\n");
die(5);
}
xecho("Loading configuration for $uname: ");
$conf = new GoByteConfig($uname);
if ($conf->isConfigLoaded()) {
echo "OK\n";
}
else {
echo "Error (Loading configuration)\n";
die(2);
}
xecho("Executing RPC command '$rpccommand': ");
$rpc = new \gobyte\EasyGoByte($conf->getconfig('rpcuser'),$conf->getconfig('rpcpassword'),'localhost',$conf->getconfig('rpcport'));
$rpclist = explode(' ',$rpccommand);
$rpcparams = array();
for ($x = 1;$x<count($rpclist);$x++) {
if (ctype_digit($rpclist[$x])) {
$rpcparams[] = intval($rpclist[$x]);
}
elseif (is_numeric($rpclist[$x])) {
$rpcparams[] = floatval($rpclist[$x]);
}
else {
$rpcparams[] = $rpclist[$x];
}
}
$rpccommand = $rpclist[0];
$result = call_user_func_array(array($rpc, $rpccommand),$rpcparams);
if ($result === false) {
echo "Error (Calling RPC $rpccommand with ".count($rpcparams)." parameters) [".$rpc->response['error']['message']."]\n";
die(3);
}
echo "OK\n";
xecho("Writing file $outputfile: ");
if (is_array($result)) {
$res = file_put_contents($outputfile,$rpc->raw_response);
}
else {
$res = file_put_contents($outputfile,$result);
}
if ($res === false) {
xecho("Error (Writing file)\n");
die(4);
}
else {
echo "OK ($res bytes written)\n";
die(0);
}
?>