From ded0a04277357fb950e4cb7930e61d381bbe0f97 Mon Sep 17 00:00:00 2001 From: idk Date: Thu, 28 Apr 2022 10:28:15 -0400 Subject: [PATCH] checkin logging --- .../snark/web/XI2PTorrentLocationFilter.java | 109 ++++++++++++++++-- 1 file changed, 97 insertions(+), 12 deletions(-) diff --git a/apps/i2psnark/java/src/org/klomp/snark/web/XI2PTorrentLocationFilter.java b/apps/i2psnark/java/src/org/klomp/snark/web/XI2PTorrentLocationFilter.java index 937503c01f..8931a5317c 100644 --- a/apps/i2psnark/java/src/org/klomp/snark/web/XI2PTorrentLocationFilter.java +++ b/apps/i2psnark/java/src/org/klomp/snark/web/XI2PTorrentLocationFilter.java @@ -1,8 +1,12 @@ package org.klomp.snark.web; +import java.io.FileWriter; import java.io.IOException; +import java.io.BufferedWriter; import java.io.File; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -15,6 +19,11 @@ import net.i2p.util.Log; import org.klomp.snark.Storage; +//import org.klomp.snark.Snark; +//import org.klomp.snark.SnarkManager; +import org.klomp.snark.I2PSnarkUtil; +import org.klomp.snark.MetaInfo; +import org.eclipse.jetty.server.Handler; /** * Adds a header, X-I2P-TorrentLocation, to requests for files which contains @@ -25,12 +34,26 @@ * * @since 0.9.51 */ -public class XI2PTorrentLocationFilter extends HandlerWrapper { +public class XI2PTorrentLocationFilter extends HandlerWrapper implements Handler { private static final String encodeUTF = StandardCharsets.UTF_8.toString(); private final Log _log = I2PAppContext.getGlobalContext().logManager().getLog(XI2PTorrentLocationFilter.class); + private final I2PSnarkUtil _util; + + public XI2PTorrentLocationFilter() { + // get the I2PAppContext + I2PAppContext ctx = I2PAppContext.getGlobalContext(); + _util = new I2PSnarkUtil(ctx); + } + + public XI2PTorrentLocationFilter(I2PSnarkUtil util) { + _util = util; + } private synchronized File shouldRecheck(final HttpServletRequest httpRequest) { File recheck = null; + if (_log.shouldLog(_log.DEBUG)) { + _log.debug("shouldRecheck: " + httpRequest.getRequestURI()); + } // get the request URI path only String path = httpRequest.getRequestURI(); if (path != null && !path.endsWith("/")) { @@ -40,13 +63,25 @@ private synchronized File shouldRecheck(final HttpServletRequest httpRequest) { File filepath = new File(docroot, path); // exist? if (filepath.exists()) { + if (_log.shouldLog(_log.DEBUG)) { + _log.debug("shouldRecheck: exists"); + } // is *not* it a torrent file? if (!filepath.getName().endsWith(".torrent")) { + if (_log.shouldLog(_log.DEBUG)) { + _log.debug("shouldRecheck: not a torrent file"); + } // is it a directory? if (!filepath.isDirectory()) { + if (_log.shouldLog(_log.DEBUG)) { + _log.debug("shouldRecheck: not a directory"); + } File precheck = filepath.getAbsoluteFile(); File torrentcheck = new File(precheck.getParentFile(), precheck.getName() + ".torrent"); if (torrentcheck.exists()) { + if (_log.shouldLog(_log.DEBUG)) { + _log.debug("shouldRecheck: torrent exists"); + } // get the last modified fime of precheck long lastModified = precheck.lastModified(); // get the last modified time of torrentcheck @@ -58,6 +93,9 @@ private synchronized File shouldRecheck(final HttpServletRequest httpRequest) { recheck = precheck; } } else { + if (_log.shouldLog(_log.DEBUG)) { + _log.debug("shouldRecheck: torrent does not exist"); + } // set the recheck to the precheck recheck = precheck; } @@ -72,18 +110,60 @@ private synchronized String headerContents(final HttpServletRequest httpRequest) File recheck = shouldRecheck(httpRequest); if (recheck != null) { // return null; + List openTrackers = _util.getOpenTrackers(); + List> announce_list = null; + if (openTrackers != null) { + if (_log.shouldLog(_log.DEBUG)) { + _log.debug("headerContents: has opentrackers"); + } + if (openTrackers.size() > 1) { + announce_list = new ArrayList>(); + // for (String tracker : openTrackers) { + for (int i = 1; i < openTrackers.size(); i++) { + String tracker = openTrackers.get(i); + List announce = new ArrayList(); + announce.add(tracker); + announce_list.add(announce); + } + } + String announce = openTrackers.get(0); + List url_list = new ArrayList(); + String url = httpRequest.getRequestURL().toString(); + url_list.add(url); + try { + Storage torrentData = new Storage(_util, + recheck, + announce, + announce_list, + null, + false, + url_list, + null, + null); + String torrentString = torrentData.toString(); + MetaInfo metaInfo = torrentData.getMetaInfo(); + String magnet = "magnet:?xt=urn:btih:" + metaInfo.getInfoHash() + + "&dn=" + metaInfo.getName() + + "&tr=" + announce + + "&ws=" + url; + // write torrentString to file recheck.torrent + File torrentFile = new File(recheck.getParentFile(), recheck.getName() + ".torrent"); + if (torrentFile.exists()) { + torrentFile.delete(); + } + BufferedWriter writer = new BufferedWriter(new FileWriter(torrentFile)); + writer.write(torrentString); + writer.close(); + torrentData.close(); + return magnet; - Storage torrentData;/* - * = new Storage(I2PSnarkUtil util, - * File baseFile, - * String announce, - * List> announce_list, - * String created_by, - * boolean privateTorrent, - * List url_list, - * String comment, - * StorageListener listener) - */ + } catch (IOException e) { + _log.error("Error creating torrent", e); + } + } + } + if (_log.shouldLog(_log.DEBUG)) { + _log.debug("headerContents: unable to create torrent"); } return ""; } @@ -93,6 +173,11 @@ public void handle(final String target, final Request request, final HttpServlet HttpServletResponse httpResponse) throws IOException, ServletException { + String header = headerContents(httpRequest); + if (header != null && !header.equals("")) { + httpResponse.setHeader("X-I2P-TorrentLocation", header); + } + _handler.handle(target, request, httpRequest, httpResponse); } }