📄 socks5send.java
字号:
/** * $RCSfile$ * $Revision: 2495 $ * $Date: 2005-05-30 10:14:25 -0500 (Mon, 30 May 2005) $ * * Copyright 2003-2004 Jive Software. * * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.jivesoftware.smackx.filetransfer;import java.io.*;import org.jivesoftware.smack.*;import org.jivesoftware.smack.filter.*;import org.jivesoftware.smack.packet.*;import org.jivesoftware.smackx.packet.*;import org.jivesoftware.smackx.filetransfer.jsocks.*;import java.util.*;import java.net.*;/** * Allows for sending data via Socks5 Proxy * <br> * See <a href="http://www.jabber.org/jeps/jep-0065.html">JEP-0065</a> * * @author synic */public class Socks5Send extends FileTransfer { private FileSend send; private XMPPConnection connection; private InputStream stream; long totalSize = 0; private ArrayList progress = new ArrayList(); private boolean ft = false; private boolean cancelled = false; /** * Creates a new instance for the purpose of sending a file. * Used internally by FileTransferManager *@param send The FileSend object that is initiating this transfer *@param stream The InputStream to read from */ protected Socks5Send(FileSend send, InputStream stream) { this.connection = send.getManager().getConnection(); this.send = send; this.stream = stream; this.totalSize = send.getSize(); ft = true; } /** * Used to create a standalone Socks5 stream *@param connection The connection to use when sending the stream *@param stream The stream to send */ public Socks5Send(XMPPConnection connection, InputStream stream) { this.connection = connection; this.stream = stream; } /** * Cancels the transfer */ public void cancel() { cancelled = true; } /** * Sets the FileProgressListeners interested in the progress of this stream *@param list The list of listeners */ void setProgressListeners(ArrayList list) { this.progress = list; } /** * Sends the stream *@param to The JID (full jid with resource) of the user you want to send to *@throws IOException if an error reading the input stream occurs *@throws XMPPException if an error sending the stream occurs */ public void send(String to) throws IOException, XMPPException { StreamHost host = new StreamHost(send.getSI().getSid()); ArrayList hosts = send.getManager().getStreamHosts(); if(hosts.size() == 0) { throw new XMPPException("You did not specify any stream hosts."); } for(int i = 0; i < hosts.size(); i++) { String h = (String)hosts.get(i); String items[] = h.split(":"); String jid = items[0]; int port = 0; if(items.length == 2) { try { port = Integer.parseInt(items[1]); } catch(NumberFormatException nfe) {} } try { InetAddress address = InetAddress.getByName(jid); String a = address.getHostAddress(); host.addHost(new StreamHost.Host(a, jid, port)); } catch(java.net.UnknownHostException ex) { // couldn't find the host } } PacketCollector collector = connection.createPacketCollector( new PacketIDFilter(host.getPacketID())); host.setTo(to); connection.sendPacket(host); int timeout = SmackConfiguration.getPacketReplyTimeout(); if (ft) { timeout = send.getManager().getTimeout(); } IQ result = (IQ) collector.nextResult(timeout); collector.cancel(); if(result == null || !(result instanceof StreamHostUsed) || result.getType() == IQ.Type.ERROR) { if(result != null && result.getError() != null) { throw new XMPPException(result.getError()); } throw new XMPPException("Connection could not be established."); } String jid = ((StreamHostUsed)result).getJid(); StreamHost.Host h = host.getHostFor(jid); if(h == null) { throw new XMPPException("Could not find host for " + jid); } String digest = FileTransferManager.getDigest(host.getSid() + connection.getUser() + host.getTo()); SocksSocket s = new SocksSocket(new Socks5Proxy(h.getHost(), h.getPort()), digest, 0); StreamHostActivate activate = new StreamHostActivate(host.getSid(), host.getTo()); activate.setFrom(connection.getUser()); activate.setTo(h.getJid()); connection.sendPacket(activate); collector = connection.createPacketCollector(new PacketIDFilter(activate.getPacketID())); result = (IQ) collector.nextResult(timeout); collector.cancel(); if(result == null || result.getType() == IQ.Type.ERROR) { if(result != null && result.getError() != null) { throw new XMPPException(result.getError()); } throw new XMPPException("No response from server."); } OutputStream out = s.getOutputStream(); byte buf[] = new byte[4096]; int size = 0; boolean done = false; int total = 0; while(!done) { size = stream.read(buf); if(cancelled) { if(ft) { FileTransferManager.update(FileTransferManager.Event.CANCELLED, progress, (float) total / (float) totalSize, total); } out.close(); stream.close(); } if(ft) { FileTransferManager.update(FileTransferManager.Event.TRANSFERRING, progress, (float) total / (float) totalSize, total); } if(size != -1) { total += size; out.write(buf, 0, size); out.flush(); } else done = true; } if(ft) { FileTransferManager.update(FileTransferManager.Event.DONE, progress, 1, total); } stream.close(); out.close(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -