|
| 1 | +#include "remote.hpp" |
| 2 | + |
| 3 | +#include <iostream> |
| 4 | + |
| 5 | +#ifdef Q_OS_UNIX |
| 6 | +#include <netdb.h> |
| 7 | +#include <unistd.h> |
| 8 | +#endif |
| 9 | + |
| 10 | +bool Remote::rewire_url(QUrl &url) { |
| 11 | + if (!init_done) { |
| 12 | + init(); |
| 13 | + } |
| 14 | + if (!ok) { |
| 15 | + return false; |
| 16 | + } |
| 17 | + |
| 18 | + url.setScheme("sftp"); |
| 19 | + url.setUserName(username); |
| 20 | + url.setHost(host); |
| 21 | + url.setPort(port); |
| 22 | + |
| 23 | + return true; |
| 24 | +} |
| 25 | + |
| 26 | +void Remote::init() { |
| 27 | + init_done = true; |
| 28 | + |
| 29 | + username = get_username(); |
| 30 | + if (username.isEmpty()) { |
| 31 | + std::cerr << "Could not read username" << std::endl; |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + host = get_local_domain(); |
| 36 | + if (host.isEmpty()) { |
| 37 | + std::cerr << "Could not read host" << std::endl; |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + // not a big deal if this fails, usually it still works without an explicit port |
| 42 | + port = get_port(); |
| 43 | + |
| 44 | + ok = true; |
| 45 | +} |
| 46 | + |
| 47 | +QString Remote::get_local_domain() { |
| 48 | +#ifndef Q_OS_UNIX |
| 49 | + return {}; |
| 50 | +#else |
| 51 | + char host[HOST_NAME_MAX]; |
| 52 | + if (gethostname(host, HOST_NAME_MAX)) { |
| 53 | + std::cerr << "gethostname failed" << std::endl; |
| 54 | + return {}; |
| 55 | + } |
| 56 | + |
| 57 | + // got the host, try to get the FQDN |
| 58 | + struct addrinfo hints, *res; |
| 59 | + memset(&hints, 0, sizeof(hints)); |
| 60 | + hints.ai_family = AF_UNSPEC; |
| 61 | + hints.ai_socktype = SOCK_STREAM; |
| 62 | + hints.ai_flags = AI_CANONNAME; |
| 63 | + |
| 64 | + int err = getaddrinfo(host, "http", &hints, &res); |
| 65 | + if (err) { |
| 66 | + std::cerr << "getaddrinfo failed: " << gai_strerror(err) << std::endl; |
| 67 | + return {}; |
| 68 | + } |
| 69 | + |
| 70 | + const auto fqdn = res->ai_canonname; |
| 71 | + std::string result; |
| 72 | + |
| 73 | + if (!strncmp(fqdn, host, HOST_NAME_MAX)) { |
| 74 | + std::cerr << "failed to retreive a proper FQDN for " << host << std::endl; |
| 75 | + } else { |
| 76 | + result = fqdn; |
| 77 | + |
| 78 | + // remove possible leading dot |
| 79 | + if (result.starts_with('.')) { |
| 80 | + result = result.substr(1); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + freeaddrinfo(res); |
| 85 | + return QString::fromStdString(result); |
| 86 | +#endif |
| 87 | +} |
| 88 | + |
| 89 | +QString Remote::get_username() { |
| 90 | +#ifdef Q_OS_UNIX |
| 91 | + return getlogin(); |
| 92 | +#else |
| 93 | + return {}; |
| 94 | +#endif |
| 95 | +} |
| 96 | + |
| 97 | +int Remote::get_port() { |
| 98 | + const auto env = std::getenv("SSH_CONNECTION"); |
| 99 | + if (env) { |
| 100 | + std::string ssh_connection {env}; |
| 101 | + const auto n = ssh_connection.rfind(' '); |
| 102 | + if (n != std::string::npos) { |
| 103 | + try { |
| 104 | + return std::stoi(ssh_connection.substr(n + 1)); |
| 105 | + } catch (std::invalid_argument const &) { |
| 106 | + std::cerr << "Failed to parse port number " << ssh_connection << std::endl; |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + return -1; |
| 111 | +} |
0 commit comments