83 lines
2.0 KiB
PHP
83 lines
2.0 KiB
PHP
<?php
|
|
|
|
use Symfony\Component\Yaml\Yaml;
|
|
|
|
require './vendor/autoload.php';
|
|
|
|
$config = Yaml::parseFile('config.yaml');
|
|
|
|
$data = [
|
|
'http' => [
|
|
'routers' => [],
|
|
'services' => [],
|
|
],
|
|
];
|
|
foreach ($config['servers'] as $server_name => $server_ip) {
|
|
$proxy_routers = @file_get_contents("http://{$server_ip}:8080/api/http/routers");
|
|
if (empty($proxy_routers)) {
|
|
continue;
|
|
}
|
|
$proxy_routers = json_decode($proxy_routers, TRUE);
|
|
$server_key = 'proxy-'.$server_name;
|
|
$data['http']['services'][$server_key] = [
|
|
'loadBalancer' => [
|
|
'servers' => [
|
|
['url' => "http://{$server_ip}:80"],
|
|
],
|
|
'passHostHeader' => TRUE,
|
|
],
|
|
];
|
|
foreach ($proxy_routers as $router) {
|
|
if ($router['status'] !== 'enabled' || $router['provider'] !== 'docker') {
|
|
continue;
|
|
}
|
|
preg_match_all("/Host\(\`(?'domain'[a-zA-Z\.\-]+)\`\)/m", $router['rule'], $rule_hosts);
|
|
if (empty($rule_hosts['domain'])) {
|
|
continue;
|
|
}
|
|
|
|
$domains = [];
|
|
foreach ($rule_hosts['domain'] as $domain) {
|
|
$is_docker_name = !str_contains($domain, '.');
|
|
if ($is_docker_name) {
|
|
continue;
|
|
}
|
|
$domains[] = $domain;
|
|
}
|
|
if (empty($domains)) {
|
|
continue;
|
|
}
|
|
|
|
$http_router = [
|
|
'entryPoints' => ['web'],
|
|
'rule' => $router['rule'],
|
|
'service' => $server_key,
|
|
];
|
|
|
|
$ssl_router = [
|
|
'entryPoints' => ['websecure'],
|
|
'rule' => $router['rule'],
|
|
'service' => $server_key,
|
|
'tls' => [
|
|
'certResolver' => 'defaultResolver',
|
|
'domains' => [],
|
|
],
|
|
];
|
|
|
|
foreach ($domains as $domain) {
|
|
$ssl_router['tls']['domains'][] = [
|
|
'main' => $domain,
|
|
'sans' => [],
|
|
];
|
|
}
|
|
|
|
$router_name = str_replace(['@'], '_', $router['name']);
|
|
$data['http']['routers'][$server_key . '--https-' . $router_name] = $ssl_router;
|
|
$data['http']['routers'][$server_key . '--http-' . $router_name] = $http_router;
|
|
}
|
|
}
|
|
|
|
|
|
header('application/x-yaml');
|
|
echo Yaml::dump($data, 4, 2, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK | Yaml::DUMP_OBJECT_AS_MAP);
|