📄 sendfile.java
字号:
package com.valhalla.jbother.jabber.smack;import javax.swing.*;import java.io.*;import java.net.InetSocketAddress;import java.net.ServerSocket;import java.nio.channels.ServerSocketChannel;import java.nio.channels.FileChannel;import java.nio.channels.ByteChannel;/** * Created by luke on Feb 8, 2005 10:45:11 AM *//** * this class implements methods for proper authentication and sending data to a certain JID */public class SendFile extends FileTransfer{ private static String fId = "$Id$"; private ServerSocketChannel fServerSocketChannel; public SendFile(StreamInitiation.FileDetails aFileDetails, String aHostname, int aPort, String aSid, String aJidFrom, String aJidTo) { super(aFileDetails,aHostname,aPort,aSid,aJidFrom,aJidTo); } /** * this method will block here until someone will connect to this serverChannel * I'm not sure if this works... */ public ByteChannel getByteChannel() throws IOException, java.net.ConnectException, java.net.SocketException { fServerSocketChannel = ServerSocketChannel.open(); try { fServerSocketChannel.socket().bind(new InetSocketAddress(inetAddress,port)); } catch(java.net.SocketException e) { cleanUp(); return null; } return fServerSocketChannel.accept(); } public FileChannel getFileChannel() throws FileNotFoundException { return new FileInputStream(fileDetails.getFile()).getChannel(); } /** * authenticate with Target. This includes: * <ul> * <li> finding out what types of SOCKS5 authentication provides the other end, * <li> replying with "no authentication" type of authentication (sic!), * <li> reading a SOCKS5 CONNECT request, with hostname containing hashed (using <b>now broken</b> * SHA1 of ( SID + Initiator JID + TARGET JID ) and comparing it with expected value taken from * own values of SID and 2 JIDs, * <li> * </ul> * @return true if authentication succeeded, false otherwise */ public boolean authenticate() { int count = readFromSocket(); // first, check what kinds of SOCKS5 authentication methods provide the other end if(! Socks5Utils.providesSOCKS5NoAuthMethod(buffer)) { System.err.println("Target does not provide SOCKS5 NOAUTH authentication method"); return false; } // send the reply to the Target: OK, we decide to go for no authentication byte[] reply = new byte[2]; reply[0] = Socks5Utils.SOCKS_VERSION; reply[1] = Socks5Utils.SOCKS_AUTH_NOAUTH; count = writeToSocket(reply); // next the Target should send CONNECT message with specially formatted host part // we need to verify if SHA1 hashes match and reply accordingly count = readFromSocket(); if( buffer.get(1) == Socks5Utils.SOCKS_COMMAND_CONNECT && buffer.get(3) == Socks5Utils.SOCKS_ATYP_DOMAINNAME ) { byte[] hostname = new byte[count - 7]; for(int i=0; i<hostname.length; i++) { hostname[i] = buffer.get(i + 5); } String sha1Checksum = new String(hostname); // this is what we expect, according to our information String expectedChecksum = getSHA1HashString(sid + jidFrom + jidTo); if( sha1Checksum.equals(expectedChecksum) == false ) { //DEBUG System.out.println("SHA1 checksums not matching!! aborting file transfer"); return false; } } // ok, send the confirmation that checksum is all right. this means that we need // to change the 2nd byte to 0x00 (OK) and re-send the SOCKS5 packet buffer.flip(); buffer.put(1,(byte)0x00); writeToSocket(buffer); count = writeToSocket(buffer); return true; } /** * send file to the socket * @return true if no problems, false otherwise */ public boolean sendFile() { return transferFromFileToSocket(); } public void cleanUp() { super.cleanUp(); try { fServerSocketChannel.close(); } catch (IOException e) { e.printStackTrace(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -