http.javascript config param

This commit is contained in:
orignal
2026-08-24 18:16:50 -04:00
parent 393da480da
commit 558c50ca24
2 changed files with 63 additions and 52 deletions
+62 -52
View File
@@ -1212,6 +1212,67 @@ namespace http {
}
}
static void ShowTorrentsPage (std::stringstream& s, std::map<std::string, std::string>& params)
{
bool js; i2p::config::GetOption("http.javascript", js);
if (!js)
{
s << "<h1 style='color:red'>Javascript is off</h1>\r\n";
return;
}
std::string tunnelname = params["tunnelname"];
auto findTunnelEndpoint = [](const std::string& tunnelname) -> std::pair<uint16_t, std::string>
{
LogPrint(eLogDebug,"HTTPServer: GetTorrentsRPC");
for (auto& [port, server] : i2p::client::context.GetTorrentsRPCServers())
{
if (!server)
{
LogPrint(eLogError,"HTTPServer: GetTorrentsRPCServers return nothing");
continue;
}
for (const auto& [rpcPath, weakTunnel] : server->GetTunnels())
{
if (auto tunnel = weakTunnel.lock())
{
LogPrint(eLogDebug, "HTTPServer: TUNNEL_OPENJS", tunnel->GetName());
if (tunnel->GetName() == tunnelname)
{
uint16_t rpcPort = server->GetAcceptor().local_endpoint().port();
return { rpcPort, rpcPath };
}
}
}
}
return { 0, "" };
};
auto [rpcPort, rpcPath] = findTunnelEndpoint(tunnelname);
if (rpcPort && rpcPath.length())
{
std::string tjs_file = i2p::fs::DataDirPath("webconsole/torrent-rpc.js");
std::string tclient_file = i2p::fs::DataDirPath("webconsole/torrent_client.html");
if (i2p::fs::Exists(tjs_file))
{
std::ifstream f(tjs_file, std::ifstream::binary);
std::stringstream tjsss;
tjsss << f.rdbuf();
s << "<script>" << tjsss.str() << "</script>\r\n";
s << "<script>window.tjs = new TorrentClient('127.0.0.1', " << rpcPort << ", '" << rpcPath << "');</script>\r\n";
}
else
s << "<h1 style='color:red'>Not found $datadir/webconsole/torrent-rpc.js</h1>\r\n";
if (i2p::fs::Exists(tclient_file))
{
std::ifstream f(tclient_file, std::ifstream::binary);
s << f.rdbuf() << "\r\n";
}
else
s << "<h1 style='color:red'>Not found $datadir/webconsole/torrent_client.html</h1>\r\n";
}
else
s << "<h1 style='color:red'>Not found rpc tunnel " << tunnelname << "</h1>\r\n";
}
HTTPConnection::HTTPConnection (std::string hostname, std::shared_ptr<boost::asio::ip::tcp::socket> socket):
m_Socket (socket), m_BufferLen (0), expected_host(hostname)
{
@@ -1402,58 +1463,7 @@ namespace http {
else if (page == HTTP_PAGE_I2P_TUNNELS)
ShowI2PTunnels (s);
else if (page == OPENJS_TORRENT_CLIENT_PAGE)
{
// todo: to an another method with return;
std::string tunnelname = params["tunnelname"];
auto findTunnelEndpoint = [](const std::string& tunnelname) -> std::pair<uint16_t, std::string> {
LogPrint(eLogDebug,"GetTorrentsRPC");
for (auto& [port, server] : i2p::client::context.GetTorrentsRPCServers())
{
if (!server) {
LogPrint(eLogError,"GetTorrentsRPCServers return nothing");
continue;
}
for (const auto& [rpcPath, weakTunnel] : server->GetTunnels())
{
if (auto tunnel = weakTunnel.lock())
{
LogPrint(eLogDebug, "TUNNEL_OPENJS", tunnel->GetName());
if (tunnel->GetName() == tunnelname)
{
uint16_t rpcPort = server->GetAcceptor().local_endpoint().port();
return std::pair<uint16_t, std::string>(rpcPort, rpcPath);
}
}
}
}
return std::pair<uint16_t, std::string>(0, "");
};
auto [rpcPort, rpcPath] = findTunnelEndpoint(tunnelname);
if (rpcPort && rpcPath.length())
{
std::string tjs_file = i2p::fs::DataDirPath("webconsole/torrent-rpc.js");
std::string tclient_file = i2p::fs::DataDirPath("webconsole/torrent_client.html");
if (i2p::fs::Exists(tjs_file)) {
std::ifstream f(tjs_file, std::ifstream::binary);
std::stringstream tjsss;
tjsss << f.rdbuf();
s << "<script>" << tjsss.str() << "</script>\r\n";
s << "<script>window.tjs = new TorrentClient('127.0.0.1', " << rpcPort << ", '" << rpcPath << "');</script>\r\n";
}
else {
s << "<h1 style='color:red'>Not found $datadir/webconsole/torrent-rpc.js</h1>\r\n";
}
if (i2p::fs::Exists(tclient_file)) {
std::ifstream f(tclient_file, std::ifstream::binary);
s << f.rdbuf() << "\r\n";
}
else {
s << "<h1 style='color:red'>Not found $datadir/webconsole/torrent_client.html</h1>\r\n";
}
} else {
s << "<h1 style='color:red'>Not found rpc tunnel " << tunnelname << "</h1>\r\n";
}
}
ShowTorrentsPage (s, params);
else if (page == HTTP_PAGE_LEASESETS)
ShowLeasesSets(s);
else {