main.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. ErrorOr<String> find_certificates(StringView serenity_resource_root)
  22. {
  23. auto cert_path = TRY(String::formatted("{}/res/ladybird/cacert.pem", serenity_resource_root));
  24. if (!FileSystem::exists(cert_path)) {
  25. auto app_dir = LexicalPath::dirname(TRY(Core::System::current_executable_path()).to_deprecated_string());
  26. cert_path = TRY(String::formatted("{}/cacert.pem", LexicalPath(app_dir).parent()));
  27. if (!FileSystem::exists(cert_path))
  28. return Error::from_string_view("Don't know how to load certs!"sv);
  29. }
  30. return cert_path;
  31. }
  32. ErrorOr<int> serenity_main(Main::Arguments arguments)
  33. {
  34. int fd_passing_socket { -1 };
  35. StringView serenity_resource_root;
  36. Core::ArgsParser args_parser;
  37. args_parser.add_option(fd_passing_socket, "File descriptor of the fd passing socket", "fd-passing-socket", 'c', "fd-passing-socket");
  38. args_parser.add_option(serenity_resource_root, "Absolute path to directory for serenity resources", "serenity-resource-root", 'r', "serenity-resource-root");
  39. args_parser.parse(arguments);
  40. // Ensure the certificates are read out here.
  41. DefaultRootCACertificates::set_default_certificate_path(TRY(find_certificates(serenity_resource_root)));
  42. [[maybe_unused]] auto& certs = DefaultRootCACertificates::the();
  43. Core::EventLoop event_loop;
  44. [[maybe_unused]] auto gemini = make<RequestServer::GeminiProtocol>();
  45. [[maybe_unused]] auto http = make<RequestServer::HttpProtocol>();
  46. [[maybe_unused]] auto https = make<RequestServer::HttpsProtocol>();
  47. auto client = TRY(IPC::take_over_accepted_client_from_system_server<RequestServer::ConnectionFromClient>());
  48. client->set_fd_passing_socket(TRY(Core::LocalSocket::adopt_fd(fd_passing_socket)));
  49. auto result = event_loop.exec();
  50. // FIXME: We exit instead of returning, so that protocol destructors don't get called.
  51. // The Protocol base class should probably do proper de-registration instead of
  52. // just VERIFY_NOT_REACHED().
  53. exit(result);
  54. }