main.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/LexicalPath.h>
  8. #include <AK/OwnPtr.h>
  9. #include <LibCore/ArgsParser.h>
  10. #include <LibCore/EventLoop.h>
  11. #include <LibCore/LocalServer.h>
  12. #include <LibCore/System.h>
  13. #include <LibFileSystem/FileSystem.h>
  14. #include <LibIPC/SingleServer.h>
  15. #include <LibMain/Main.h>
  16. #include <LibTLS/Certificate.h>
  17. #include <RequestServer/ConnectionFromClient.h>
  18. #include <RequestServer/GeminiProtocol.h>
  19. #include <RequestServer/HttpProtocol.h>
  20. #include <RequestServer/HttpsProtocol.h>
  21. // FIXME: Share b/w RequestServer and WebSocket
  22. ErrorOr<ByteString> find_certificates(StringView serenity_resource_root)
  23. {
  24. auto cert_path = ByteString::formatted("{}/res/ladybird/cacert.pem", serenity_resource_root);
  25. if (!FileSystem::exists(cert_path)) {
  26. auto app_dir = LexicalPath::dirname(TRY(Core::System::current_executable_path()));
  27. cert_path = ByteString::formatted("{}/cacert.pem", LexicalPath(app_dir).parent());
  28. if (!FileSystem::exists(cert_path))
  29. return Error::from_string_view("Don't know how to load certs!"sv);
  30. }
  31. return cert_path;
  32. }
  33. ErrorOr<int> serenity_main(Main::Arguments arguments)
  34. {
  35. AK::set_rich_debug_enabled(true);
  36. int fd_passing_socket { -1 };
  37. StringView serenity_resource_root;
  38. Vector<ByteString> certificates;
  39. Core::ArgsParser args_parser;
  40. args_parser.add_option(fd_passing_socket, "File descriptor of the fd passing socket", "fd-passing-socket", 'c', "fd-passing-socket");
  41. args_parser.add_option(certificates, "Path to a certificate file", "certificate", 'C', "certificate");
  42. args_parser.add_option(serenity_resource_root, "Absolute path to directory for serenity resources", "serenity-resource-root", 'r', "serenity-resource-root");
  43. args_parser.parse(arguments);
  44. // Ensure the certificates are read out here.
  45. if (certificates.is_empty())
  46. certificates.append(TRY(find_certificates(serenity_resource_root)));
  47. DefaultRootCACertificates::set_default_certificate_paths(certificates.span());
  48. [[maybe_unused]] auto& certs = DefaultRootCACertificates::the();
  49. Core::EventLoop event_loop;
  50. [[maybe_unused]] auto gemini = make<RequestServer::GeminiProtocol>();
  51. [[maybe_unused]] auto http = make<RequestServer::HttpProtocol>();
  52. [[maybe_unused]] auto https = make<RequestServer::HttpsProtocol>();
  53. auto client = TRY(IPC::take_over_accepted_client_from_system_server<RequestServer::ConnectionFromClient>());
  54. client->set_fd_passing_socket(TRY(Core::LocalSocket::adopt_fd(fd_passing_socket)));
  55. auto result = event_loop.exec();
  56. // FIXME: We exit instead of returning, so that protocol destructors don't get called.
  57. // The Protocol base class should probably do proper de-registration instead of
  58. // just VERIFY_NOT_REACHED().
  59. exit(result);
  60. }