main.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. #if defined(AK_OS_MACOS)
  22. # include <LibCore/Platform/ProcessStatisticsMach.h>
  23. #endif
  24. ErrorOr<ByteString> find_certificates(StringView serenity_resource_root)
  25. {
  26. auto cert_path = ByteString::formatted("{}/ladybird/cacert.pem", serenity_resource_root);
  27. if (!FileSystem::exists(cert_path))
  28. return Error::from_string_view("Don't know how to load certs!"sv);
  29. return cert_path;
  30. }
  31. ErrorOr<int> serenity_main(Main::Arguments arguments)
  32. {
  33. AK::set_rich_debug_enabled(true);
  34. StringView serenity_resource_root;
  35. Vector<ByteString> certificates;
  36. StringView mach_server_name;
  37. Core::ArgsParser args_parser;
  38. args_parser.add_option(certificates, "Path to a certificate file", "certificate", 'C', "certificate");
  39. args_parser.add_option(serenity_resource_root, "Absolute path to directory for serenity resources", "serenity-resource-root", 'r', "serenity-resource-root");
  40. args_parser.add_option(mach_server_name, "Mach server name", "mach-server-name", 0, "mach_server_name");
  41. args_parser.parse(arguments);
  42. // Ensure the certificates are read out here.
  43. if (certificates.is_empty())
  44. certificates.append(TRY(find_certificates(serenity_resource_root)));
  45. DefaultRootCACertificates::set_default_certificate_paths(certificates.span());
  46. [[maybe_unused]] auto& certs = DefaultRootCACertificates::the();
  47. Core::EventLoop event_loop;
  48. #if defined(AK_OS_MACOS)
  49. if (!mach_server_name.is_empty())
  50. Core::Platform::register_with_mach_server(mach_server_name);
  51. #endif
  52. RequestServer::GeminiProtocol::install();
  53. RequestServer::HttpProtocol::install();
  54. RequestServer::HttpsProtocol::install();
  55. auto client = TRY(IPC::take_over_accepted_client_from_system_server<RequestServer::ConnectionFromClient>());
  56. return event_loop.exec();
  57. }