add-http-dns.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. if (isset($_SESSION['username']))
  3. $dirsStatuses = dirsStatuses($_SESSION['username'], "dns", "http");
  4. else
  5. $dirsStatuses = [];
  6. if (processForm()) {
  7. $_POST['domain'] = formatDomain($_POST['domain']);
  8. if ($dirsStatuses[$_POST['dir']] !== false)
  9. output(403, 'Wrong value for <code>dir</code>.');
  10. if (query('select', 'sites', ['domain' => $_POST['domain']], 'domain') !== [])
  11. output(403, 'Ce domaine existe déjà sur ce service.');
  12. $remoteAaaaRecords = dns_get_record($_POST['domain'], DNS_AAAA);
  13. if (is_array($remoteAaaaRecords) !== true)
  14. output(403, 'Ce domaine n\'existe pas.');
  15. if (equalArrays([CONF['ht']['ipv6_address']], array_column($remoteAaaaRecords, 'ipv6')) !== true)
  16. output(403, 'Ce domaine doit avoir pour unique enregistrement AAAA <code>' . CONF['ht']['ipv6_address'] . '</code>.');
  17. $remoteARecords = dns_get_record($_POST['domain'], DNS_A);
  18. if (is_array($remoteARecords) !== true)
  19. output(403, 'Ce domaine n\'existe pas.');
  20. if (equalArrays([CONF['ht']['ipv4_address']], array_column($remoteARecords, 'ip')) !== true)
  21. output(403, 'Ce domaine doit avoir pour unique enregistrement A <code>' . CONF['ht']['ipv4_address'] . '</code>.');
  22. addSite($_SESSION['username'], $_POST['dir'], $_POST['domain'], "dns", "http");
  23. exec(CONF['ht']['sudo_path'] . " " . CONF['ht']['certbot_path'] . " certonly --quiet" . (CONF['ht']['letsencrypt_use_production'] ? '' : ' --test-cert') . " --key-type rsa --rsa-key-size 3072 --webroot --webroot-path /srv/niver/acme --domain " . $_POST['domain'], $output, $returnCode);
  24. if ($returnCode !== 0)
  25. output(500, 'Certbot failed to get a Let\'s Encrypt certificate.');
  26. $nginxConf = 'server {
  27. listen [' . CONF['ht']['ipv6_listen_address'] . ']:' . CONF['ht']['https_port'] . ' ssl http2;
  28. listen ' . CONF['ht']['ipv4_listen_address'] . ':' . CONF['ht']['https_port'] . ' ssl http2;
  29. server_name ' . $_POST['domain'] . ';
  30. root ' . CONF['ht']['ht_path'] . '/' . $_SESSION['username'] . '/' . $_POST['dir'] . ';
  31. ssl_certificate /etc/letsencrypt/live/' . $_POST['domain'] . '/fullchain.pem;
  32. ssl_certificate_key /etc/letsencrypt/live/' . $_POST['domain'] . '/privkey.pem;
  33. include inc/ht-tls.conf;
  34. }
  35. ';
  36. if (file_put_contents(CONF['ht']['nginx_config_path'] . "/" . $_POST['domain'] . ".conf", $nginxConf) === false)
  37. output(500, 'Failed to write Nginx configuration.');
  38. // Reload Nginx
  39. exec(CONF['ht']['sudo_path'] . " " . CONF['ht']['systemctl_path'] . " reload nginx", result_code: $code);
  40. if ($code !== 0)
  41. output(500, 'Failed to reload Nginx.');
  42. output(200, 'Accès HTTP par domaine ajouté sur ce dossier !');
  43. }
  44. ?>
  45. <p>
  46. Ajouter sur un dossier de site un accès <?= linkToDocs('http', 'HTTP') ?> par <?= linkToDocs('dns', 'DNS') ?> et <?= linkToDocs('tls', 'TLS') ?> <?= linkToDocs('ca', 'authentifié par <em>Let\'s Encrypt</em>') ?>.
  47. </p>
  48. <p>
  49. Le domaine doit contenir ces enregistrements :
  50. <dl>
  51. <dt><code>AAAA</code></dt>
  52. <dd>
  53. <code><?= CONF['ht']['ipv6_address'] ?></code>
  54. </dd>
  55. <dt><code>A</code></dt>
  56. <dd>
  57. <code><?= CONF['ht']['ipv4_address'] ?></code>
  58. </dd>
  59. </dl>
  60. </p>
  61. <form method="post">
  62. <label for="domain">Domaine sur lequel répondre</label><br>
  63. <input required="" placeholder="site.<?= PLACEHOLDER_DOMAIN ?>" id="domain" name="domain" type="text"><br>
  64. <label for="dir">Dossier ciblé</label><br>
  65. <select required="" name="dir" id="dir">
  66. <option value="" disabled="" selected="">---</option>
  67. <?php
  68. foreach ($dirsStatuses as $dir => $alreadyEnabled)
  69. echo ' <option' . ($alreadyEnabled ? ' disabled=""' : '') . ' value="' . $dir . '">' . $dir . '</option>' . "\n";
  70. ?>
  71. </select>
  72. <br>
  73. <input value="Valider" type="submit">
  74. </form>