commit b1f4aa005113761bc64541aba7036d012fe8ec0e Author: Orph Date: Sun Nov 6 17:40:18 2022 +0000 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d9f9eae --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +vendor/ +config.yaml \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..7d45ec2 --- /dev/null +++ b/composer.json @@ -0,0 +1,13 @@ +{ + "name": "devbones/traefik-proxy", + "type": "project", + "license": "MIT", + "authors": [ + { + "name": "conky" + } + ], + "require": { + "symfony/yaml": "^6.1" + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..73ff2e3 --- /dev/null +++ b/composer.lock @@ -0,0 +1,175 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "b86f98087a378b070b52e8464cfd3b19", + "packages": [ + { + "name": "symfony/polyfill-ctype", + "version": "v1.26.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-ctype.git", + "reference": "6fd1b9a79f6e3cf65f9e679b23af304cd9e010d4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/6fd1b9a79f6e3cf65f9e679b23af304cd9e010d4", + "reference": "6fd1b9a79f6e3cf65f9e679b23af304cd9e010d4", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "provide": { + "ext-ctype": "*" + }, + "suggest": { + "ext-ctype": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "1.26-dev" + }, + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ], + "psr-4": { + "Symfony\\Polyfill\\Ctype\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Gert de Pagter", + "email": "BackEndTea@gmail.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for ctype functions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "ctype", + "polyfill", + "portable" + ], + "support": { + "source": "https://github.com/symfony/polyfill-ctype/tree/v1.26.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2022-05-24T11:49:31+00:00" + }, + { + "name": "symfony/yaml", + "version": "v6.1.6", + "source": { + "type": "git", + "url": "https://github.com/symfony/yaml.git", + "reference": "66c6b0cf52b00f74614a2cf7ae7db08ea1095931" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/yaml/zipball/66c6b0cf52b00f74614a2cf7ae7db08ea1095931", + "reference": "66c6b0cf52b00f74614a2cf7ae7db08ea1095931", + "shasum": "" + }, + "require": { + "php": ">=8.1", + "symfony/polyfill-ctype": "^1.8" + }, + "conflict": { + "symfony/console": "<5.4" + }, + "require-dev": { + "symfony/console": "^5.4|^6.0" + }, + "suggest": { + "symfony/console": "For validating YAML files using the lint command" + }, + "bin": [ + "Resources/bin/yaml-lint" + ], + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\Yaml\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Loads and dumps YAML files", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/yaml/tree/v6.1.6" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2022-10-07T08:04:03+00:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [], + "plugin-api-version": "2.3.0" +} diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..ea333a5 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,11 @@ +version: '3' + +services: + handler: + image: thecodingmachine/php:8.1-v4-slim-apache + restart: unless-stopped + ports: + - "8080:80" + volumes: + - "./:/var/www/html" + - /var/run/docker.sock:/var/run/docker.sock diff --git a/index.php b/index.php new file mode 100644 index 0000000..2b146f7 --- /dev/null +++ b/index.php @@ -0,0 +1,76 @@ + [ + '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; + } + + $main_router = [ + 'entryPoints' => ['websecure'], + 'rule' => $router['rule'], + 'service' => $server_key, + 'tls' => [ + 'certResolver' => 'defaultResolver', + 'domains' => [], + ], + ]; + + foreach ($domains as $domain) { + $main_router['tls']['domains'][] = [ + 'main' => $domain, + 'sans' => [], + ]; + } + + $router_name = $server_key . '--' . $router['name']; + $router_name = str_replace(['@'], '_', $router_name); + $data['http']['routers'][$router_name] = $main_router; + } +} + + +header('application/x-yaml'); +echo Yaml::dump($data, 4, 2, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK | Yaml::DUMP_OBJECT_AS_MAP);