|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use GuzzleHttp\Client; |
|
4
|
|
|
use GuzzleHttp\HandlerStack; |
|
5
|
|
|
use IntegerNet\CallbackProxy\Dispatcher; |
|
6
|
|
|
use IntegerNet\CallbackProxy\DispatchStrategy; |
|
7
|
|
|
use IntegerNet\CallbackProxy\HttpClient; |
|
8
|
|
|
use IntegerNet\CallbackProxy\Targets; |
|
9
|
|
|
use Monolog\Logger; |
|
10
|
|
|
use Monolog\Handler\StreamHandler; |
|
11
|
|
|
use Namshi\Cuzzle\Formatter\CurlFormatter; |
|
12
|
|
|
use Namshi\Cuzzle\Middleware\CurlFormatterMiddleware; |
|
13
|
|
|
use Psr\Http\Message\RequestInterface; |
|
14
|
|
|
use Slim\Container; |
|
15
|
|
|
use Slim\Http\Response; |
|
16
|
|
|
|
|
17
|
|
|
require __DIR__ . '/../vendor/autoload.php'; |
|
18
|
|
|
|
|
19
|
|
|
$config = [ |
|
20
|
|
|
'settings' => include '../config.php', |
|
21
|
|
|
'log' => function (Container $container) : Logger { |
|
22
|
|
|
$log = new Logger('proxy'); |
|
23
|
|
|
$log->pushHandler(new StreamHandler(dirname(__DIR__) . '/proxy.log')); |
|
24
|
|
|
return $log; |
|
25
|
|
|
}, |
|
26
|
|
|
'httpClient' => function (Container $container) : HttpClient { |
|
27
|
|
|
$httpHandler = HandlerStack::create(); |
|
28
|
|
|
$httpHandler->after('cookies', new CurlFormatterMiddleware($container->get('log'))); |
|
29
|
|
|
return new HttpClient( |
|
30
|
|
|
new Client( |
|
31
|
|
|
[ |
|
32
|
|
|
'handler' => $httpHandler, |
|
33
|
|
|
'verify' => false, |
|
34
|
|
|
] |
|
35
|
|
|
) |
|
36
|
|
|
); |
|
37
|
|
|
}, |
|
38
|
|
|
'proxyTargets' => function (Container $container) { |
|
39
|
|
|
return array_map( |
|
40
|
|
|
function ($config) { |
|
41
|
|
|
return Targets::fromConfig($config); |
|
42
|
|
|
}, |
|
43
|
|
|
$container->settings['proxy']['targets'] |
|
44
|
|
|
); |
|
45
|
|
|
}, |
|
46
|
|
|
'dispatchStrategy' => function (Container $container) : DispatchStrategy { |
|
47
|
|
|
$class = $container->settings['proxy']['strategy'] ?? DispatchStrategy\DispatchAllReturnFirstSuccess::class; |
|
48
|
|
|
return new $class; |
|
49
|
|
|
}, |
|
50
|
|
|
]; |
|
51
|
|
|
$app = new \Slim\App($config); |
|
52
|
|
|
|
|
53
|
|
|
$app->any( |
|
54
|
|
|
'/{target}/{action}', |
|
55
|
|
|
function (RequestInterface $request, Response $response, array $args) { |
|
56
|
|
|
/** @var $this Container */ |
|
57
|
|
|
$targets = $this->get('proxyTargets'); |
|
58
|
|
|
if (!isset($targets[$args['target']])) { |
|
59
|
|
|
return $response->withStatus(500)->write("Target {$args['target']} does not exist."); |
|
60
|
|
|
} |
|
61
|
|
|
$this->get('log')->debug('INCOMING: ' . (new CurlFormatter)->format($request, [])); |
|
62
|
|
|
$dispatcher = new Dispatcher( |
|
63
|
|
|
$this->get('httpClient'), |
|
64
|
|
|
$targets[$args['target']], |
|
65
|
|
|
$this->get('dispatchStrategy') |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
|
|
$response = $dispatcher->dispatch($request, $response, $args['action']); |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
$body = $response->getBody(); |
|
71
|
|
|
if ($body->isSeekable()) { |
|
72
|
|
|
$previousPosition = $body->tell(); |
|
73
|
|
|
$body->rewind(); |
|
74
|
|
|
} |
|
75
|
|
|
$contents = $body->getContents(); |
|
76
|
|
|
if ($body->isSeekable()) { |
|
77
|
|
|
$body->seek($previousPosition); |
|
78
|
|
|
} |
|
79
|
|
|
$this->get('log')->debug('RESPONSE BODY:' . $contents); |
|
80
|
|
|
|
|
81
|
|
|
return $response->withoutHeader('Transfer-Encoding'); |
|
82
|
|
|
} |
|
83
|
|
|
); |
|
84
|
|
|
|
|
85
|
|
|
$app->run(); |
|
86
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.