1 package com.sslexplorer.agent.client.tunneling; 2 3 import java.io.IOException ; 4 import java.net.Socket ; 5 import java.text.MessageFormat ; 6 7 import com.maverick.multiplex.Channel; 8 import com.maverick.multiplex.ChannelOpenException; 9 import com.maverick.multiplex.IOStreamConnector; 10 import com.maverick.util.ByteArrayReader; 11 import com.sslexplorer.agent.client.Agent; 12 import com.sslexplorer.agent.client.AgentClientGUI; 13 import com.sslexplorer.agent.client.util.TunnelConfiguration; 14 15 public class RemoteTunnelChannel extends Channel { 16 17 public static final String CHANNEL_TYPE = "remote-tunnel"; 18 19 private Socket socket = null; 20 private IOStreamConnector input; 21 private IOStreamConnector output; 22 private TunnelConfiguration configuration; 23 private long lastData; 24 private Agent agent; 25 26 public RemoteTunnelChannel(Agent agent) { 27 super(CHANNEL_TYPE, 32768, 35000); 28 this.agent = agent; 29 } 30 31 public TunnelConfiguration getConfiguration() { 32 return configuration; 33 } 34 35 public byte[] create() throws IOException { 36 return null; 37 } 38 39 public void onChannelData(byte[] buf, int off, int len) { 40 super.onChannelData(buf, off, len); 41 lastData = System.currentTimeMillis(); 42 }; 43 44 public void onChannelClose() { 45 if (input != null) 46 input.close(); 47 if (output != null) 48 output.close(); 49 try { 50 if (socket != null) 51 socket.close(); 52 } catch (IOException e) { 53 } 54 } 55 56 public void onChannelOpen(byte[] data) { 57 lastData = System.currentTimeMillis(); 58 if (socket != null) { 59 try { 60 input = new IOStreamConnector(socket.getInputStream(), getOutputStream()); 61 output = new IOStreamConnector(getInputStream(), socket.getOutputStream()); 62 } catch (IOException ex) { 63 close(); 64 } 65 } 66 } 67 68 public byte[] open(byte[] data) throws IOException , ChannelOpenException { 69 ByteArrayReader reply = new ByteArrayReader(data); 70 71 String launchId = reply.readString(); 72 int id = (int) reply.readInt(); 73 String name = reply.readString(); 74 int type = (int) reply.readInt(); 75 String transport = reply.readString(); 76 String sourceInterface = reply.readString(); 77 int sourcePort = (int) reply.readInt(); 78 int destinationPort = (int) reply.readInt(); 79 String destinationHost = reply.readString(); 80 81 82 if(agent.getConfiguration().isRemoteTunnelsRequireConfirmation()) { 83 if(!agent.getGUI().confirm(AgentClientGUI.WARNING, Messages.getString("RemoteForwardingChannelListener.confirmRemoteTunnel"), Messages.getString("RemoteForwardingChannelListener.cancelRemoteTunnel"), Messages.getString("RemoteForwardingChannelListener.incoming.title"), MessageFormat.format(Messages.getString("RemoteForwardingChannelListener.incoming.text"), new Object [] { destinationHost + ":" + destinationPort } ))) { throw new ChannelOpenException(ChannelOpenException.CHANNEL_REFUSED, "Rejected by user."); 88 } 89 } 90 91 configuration = new DefaultTunnel(id, 92 type, 93 transport, 94 sourceInterface, 95 sourcePort, 96 destinationPort, 97 destinationHost, 98 true, 99 false, 100 name, 101 launchId); 102 103 try { 104 this.socket = new Socket (destinationHost, destinationPort); 105 } catch ( IOException ioe) { 106 throw new ChannelOpenException(ChannelOpenException.CONNECT_FAILED, "Failed to open socket."); 107 } 108 return null; 109 } 110 111 public long getDataLastTransferredTime() { 112 return lastData; 113 } 114 } 115 | Popular Tags |