forked from albertobraschi/Tech-Eyes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sm_tunnel.php
656 lines (534 loc) · 16.9 KB
/
sm_tunnel.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
<?php
/* ##########################
Devart HttpTunnel v1.66.
HTTP tunnel script.
This script allows you to manage database server even if the corresponding port is blocked or remote access to database server is not allowed.
##########################
*/
$version = '$Revision: 65 $';
// Please create this directory or change to any existing temporary directory
// temporary directory should be writable by php script (chmod 0777)
$temporary_dir = "./tmp"; // on some systems if you get output with 0 size, try to use some local temporary folder
if(empty($temporary_dir)) {
// Get temporary directory
$temporary_dir = smTunnelGetTempDir();
}
// checking temporary directory
if(!is_dir($temporary_dir) || !is_writable($temporary_dir)) {
echo "Temporary Directory specified in sm_tunnel.php doesn't exist or is not writeable";
exit;
}
$LOG = 0; // Set to "0" to disable logging
$LOG_DEBUG = 0; // Set to "0" to disable additional debug logging
$LOGFILE = $temporary_dir."/httptunnel_server.log";
$LOGFILEHANDLE = 0;
$MAXLOGSIZE = "4000000";
$LIFETIME = 180; // script lifetime in seconds. If script was started and got no client within that time - it exits.
$READ_WRITE_ATTEMPTS = 100;
function checkFunctionExists($functionName) {
if (!function_exists($functionName)) {
echo "Required function <b>$functionName</b> does not exist.</br>";
return false;
}
return true;
}
if (!isset($_REQUEST["a"])) { // query from browser
echo "Devart HttpTunnel v1.66<br />";
$functionList = array(
"set_time_limit",
"stream_socket_server",
"stream_socket_client",
"stream_socket_get_name",
"stream_set_blocking",
"stream_socket_accept",
);
$exist = true;
foreach($functionList as $functionName) {
$result = checkFunctionExists($functionName);
$exist = $exist && $result;
}
if ($exist)
echo "Tunnel script is installed correctly. <br />You can establish connections through the HTTP tunnel.";
else
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.";
exit;
}
function smTunnelGetTempDir() {
$commonTempDir = dirname(__FILE__) . '/tmp/';
if(is_dir($commonTempDir) && is_writable($commonTempDir) && is_executable($commonTempDir)) {
return $commonTempDir;
}
global $xcart_dir;
$dir_tmp1 = './tmp';
$dir_tmp2 = './temp';
$dir_tmp3 = '../tmp';
$dir_tmp4 = '../temp';
$dir_oscommerce = DIR_FS_CATALOG . 'images/';
$dir_creloaded = DIR_FS_CATALOG . 'tmp/';
$dir_zencart = DIR_FS_CATALOG . 'cache/';
if( null != $xcart_dir) {
$dir_xcart1 = $xcart_dir . '/var/tmp/';
$dir_xcart2 = $xcart_dir . '/templates_c/';
}
if(is_dir($dir_oscommerce) && is_writable($dir_oscommerce)) {
$temporaryDir = $dir_oscommerce;
} elseif(is_dir($dir_tmp1) && is_writable($dir_tmp1)) {
$temporaryDir = $dir_tmp1;
} elseif(is_dir($dir_tmp2) && is_writable($dir_tmp2)) {
$temporaryDir = $dir_tmp2;
} elseif(is_dir($dir_tmp3) && is_writable($dir_tmp3)) {
$temporaryDir = $dir_tmp3;
} elseif(is_dir($dir_tmp4) && is_writable($dir_tmp4)) {
$temporaryDir = $dir_tmp4;
} elseif(is_dir($dir_creloaded) && is_writable($dir_creloaded)) {
$temporaryDir = $dir_creloaded;
} elseif(is_dir($dir_zencart) && is_writable($dir_zencart)) {
$temporaryDir = $dir_zencart;
} elseif(is_dir($dir_xcart1) && is_writable($dir_xcart1)) {
$temporaryDir = $dir_xcart1;
} elseif(is_dir($dir_xcart2) && is_writable($dir_xcart2)) {
$temporaryDir = $dir_xcart2;
} elseif(ini_get('open_basedir') == null) {
// Get global temporary directory
if(!empty($_ENV['TMP'])) {
$temporaryDir = $_ENV['TMP'];
} elseif(!empty($_ENV['TMPDIR'])) {
$temporaryDir = $_ENV['TMPDIR'];
} elseif(!empty($_ENV['TEMP'])) {
$temporaryDir = $_ENV['TEMP'];
} elseif(!empty($_ENV['windir'])) { // temporary dir under windows
$temporaryDir = $_ENV['windir'];
} elseif(ini_get('session.save_path') != null && is_dir(ini_get('session.save_path')) && is_writable(ini_get('session.save_path')) ) {
$temporaryDir = ini_get('session.save_path');
} elseif(is_writable('/tmp') && is_dir('/tmp')) {
$temporaryDir = '/tmp';
} elseif(is_dir(dirname(tempnam('', 'na'))) && is_writable(dirname(tempnam('', 'na')))) {
$temporaryDir = dirname(tempnam('', 'na'));
} else {
$temporaryDir = '/tmp';
}
}
return realpath($temporaryDir);
}
function myErrorHandler($errno, $errstr, $errfile, $errline) {
switch ($errno) {
case E_ERROR:
$errfile=preg_replace('|^.*[\\\\/]|','',$errfile);
echo $ERRSTR."Error in line $errline of file $errfile: [$errno] $errstr\n";
exit;
}
}
function shutdown () {
global $ipsock, $rmsock, $outcount, $incount, $td, $te, $sockname, $useunix;
if (connection_status() & 1) { // ABORTED
logline ($_SERVER["REMOTE_ADDR"].": Irregular tunnel disconnect -> disconnecting server");
logline ($_SERVER["REMOTE_ADDR"].": Sent ".$outcount." bytes, received ".$incount." bytes");
} elseif (connection_status() & 2) { // TIMEOUT
logline ($_SERVER["REMOTE_ADDR"].": PHP script timeout -> disconnecting server");
logline ($_SERVER["REMOTE_ADDR"].": Sent ".$outcount." bytes, received ".$incount." bytes");
}
if ($ipsock) fclose($ipsock);
if ($rmsock) fclose($rmsock);
}
function logline ($msg) {
log_line_to_file(0, $msg);
}
function logdebug($msg) {
log_line_to_file(1, $msg);
}
function logerr($msg) {
global $ERRSTR;
logline($msg);
echo $ERRSTR;
echo $msg;
}
function log_line_to_file ($debug, $msg) {
global $LOG, $LOG_DEBUG, $MAXLOGSIZE, $LOGFILE, $LOGFILEHANDLE;
if ($LOG && ((! $debug) || $LOG_DEBUG)) {
$LOGFILEHANDLE=fopen ($LOGFILE, "a");
if ($LOGFILEHANDLE) {
fwrite ($LOGFILEHANDLE, date("d.m.Y H:i:s")." - $msg\r\n");
$lstat=fstat($LOGFILEHANDLE);
if ($lstat["size"]>$MAXLOGSIZE) rotatelog();
fclose($LOGFILEHANDLE);
}
}
}
function rotatelog() {
global $LOG, $MAXLOGSIZE, $LOGFILE, $LOGFILEHANDLE;
if ($LOG) {
fwrite ($LOGFILEHANDLE, date("d.m.Y H:i:s")." - Logfile reached maximum size ($MAXLOGSIZE)- rotating.\r\n");
fclose ($LOGFILEHANDLE);
rename ($LOGFILE,"$LOGFILE.old");
$LOGFILEHANDLE=fopen ($LOGFILE, "a");
if (!$LOGFILEHANDLE)
$LOG=0;
else
fwrite ($LOGFILEHANDLE, date("d.m.Y H:i:s")." - Opening new Logfile.\r\n");
}
}
function create_client_socket() {
global $_REQUEST;
if (!isset($_REQUEST["port"])) {
echo $ERRSTR."Port not set.";
return 0;
}
$port = $_REQUEST["port"];
$client = stream_socket_client("tcp://127.0.0.1:".$port);
if ($client) {
stream_set_blocking($client, 1);
}
return $client;
}
function send_server_script_message($command) {
global $_REQUEST;
$client = create_client_socket();
if (!$client) {
logerr("Failed to create client socket");
return FALSE;
}
if (fwrite($client, $command, 1) === FALSE) {
logerr("Failed to send message to server script.");
fclose($client);
return FALSE;
}
fclose($client);
return TRUE;
}
function increase_script_lifetime() {
global $LIFETIME;
set_time_limit($LIFETIME);
logdebug("Script liftetime incremented with $LIFETIME");
}
function write_to_socket($socket, $buffer, $count) {
global $READ_WRITE_ATTEMPTS;
$totalCount = 0;
$retryCount = 0;
do {
if ($retryCount > 0) {
usleep(10000); // 10ms
}
if (!$socket)
break;
$written = fwrite($socket, $buffer, $count);
$buffer = substr($buffer, $written);
$totalCount += $written;
if ($retryCount > 0) {
logdebug("Attempt to write #".($retryCount + 1)." Write: ".$written);
}
$retryCount = $retryCount + 1;
} while($totalCount < $count && $retryCount < $READ_WRITE_ATTEMPTS);
if ($totalCount != $count)
logline("ERROR: Failed to write to socket $count bytes, $totalCount actually written.");
return $totalCount;
}
// reads specified byte count from socket
function read_from_socket($socket, &$buffer, $count) {
global $READ_WRITE_ATTEMPTS;
$totalCount = 0;
$retryCount = 0;
$buffer = "";
$readBuffer;
do {
if ($retryCount > 0) {
usleep(10000); // 10ms
}
if (!$socket)
break;
$readBuffer = fread($socket, $count);
$read = strlen($readBuffer);
$buffer = $buffer.$readBuffer;
if ($retryCount > 0) {
logdebug("Attempt to read #".($retryCount + 1)." Read: ".$read);
}
$totalCount += $read;
$retryCount = $retryCount + 1;
} while($totalCount < $count && $retryCount < $READ_WRITE_ATTEMPTS);
if ($totalCount != $count)
logerr("Failed to read from socket $count bytes, $totalCount actually read.");
return $totalCount;
}
// packet: size of data count | data count | data
// lengths: 1 byte | up to 255 bytes, typically 1 - 5| up to $MaxCount
function write_data_packet($socket, &$buffer, $count) {
$countLength = strlen($count);
// write length of data count digit
write_to_socket($socket, $countLength, 1);
// write data count
write_to_socket($socket, $count, $countLength);
// write data
$writeCount = write_to_socket($socket, $buffer, $count);
if ($writeCount == $count)
return $writeCount;
else
return 0;
}
function read_data_packet($socket, &$buffer) {
// obtain data length digit length
read_from_socket($socket, $countSize, 1);
// read data length
read_from_socket($socket, $readCount, $countSize);
$expectedReadCount = $readCount;
// read data
$readCount = read_from_socket($socket, $buffer, $readCount);
if ($readCount == $expectedReadCount)
return $readCount;
else
return FALSE;
}
// Start of the tunnel script
$isServer = FALSE;
if (version_compare("5.0.0",phpversion())==1) die ("Only PHP 5 or above supported");
error_reporting(0);
set_error_handler("myErrorHandler");
register_shutdown_function ("shutdown");
ob_implicit_flush();
// Maximum bytes to read at once
$MaxReadCount = 16*1024;
// operation success identification
$OKSTR = "OK:";
$ERRSTR = "ER:";
$CONN_FILE = $temporary_dir."/_connections.id.php";
$CONN_FILE_MAXSIZE = 100;
// Primary tunnel connection
// need the following REQUEST vars:
// a: "c"
// s: remote server name
// p: remote server port
// every operation output at least first three chars identifying the success of operation: "OK:" if succeeded, "ER:" if not.
//
if ($_REQUEST["a"]=="c") { // run server script
$isServer=TRUE;
// clear log
if ($LOG_DEBUG) {
// truncate log file
$logfile = fopen($LOGFILE, 'w');
fclose($logfile);
}
$dad=$_REQUEST["s"];
$dpo=$_REQUEST["p"];
// open the interprocess socket
$errno = 0;
$errstr = "";
$ipsock = stream_socket_server("tcp://127.0.0.1:0", $errno, $errstr);
if (!$ipsock) {
logerr("stream_socket_server() failed: reason:".$errno." ".$errstr);
exit;
}
$port=stream_socket_get_name($ipsock,false);
$port=preg_replace('/^.*?:/','', $port);
stream_set_blocking($ipsock, 1);
// open the remote socket
$rmsock = stream_socket_client("tcp://".$dad.":".$dpo, $errno, $errstr);
if (!$rmsock) {
logerr("Failed to create remote socket at $dad: $dpo. ".$errno." ".$errstr);
exit;
}
else {
if (isset($_REQUEST["nonblock"]))
$block = 0;
else
$block = 1;
stream_set_blocking($rmsock, $block);
logline("Connected to remote $dad: $dpo");
}
// write connection identificator to file. Echo'ing is not appropriate in case of antiviral software, it would be blocked until script finishes
$newConnFile = FALSE;
$connFileMode = "a";
if (file_exists($CONN_FILE)) {
$connFile = fopen($CONN_FILE, "r");
$lstat=fstat($connFile);
fclose($connFile);
if ($lstat["size"]>$CONN_FILE_MAXSIZE) {
$connFileMode = "w";
$newConnFile = TRUE;
}
}
else {
$newConnFile = TRUE;
}
$connFile = fopen($CONN_FILE, $connFileMode);
if ($connFile) {
if ($newConnFile) {
fwrite($connFile, "<?php echo 'Devart HTTP tunnel temporary file.'; exit; ?>\r\n"); // forbid viewing this file through browser
}
$connectionId = str_replace("_", " ", $_REQUEST["id"]);
fwrite ($connFile, $connectionId." ".$port."\r\n");
fclose($connFile);
}
else {
logerr("Failed to create connection temporary file.");
exit;
}
set_time_limit($LIFETIME);
$exit = false;
$buffer = array();
$countBuffer = array();
while (!$exit) {
logdebug("Waiting for client...");
$client = stream_socket_accept($ipsock);
logline("Client accepted");
if ($client === FALSE) {
logline("ERROR: Bad client.");
continue;
}
// read command
$count = read_from_socket($client, $buffer, 1);
if ($count == 0) {
logline("Error reading client command.");
$exit = true;
}
logdebug("Read from client ($count): ".$buffer[0]);
$command = $buffer[0];
increase_script_lifetime();
if ($command == "x") { // close
logline("Shutting down on client request.");
$exit = true; // shutdown
}
else if ($command == "r") { // read
if (!$rmsock) {
logline("ERROR: rmsock is off");
$exit = true;
break;
}
$readCount = 0;
$buffer = fread($rmsock, $MaxReadCount);
if ($buffer === FALSE) {
logline("ERROR: Remote server disconnected.");
$exit = true;
break;
}
else {
$readCount = strlen($buffer);
logline("Read from remote:($readCount)");
}
if ($readCount >= 0) {
if ($readCount == 0)
logline("Nothing to read from remote.");
$writeCount = write_data_packet($client, $buffer, $readCount);
logdebug("Write to client($writeCount): $buffer");
if ($readCount > 0 && $writeCount == 0) {
logerr("Failed to write to client.");
$exit = true;
}
}
}
else if ($command == "w") { // write
if (!$rmsock) {
logline("ERROR: rmsock is off");
$exit = true;
break;
}
$readCount = read_data_packet($client, $buffer);
logline("Write from client: $readCount");
if ($readCount > 0) {
$writeCount = write_to_socket($rmsock, $buffer, $readCount);
logdebug("Write to remote($writeCount): $buffer");
}
}
else if ($command == "l") { // increment lease time
logline("Lease time increased.");
}
else if ($command == "t") { // test connection command
$writeCount = write_to_socket($client, $OKSTR, strlen($OKSTR));
if ($writeCount == 0)
$exit = true;
}
else {
logline("ERROR: Unknown command: $command. Exiting.");
$exit = true;
}
}
logline("Server script closed.");
exit;
}
if ($_REQUEST["a"]=="r") { // read
$client = create_client_socket();
if (!$client) {
logerr("Failed to connect to server script.");
exit;
}
logdebug("Client: Reading from server script");
if (write_to_socket($client, "r", 1) == 0) { // write "Read" command
logerr("Write to server script failed.");
fclose($client);
exit;
}
$buffer;
$readCount = read_data_packet($client, $buffer);
if ($readCount === FALSE) {
logerr("Failed to read response from server script.");
fclose($client);
exit;
}
$totalCount = strlen($OKSTR) + $readCount;
$outputStr = $OKSTR.$buffer;
header("Content-Length: ".$totalCount);
header("Content-Type: application/octet-stream");
logline("Client: Read from server $readCount");
echo $outputStr;
fclose($client);
exit;
}
if ($_REQUEST["a"]=="w") { // write
$client = create_client_socket();
if (!$client) {
logerr("Failed to connect to server script.");
exit;
}
$postBody = file_get_contents("php://input"); // Retrieve RAW POST data
$writeData = $postBody;
$expectedWriteCount = strlen($writeData);
$writeCount = write_to_socket($client, "w", 1); // indicate that this is the "Write" command
if ($writeCount > 0)
$writeCount = write_data_packet($client, $writeData, $expectedWriteCount);
if ($writeCount == 0) {
logerr("Write to server script failed.");
fclose($client);
exit;
}
logdebug("Client: Written $writeCount");
fclose($client);
echo $OKSTR;
exit;
}
if ($_REQUEST["a"]=="x") { // close
echo $OKSTR."Shutted down.";
send_server_script_message("x");
exit;
}
if ($_REQUEST["a"] == "l") { // increment server script lease time
if (send_server_script_message("l"))
echo $OKSTR."Incremented server script lease time.";
exit;
}
if ($_REQUEST["a"] == "t") { // test newly created connection
$connectionId = str_replace("_", " ", $_REQUEST["id"]);
logline($connectionId);
$connections = file_get_contents($CONN_FILE);
if ($connections === FALSE) {
logerr("Failed to open $CONN_FILE.");
exit;
}
$lines = explode("\r\n", $connections);
// skip first line
for($i = 1; $i < count($lines); ++$i) {
$line = $lines[$i];
$pos = strpos($line, $connectionId);
if ($pos === FALSE)
continue;
if ($pos === 0) { // starts with
$parts = explode(" ", $line);
if (count($parts) != 3) {
echo "Invalid connection record";
exit;
}
echo $OKSTR.$parts[2]."\n"."$LIFETIME\n";
exit;
}
}
logerr("Connection entry not found.");
exit;
}
logerr("Invalid tunneling script parameter: ".$_REQUEST["a"]);
?>