1
1
package org .klomp .snark .web ;
2
2
3
+ import java .io .FileWriter ;
3
4
import java .io .IOException ;
5
+ import java .io .BufferedWriter ;
4
6
import java .io .File ;
5
7
import java .nio .charset .StandardCharsets ;
8
+ import java .util .ArrayList ;
9
+ import java .util .List ;
6
10
7
11
import javax .servlet .ServletException ;
8
12
import javax .servlet .http .HttpServletRequest ;
15
19
import net .i2p .util .Log ;
16
20
17
21
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 ;
18
27
19
28
/**
20
29
* Adds a header, X-I2P-TorrentLocation, to requests for files which contains
25
34
*
26
35
* @since 0.9.51
27
36
*/
28
- public class XI2PTorrentLocationFilter extends HandlerWrapper {
37
+ public class XI2PTorrentLocationFilter extends HandlerWrapper implements Handler {
29
38
private static final String encodeUTF = StandardCharsets .UTF_8 .toString ();
30
39
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
+ }
31
51
32
52
private synchronized File shouldRecheck (final HttpServletRequest httpRequest ) {
33
53
File recheck = null ;
54
+ if (_log .shouldLog (_log .DEBUG )) {
55
+ _log .debug ("shouldRecheck: " + httpRequest .getRequestURI ());
56
+ }
34
57
// get the request URI path only
35
58
String path = httpRequest .getRequestURI ();
36
59
if (path != null && !path .endsWith ("/" )) {
@@ -40,13 +63,25 @@ private synchronized File shouldRecheck(final HttpServletRequest httpRequest) {
40
63
File filepath = new File (docroot , path );
41
64
// exist?
42
65
if (filepath .exists ()) {
66
+ if (_log .shouldLog (_log .DEBUG )) {
67
+ _log .debug ("shouldRecheck: exists" );
68
+ }
43
69
// is *not* it a torrent file?
44
70
if (!filepath .getName ().endsWith (".torrent" )) {
71
+ if (_log .shouldLog (_log .DEBUG )) {
72
+ _log .debug ("shouldRecheck: not a torrent file" );
73
+ }
45
74
// is it a directory?
46
75
if (!filepath .isDirectory ()) {
76
+ if (_log .shouldLog (_log .DEBUG )) {
77
+ _log .debug ("shouldRecheck: not a directory" );
78
+ }
47
79
File precheck = filepath .getAbsoluteFile ();
48
80
File torrentcheck = new File (precheck .getParentFile (), precheck .getName () + ".torrent" );
49
81
if (torrentcheck .exists ()) {
82
+ if (_log .shouldLog (_log .DEBUG )) {
83
+ _log .debug ("shouldRecheck: torrent exists" );
84
+ }
50
85
// get the last modified fime of precheck
51
86
long lastModified = precheck .lastModified ();
52
87
// get the last modified time of torrentcheck
@@ -58,6 +93,9 @@ private synchronized File shouldRecheck(final HttpServletRequest httpRequest) {
58
93
recheck = precheck ;
59
94
}
60
95
} else {
96
+ if (_log .shouldLog (_log .DEBUG )) {
97
+ _log .debug ("shouldRecheck: torrent does not exist" );
98
+ }
61
99
// set the recheck to the precheck
62
100
recheck = precheck ;
63
101
}
@@ -72,18 +110,60 @@ private synchronized String headerContents(final HttpServletRequest httpRequest)
72
110
File recheck = shouldRecheck (httpRequest );
73
111
if (recheck != null ) {
74
112
// 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 ;
75
159
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" );
87
167
}
88
168
return "" ;
89
169
}
@@ -93,6 +173,11 @@ public void handle(final String target, final Request request, final HttpServlet
93
173
HttpServletResponse httpResponse )
94
174
throws IOException , ServletException {
95
175
176
+ String header = headerContents (httpRequest );
177
+ if (header != null && !header .equals ("" )) {
178
+ httpResponse .setHeader ("X-I2P-TorrentLocation" , header );
179
+ }
180
+
96
181
_handler .handle (target , request , httpRequest , httpResponse );
97
182
}
98
183
}
0 commit comments