-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver
79 lines (65 loc) · 2.27 KB
/
server
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
#!/usr/bin/env php
<?php
/*
*---------------------------------------------------------------
* Pure php webserver
*---------------------------------------------------------------
*
* This is the source code for the tutorial:
*/
use davelip\NTLM\Proxy\Proxy;
use davelip\NTLM\Proxy\Server;
use davelip\NTLM\Proxy\Request;
use davelip\NTLM\Proxy\Response;
use Monolog\Logger;
require 'vendor/autoload.php';
# environment configuration
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
# Logger
$logger = new Logger('chanel-name');
$handler = new Monolog\Handler\RotatingFileHandler(
getenv('LOG_PATH') . DIRECTORY_SEPARATOR . getenv('LOG_FILENAME')
, (int)getenv('LOG_ROTATE_COUNT')
, getenv('LOG_LEVEL')
);
# '/' in date format is treated like '/' in directory path
# so Y/m/d-filename will create path: eg. 2017/07/21-filename.log
$handler->setFilenameFormat('{date}-{filename}', 'Y-m');
$logger->pushHandler($handler);
// we never need the first argument
array_shift($argv);
// the next argument should be the port if not use 80
if (empty($argv)) {
$port = getenv('PORT');
} else {
$port = array_shift( $argv );
}
// create a new server instance
try {
$server = new Server('127.0.0.1', $port, $logger);
$logger->info('Server start', []);
// start listening
$server->listen(function(Request $request, Proxy $proxy, Logger $logger)
{
try {
$logger->info('Server listen', []);
// print information that we recived the request
$data = [$request->type(), $request->sessionId(), $request->param()];
$logger->info('Request data', $data);
if ($request->type()=='type1') {
$r = base64_encode($proxy->negotiate(base64_decode($request->param())));
$response = new Response($r);
} else {
$r = base64_encode(($proxy->handleType3(base64_decode($request->param())) ? 'valid' : 'invalid'));
$response = new Response($r);
}
} catch (Exception $e) {
$response = new Response('Generic error');
}
return $response;
}, $logger);
} catch (Exception $e) {
$logger->error('Start up error', [$e->getMessage()]);
echo "Errore: " . $e->getMessage();
}