KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > agent > client > tunneling > RemoteTunnelChannel


1 package com.sslexplorer.agent.client.tunneling;
2
3 import java.io.IOException JavaDoc;
4 import java.net.Socket JavaDoc;
5 import java.text.MessageFormat JavaDoc;
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 JavaDoc CHANNEL_TYPE = "remote-tunnel";
18
19     private Socket JavaDoc 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 JavaDoc {
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 JavaDoc 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 JavaDoc ex) {
63                 close();
64             }
65         }
66     }
67
68     public byte[] open(byte[] data) throws IOException JavaDoc, ChannelOpenException {
69         ByteArrayReader reply = new ByteArrayReader(data);
70
71         String JavaDoc launchId = reply.readString();
72         int id = (int) reply.readInt();
73         String JavaDoc name = reply.readString();
74         int type = (int) reply.readInt();
75         String JavaDoc transport = reply.readString();
76         String JavaDoc sourceInterface = reply.readString();
77         int sourcePort = (int) reply.readInt();
78         int destinationPort = (int) reply.readInt();
79         String JavaDoc destinationHost = reply.readString();
80         
81
82         if(agent.getConfiguration().isRemoteTunnelsRequireConfirmation()) {
83             if(!agent.getGUI().confirm(AgentClientGUI.WARNING, Messages.getString("RemoteForwardingChannelListener.confirmRemoteTunnel"), //$NON-NLS-1$$
84
Messages.getString("RemoteForwardingChannelListener.cancelRemoteTunnel"), //$NON-NLS-1$$
85
Messages.getString("RemoteForwardingChannelListener.incoming.title"), //$NON-NLS-1$$
86
MessageFormat.format(Messages.getString("RemoteForwardingChannelListener.incoming.text"), new Object JavaDoc[] { destinationHost + ":" + destinationPort } ))) { //$NON-NLS-1$$
87
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 JavaDoc(destinationHost, destinationPort);
105         } catch ( IOException JavaDoc 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