KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > tunnels > agent > RemoteTunnel


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.tunnels.agent;
21
22 import java.io.IOException JavaDoc;
23 import java.net.InetAddress JavaDoc;
24 import java.net.ServerSocket JavaDoc;
25 import java.net.Socket JavaDoc;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 import com.maverick.multiplex.Channel;
31 import com.maverick.multiplex.ChannelOpenException;
32 import com.maverick.multiplex.Request;
33 import com.maverick.util.ByteArrayWriter;
34 import com.sslexplorer.agent.AgentTunnel;
35 import com.sslexplorer.boot.CustomServerSocketFactory;
36 import com.sslexplorer.boot.Util;
37 import com.sslexplorer.core.stringreplacement.VariableReplacement;
38 import com.sslexplorer.policyframework.LaunchSession;
39 import com.sslexplorer.policyframework.LaunchSessionFactory;
40 import com.sslexplorer.tunnels.Tunnel;
41 import com.sslexplorer.tunnels.TunnelingService;
42
43 /**
44  * Manages the life cycle of a single remote tunnel on the server end. This
45  * object sets up a server socket and listens for conenctions. Whenever a new
46  * connection is made a channel is created to the associated agent which in turn
47  * opens the connection to the required service.
48  *
49  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
50  */

51 public class RemoteTunnel extends Thread JavaDoc {
52
53     final static Log log = LogFactory.getLog(RemoteTunnel.class);
54
55     private AgentTunnel agent;
56     private Tunnel tunnel;
57     private ServerSocket JavaDoc listeningSocket;
58     private boolean running;
59     private RemoteTunnelManager tunnelManager;
60     private LaunchSession launchSession;
61
62     /**
63      * Constructor.
64      *
65      * @param tunnel tunnel
66      * @param agent agent
67      * @param tunnelManager tunnel manager
68      * @param launchSession launch session
69      * @throws IOException
70      */

71     public RemoteTunnel(Tunnel tunnel, AgentTunnel agent, RemoteTunnelManager tunnelManager, LaunchSession launchSession) throws IOException JavaDoc {
72         this.agent = agent;
73         this.tunnel = tunnel;
74         this.launchSession = launchSession;
75         this.tunnelManager = tunnelManager;
76         listeningSocket = CustomServerSocketFactory.getDefault().createServerSocket(
77                         tunnel.getSourcePort(),
78                         50,
79                         Util.isNullOrTrimmedBlank(tunnel.getSourceInterface()) ? null : InetAddress.getByName(tunnel
80                                         .getSourceInterface()));
81
82     }
83
84     /**
85      * Get the agent that is dealing with this tunnel.
86      *
87      * @return agent
88      */

89     public AgentTunnel getAgent() {
90         return agent;
91     }
92
93     /**
94      * Get the tunnel configuration.
95      *
96      * @return tunnel
97      */

98     public Tunnel getTunnel() {
99         return tunnel;
100     }
101
102     /**
103      * Get if this remote tunnel is currently running.
104      *
105      * @return running
106      */

107     public boolean isRunning() {
108         return running;
109     }
110
111     /**
112      * Stop the listening socket.
113      */

114     public void stopListener() {
115         if (running) {
116             if (log.isInfoEnabled())
117                 log.info("Stopping remote listener on " + tunnel.getSourcePort());
118             running = false;
119             try {
120                 listeningSocket.close();
121             } catch (IOException JavaDoc ioe) {
122                 log.error("Failed to stop listening socket for remote tunnel.", ioe);
123             }
124             if (log.isInfoEnabled())
125                 log.info("Stopped remote listener on " + tunnel.getSourcePort());
126
127         }
128     }
129
130     /*
131      * (non-Javadoc)
132      *
133      * @see java.lang.Thread#run()
134      */

135     public void run() {
136         // Process destination host and port for replacement variables
137
VariableReplacement r = new VariableReplacement();
138         r.setLaunchSession(launchSession);
139         String JavaDoc destHost = r.replace(tunnel.getDestination().getHost());
140         
141         ByteArrayWriter msg = new ByteArrayWriter();
142         
143         try {
144             
145             msg.writeInt(tunnel.getResourceId());
146             msg.writeString(tunnel.getSourceInterface());
147             msg.writeString(launchSession.getId());
148             msg.writeString(tunnel.getSourceInterface());
149             msg.writeInt(tunnel.getSourcePort());
150             msg.writeString(destHost);
151             msg.writeInt(tunnel.getDestination().getPort());
152             
153             
154             Request request = new Request(TunnelingService.START_REMOTE_TUNNEL, msg.toByteArray());
155             agent.sendRequest(request, false);
156             
157         } catch(IOException JavaDoc ex) {
158             ex.printStackTrace();
159         }
160         
161         running = true;
162         if (log.isInfoEnabled())
163             log.info("Starting remote listener on " + tunnel.getSourcePort());
164         try {
165             while (running) {
166                 try {
167                     Socket JavaDoc s = listeningSocket.accept();
168                     if (log.isInfoEnabled())
169                         log.info("Received new connection on " + tunnel.getSourcePort() + " from " + s.getInetAddress());
170                     RemoteForwardingChannel channel = new RemoteForwardingChannel(this, s, tunnel, launchSession);
171                     try {
172                         agent.openChannel(channel);
173                     } catch (ChannelOpenException e) {
174                         log.error("Error opening channel. Remote tunnel remaining open but closing connection.", e);
175                         try {
176                             s.close();
177                         } catch (IOException JavaDoc ioe) {
178                         }
179                     }
180                 } catch (IOException JavaDoc e) {
181                     if (running) {
182                         log.error("IO error waiting for connection, stopping remote tunnel.", e);
183                     }
184                 }
185             }
186         } finally {
187             
188             Request request = new Request(TunnelingService.STOP_REMOTE_TUNNEL, msg.toByteArray());
189             try {
190                 agent.sendRequest(request, false);
191             } catch (IOException JavaDoc e) {
192             }
193             
194             Channel[] c = agent.getActiveChannels();
195             if (c != null) {
196                 for (int i = 0; i < c.length; i++) {
197                     if (c[i] instanceof RemoteForwardingChannel) {
198                         RemoteForwardingChannel rfc = (RemoteForwardingChannel) c[i];
199                         if (rfc.getRemoteTunnel() == this && rfc.getConnection() != null) {
200                             try {
201                                 rfc.close();
202                             } catch (Throwable JavaDoc t) {
203                                 // TODO workaround for NPE
204
log.error("Failed to close channel.", t);
205                             }
206                         }
207                     }
208                 }
209             }
210             tunnelManager.removeRemoteTunnel(this);
211             LaunchSessionFactory.getInstance().removeLaunchSession(launchSession);
212             running = false;
213         }
214     }
215
216 }
Popular Tags