Skip to content

Commit ded0a04

Browse files
committed
checkin logging
1 parent c7e477e commit ded0a04

File tree

1 file changed

+97
-12
lines changed

1 file changed

+97
-12
lines changed

apps/i2psnark/java/src/org/klomp/snark/web/XI2PTorrentLocationFilter.java

+97-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package org.klomp.snark.web;
22

3+
import java.io.FileWriter;
34
import java.io.IOException;
5+
import java.io.BufferedWriter;
46
import java.io.File;
57
import java.nio.charset.StandardCharsets;
8+
import java.util.ArrayList;
9+
import java.util.List;
610

711
import javax.servlet.ServletException;
812
import javax.servlet.http.HttpServletRequest;
@@ -15,6 +19,11 @@
1519
import net.i2p.util.Log;
1620

1721
import org.klomp.snark.Storage;
22+
//import org.klomp.snark.Snark;
23+
//import org.klomp.snark.SnarkManager;
24+
import org.klomp.snark.I2PSnarkUtil;
25+
import org.klomp.snark.MetaInfo;
26+
import org.eclipse.jetty.server.Handler;
1827

1928
/**
2029
* Adds a header, X-I2P-TorrentLocation, to requests for files which contains
@@ -25,12 +34,26 @@
2534
*
2635
* @since 0.9.51
2736
*/
28-
public class XI2PTorrentLocationFilter extends HandlerWrapper {
37+
public class XI2PTorrentLocationFilter extends HandlerWrapper implements Handler {
2938
private static final String encodeUTF = StandardCharsets.UTF_8.toString();
3039
private final Log _log = I2PAppContext.getGlobalContext().logManager().getLog(XI2PTorrentLocationFilter.class);
40+
private final I2PSnarkUtil _util;
41+
42+
public XI2PTorrentLocationFilter() {
43+
// get the I2PAppContext
44+
I2PAppContext ctx = I2PAppContext.getGlobalContext();
45+
_util = new I2PSnarkUtil(ctx);
46+
}
47+
48+
public XI2PTorrentLocationFilter(I2PSnarkUtil util) {
49+
_util = util;
50+
}
3151

3252
private synchronized File shouldRecheck(final HttpServletRequest httpRequest) {
3353
File recheck = null;
54+
if (_log.shouldLog(_log.DEBUG)) {
55+
_log.debug("shouldRecheck: " + httpRequest.getRequestURI());
56+
}
3457
// get the request URI path only
3558
String path = httpRequest.getRequestURI();
3659
if (path != null && !path.endsWith("/")) {
@@ -40,13 +63,25 @@ private synchronized File shouldRecheck(final HttpServletRequest httpRequest) {
4063
File filepath = new File(docroot, path);
4164
// exist?
4265
if (filepath.exists()) {
66+
if (_log.shouldLog(_log.DEBUG)) {
67+
_log.debug("shouldRecheck: exists");
68+
}
4369
// is *not* it a torrent file?
4470
if (!filepath.getName().endsWith(".torrent")) {
71+
if (_log.shouldLog(_log.DEBUG)) {
72+
_log.debug("shouldRecheck: not a torrent file");
73+
}
4574
// is it a directory?
4675
if (!filepath.isDirectory()) {
76+
if (_log.shouldLog(_log.DEBUG)) {
77+
_log.debug("shouldRecheck: not a directory");
78+
}
4779
File precheck = filepath.getAbsoluteFile();
4880
File torrentcheck = new File(precheck.getParentFile(), precheck.getName() + ".torrent");
4981
if (torrentcheck.exists()) {
82+
if (_log.shouldLog(_log.DEBUG)) {
83+
_log.debug("shouldRecheck: torrent exists");
84+
}
5085
// get the last modified fime of precheck
5186
long lastModified = precheck.lastModified();
5287
// get the last modified time of torrentcheck
@@ -58,6 +93,9 @@ private synchronized File shouldRecheck(final HttpServletRequest httpRequest) {
5893
recheck = precheck;
5994
}
6095
} else {
96+
if (_log.shouldLog(_log.DEBUG)) {
97+
_log.debug("shouldRecheck: torrent does not exist");
98+
}
6199
// set the recheck to the precheck
62100
recheck = precheck;
63101
}
@@ -72,18 +110,60 @@ private synchronized String headerContents(final HttpServletRequest httpRequest)
72110
File recheck = shouldRecheck(httpRequest);
73111
if (recheck != null) {
74112
// return null;
113+
List<String> openTrackers = _util.getOpenTrackers();
114+
List<List<String>> announce_list = null;
115+
if (openTrackers != null) {
116+
if (_log.shouldLog(_log.DEBUG)) {
117+
_log.debug("headerContents: has opentrackers");
118+
}
119+
if (openTrackers.size() > 1) {
120+
announce_list = new ArrayList<List<String>>();
121+
// for (String tracker : openTrackers) {
122+
for (int i = 1; i < openTrackers.size(); i++) {
123+
String tracker = openTrackers.get(i);
124+
List<String> announce = new ArrayList<String>();
125+
announce.add(tracker);
126+
announce_list.add(announce);
127+
}
128+
}
129+
String announce = openTrackers.get(0);
130+
List<String> url_list = new ArrayList<String>();
131+
String url = httpRequest.getRequestURL().toString();
132+
url_list.add(url);
133+
try {
134+
Storage torrentData = new Storage(_util,
135+
recheck,
136+
announce,
137+
announce_list,
138+
null,
139+
false,
140+
url_list,
141+
null,
142+
null);
143+
String torrentString = torrentData.toString();
144+
MetaInfo metaInfo = torrentData.getMetaInfo();
145+
String magnet = "magnet:?xt=urn:btih:" + metaInfo.getInfoHash() +
146+
"&dn=" + metaInfo.getName() +
147+
"&tr=" + announce +
148+
"&ws=" + url;
149+
// write torrentString to file recheck.torrent
150+
File torrentFile = new File(recheck.getParentFile(), recheck.getName() + ".torrent");
151+
if (torrentFile.exists()) {
152+
torrentFile.delete();
153+
}
154+
BufferedWriter writer = new BufferedWriter(new FileWriter(torrentFile));
155+
writer.write(torrentString);
156+
writer.close();
157+
torrentData.close();
158+
return magnet;
75159

76-
Storage torrentData;/*
77-
* = new Storage(I2PSnarkUtil util,
78-
* File baseFile,
79-
* String announce,
80-
* List<List<String>> announce_list,
81-
* String created_by,
82-
* boolean privateTorrent,
83-
* List<String> url_list,
84-
* String comment,
85-
* StorageListener listener)
86-
*/
160+
} catch (IOException e) {
161+
_log.error("Error creating torrent", e);
162+
}
163+
}
164+
}
165+
if (_log.shouldLog(_log.DEBUG)) {
166+
_log.debug("headerContents: unable to create torrent");
87167
}
88168
return "";
89169
}
@@ -93,6 +173,11 @@ public void handle(final String target, final Request request, final HttpServlet
93173
HttpServletResponse httpResponse)
94174
throws IOException, ServletException {
95175

176+
String header = headerContents(httpRequest);
177+
if (header != null && !header.equals("")) {
178+
httpResponse.setHeader("X-I2P-TorrentLocation", header);
179+
}
180+
96181
_handler.handle(target, request, httpRequest, httpResponse);
97182
}
98183
}

0 commit comments

Comments
 (0)