main.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright (c) 2021, Dex♪ <dexes.ttp@gmail.com>
  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 <LibCore/ArgsParser.h>
  9. #include <LibCore/EventLoop.h>
  10. #include <LibCore/LocalServer.h>
  11. #include <LibCore/System.h>
  12. #include <LibFileSystem/FileSystem.h>
  13. #include <LibIPC/SingleServer.h>
  14. #include <LibMain/Main.h>
  15. #include <LibTLS/Certificate.h>
  16. #include <WebSocket/ConnectionFromClient.h>
  17. // FIXME: Share b/w RequestServer and WebSocket
  18. ErrorOr<ByteString> find_certificates(StringView serenity_resource_root)
  19. {
  20. auto cert_path = ByteString::formatted("{}/res/ladybird/cacert.pem", serenity_resource_root);
  21. if (!FileSystem::exists(cert_path)) {
  22. auto app_dir = LexicalPath::dirname(TRY(Core::System::current_executable_path()));
  23. cert_path = ByteString::formatted("{}/cacert.pem", LexicalPath(app_dir).parent());
  24. if (!FileSystem::exists(cert_path))
  25. return Error::from_string_view("Don't know how to load certs!"sv);
  26. }
  27. return cert_path;
  28. }
  29. ErrorOr<int> serenity_main(Main::Arguments arguments)
  30. {
  31. AK::set_rich_debug_enabled(true);
  32. int fd_passing_socket { -1 };
  33. StringView serenity_resource_root;
  34. Vector<ByteString> certificates;
  35. Core::ArgsParser args_parser;
  36. args_parser.add_option(fd_passing_socket, "File descriptor of the fd passing socket", "fd-passing-socket", 'c', "fd-passing-socket");
  37. args_parser.add_option(certificates, "Path to a certificate file", "certificate", 'C', "certificate");
  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. if (certificates.is_empty())
  42. certificates.append(TRY(find_certificates(serenity_resource_root)));
  43. DefaultRootCACertificates::set_default_certificate_paths(certificates.span());
  44. [[maybe_unused]] auto& certs = DefaultRootCACertificates::the();
  45. Core::EventLoop event_loop;
  46. auto client = TRY(IPC::take_over_accepted_client_from_system_server<WebSocket::ConnectionFromClient>());
  47. client->set_fd_passing_socket(TRY(Core::LocalSocket::adopt_fd(fd_passing_socket)));
  48. return event_loop.exec();
  49. }