-
Notifications
You must be signed in to change notification settings - Fork 8
/
ConnectionManager.java
184 lines (167 loc) · 7.51 KB
/
ConnectionManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* Java Bittorrent API as its name indicates is a JAVA API that implements the Bittorrent Protocol
* This project contains two packages:
* 1. jBittorrentAPI is the "client" part, i.e. it implements all classes needed to publish
* files, share them and download them.
* This package also contains example classes on how a developer could create new applications.
* 2. trackerBT is the "tracker" part, i.e. it implements a all classes needed to run
* a Bittorrent tracker that coordinates peers exchanges. *
*
* Copyright (C) 2007 Baptiste Dubuis, Artificial Intelligence Laboratory, EPFL
*
* This file is part of jbittorrentapi-v1.0.zip
*
* Java Bittorrent API is free software and a free user study set-up;
* you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Java Bittorrent API is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Java Bittorrent API; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @version 1.0
* @author Baptiste Dubuis
* To contact the author:
* email: baptiste.dubuis@gmail.com
*
* More information about Java Bittorrent API:
* http://sourceforge.net/projects/bitext/
*/
package jBittorrentAPI;
import java.net.*;
import java.io.*;
import java.util.*;
/**
* Provides methods for interaction with remote host, like downloading or uploading
* files using HTTP protocol.
* @todo Optimize publish method to be able to specify (for example in a xml file)
* the protocol to upload files to a given website
*/
public class ConnectionManager {
/**
* Download a file from a web site and save it according to the provided path
* @param host Name of the host the torrent is located on
* @param port Port of the host that provides the torrent file
* @param filename Path of the file on the host
* @param renameFile Path where the torrent will be saved
* @return boolean True if the file has been successfully downloaded, false otherwise
*/
public static boolean downloadFile(String host, int port, String filename,
String renameFile) {
try {
URL source = new URL("HTTP", host, port, filename);
//IOManager iom = new IOManager(renameFile);
InputStream is = source.openStream();
IOManager.saveFromURL(is, renameFile);
is.close();
return true;
} catch (MalformedURLException murle) {
System.err.println("URL not valid...");
System.exit(1);
} catch (Exception e) {
System.err.println(
"Unable to download torrent file, host unreachable...");
e.printStackTrace();
}
return false;
}
public static boolean publish(String torrentPath){
return publish(torrentPath, "", "", "", "", "", "", "");
}
public static boolean publish(String torrentPath, String trackerName,
String username, String pwd, String torrentRename,
String info, String comment, String catid) {
URL url;
URLConnection urlConn;
DataOutputStream printout;
DataInputStream input;
int tracker = 1;
if(trackerName.equalsIgnoreCase("")){
tracker = new Integer(IOManager.readUserInput(
"On which tracker do you want your " +
"torrent to be published?\r\n1. " +
"smartorrent.com\r\n\t2.Localhost\r\nYour choice: ")).
intValue();
}else if(trackerName.contains("smartorrent")){
tracker = 1;
}else
tracker = 2;
ClientHttpRequest c = null;
try {
switch (tracker) {
case 2:
c = new ClientHttpRequest(
trackerName);
c.setParameter("name", torrentRename);
c.setParameter("torrent", new File(torrentPath));
c.setParameter("info", info);
c.setParameter("comment", comment);
c.post();
break;
case 1:
default:
url = new URL("http://www.smartorrent.com/?page=login");
urlConn = url.openConnection();
Map<String, List<String>> headers = urlConn.getHeaderFields();
List<String> values = headers.get("Set-Cookie");
String cookieValue = null;
for (Iterator iter = values.iterator(); iter.hasNext(); ) {
String v = (((String) iter.next()).split(";"))[0];
if (cookieValue == null)
cookieValue = v;
else
cookieValue = cookieValue + ";" + v;
}
url = new URL("http://www.smartorrent.com/?page=login");
urlConn = url.openConnection();
urlConn.setDoInput(true);
urlConn.setDoOutput(true);
urlConn.setUseCaches(false);
urlConn.setRequestProperty
("Content-Type", "application/x-www-form-urlencoded");
urlConn.setRequestProperty("Referer",
"http://www.smartorrent.com/?page=upload");
urlConn.setRequestProperty("Cookie",
cookieValue);
printout = new DataOutputStream(urlConn.getOutputStream());
String data = URLEncoder.encode("loginreturn", "UTF-8") + "=" +
URLEncoder.encode("/?page=login", "UTF-8");
data += "&" + URLEncoder.encode("loginusername", "UTF-8") + "=" +
URLEncoder.encode(username, "UTF-8");
data += "&" + URLEncoder.encode("loginpassword", "UTF-8") + "=" +
URLEncoder.encode(pwd, "UTF-8");
printout.writeBytes(data);
printout.flush();
printout.close();
BufferedReader rd = new BufferedReader(new InputStreamReader(
urlConn.
getInputStream()));
while (rd.readLine() != null);
rd.close();
c = new ClientHttpRequest(
"http://www.smartorrent.com/?page=upload");
String[] cookie = cookieValue.split("=");
c.setCookie(cookie[0], cookie[1]);
c.postCookies();
c.setParameter("name", torrentRename);
c.setParameter("torrent", new File(torrentPath));
c.setParameter("catid", catid);
c.setParameter("info", info);
c.setParameter("comment", comment);
c.setParameter("submit", "Upload");
c.post();
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}