-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathartbot_rest_server.php
132 lines (111 loc) · 4.42 KB
/
artbot_rest_server.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
<?php
use Amp\Http\Server\Router;
use Amp\Http\Server\DefaultErrorHandler;
use Symfony\Component\Yaml\Yaml;
use Amp\Http\Server\Request;
use Amp\Http\Server\RequestHandler\ClosureRequestHandler;
use Amp\Http\Server\Response;
use Amp\Http\Server\SocketHttpServer;
use Amp\Http\HttpStatus;
use Amp\Http\Server\ErrorHandler;
use Amp\Http\Server\RequestHandler;
use Amp\Socket;
use Monolog\Logger;
use Cspray\Labrador\Http\Cors\ArrayConfiguration;
use Cspray\Labrador\Http\Cors\SimpleConfigurationLoader;
use Cspray\Labrador\Http\Cors\CorsMiddleware;
use function Amp\Http\Server\Middleware\stackMiddleware;
class artbot_rest_server {
public Router $restRouter;
public Logger $logger;
public SocketHttpServer $server;
public RequestHandler $stack;
public ErrorHandler $errorHandler;
public function __construct(
public $logHandler
)
{
$this->logger = new Logger('server');
$this->logger->pushHandler($logHandler);
}
public function initRestServer() {
global $config;
if(!isset($config['listen'])) {
return null;
}
if(isset($config['listen_cert'])) {
$cert = new Socket\Certificate($config['listen_cert']);
$context = (new Socket\BindContext)
->withTlsContext((new Socket\ServerTlsContext)->withDefaultCertificate($cert));
} else {
$context = null;
}
$this->server = SocketHttpServer::createForDirectAccess($this->logger);
if(is_array($config['listen'])) {
foreach ($config['listen'] as $address) {
$this->server->expose($address, $context);
}
} else {
$this->server->expose($config['listen'], $context);
}
$arrayConfig = [
'origins' => ['*'],
'allowed_methods' => ['GET', 'POST', 'PUT'],
'max_age' => 8600,
'allowed_headers' => ['content-type'],
'exposable_headers' => ['content-type'],
'allow_credentials' => false
];
$loader = new SimpleConfigurationLoader(new ArrayConfiguration($arrayConfig));
$middleware = new CorsMiddleware($loader);
$this->errorHandler = new DefaultErrorHandler();
$this->restRouter = new Router($this->server, $this->logger, $this->errorHandler);
$this->stack = stackMiddleware($this->restRouter, $middleware);
$this->setupRoutes();
return $this->server;
}
public function start() {
if(isset($this->server))
$this->server->start($this->stack, $this->errorHandler);
}
public function stop() {
if(isset($this->server))
$this->server->stop();
}
public function addRoute(string $method, string $uri, RequestHandler $requestHandler) {
global $config;
if(!isset($config['listen'])) {
return null;
}
$this->restRouter->addRoute($method, $uri, $requestHandler);
}
private function setupRoutes() {
$this->restRouter->addRoute("POST", "/privmsg/{chan}", new ClosureRequestHandler(
function (Request $request): Response {
$notifier_keys = Yaml::parseFile(__DIR__. '/notifier_keys.yaml');
$key = $request->getHeader('key');
if (isset($notifier_keys[$key])) {
echo "Request from $notifier_keys[$key] ($key)\n";
} else {
echo \Irc\stripForTerminal("Blocked request for bad key $key\n");
return new Response(HttpStatus::FORBIDDEN, [
"content-type" => "text/plain; charset=utf-8"
], "Invalid key");
}
$args = $request->getAttribute(Router::class);
if(!isset($args['chan'])) { // todo not sure if needed
return new Response(HttpStatus::BAD_REQUEST, [
"content-type" => "text/plain; charset=utf-8"
], "Must specify a chan to privmsg");
}
$chan = "#{$args['chan']}";
$msg = $request->getBody()->buffer();
$msg = str_replace("\r", "\n", $msg);
$msg = explode("\n", $msg);
pumpToChan($chan, $msg);
return new Response(HttpStatus::OK, [
"content-type" => "text/plain; charset=utf-8"
], "PRIVMSG sent\n");
}));
}
}