1 | /* |
---|
2 | * Jeti, a Java Jabber client, Copyright (C) 2004 E.S. de Boer |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation; either version 2 of the License, or |
---|
7 | * (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program; if not, write to the Free Software |
---|
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | * |
---|
18 | * For questions, comments etc, |
---|
19 | * use the website at http://jeti.jabberstudio.org |
---|
20 | * or mail me at jeti@jabber.org |
---|
21 | */ |
---|
22 | package nu.fw.jeti.plugins.filetransfer.socks5; |
---|
23 | |
---|
24 | import java.io.*; |
---|
25 | import java.net.*; |
---|
26 | import java.util.Iterator; |
---|
27 | import java.util.LinkedList; |
---|
28 | import java.util.List; |
---|
29 | |
---|
30 | import nu.fw.jeti.events.IQResultListener; |
---|
31 | import nu.fw.jeti.events.StatusChangeListener; |
---|
32 | import nu.fw.jeti.jabber.Backend; |
---|
33 | import nu.fw.jeti.jabber.JID; |
---|
34 | import nu.fw.jeti.jabber.elements.IQExtension; |
---|
35 | import nu.fw.jeti.jabber.elements.InfoQuery; |
---|
36 | import nu.fw.jeti.plugins.filetransfer.Plugin; |
---|
37 | import nu.fw.jeti.plugins.filetransfer.SendFileProgress; |
---|
38 | import nu.fw.jeti.plugins.filetransfer.StreamSend; |
---|
39 | import nu.fw.jeti.plugins.filetransfer.socks5.StreamHost; |
---|
40 | import nu.fw.jeti.plugins.filetransfer.socks5.jsocks.ProxyServer; |
---|
41 | import nu.fw.jeti.plugins.filetransfer.socks5.jsocks.ServerAuthenticatorNone; |
---|
42 | import nu.fw.jeti.plugins.filetransfer.socks5.jsocks.Socks5Proxy; |
---|
43 | import nu.fw.jeti.plugins.filetransfer.socks5.jsocks.SocksSocket; |
---|
44 | import nu.fw.jeti.util.I18N; |
---|
45 | import nu.fw.jeti.util.Popups; |
---|
46 | import nu.fw.jeti.util.Preferences; |
---|
47 | |
---|
48 | |
---|
49 | |
---|
50 | //31-okt-2004 |
---|
51 | public class Socks5Send implements StreamSend |
---|
52 | { |
---|
53 | private Backend backend; |
---|
54 | private String sid; |
---|
55 | private static List proxies = new LinkedList(); |
---|
56 | private List streamHosts; |
---|
57 | private File file; |
---|
58 | private long bytes; |
---|
59 | private SendFileProgress window; |
---|
60 | private ProxyServer proxyServer; |
---|
61 | private Thread proxyServerThread; |
---|
62 | private Thread sendThread; |
---|
63 | private int port = 7777; |
---|
64 | |
---|
65 | public Socks5Send(File file,final Backend backend,SendFileProgress f,JID jid,String id) |
---|
66 | { |
---|
67 | window = f; |
---|
68 | sid = id; |
---|
69 | streamHosts = new LinkedList(); |
---|
70 | streamHosts.addAll(proxies); |
---|
71 | this.backend = backend; |
---|
72 | this.file = file; |
---|
73 | |
---|
74 | /* |
---|
75 | * Esta alteração visa recupera a inteface de rede local, usada para transferir arquivo. |
---|
76 | * Previne erro no caso do cliente ter múltiplas interface de rede. |
---|
77 | * |
---|
78 | * emmanuel.ferro@serpro.gov.br em 08/10/2010 |
---|
79 | * |
---|
80 | */ |
---|
81 | |
---|
82 | String ip = backend.getLocalIP().getHostAddress(); |
---|
83 | System.out.println("getLocalIP = " + ip); |
---|
84 | streamHosts.add( new StreamHost( backend.getMyJID(), ip, 7777, null)); |
---|
85 | startSocksServer(file, backend, jid); |
---|
86 | |
---|
87 | if( Preferences.getBoolean( "filetransfer", "useLocalIP", true ) ) |
---|
88 | { |
---|
89 | ip = Plugin.getIP(); |
---|
90 | |
---|
91 | if ( ip != null ) |
---|
92 | { |
---|
93 | streamHosts.add(new StreamHost(backend.getMyJID(),ip, Preferences.getInteger("filetransfer","port",7777),null)); |
---|
94 | startSocksServer(file, backend, jid); |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | backend.send(new InfoQuery(jid,"set",backend.getIdentifier(),new Socks5Extension(sid,streamHosts)), |
---|
99 | new IQResultListener() |
---|
100 | { |
---|
101 | public void iqResult(InfoQuery iq) |
---|
102 | { |
---|
103 | if(iq.getType().equals("result")) |
---|
104 | { |
---|
105 | IQExtension e = iq.getIQExtension(); |
---|
106 | if(e instanceof Socks5Extension) |
---|
107 | { |
---|
108 | JID streamHostJID = ((Socks5Extension)e).getStreamHostUsed(); |
---|
109 | if(streamHostJID!=null) |
---|
110 | { |
---|
111 | if(!streamHostJID.equals(backend.getMyJID())) |
---|
112 | { |
---|
113 | if(proxyServerThread!=null)proxyServerThread.interrupt(); |
---|
114 | doProxy(streamHostJID,iq.getFrom()); |
---|
115 | } |
---|
116 | } |
---|
117 | } |
---|
118 | } |
---|
119 | else |
---|
120 | { //TODO add refuse error |
---|
121 | Popups.messagePopup(I18N.gettext("filetransfer.Problem_during_file_transfer,_transfer_aborted"),I18N.gettext("filetransfer.File_Transfer")); |
---|
122 | } |
---|
123 | } |
---|
124 | },0); |
---|
125 | |
---|
126 | } |
---|
127 | |
---|
128 | public static void initProxies(final Backend backend) |
---|
129 | { |
---|
130 | backend.addListener(StatusChangeListener.class,new StatusChangeListener() |
---|
131 | { |
---|
132 | public void connectionChanged(boolean online) |
---|
133 | { |
---|
134 | String proxy = Preferences.getString("filetransfer", "proxy", null); |
---|
135 | |
---|
136 | if( proxy != null ) |
---|
137 | getProxyInfo(backend,new JID(proxy)); |
---|
138 | |
---|
139 | //getProxyInfo(backend,new JID("proxy.jabber.org")); |
---|
140 | //getProxyInfo(backend,new JID("proxy65.jabber.ccc.de")); |
---|
141 | //getProxyInfo(backend,new JID("proxy65.jabber.autocom.pl")); |
---|
142 | //getProxyInfo(backend,new JID("proxy.netlab.cz")); |
---|
143 | //getProxyInfo(backend,new JID("proxy.jabber.cd.chalmers.se")); |
---|
144 | //getProxyInfo(backend,new JID("proxy65.jaim.at")); |
---|
145 | } |
---|
146 | |
---|
147 | public void ownPresenceChanged(int show, String status) {} |
---|
148 | |
---|
149 | public void exit(){} |
---|
150 | }); |
---|
151 | } |
---|
152 | |
---|
153 | public static boolean isProxyAvailable() |
---|
154 | { |
---|
155 | System.out.println(proxies); |
---|
156 | System.out.println(!proxies.isEmpty()); |
---|
157 | return !proxies.isEmpty(); |
---|
158 | } |
---|
159 | |
---|
160 | private static void getProxyInfo(final Backend backend,JID jid) |
---|
161 | { |
---|
162 | backend.send(new InfoQuery(jid,"get",new Socks5Extension((JID)null,null)), new IQResultListener() |
---|
163 | { |
---|
164 | public void iqResult(InfoQuery iq) |
---|
165 | { |
---|
166 | Socks5Extension s = (Socks5Extension)iq.getIQExtension(); |
---|
167 | if(s!=null && s.hasStreamHosts()) |
---|
168 | { |
---|
169 | for(Iterator i= s.getStreamHosts();i.hasNext();) |
---|
170 | { |
---|
171 | StreamHost h = (StreamHost)i.next(); |
---|
172 | proxies.add(h); |
---|
173 | } |
---|
174 | } |
---|
175 | } |
---|
176 | }, 100); |
---|
177 | } |
---|
178 | |
---|
179 | private void startSocksServer(final File file,final Backend backend, final JID jid) |
---|
180 | { |
---|
181 | proxyServerThread = new Thread(new Runnable() |
---|
182 | { |
---|
183 | public void run() |
---|
184 | { |
---|
185 | // set socks5 server |
---|
186 | String digest =Plugin.sha(sid,backend.getMyJID(),jid); |
---|
187 | ServerSocket ss=null; |
---|
188 | try{ |
---|
189 | ss = new ServerSocket(port); |
---|
190 | // while(true){ |
---|
191 | Socket s = ss.accept(); |
---|
192 | System.out.println("Accepted from:"+s.getInetAddress().getHostName()+":" |
---|
193 | +s.getPort()); |
---|
194 | proxyServer = new ProxyServer(new ServerAuthenticatorNone(),s,file,digest,Socks5Send.this); |
---|
195 | //} |
---|
196 | }catch(IOException ioe){ |
---|
197 | ioe.printStackTrace(); |
---|
198 | } |
---|
199 | finally |
---|
200 | { |
---|
201 | try { |
---|
202 | if(ss!=null)ss.close(); |
---|
203 | } catch (IOException e) { |
---|
204 | // TODO Auto-generated catch block |
---|
205 | e.printStackTrace(); |
---|
206 | } |
---|
207 | } |
---|
208 | } |
---|
209 | }); |
---|
210 | proxyServerThread.setPriority(Thread.NORM_PRIORITY); |
---|
211 | proxyServerThread.start(); |
---|
212 | } |
---|
213 | |
---|
214 | private void doProxy(final JID streamHostID,final JID targetJID) |
---|
215 | { |
---|
216 | new Thread(new Runnable() |
---|
217 | { |
---|
218 | public void run() |
---|
219 | { |
---|
220 | StreamHost streamHost=null; |
---|
221 | for(Iterator i= streamHosts.iterator();i.hasNext();) |
---|
222 | { |
---|
223 | StreamHost s = (StreamHost)i.next(); |
---|
224 | if(streamHostID.equals(s.getJID())) |
---|
225 | { |
---|
226 | streamHost =s; |
---|
227 | break; |
---|
228 | } |
---|
229 | } |
---|
230 | //if(streamHost==null) sendError(); |
---|
231 | |
---|
232 | |
---|
233 | try{ |
---|
234 | //TODO stringprep |
---|
235 | |
---|
236 | String digest =Plugin.sha(sid,backend.getMyJID(),targetJID); |
---|
237 | |
---|
238 | |
---|
239 | final SocksSocket s = new SocksSocket(new Socks5Proxy(streamHost.getHost(),streamHost.getPort()),digest,0); |
---|
240 | // TODO get sid? |
---|
241 | final StreamHost chosenStreamHost = streamHost; |
---|
242 | backend.send(new InfoQuery(streamHostID,"set",backend.getIdentifier(), |
---|
243 | new Socks5Extension(sid,targetJID)),new IQResultListener() |
---|
244 | { |
---|
245 | public void iqResult(InfoQuery iq) |
---|
246 | { |
---|
247 | if(iq.getType().equals("result"))sendFile(s); |
---|
248 | else if(iq.getType().equals("error")) |
---|
249 | {//try different streamhost and remove faulty one |
---|
250 | proxies.remove(chosenStreamHost); |
---|
251 | streamHosts.remove(chosenStreamHost); |
---|
252 | Popups.errorPopup("file could not be send, try again","File transfer"); |
---|
253 | cancel(); |
---|
254 | return; |
---|
255 | } |
---|
256 | } |
---|
257 | },0); |
---|
258 | } |
---|
259 | catch (IOException e2) |
---|
260 | { |
---|
261 | e2.printStackTrace(); |
---|
262 | proxies.remove(streamHost); |
---|
263 | streamHosts.remove(streamHost); |
---|
264 | //sendError(); |
---|
265 | Popups.errorPopup("file could not be downloaded","File transfer"); |
---|
266 | cancel(); |
---|
267 | return; |
---|
268 | } |
---|
269 | } |
---|
270 | }).start(); |
---|
271 | } |
---|
272 | |
---|
273 | private void sendFile(final SocksSocket s) |
---|
274 | { |
---|
275 | sendThread = new Thread(new Runnable() |
---|
276 | { |
---|
277 | public void run() |
---|
278 | { |
---|
279 | try |
---|
280 | { |
---|
281 | InputStream is = new FileInputStream(file.getAbsolutePath()); |
---|
282 | OutputStream out = s.getOutputStream(); |
---|
283 | //OutputStream out = (OutputStream) ps; |
---|
284 | byte[] buf = new byte[4096]; |
---|
285 | try |
---|
286 | { |
---|
287 | int n; |
---|
288 | while ((n = is.read(buf)) > 0) |
---|
289 | { |
---|
290 | out.write(buf, 0, n); |
---|
291 | out.flush(); |
---|
292 | bytes+=n; |
---|
293 | if (Thread.interrupted()) |
---|
294 | { |
---|
295 | is.close(); |
---|
296 | s.close(); |
---|
297 | return; |
---|
298 | } |
---|
299 | //if(ps.checkError()) throw new IOException("I/O Error"); |
---|
300 | } |
---|
301 | } finally |
---|
302 | { |
---|
303 | is.close(); |
---|
304 | s.close(); |
---|
305 | } |
---|
306 | window.done(); |
---|
307 | } catch (IOException e) |
---|
308 | { |
---|
309 | e.printStackTrace(); |
---|
310 | } |
---|
311 | } |
---|
312 | }); |
---|
313 | sendThread.setPriority(Thread.NORM_PRIORITY); |
---|
314 | sendThread.start(); |
---|
315 | } |
---|
316 | |
---|
317 | public void finished() |
---|
318 | { |
---|
319 | window.stop(); |
---|
320 | } |
---|
321 | |
---|
322 | public void addBytes(int bytes) |
---|
323 | { |
---|
324 | this.bytes+=bytes; |
---|
325 | } |
---|
326 | |
---|
327 | public long getBytes() |
---|
328 | { |
---|
329 | return bytes; |
---|
330 | } |
---|
331 | |
---|
332 | public void cancel() |
---|
333 | { |
---|
334 | if(proxyServer!=null)proxyServer.stop(); |
---|
335 | if(sendThread!=null)sendThread.interrupt(); |
---|
336 | } |
---|
337 | } |
---|
338 | |
---|
339 | /* |
---|
340 | * Overrides for emacs |
---|
341 | * Local variables: |
---|
342 | * tab-width: 4 |
---|
343 | * End: |
---|
344 | */ |
---|