source: branches/2.2/jabberit_messenger/java_source/src/nu/fw/jeti/plugins/filetransfer/socks5/Socks5Send.java @ 3102

Revision 3102, 8.8 KB checked in by amuller, 14 years ago (diff)

Ticket #986 - Efetuado merge para o Branch 2.2( atualizacao do modulo)

  • Property svn:executable set to *
Line 
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 */
22package nu.fw.jeti.plugins.filetransfer.socks5;
23
24import java.io.*;
25import java.net.*;
26import java.net.ServerSocket;
27import java.net.Socket;
28import java.util.Iterator;
29import java.util.LinkedList;
30import java.util.List;
31
32import nu.fw.jeti.events.IQResultListener;
33import nu.fw.jeti.events.StatusChangeListener;
34import nu.fw.jeti.jabber.Backend;
35import nu.fw.jeti.jabber.JID;
36import nu.fw.jeti.jabber.elements.IQExtension;
37import nu.fw.jeti.jabber.elements.InfoQuery;
38import nu.fw.jeti.plugins.filetransfer.Plugin;
39import nu.fw.jeti.plugins.filetransfer.SendFileProgress;
40import nu.fw.jeti.plugins.filetransfer.StreamSend;
41import nu.fw.jeti.plugins.filetransfer.socks5.jsocks.ProxyServer;
42import nu.fw.jeti.plugins.filetransfer.socks5.jsocks.ServerAuthenticatorNone;
43import nu.fw.jeti.plugins.filetransfer.socks5.jsocks.Socks5Proxy;
44import nu.fw.jeti.plugins.filetransfer.socks5.jsocks.SocksSocket;
45import nu.fw.jeti.util.I18N;
46import nu.fw.jeti.util.Popups;
47import nu.fw.jeti.util.Preferences;
48
49
50
51//31-okt-2004
52public class Socks5Send implements StreamSend
53{
54        private Backend backend;
55        private String sid;
56        private static List proxies = new LinkedList();
57        private List streamHosts;
58        private File file;
59        private long bytes;
60        private SendFileProgress window;
61        private ProxyServer proxyServer;
62        private Thread proxyServerThread;
63        private Thread sendThread;
64        private int port = 7777;
65               
66        public Socks5Send(File file,final Backend backend,SendFileProgress f,JID jid,String id)
67        {
68                window = f;
69                sid = id;
70        streamHosts = new LinkedList();
71        streamHosts.addAll(proxies);
72        this.backend = backend;
73        this.file = file;
74       
75        if( Preferences.getBoolean("filetransfer", "useLocalIP", true) )
76        {
77                String ip = Plugin.getIP();
78
79                if ( ip != null )
80                {
81                        streamHosts.add(new StreamHost(backend.getMyJID(),ip, Preferences.getInteger("filetransfer","port",7777),null));
82                                startSocksServer(file, backend, jid);
83                }
84        }
85
86        backend.send(new InfoQuery(jid,"set",backend.getIdentifier(),new Socks5Extension(sid,streamHosts)),
87                new IQResultListener()
88                {
89                public void iqResult(InfoQuery iq)
90                        {
91                        if(iq.getType().equals("result"))
92                                {
93                                        IQExtension e = iq.getIQExtension();
94                                        if(e instanceof Socks5Extension)
95                                        {
96                                                JID streamHostJID = ((Socks5Extension)e).getStreamHostUsed();
97                                                if(streamHostJID!=null)
98                                                {
99                                                        if(!streamHostJID.equals(backend.getMyJID()))
100                                                        {
101                                                                if(proxyServerThread!=null)proxyServerThread.interrupt();
102                                                                doProxy(streamHostJID,iq.getFrom());
103                                                        }
104                                                }
105                                        }
106                                }
107                                else
108                                {  //TODO add refuse error
109                                        Popups.messagePopup(I18N.gettext("filetransfer.Problem_during_file_transfer,_transfer_aborted"),I18N.gettext("filetransfer.File_Transfer"));
110                                }
111                        }
112                },0);
113
114        }
115       
116        public static void initProxies(final Backend backend)
117        {
118                backend.addListener(StatusChangeListener.class,new StatusChangeListener()
119                {
120                        public void connectionChanged(boolean online)
121                        {
122                                String proxy = Preferences.getString("filetransfer", "proxy", null);
123                               
124                                if( proxy != null )
125                                        getProxyInfo(backend,new JID(proxy));
126
127                                //getProxyInfo(backend,new JID("proxy.jabber.org"));
128                                //getProxyInfo(backend,new JID("proxy65.jabber.ccc.de"));
129                                //getProxyInfo(backend,new JID("proxy65.jabber.autocom.pl"));
130                        //getProxyInfo(backend,new JID("proxy.netlab.cz"));
131                                //getProxyInfo(backend,new JID("proxy.jabber.cd.chalmers.se"));
132                                //getProxyInfo(backend,new JID("proxy65.jaim.at"));
133                        }
134
135                        public void ownPresenceChanged(int show, String status) {}
136
137                        public void exit(){}
138                });
139        }
140       
141        public static boolean isProxyAvailable()
142        {
143                System.out.println(proxies);
144                System.out.println(!proxies.isEmpty());
145                return !proxies.isEmpty();
146        }
147       
148        private static void getProxyInfo(final Backend backend,JID jid)
149        {
150                backend.send(new InfoQuery(jid,"get",new Socks5Extension((JID)null,null)), new IQResultListener()
151                {
152                        public void iqResult(InfoQuery iq)
153                        {
154                                Socks5Extension s = (Socks5Extension)iq.getIQExtension();
155                                if(s!=null && s.hasStreamHosts())
156                                {
157                                        for(Iterator i= s.getStreamHosts();i.hasNext();)
158                                        {
159                                                StreamHost h = (StreamHost)i.next();
160                                                proxies.add(h);
161                                        }
162                                }
163                        }
164                }, 100);
165        }
166       
167        private void startSocksServer(final File file,final Backend backend, final JID jid)
168        {
169                proxyServerThread = new Thread(new Runnable()
170                  {
171                        public void run()
172                        {
173//                      set socks5 server
174                                String digest =Plugin.sha(sid,backend.getMyJID(),jid);
175                                ServerSocket ss=null;                   
176                                try{
177                                         ss = new ServerSocket(port);
178                                // while(true){
179                                  Socket s = ss.accept();
180                                  System.out.println("Accepted from:"+s.getInetAddress().getHostName()+":"
181                                                      +s.getPort());
182                                  proxyServer = new ProxyServer(new ServerAuthenticatorNone(),s,file,digest,Socks5Send.this);
183                                //}
184                              }catch(IOException ioe){
185                                ioe.printStackTrace();
186                              }
187                              finally
188                                  {
189                                try {
190                                                if(ss!=null)ss.close();
191                                        } catch (IOException e) {
192                                                // TODO Auto-generated catch block
193                                                e.printStackTrace();
194                                        }
195                                  }
196                        }
197                  });
198                proxyServerThread.setPriority(Thread.NORM_PRIORITY);
199                proxyServerThread.start();
200        }
201
202        private void doProxy(final JID streamHostID,final JID targetJID)
203        {
204                new Thread(new Runnable()
205                {
206                        public void run()
207                        {
208                                StreamHost streamHost=null;
209                                for(Iterator i= streamHosts.iterator();i.hasNext();)
210                                {
211                                        StreamHost s = (StreamHost)i.next();
212                                        if(streamHostID.equals(s.getJID()))
213                                        {
214                                                streamHost =s;
215                                                break;
216                                        }
217                                }
218                                //if(streamHost==null) sendError();
219                               
220                                 
221                                try{
222                                        //TODO stringprep
223                                       
224                                        String digest =Plugin.sha(sid,backend.getMyJID(),targetJID);
225                                                                       
226                                       
227                                        final SocksSocket s = new SocksSocket(new Socks5Proxy(streamHost.getHost(),streamHost.getPort()),digest,0);
228                //                      TODO get sid?
229                                        final StreamHost chosenStreamHost = streamHost;
230                                        backend.send(new InfoQuery(streamHostID,"set",backend.getIdentifier(),
231                                        new Socks5Extension(sid,targetJID)),new IQResultListener()
232                                        {
233                                                public void iqResult(InfoQuery iq)
234                                                {
235                                                        if(iq.getType().equals("result"))sendFile(s);
236                                                        else if(iq.getType().equals("error"))
237                                                        {//try different streamhost and remove faulty one
238                                                                proxies.remove(chosenStreamHost);
239                                                                streamHosts.remove(chosenStreamHost);
240                                                                Popups.errorPopup("file could not be send, try again","File transfer");
241                                                                cancel();
242                                                                return;
243                                                        }
244                                                }
245                                        },0);
246                                }
247                                catch (IOException e2)
248                                {
249                                        e2.printStackTrace();
250                                        proxies.remove(streamHost);
251                                        streamHosts.remove(streamHost);
252                                        //sendError();
253                                        Popups.errorPopup("file could not be downloaded","File transfer");
254                                        cancel();
255                                        return;
256                                }
257                        }
258                }).start();
259        }
260       
261        private void sendFile(final SocksSocket s)
262        {
263                sendThread = new Thread(new Runnable()
264                {
265                        public void run()
266                        {
267                                try
268                                {
269                                        InputStream is = new FileInputStream(file.getAbsolutePath());
270                                        OutputStream out = s.getOutputStream();
271                                        //OutputStream out = (OutputStream) ps;
272                                        byte[] buf = new byte[4096];
273                                        try
274                                        {
275                                                int n;
276                                                while ((n = is.read(buf)) > 0)
277                                                {
278                                                        out.write(buf, 0, n);
279                                                        out.flush();
280                                                        bytes+=n;
281                                                        if (Thread.interrupted())
282                                                        {
283                                                                is.close();
284                                                                s.close();
285                                                                return;
286                                                        }
287                                                        //if(ps.checkError()) throw new IOException("I/O Error");
288                                                }
289                                        } finally
290                                        {
291                                                is.close();
292                                                s.close();
293                                        }
294                                        window.done();
295                                } catch (IOException e)
296                                {
297                                        e.printStackTrace();
298                                }
299                        }
300                });
301                sendThread.setPriority(Thread.NORM_PRIORITY);
302                sendThread.start();
303        }
304               
305        public void finished()
306        {
307                window.stop();
308        }
309
310        public void addBytes(int bytes)
311        {
312                this.bytes+=bytes;
313        }
314       
315        public long getBytes()
316        {
317                return bytes;
318        }
319       
320        public void cancel()
321        {
322                if(proxyServer!=null)proxyServer.stop();
323                if(sendThread!=null)sendThread.interrupt();
324        }
325}
326
327/*
328 * Overrides for emacs
329 * Local variables:
330 * tab-width: 4
331 * End:
332 */
Note: See TracBrowser for help on using the repository browser.