[3102] | 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 eric@jeti.tk |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | package nu.fw.jeti.plugins.filetransfer.socks5; |
---|
| 24 | |
---|
| 25 | import java.io.*; |
---|
| 26 | import java.util.Iterator; |
---|
| 27 | |
---|
| 28 | import javax.swing.ProgressMonitorInputStream; |
---|
| 29 | |
---|
| 30 | import nu.fw.jeti.jabber.Backend; |
---|
| 31 | import nu.fw.jeti.jabber.elements.InfoQuery; |
---|
| 32 | import nu.fw.jeti.jabber.elements.XMPPError; |
---|
| 33 | import nu.fw.jeti.jabber.elements.XMPPErrorTag; |
---|
| 34 | import nu.fw.jeti.plugins.filetransfer.GetFileWindow; |
---|
| 35 | import nu.fw.jeti.plugins.filetransfer.Plugin; |
---|
| 36 | import nu.fw.jeti.plugins.filetransfer.StreamReceive; |
---|
| 37 | import nu.fw.jeti.plugins.filetransfer.socks5.jsocks.Socks5Proxy; |
---|
| 38 | import nu.fw.jeti.plugins.filetransfer.socks5.jsocks.SocksSocket; |
---|
| 39 | import nu.fw.jeti.util.I18N; |
---|
| 40 | import nu.fw.jeti.util.Popups; |
---|
| 41 | |
---|
| 42 | //31-okt-2004 |
---|
| 43 | |
---|
| 44 | public class ReceiveSocks5 extends Thread implements StreamReceive |
---|
| 45 | { |
---|
| 46 | static final int BUF_SIZE = 8192; |
---|
| 47 | private Backend backend; |
---|
| 48 | private InfoQuery iq; |
---|
| 49 | private GetFileWindow getFileWindow; |
---|
| 50 | private long bytes; |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | public ReceiveSocks5(Backend backend,InfoQuery iq,GetFileWindow window) |
---|
| 54 | { |
---|
| 55 | this.iq =iq; |
---|
| 56 | this.getFileWindow = window; |
---|
| 57 | this.backend = backend; |
---|
| 58 | setPriority(NORM_PRIORITY); |
---|
| 59 | start(); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | public void run() |
---|
| 63 | { |
---|
| 64 | Socks5Extension s5 =(Socks5Extension)iq.getIQExtension(); |
---|
| 65 | if(!s5.hasStreamHosts())sendError(); |
---|
| 66 | //s5.getSid(); |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | //check if enough space on hd |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | // TODO stringprep |
---|
| 74 | String digest =Plugin.sha(s5.getSid(),iq.getFrom(),backend.getMyJID()); |
---|
| 75 | |
---|
| 76 | |
---|
| 77 | SocksSocket s=null; |
---|
| 78 | if(s5.hasStreamHosts()) |
---|
| 79 | { |
---|
| 80 | for(Iterator i=s5.getStreamHosts();i.hasNext();) |
---|
| 81 | { |
---|
| 82 | |
---|
| 83 | StreamHost streamHost = (StreamHost)i.next(); |
---|
| 84 | |
---|
| 85 | |
---|
| 86 | try{ |
---|
| 87 | //Proxy p = new Socks5Proxy("192.168.10.2",5080); |
---|
| 88 | //TODO add timeout |
---|
| 89 | s = new SocksSocket(new Socks5Proxy(streamHost.getHost(),streamHost.getPort()),digest,0); |
---|
| 90 | // check if connection is open |
---|
| 91 | backend.send(new InfoQuery(iq.getFrom(),"result",iq.getID(), |
---|
| 92 | new Socks5Extension(streamHost.getJID(),null))); |
---|
| 93 | break; |
---|
| 94 | } |
---|
| 95 | catch (IOException e2) |
---|
| 96 | { |
---|
| 97 | //e2.printStackTrace(); |
---|
| 98 | System.out.println(e2.getMessage()); |
---|
| 99 | continue; |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | if(s==null) |
---|
| 105 | { |
---|
| 106 | sendError(); |
---|
| 107 | Popups.messagePopup(I18N.gettext("filetransfer.Problem_during_file_transfer,_transfer_aborted"),I18N.gettext("filetransfer.File_Transfer")); |
---|
| 108 | getFileWindow.stopDownloading(); |
---|
| 109 | return; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | InputStream in=null; |
---|
| 113 | OutputStream out=getFileWindow.getOutputStream(); |
---|
| 114 | in = s.getInputStream(); |
---|
| 115 | |
---|
| 116 | if(in!=null) |
---|
| 117 | { |
---|
| 118 | if(out!=null) |
---|
| 119 | { |
---|
| 120 | try{ |
---|
| 121 | byte[] buf = new byte[BUF_SIZE]; |
---|
| 122 | int n; |
---|
| 123 | while ((n = in.read(buf)) > 0) |
---|
| 124 | { |
---|
| 125 | out.write(buf, 0, n); |
---|
| 126 | out.flush(); |
---|
| 127 | bytes+=n; |
---|
| 128 | yield(); |
---|
| 129 | if(Thread.interrupted()) |
---|
| 130 | { |
---|
| 131 | out.close(); |
---|
| 132 | in.close(); |
---|
| 133 | return; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | } |
---|
| 137 | //download ok |
---|
| 138 | // backend.send(new InfoQuery(jid,"result",id,null)); |
---|
| 139 | }catch (IOException e2) //probaly out of disk space |
---|
| 140 | { |
---|
| 141 | Popups.errorPopup(e2.getMessage() + " while downloading " ,"File transfer"); |
---|
| 142 | getFileWindow.stopDownloading(); |
---|
| 143 | //download not ok |
---|
| 144 | // backend.send(new InfoQuery(jid,"error",id,null,"Not Acceptable",406)); |
---|
| 145 | }//interuppeted io exception for cancel |
---|
| 146 | finally |
---|
| 147 | { |
---|
| 148 | try |
---|
| 149 | { |
---|
| 150 | out.close(); |
---|
| 151 | in.close(); |
---|
| 152 | } catch (IOException e) |
---|
| 153 | { |
---|
| 154 | e.printStackTrace(); |
---|
| 155 | } |
---|
| 156 | getFileWindow.stopDownloading(); |
---|
| 157 | } |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | public long getBytes() |
---|
| 165 | { |
---|
| 166 | return bytes; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | public void cancel() |
---|
| 170 | { |
---|
| 171 | interrupt(); |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | private void sendError() |
---|
| 175 | { |
---|
| 176 | XMPPError error = new XMPPError("cancel",404); |
---|
| 177 | error.addError(new XMPPErrorTag("item-not-found")); |
---|
| 178 | backend.send(new InfoQuery(iq.getFrom(),iq.getID(),error)); |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | |
---|
| 182 | } |
---|
| 183 | /* |
---|
| 184 | * Overrides for emacs |
---|
| 185 | * Local variables: |
---|
| 186 | * tab-width: 4 |
---|
| 187 | * End: |
---|
| 188 | */ |
---|