-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathemcproxy.php
More file actions
135 lines (120 loc) · 3.52 KB
/
Copy pathemcproxy.php
File metadata and controls
135 lines (120 loc) · 3.52 KB
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
<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
header('Access-Control-Allow-Credentials: true');
class Emc {
private $host;
private $port;
private $protocol;
private $username;
private $password;
private $connect;
private $allowed_commands = [
'getbestblockhash',
'getblock',
'getblockchaininfo',
'getblockcount',
'getblockhash',
'getchaintips',
'getdifficulty',
'getmempoolinfo',
'getrawmempool',
'gettxlistfor',
'gettxout',
'gettxoutsetinfo',
'name_filter',
'name_history',
'name_mempool',
'name_scan',
'name_show',
'verifychain',
'getcheckpoint',
'createrawtransaction',
'decoderawtransaction',
'decodescript',
'getrawtransaction',
'sendrawtransaction',
'signrawtransaction',
'validateaddress',
'verifymessage',
];
/**
* Emc constructor.
*/
public function __construct()
{
$this->host = 'host';
$this->port = 'port';
$this->protocol = 'protocol';
$this->username = 'username';
$this->password = 'password';
$this->connect = "$this->protocol://$this->username:$this->password@$this->host:$this->port'";
}
/**
* @param $method
* @param array $params
* @return bool|mixed|string
* @throws Exception
*/
public function request($method, $params = [])
{
if (!in_array($method, $this->allowed_commands)) {
$command_error = true;
}
if (isset($command_error) && $command_error === true) {
throw new Exception('Method not allowed', 405);
}
foreach ($params as $param) {
$p[] = is_numeric($param) ? (int)$param : $param;
}
if (!isset($p)) {
$p = $params;
}
$request = json_encode(['method' => $method, 'params' => $p], JSON_UNESCAPED_UNICODE);
$opts = [
'http' => [
'method' => 'POST',
'header' => join(
"\r\n",
[
'Content-Type: application/json; charset=utf-8',
'Accept-Charset: utf-8;q=0.7,*;q=0.7',
]
),
'content' => $request,
'ignore_errors' => true,
'timeout' => 10,
],
'ssl' => [
"verify_peer" => false,
"verify_peer_name" => false,
],
];
$response = @file_get_contents($this->connect, false, stream_context_create($opts));
$response = json_decode($response, true);
if($response == null) {
throw new Exception('Bad Gateway', 502);
}
return $response;
}
}
try {
if(file_get_contents('php://input')) {
$post = json_decode(file_get_contents('php://input'), true);
$params = [];
if($post['params']) {
$params = $post['params'];
}
$emc = new Emc();
echo json_encode($emc->request($post['method'], $params), JSON_UNESCAPED_UNICODE);
}
} catch (Exception $e) {
echo json_encode([
'error' => [
'code' => $e->getCode(),
'message' => $e->getMessage()
]
], JSON_UNESCAPED_UNICODE);
}