|
| 1 | +#include "core/app.h" |
| 2 | + |
| 3 | +#include "core/cli.h" |
| 4 | +#include "core/logging.h" |
| 5 | +#include "core/output.h" |
| 6 | +#include "core/ping_loop.h" |
| 7 | +#include "core/resolve.h" |
| 8 | +#include "core/scan_runner.h" |
| 9 | +#include "core/scan_utils.h" |
| 10 | +#include "net/icmp_scan.h" |
| 11 | +#include "platform/sandbox.h" |
| 12 | + |
| 13 | +#include <boost/asio.hpp> |
| 14 | +#include <boost/asio/awaitable.hpp> |
| 15 | + |
| 16 | +#include <iostream> |
| 17 | +#include <vector> |
| 18 | + |
| 19 | +namespace asio = boost::asio; |
| 20 | + |
| 21 | +using asio::awaitable; |
| 22 | +using asio::co_spawn; |
| 23 | +using asio::redirect_error; |
| 24 | +using asio::use_awaitable; |
| 25 | + |
| 26 | +namespace { |
| 27 | + |
| 28 | +awaitable<void> run_port_scans(const std::vector<std::string> &hosts, ScanOptions opts) { |
| 29 | + auto executor = co_await asio::this_coro::executor; |
| 30 | + asio::io_context &io = static_cast<asio::io_context &>(executor.context()); |
| 31 | + asio::ip::tcp::resolver resolver(io); |
| 32 | + |
| 33 | + for (const auto &host : hosts) { |
| 34 | + bool used_range = false; |
| 35 | + auto addresses = resolve_or_expand(host, resolver, opts, used_range); |
| 36 | + if (addresses.empty()) { |
| 37 | + if (used_range) { |
| 38 | + std::cerr << "No addresses after IP filter for host " << host << "\n"; |
| 39 | + } |
| 40 | + continue; |
| 41 | + } |
| 42 | + |
| 43 | + co_await run_scans( |
| 44 | + host, addresses, opts, |
| 45 | + [&](const ScanRecord &record) { |
| 46 | + if (!should_report(record.result, opts.open_only)) { |
| 47 | + return; |
| 48 | + } |
| 49 | + std::cout << record.host << " " |
| 50 | + << format_address(record.addr) << ":" |
| 51 | + << record.result.port << " -> " |
| 52 | + << record.result.state << " (" |
| 53 | + << record.result.detail << ")\n"; |
| 54 | + }); |
| 55 | + } |
| 56 | + co_return; |
| 57 | +} |
| 58 | + |
| 59 | +} // namespace |
| 60 | + |
| 61 | +int run_app(int argc, char **argv) { |
| 62 | + ScanOptions opts; |
| 63 | + std::vector<std::string> hosts; |
| 64 | + int exit_code = parse_cli(argc, argv, opts, hosts); |
| 65 | + if (exit_code != 0) { |
| 66 | + return exit_code; |
| 67 | + } |
| 68 | + |
| 69 | + if (opts.sandbox) { |
| 70 | + std::string message; |
| 71 | + SandboxStatus status = apply_sandbox(opts, hosts, message); |
| 72 | + if (!message.empty()) { |
| 73 | + std::cerr << message << "\n"; |
| 74 | + } |
| 75 | + if (status == SandboxStatus::Failed) { |
| 76 | + return 1; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + asio::io_context io; |
| 81 | + if (opts.icmp_ping && opts.ping_mode) { |
| 82 | + co_spawn( |
| 83 | + io, icmp_ping_loop(hosts, opts), |
| 84 | + [](std::exception_ptr eptr) { |
| 85 | + if (!eptr) { |
| 86 | + return; |
| 87 | + } |
| 88 | + try { |
| 89 | + std::rethrow_exception(eptr); |
| 90 | + } catch (const std::exception &e) { |
| 91 | + log_exception("ICMP ping loop error", e); |
| 92 | + } |
| 93 | + }); |
| 94 | + } else if (opts.icmp_ping) { |
| 95 | + co_spawn( |
| 96 | + io, icmp_scan_hosts(hosts, opts, nullptr, false), |
| 97 | + [](std::exception_ptr eptr) { |
| 98 | + if (!eptr) { |
| 99 | + return; |
| 100 | + } |
| 101 | + try { |
| 102 | + std::rethrow_exception(eptr); |
| 103 | + } catch (const std::exception &e) { |
| 104 | + log_exception("ICMP ping error", e); |
| 105 | + } |
| 106 | + }); |
| 107 | + } else if (opts.ping_mode) { |
| 108 | + co_spawn( |
| 109 | + io, ping_loop(hosts, opts), |
| 110 | + [](std::exception_ptr eptr) { |
| 111 | + if (!eptr) { |
| 112 | + return; |
| 113 | + } |
| 114 | + try { |
| 115 | + std::rethrow_exception(eptr); |
| 116 | + } catch (const std::exception &e) { |
| 117 | + log_exception("Ping loop error", e); |
| 118 | + } |
| 119 | + }); |
| 120 | + } else { |
| 121 | + co_spawn( |
| 122 | + io, run_port_scans(hosts, opts), |
| 123 | + [](std::exception_ptr eptr) { |
| 124 | + if (!eptr) { |
| 125 | + return; |
| 126 | + } |
| 127 | + try { |
| 128 | + std::rethrow_exception(eptr); |
| 129 | + } catch (const std::exception &e) { |
| 130 | + log_exception("Scan runner error", e); |
| 131 | + } |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + io.run(); |
| 136 | + return 0; |
| 137 | +} |
0 commit comments