devart-sqlserver-connector 1.4.0__cp315-cp315-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- devart/sqlserver/__init__.py +25 -0
- devart/sqlserver/http/tunnel.php +743 -0
- devart/sqlserver/sqlalchemy/__init__.py +26 -0
- devart/sqlserver/sqlserver.cp315-win_amd64.pyd +0 -0
- devart/sqlserver/sqlserver.pyi +402 -0
- devart_sqlserver_connector-1.4.0.dist-info/METADATA +144 -0
- devart_sqlserver_connector-1.4.0.dist-info/RECORD +10 -0
- devart_sqlserver_connector-1.4.0.dist-info/WHEEL +5 -0
- devart_sqlserver_connector-1.4.0.dist-info/licenses/LICENSE +85 -0
- devart_sqlserver_connector-1.4.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,743 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
/* ##########################
|
|
4
|
+
|
|
5
|
+
Devart HttpTunnel v1.70.
|
|
6
|
+
|
|
7
|
+
HTTP tunnel script.
|
|
8
|
+
|
|
9
|
+
This script allows you to manage server even if the corresponding port is blocked or remote access to server is not allowed.
|
|
10
|
+
|
|
11
|
+
##########################
|
|
12
|
+
*/
|
|
13
|
+
if ( !function_exists('sys_get_temp_dir')) {
|
|
14
|
+
function sys_get_temp_dir() {
|
|
15
|
+
if (!empty($_ENV['TMP'])) { return realpath($_ENV['TMP']); }
|
|
16
|
+
if (!empty($_ENV['TMPDIR'])) { return realpath( $_ENV['TMPDIR']); }
|
|
17
|
+
if (!empty($_ENV['TEMP'])) { return realpath( $_ENV['TEMP']); }
|
|
18
|
+
$tempfile=tempnam(__FILE__,'');
|
|
19
|
+
if (file_exists($tempfile)) {
|
|
20
|
+
unlink($tempfile);
|
|
21
|
+
return realpath(dirname($tempfile));
|
|
22
|
+
}
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
$tmp_dir = sys_get_temp_dir();
|
|
28
|
+
if(!$tmp_dir){
|
|
29
|
+
define('SYSTEM_TMP_DIR', '');
|
|
30
|
+
}
|
|
31
|
+
else{
|
|
32
|
+
$last_symbol = substr($tmp_dir, -1);
|
|
33
|
+
if($last_symbol == DIRECTORY_SEPARATOR){
|
|
34
|
+
define('SYSTEM_TMP_DIR', $tmp_dir);
|
|
35
|
+
}
|
|
36
|
+
else{
|
|
37
|
+
define('SYSTEM_TMP_DIR', $tmp_dir . DIRECTORY_SEPARATOR);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
$SUB_DIRECTORY = SYSTEM_TMP_DIR . 'tunnel_files';
|
|
42
|
+
$LOG_FILE_NAME = 'httptunnel_server.log';
|
|
43
|
+
$CONN_FILE_NAME = '_connections.id.php';
|
|
44
|
+
$LOGFILE = $SUB_DIRECTORY . '/' . $LOG_FILE_NAME;
|
|
45
|
+
$CONN_FILE = $SUB_DIRECTORY . '/' . $CONN_FILE_NAME;
|
|
46
|
+
|
|
47
|
+
$LOG = 0; // Set to "0" to disable logging
|
|
48
|
+
$LOG_DEBUG = 0; // Set to "0" to disable additional debug logging
|
|
49
|
+
$LOGFILEHANDLE = 0;
|
|
50
|
+
$MAXLOGSIZE = "4000000";
|
|
51
|
+
$LIFETIME = 180; // script lifetime in seconds. If script was started and got no client within that time - it exits.
|
|
52
|
+
$READ_WRITE_ATTEMPTS = 100;
|
|
53
|
+
$CREATE_CLIENT_SOCKET_ATTEMPTS = 3;
|
|
54
|
+
$IPC_TMPDIR = "/tmp/";
|
|
55
|
+
|
|
56
|
+
global $SUB_DIRECTORY;
|
|
57
|
+
|
|
58
|
+
function checkFunctionExists($functionName) {
|
|
59
|
+
if (!function_exists($functionName)) {
|
|
60
|
+
echo "Required function <b>$functionName</b> does not exist.</br>";
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Creates connection temporary file if not exists and checks permission to write
|
|
68
|
+
function CreateAndCheckConnFile($fileName) {
|
|
69
|
+
|
|
70
|
+
global $SUB_DIRECTORY;
|
|
71
|
+
|
|
72
|
+
if (file_exists($fileName)){
|
|
73
|
+
$newFile = @fopen($fileName, 'a');
|
|
74
|
+
if($newFile)
|
|
75
|
+
fclose($newFile);
|
|
76
|
+
else
|
|
77
|
+
echo "<b>Error</b>: Failed to open ($fileName) file: Permission denied.";
|
|
78
|
+
|
|
79
|
+
}
|
|
80
|
+
else{
|
|
81
|
+
if(!is_dir($SUB_DIRECTORY)){
|
|
82
|
+
mkdir($SUB_DIRECTORY);
|
|
83
|
+
}
|
|
84
|
+
$newFile = @fopen($fileName, 'w');
|
|
85
|
+
if($newFile){
|
|
86
|
+
fwrite($newFile, "<?php echo 'Devart HTTP tunnel temporary file.'; exit; ?>\r\n"); // forbid viewing this file through browser
|
|
87
|
+
fclose($newFile);
|
|
88
|
+
}
|
|
89
|
+
else
|
|
90
|
+
echo "<b>Error</b>: Failed to create ($fileName) file: Permission denied.";
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if(!$newFile)
|
|
94
|
+
exit;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (!isset($_REQUEST["a"])) { // query from browser
|
|
98
|
+
|
|
99
|
+
echo "Devart HttpTunnel v1.70<br />";
|
|
100
|
+
|
|
101
|
+
$functionList = array(
|
|
102
|
+
// "set_time_limit",
|
|
103
|
+
"stream_socket_server",
|
|
104
|
+
"stream_socket_client",
|
|
105
|
+
"stream_socket_get_name",
|
|
106
|
+
"stream_set_blocking",
|
|
107
|
+
"stream_socket_accept",
|
|
108
|
+
"stream_select"
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
$exist = true;
|
|
112
|
+
foreach($functionList as $functionName) {
|
|
113
|
+
$result = checkFunctionExists($functionName);
|
|
114
|
+
$exist = $exist && $result;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if ($exist) {
|
|
118
|
+
echo "Tunnel script is installed correctly. <br />You can establish connections through the HTTP tunnel.";
|
|
119
|
+
if ($LOG==1)
|
|
120
|
+
echo "<br /> <br /><b>Loging is enabled.</b><br />Log files are located in the tunnel_files folder, which, in its turn, is located in the temporary folder of the operating system: " .$LOGFILE;
|
|
121
|
+
}
|
|
122
|
+
else
|
|
123
|
+
echo "Required PHP functions listed above are not available. Tunneling script will not work without these functions. Please read PHP manuals about how to install listed functions.";
|
|
124
|
+
|
|
125
|
+
exit;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function myErrorHandler($errno, $errstr, $errfile, $errline) {
|
|
129
|
+
switch ($errno) {
|
|
130
|
+
case E_ERROR:
|
|
131
|
+
$errfile = preg_replace('|^.*[\\\\/]|','',$errfile);
|
|
132
|
+
echo $ERRSTR."Error in line $errline of file $errfile: [$errno] $errstr\n";
|
|
133
|
+
exit;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function shutdown () {
|
|
138
|
+
global $ipsock, $rmsock, $outcount, $incount, $td, $te, $sockname, $useunix;
|
|
139
|
+
|
|
140
|
+
if (connection_status() & 1) { // ABORTED
|
|
141
|
+
logline ($_SERVER["REMOTE_ADDR"].": Irregular tunnel disconnect -> disconnecting server");
|
|
142
|
+
logline ($_SERVER["REMOTE_ADDR"].": Sent ".$outcount." bytes, received ".$incount." bytes");
|
|
143
|
+
} elseif (connection_status() & 2) { // TIMEOUT
|
|
144
|
+
logline ($_SERVER["REMOTE_ADDR"].": PHP script timeout -> disconnecting server");
|
|
145
|
+
logline ($_SERVER["REMOTE_ADDR"].": Sent ".$outcount." bytes, received ".$incount." bytes");
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if ($ipsock) fclose($ipsock);
|
|
149
|
+
if ($rmsock) fclose($rmsock);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function logline ($msg) {
|
|
153
|
+
log_line_to_file(0, $msg);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function logdebug($msg) {
|
|
157
|
+
log_line_to_file(1, $msg);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function logerr($msg) {
|
|
161
|
+
global $ERRSTR;
|
|
162
|
+
logline($msg);
|
|
163
|
+
echo $ERRSTR;
|
|
164
|
+
echo $msg;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function log_line_to_file ($debug, $msg) {
|
|
168
|
+
global $LOG, $LOG_DEBUG, $MAXLOGSIZE, $LOGFILE, $LOGFILEHANDLE;
|
|
169
|
+
if ($LOG && ((! $debug) || $LOG_DEBUG)) {
|
|
170
|
+
$LOGFILEHANDLE=fopen ($LOGFILE, "a");
|
|
171
|
+
if ($LOGFILEHANDLE) {
|
|
172
|
+
fwrite ($LOGFILEHANDLE, date("d.m.Y H:i:s")." - $msg\r\n");
|
|
173
|
+
$lstat=fstat($LOGFILEHANDLE);
|
|
174
|
+
if ($lstat["size"]>$MAXLOGSIZE) rotatelog();
|
|
175
|
+
fclose($LOGFILEHANDLE);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function rotatelog() {
|
|
181
|
+
global $LOG, $MAXLOGSIZE, $LOGFILE, $LOGFILEHANDLE;
|
|
182
|
+
if ($LOG) {
|
|
183
|
+
fwrite ($LOGFILEHANDLE, date("d.m.Y H:i:s")." - Logfile reached maximum size ($MAXLOGSIZE)- rotating.\r\n");
|
|
184
|
+
fclose ($LOGFILEHANDLE);
|
|
185
|
+
$suffix = date("Ymd-His") . (function_exists("getmypid") ? "-" . getmypid() : "-" . substr(microtime(), 2, 3));
|
|
186
|
+
rename ($LOGFILE, substr_replace($LOGFILE, $suffix, -3) . ".log");
|
|
187
|
+
$LOGFILEHANDLE=fopen ($LOGFILE, "a");
|
|
188
|
+
if (!$LOGFILEHANDLE)
|
|
189
|
+
$LOG=0;
|
|
190
|
+
else
|
|
191
|
+
fwrite ($LOGFILEHANDLE, date("d.m.Y H:i:s")." - Opening new Logfile.\r\n");
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function create_client_socket() {
|
|
196
|
+
global $CREATE_CLIENT_SOCKET_ATTEMPTS, $_REQUEST, $IPC_TMPDIR;
|
|
197
|
+
|
|
198
|
+
if (!isset($_REQUEST["port"])) {
|
|
199
|
+
echo $ERRSTR."Port not set.";
|
|
200
|
+
return 0;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
$port = $_REQUEST["port"];
|
|
204
|
+
$useunix = in_array("unix", stream_get_transports());
|
|
205
|
+
|
|
206
|
+
$retryCount = 0;
|
|
207
|
+
do {
|
|
208
|
+
if ($retryCount > 0) {
|
|
209
|
+
usleep(10000); // 10ms
|
|
210
|
+
logdebug("Attempt to create client socket #".($retryCount + 1)." ErrorCode:".$errcode." ErrorMessage:".$errmessage." Port:".$port);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if ($useunix) { // this is for the unix socket type
|
|
214
|
+
$sockname = preg_replace('/\\\\/','/', $IPC_TMPDIR."tun$port.sock");
|
|
215
|
+
$client = stream_socket_client("unix://".$sockname, $errcode, $errmessage);
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
$client = stream_socket_client("tcp://127.0.0.1:".$port, $errcode, $errmessage);
|
|
219
|
+
}
|
|
220
|
+
$retryCount = $retryCount + 1;
|
|
221
|
+
} while(!$client && $retryCount < $CREATE_CLIENT_SOCKET_ATTEMPTS);
|
|
222
|
+
|
|
223
|
+
if ($client) {
|
|
224
|
+
stream_set_blocking($client, 1);
|
|
225
|
+
}
|
|
226
|
+
return $client;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function send_server_script_message($command) {
|
|
230
|
+
global $_REQUEST;
|
|
231
|
+
|
|
232
|
+
$client = create_client_socket();
|
|
233
|
+
if (!$client) {
|
|
234
|
+
logerr("Failed to create client socket");
|
|
235
|
+
return FALSE;
|
|
236
|
+
}
|
|
237
|
+
if (fwrite($client, $command, 1) === FALSE) {
|
|
238
|
+
logerr("Failed to send message to server script.");
|
|
239
|
+
fclose($client);
|
|
240
|
+
return FALSE;
|
|
241
|
+
}
|
|
242
|
+
fclose($client);
|
|
243
|
+
return TRUE;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function increase_script_lifetime() {
|
|
247
|
+
global $LIFETIME;
|
|
248
|
+
if (function_exists("set_time_limit")) {
|
|
249
|
+
set_time_limit($LIFETIME);
|
|
250
|
+
logdebug("Script liftetime incremented with $LIFETIME");
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function write_to_socket($socket, $buffer, $count) {
|
|
255
|
+
global $READ_WRITE_ATTEMPTS;
|
|
256
|
+
|
|
257
|
+
$totalCount = 0;
|
|
258
|
+
$retryCount = 0;
|
|
259
|
+
|
|
260
|
+
do {
|
|
261
|
+
if ($retryCount > 0)
|
|
262
|
+
usleep(10000); // 10ms
|
|
263
|
+
|
|
264
|
+
if (!$socket)
|
|
265
|
+
break;
|
|
266
|
+
|
|
267
|
+
$written = fwrite($socket, $buffer, $count);
|
|
268
|
+
$buffer = substr($buffer, $written);
|
|
269
|
+
$totalCount += $written;
|
|
270
|
+
|
|
271
|
+
if ($retryCount > 0)
|
|
272
|
+
logdebug("Attempt to write #".($retryCount + 1)." Write: ".$written);
|
|
273
|
+
|
|
274
|
+
if ($written <= 0)
|
|
275
|
+
$retryCount = $retryCount + 1;
|
|
276
|
+
|
|
277
|
+
} while($totalCount < $count && $retryCount < $READ_WRITE_ATTEMPTS);
|
|
278
|
+
|
|
279
|
+
if ($totalCount != $count)
|
|
280
|
+
logline("ERROR: Failed to write to socket $count bytes, $totalCount actually written.");
|
|
281
|
+
|
|
282
|
+
return $totalCount;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// reads specified byte count from socket
|
|
286
|
+
function read_from_socket($socket, &$buffer, $count) {
|
|
287
|
+
global $READ_WRITE_ATTEMPTS;
|
|
288
|
+
|
|
289
|
+
$totalCount = 0;
|
|
290
|
+
$retryCount = 0;
|
|
291
|
+
|
|
292
|
+
$buffer = "";
|
|
293
|
+
$readBuffer;
|
|
294
|
+
|
|
295
|
+
do {
|
|
296
|
+
if ($retryCount > 0)
|
|
297
|
+
usleep(10000); // 10ms
|
|
298
|
+
|
|
299
|
+
if (!$socket)
|
|
300
|
+
break;
|
|
301
|
+
|
|
302
|
+
$readBuffer = fread($socket, $count);
|
|
303
|
+
$read = strlen($readBuffer);
|
|
304
|
+
$buffer = $buffer.$readBuffer;
|
|
305
|
+
|
|
306
|
+
if ($retryCount > 0)
|
|
307
|
+
logdebug("Attempt to read #".($retryCount + 1)." Read: ".$read);
|
|
308
|
+
|
|
309
|
+
$totalCount += $read;
|
|
310
|
+
if ($read <= 0)
|
|
311
|
+
$retryCount = $retryCount + 1;
|
|
312
|
+
|
|
313
|
+
} while($totalCount < $count && $retryCount < $READ_WRITE_ATTEMPTS);
|
|
314
|
+
|
|
315
|
+
if ($totalCount != $count)
|
|
316
|
+
logerr("Failed to read from socket $count bytes, $totalCount actually read.");
|
|
317
|
+
|
|
318
|
+
return $totalCount;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// packet: size of data count | data count | data
|
|
322
|
+
// lengths: 1 byte | up to 255 bytes, typically 1 - 5| up to $MaxCount
|
|
323
|
+
function write_data_packet($socket, &$buffer, $count) {
|
|
324
|
+
|
|
325
|
+
$countLength = strlen($count);
|
|
326
|
+
// write length of data count digit
|
|
327
|
+
write_to_socket($socket, $countLength, 1);
|
|
328
|
+
// write data count
|
|
329
|
+
write_to_socket($socket, $count, $countLength);
|
|
330
|
+
// write data
|
|
331
|
+
$writeCount = write_to_socket($socket, $buffer, $count);
|
|
332
|
+
if ($writeCount == $count)
|
|
333
|
+
return $writeCount;
|
|
334
|
+
else
|
|
335
|
+
return 0;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
function read_data_packet($socket, &$buffer) {
|
|
339
|
+
|
|
340
|
+
// obtain data length digit length
|
|
341
|
+
read_from_socket($socket, $countSize, 1);
|
|
342
|
+
// read data length
|
|
343
|
+
read_from_socket($socket, $readCount, $countSize);
|
|
344
|
+
$expectedReadCount = $readCount;
|
|
345
|
+
// read data
|
|
346
|
+
$readCount = read_from_socket($socket, $buffer, $readCount);
|
|
347
|
+
if ($readCount == $expectedReadCount)
|
|
348
|
+
return $readCount;
|
|
349
|
+
else
|
|
350
|
+
return FALSE;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// Start of the tunnel script
|
|
354
|
+
$isServer = FALSE;
|
|
355
|
+
|
|
356
|
+
if (version_compare("5.0.0", phpversion())==1) die ("Only PHP 5 or above supported");
|
|
357
|
+
error_reporting(0);
|
|
358
|
+
set_error_handler("myErrorHandler");
|
|
359
|
+
register_shutdown_function ("shutdown");
|
|
360
|
+
// no-cache
|
|
361
|
+
Header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
|
|
362
|
+
Header("Cache-Control: no-cache, must-revalidate");
|
|
363
|
+
Header("Pragma: no-cache"); // HTTP/1.1
|
|
364
|
+
Header("Last-Modified: ".gmdate("D, d M Y H:i:s")."GMT");
|
|
365
|
+
Header("Content-Type: application/octet-stream");
|
|
366
|
+
|
|
367
|
+
ob_implicit_flush();
|
|
368
|
+
|
|
369
|
+
CreateAndCheckConnFile($CONN_FILE);
|
|
370
|
+
|
|
371
|
+
// Maximum bytes to read at once
|
|
372
|
+
$MaxReadCount = 16*1024;
|
|
373
|
+
|
|
374
|
+
// operation success identification
|
|
375
|
+
$OKSTR = "OK:";
|
|
376
|
+
$ERRSTR = "ER:";
|
|
377
|
+
|
|
378
|
+
$CONN_FILE_MAXSIZE = 100;
|
|
379
|
+
|
|
380
|
+
// Primary tunnel connection
|
|
381
|
+
// need the following REQUEST vars:
|
|
382
|
+
// a: "c"
|
|
383
|
+
// s: remote server name
|
|
384
|
+
// p: remote server port
|
|
385
|
+
|
|
386
|
+
// every operation output at least first three chars identifying the success of operation: "OK:" if succeeded, "ER:" if not.
|
|
387
|
+
//
|
|
388
|
+
|
|
389
|
+
if ($_REQUEST["a"]=="c") { // run server script
|
|
390
|
+
$isServer=TRUE;
|
|
391
|
+
// clear log
|
|
392
|
+
if ($LOG_DEBUG) {
|
|
393
|
+
// truncate log file
|
|
394
|
+
$logfile = fopen($LOGFILE, 'w');
|
|
395
|
+
fclose($logfile);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
$useunix = in_array("unix", stream_get_transports());
|
|
399
|
+
$dad = $_REQUEST["s"];
|
|
400
|
+
$dpo = $_REQUEST["p"];
|
|
401
|
+
|
|
402
|
+
// open the interprocess socket
|
|
403
|
+
$errno = 0;
|
|
404
|
+
$errstr = "";
|
|
405
|
+
$ipsock = stream_socket_server("tcp://127.0.0.1:0", $errno, $errstr);
|
|
406
|
+
|
|
407
|
+
if (!$ipsock) {
|
|
408
|
+
logerr("stream_socket_server() failed: reason:".$errno." ".$errstr);
|
|
409
|
+
exit;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
$port = stream_socket_get_name($ipsock, false);
|
|
413
|
+
$port = preg_replace('/^.*?:/','', $port);
|
|
414
|
+
|
|
415
|
+
if ($useunix) { // this is for the unix socket type
|
|
416
|
+
if ($ipsock) fclose($ipsock);
|
|
417
|
+
$ipsock = 0;
|
|
418
|
+
$sockname = preg_replace('/\\\\/','/', $IPC_TMPDIR."tun$port.sock");
|
|
419
|
+
unlink($sockname);
|
|
420
|
+
if (file_exists($sockname)) {
|
|
421
|
+
logerr("Temporary UNIX socket exists and cannot be deleted ($sockname)");
|
|
422
|
+
exit;
|
|
423
|
+
}
|
|
424
|
+
$ipsock = stream_socket_server("unix://".$sockname, $errno, $errstr);
|
|
425
|
+
if (!$ipsock) {
|
|
426
|
+
logerr("stream_socket_server() failed: reason:".$errno." ".$errstr);
|
|
427
|
+
exit;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
stream_set_blocking($ipsock, 1);
|
|
432
|
+
|
|
433
|
+
// open the remote socket
|
|
434
|
+
logline("Connecting to remote $dad: $dpo");
|
|
435
|
+
$rmsock = stream_socket_client("tcp://".$dad.":".$dpo, $errno, $errstr);
|
|
436
|
+
|
|
437
|
+
if (!$rmsock) {
|
|
438
|
+
logerr("Failed to create remote socket at $dad: $dpo. ".$errno." ".$errstr);
|
|
439
|
+
exit;
|
|
440
|
+
}
|
|
441
|
+
else {
|
|
442
|
+
if (isset($_REQUEST["nonblock"]))
|
|
443
|
+
$block = 0;
|
|
444
|
+
else
|
|
445
|
+
$block = 1;
|
|
446
|
+
stream_set_blocking($rmsock, $block);
|
|
447
|
+
logline("Connected to remote $dad: $dpo");
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
// write connection identificator to file. Echo'ing is not appropriate in case of antiviral software, it would be blocked until script finishes
|
|
451
|
+
$newConnFile = FALSE;
|
|
452
|
+
$connFileMode = "a";
|
|
453
|
+
|
|
454
|
+
if (file_exists($CONN_FILE)) {
|
|
455
|
+
$connFile = fopen($CONN_FILE, "r");
|
|
456
|
+
$lstat=fstat($connFile);
|
|
457
|
+
fclose($connFile);
|
|
458
|
+
if ($lstat["size"]>$CONN_FILE_MAXSIZE) {
|
|
459
|
+
$connFileMode = "w";
|
|
460
|
+
$newConnFile = TRUE;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
else {
|
|
464
|
+
$newConnFile = TRUE;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
$connFile = fopen($CONN_FILE, $connFileMode);
|
|
468
|
+
if ($connFile) {
|
|
469
|
+
if ($newConnFile) {
|
|
470
|
+
fwrite($connFile, "<?php echo 'Devart HTTP tunnel temporary file.'; exit; ?>\r\n"); // forbid viewing this file through browser
|
|
471
|
+
}
|
|
472
|
+
$connectionId = str_replace("_", " ", $_REQUEST["id"]);
|
|
473
|
+
fwrite ($connFile, $connectionId." ".$port."\r\n");
|
|
474
|
+
fclose($connFile);
|
|
475
|
+
}
|
|
476
|
+
else {
|
|
477
|
+
logerr("Failed to create connection temporary file.");
|
|
478
|
+
exit;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
if (function_exists("set_time_limit")) {
|
|
482
|
+
set_time_limit($LIFETIME);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
$exit = false;
|
|
486
|
+
$buffer = array();
|
|
487
|
+
$countBuffer = array();
|
|
488
|
+
|
|
489
|
+
while (!$exit) {
|
|
490
|
+
logdebug("Waiting for client...");
|
|
491
|
+
$client = stream_socket_accept($ipsock);
|
|
492
|
+
logline("Client accepted");
|
|
493
|
+
if ($client === FALSE) {
|
|
494
|
+
logline("ERROR: Bad client.");
|
|
495
|
+
continue;
|
|
496
|
+
}
|
|
497
|
+
// read command
|
|
498
|
+
$count = read_from_socket($client, $buffer, 1);
|
|
499
|
+
if ($count == 0) {
|
|
500
|
+
logline("Error reading client command.");
|
|
501
|
+
$exit = true;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
logdebug("Read from client ($count): ".$buffer[0]);
|
|
505
|
+
|
|
506
|
+
$command = $buffer[0];
|
|
507
|
+
|
|
508
|
+
increase_script_lifetime();
|
|
509
|
+
|
|
510
|
+
if ($command == "x") { // close
|
|
511
|
+
logline("Shutting down on client request.");
|
|
512
|
+
$exit = true; // shutdown
|
|
513
|
+
}
|
|
514
|
+
else if ($command == "r") { // read
|
|
515
|
+
if (!$rmsock) {
|
|
516
|
+
logline("ERROR: rmsock is off");
|
|
517
|
+
$exit = true;
|
|
518
|
+
break;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
$readCount = 0;
|
|
522
|
+
$buffer = fread($rmsock, $MaxReadCount);
|
|
523
|
+
if ($buffer === FALSE) {
|
|
524
|
+
logline("ERROR: Remote server disconnected.");
|
|
525
|
+
$exit = true;
|
|
526
|
+
break;
|
|
527
|
+
}
|
|
528
|
+
else {
|
|
529
|
+
$readCount = strlen($buffer);
|
|
530
|
+
logline("Read from remote:($readCount)");
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
if ($readCount >= 0) {
|
|
534
|
+
if ($readCount == 0)
|
|
535
|
+
logline("Nothing to read from remote.");
|
|
536
|
+
|
|
537
|
+
$writeCount = write_data_packet($client, $buffer, $readCount);
|
|
538
|
+
logdebug("Write to client($writeCount): $buffer");
|
|
539
|
+
if ($readCount > 0 && $writeCount == 0) {
|
|
540
|
+
logerr("Failed to write to client.");
|
|
541
|
+
$exit = true;
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
else if ($command == "s") { // select
|
|
546
|
+
if (!$rmsock) {
|
|
547
|
+
logline("ERROR: rmsock is off");
|
|
548
|
+
$exit = true;
|
|
549
|
+
break;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
$readSocks = array($ipsock, $rmsock);
|
|
553
|
+
$writeSocks = NULL;
|
|
554
|
+
$exceptSocks = NULL;
|
|
555
|
+
if (!stream_select($readSocks, $writeSocks, $exceptSocks, NULL))
|
|
556
|
+
$res = $ERRSTR;
|
|
557
|
+
else
|
|
558
|
+
if (in_array($rmsock, $readSocks))
|
|
559
|
+
$res = $OKSTR;
|
|
560
|
+
else
|
|
561
|
+
$res = $ERRSTR;
|
|
562
|
+
$writeCount = write_to_socket($client, $res, strlen($res));
|
|
563
|
+
logdebug("Write to client($writeCount): $res");
|
|
564
|
+
}
|
|
565
|
+
else if ($command == "w") { // write
|
|
566
|
+
if (!$rmsock) {
|
|
567
|
+
logline("ERROR: rmsock is off");
|
|
568
|
+
$exit = true;
|
|
569
|
+
break;
|
|
570
|
+
}
|
|
571
|
+
$readCount = read_data_packet($client, $buffer);
|
|
572
|
+
logline("Read from client: $readCount");
|
|
573
|
+
if ($readCount > 0) {
|
|
574
|
+
$writeCount = write_to_socket($rmsock, $buffer, $readCount);
|
|
575
|
+
logdebug("Write to remote($writeCount): $buffer");
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
else if ($command == "l") { // increment lease time
|
|
579
|
+
logline("Lease time increased.");
|
|
580
|
+
}
|
|
581
|
+
else if ($command == "t") { // test connection command
|
|
582
|
+
$writeCount = write_to_socket($client, $OKSTR, strlen($OKSTR));
|
|
583
|
+
if ($writeCount == 0)
|
|
584
|
+
$exit = true;
|
|
585
|
+
}
|
|
586
|
+
else {
|
|
587
|
+
logline("ERROR: Unknown command: $command. Exiting.");
|
|
588
|
+
$exit = true;
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
logline("Server script closed.");
|
|
593
|
+
exit;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
if ($_REQUEST["a"]=="r") { // read
|
|
597
|
+
|
|
598
|
+
$client = create_client_socket();
|
|
599
|
+
if (!$client) {
|
|
600
|
+
logerr("Failed to connect to server script.");
|
|
601
|
+
exit;
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
logdebug("Client: Reading from server script");
|
|
605
|
+
|
|
606
|
+
if (write_to_socket($client, "r", 1) == 0) { // write "Read" command
|
|
607
|
+
logerr("Write to server script failed.");
|
|
608
|
+
fclose($client);
|
|
609
|
+
exit;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
$buffer;
|
|
613
|
+
$readCount = read_data_packet($client, $buffer);
|
|
614
|
+
if ($readCount === FALSE) {
|
|
615
|
+
logerr("Failed to read response from server script.");
|
|
616
|
+
fclose($client);
|
|
617
|
+
exit;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
$totalCount = strlen($OKSTR) + $readCount;
|
|
621
|
+
|
|
622
|
+
$outputStr = $OKSTR.$buffer;
|
|
623
|
+
|
|
624
|
+
header("Content-Length: ".$totalCount);
|
|
625
|
+
header("Content-Type: application/octet-stream");
|
|
626
|
+
|
|
627
|
+
logline("Client: Read from server $readCount");
|
|
628
|
+
echo $outputStr;
|
|
629
|
+
|
|
630
|
+
fclose($client);
|
|
631
|
+
exit;
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
if ($_REQUEST["a"]=="s") { // select
|
|
635
|
+
|
|
636
|
+
$client = create_client_socket();
|
|
637
|
+
if (!$client) {
|
|
638
|
+
logerr("Failed to connect to server script.");
|
|
639
|
+
exit;
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
logdebug("Client: Selecting from server script");
|
|
643
|
+
|
|
644
|
+
if (write_to_socket($client, "s", 1) == 0) { // write "Select" command
|
|
645
|
+
logerr("Write to server script failed.");
|
|
646
|
+
fclose($client);
|
|
647
|
+
exit;
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
$buffer;
|
|
651
|
+
$count = read_from_socket($client, $buffer, 3);
|
|
652
|
+
if ($count < 3) {
|
|
653
|
+
logerr("Failed to read response from server script.");
|
|
654
|
+
fclose($client);
|
|
655
|
+
exit;
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
fclose($client);
|
|
659
|
+
echo $buffer;
|
|
660
|
+
exit;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
if ($_REQUEST["a"]=="w") { // write
|
|
664
|
+
|
|
665
|
+
$client = create_client_socket();
|
|
666
|
+
if (!$client) {
|
|
667
|
+
logerr("Failed to connect to server script.");
|
|
668
|
+
exit;
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
$postBody= isset($_POST['base64body'])?base64_decode($_POST['base64body']):file_get_contents("php://input"); // Retrieve RAW POST data
|
|
672
|
+
$writeData = $postBody;
|
|
673
|
+
$expectedWriteCount = strlen($writeData);
|
|
674
|
+
$writeCount = write_to_socket($client, "w", 1); // indicate that this is the "Write" command
|
|
675
|
+
if ($writeCount > 0)
|
|
676
|
+
$writeCount = write_data_packet($client, $writeData, $expectedWriteCount);
|
|
677
|
+
|
|
678
|
+
if ($writeCount == 0) {
|
|
679
|
+
logerr("Write to server script failed.");
|
|
680
|
+
fclose($client);
|
|
681
|
+
exit;
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
logdebug("Client: Written $writeCount");
|
|
685
|
+
|
|
686
|
+
fclose($client);
|
|
687
|
+
echo $OKSTR;
|
|
688
|
+
exit;
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
if ($_REQUEST["a"]=="x") { // close
|
|
692
|
+
|
|
693
|
+
echo $OKSTR."Shutted down.";
|
|
694
|
+
send_server_script_message("x");
|
|
695
|
+
exit;
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
if ($_REQUEST["a"] == "l") { // increment server script lease time
|
|
699
|
+
|
|
700
|
+
if (send_server_script_message("l"))
|
|
701
|
+
echo $OKSTR."Incremented server script lease time.";
|
|
702
|
+
exit;
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
if ($_REQUEST["a"] == "t") { // test newly created connection
|
|
706
|
+
|
|
707
|
+
$connectionId = str_replace("_", " ", $_REQUEST["id"]);
|
|
708
|
+
logline($connectionId);
|
|
709
|
+
$connections = file_get_contents($CONN_FILE);
|
|
710
|
+
|
|
711
|
+
if ($connections === FALSE) {
|
|
712
|
+
logerr("Failed to open $CONN_FILE.");
|
|
713
|
+
exit;
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
$lines = explode("\r\n", $connections);
|
|
717
|
+
|
|
718
|
+
// skip first line
|
|
719
|
+
for($i = 1; $i < count($lines); ++$i) {
|
|
720
|
+
$line = $lines[$i];
|
|
721
|
+
$pos = strpos($line, $connectionId);
|
|
722
|
+
if ($pos === FALSE)
|
|
723
|
+
continue;
|
|
724
|
+
|
|
725
|
+
if ($pos === 0) { // starts with
|
|
726
|
+
$parts = explode(" ", $line);
|
|
727
|
+
if (count($parts) != 3) {
|
|
728
|
+
echo "Invalid connection record";
|
|
729
|
+
exit;
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
echo $OKSTR.$parts[2]."\n"."$LIFETIME\n";
|
|
733
|
+
exit;
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
logerr("Connection entry not found.");
|
|
738
|
+
exit;
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
logerr("Invalid tunneling script parameter: ".$_REQUEST["a"]);
|
|
742
|
+
|
|
743
|
+
?>
|