gemini-client.cpp (5367B)
1 #include "../include/gemini-client.hpp" 2 3 #include <openssl/ssl.h> 4 #include <poll.h> 5 #include <sys/socket.h> 6 7 #include <chrono> 8 #include <cstddef> 9 #include <stdexcept> 10 #include <string> 11 12 #include "../include/errors.hpp" 13 #include "../include/shared.hpp" 14 #include "../include/site.hpp" 15 #include "../include/utils.hpp" 16 17 Site* GeminiClient::getNetworkedSite(Link link, std::string crtPath, 18 std::string keyPath) { 19 if (!isSendableIfGeminiUrl(link.getLinkDestination())) { 20 return nullptr; 21 } 22 23 std::string host = link.getLinkDestination().get_host(); 24 std::string req = link.getLinkDestination().to_string() + "\r\n"; 25 std::string conn = host + ":1965"; 26 27 if (link.getLinkDestination().get_port()) { 28 conn = 29 host + ":" + std::to_string(link.getLinkDestination().get_port()); 30 } 31 32 std::unique_ptr<SSL_CTX, decltype(&SSL_CTX_free)> ctx( 33 SSL_CTX_new(TLS_client_method()), SSL_CTX_free); 34 35 if (!ctx) { 36 return nullptr; 37 } 38 39 if (crtPath != "" && keyPath != "") { 40 if (SSL_CTX_use_certificate_file(ctx.get(), crtPath.c_str(), 41 SSL_FILETYPE_PEM) <= 0 || 42 SSL_CTX_use_PrivateKey_file(ctx.get(), keyPath.c_str(), 43 SSL_FILETYPE_PEM) <= 0 || 44 !SSL_CTX_check_private_key(ctx.get())) { 45 return nullptr; 46 } 47 } 48 49 std::unique_ptr<BIO, decltype(&BIO_free_all)> bio( 50 BIO_new_ssl_connect(ctx.get()), BIO_free_all); 51 52 if (!bio) { 53 return nullptr; 54 } 55 56 SSL* ssl; 57 BIO_get_ssl(bio.get(), &ssl); 58 SSL_set_tlsext_host_name(ssl, host.c_str()); 59 BIO_set_conn_hostname(bio.get(), conn.c_str()); 60 BIO_set_nbio(bio.get(), 1); 61 62 auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(5); 63 64 while (BIO_do_connect(bio.get()) <= 0) { 65 int fd = -1; 66 67 long ms = std::chrono::duration_cast<std::chrono::milliseconds>( 68 deadline - std::chrono::steady_clock::now()) 69 .count(); 70 pollfd p{}; 71 72 // yikes. 73 if (!BIO_should_retry(bio.get()) || BIO_get_fd(bio.get(), &fd) < 0 || 74 fd < 0 || ms <= 0 || 75 (p = {fd, short(BIO_should_read(bio.get()) ? POLLIN : POLLOUT), 0}, 76 poll(&p, 1, int(ms)) <= 0)) { 77 return nullptr; 78 } 79 } 80 81 // timeout, 5 seconds 82 int fd = -1; 83 if (BIO_get_fd(bio.get(), &fd) >= 0 && fd >= 0) { 84 BIO_socket_nbio(fd, 0); 85 timeval tv{5, 0}; 86 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof tv); 87 setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof tv); 88 } 89 90 BIO_write(bio.get(), req.data(), (int)req.size()); 91 92 std::string response; 93 char buf[4096]; 94 int n; 95 96 int sizeRemaining = RESPONSE_SIZE_LIMIT_MB * 1024 * 1024; 97 98 while ((n = BIO_read(bio.get(), buf, sizeof buf)) > 0) { 99 response.append(buf, n); 100 101 sizeRemaining -= n; 102 if (sizeRemaining <= 0) { 103 break; 104 } 105 } 106 107 bio.reset(); 108 109 std::size_t nl = response.find('\n'); 110 if (nl == std::string::npos) { 111 return nullptr; 112 } 113 114 std::size_t end = nl; 115 if (end > 0 && response[end - 1] == '\r') { 116 end -= 1; 117 } 118 119 std::string status = response.substr(0, end); 120 std::string body = response.substr(nl + 1); 121 122 return new Site(status, body); 123 } 124 125 Site* GeminiClient::fetchSite(Link link, std::string crtPath, 126 std::string keyPath) { 127 std::string destination = link.getLinkDestination().to_string(); 128 129 if (isPrefixed(destination, "gemini://")) { 130 try { 131 return getNetworkedSite(link, crtPath, keyPath); 132 } catch (...) { 133 auto* unreach = new Site{"41 server unreachable", ""}; 134 unreach->setUnreachable(); 135 return unreach; 136 } 137 } else if (isPrefixed( 138 destination, 139 "file://")) { // TODO: This seems wrong; it should probably 140 // be fullpath with that prefix. 141 142 // TODO: this is messy and perhaps not necessary 143 144 std::string rest = destination.substr(7); 145 std::string host; 146 std::string path; 147 148 std::size_t slash = rest.find('/'); 149 if (slash != std::string::npos) { 150 host = rest.substr(0, slash); 151 path = rest.substr(slash); 152 } else { 153 host = rest; 154 path = ""; 155 } 156 157 if (host.empty()) { 158 host = "localhost"; 159 } 160 161 std::string fileStr = ""; 162 163 std::string fsPath; 164 165 if (host == "localhost") { 166 fsPath = path; 167 } else { 168 throw std::invalid_argument( 169 "The requested file appears to exist on another system."); 170 } 171 172 try { 173 fileStr = readFileToString(fsPath); 174 } catch (FileReadError& e) { 175 return new Site{"51 file not found", ""}; 176 } 177 // TODO: how should I discern file types? 178 return new Site{"20 text/gemini", fileStr}; 179 } else if (isPrefixed(destination, "about://")) { 180 return new Site{"20 text/gemini", getNewTab()}; 181 } else { 182 throw std::runtime_error( 183 "An invalid argument was passed to fetchsite, " + destination); 184 } 185 }