Skip to content

Commit fb1d578

Browse files
author
Jarrod Ribble
committed
Add some logging.
1 parent 07ec7bd commit fb1d578

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/main/java/com/netiq/websockify/PortUnificationHandler.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.util.Timer;
1919
import java.util.TimerTask;
20+
import java.util.logging.Logger;
2021

2122
import javax.net.ssl.SSLEngine;
2223

@@ -145,6 +146,8 @@ private boolean isFlashPolicy(int magic1, int magic2 ) {
145146
private void enableSsl(ChannelHandlerContext ctx) {
146147
ChannelPipeline p = ctx.getPipeline();
147148

149+
Logger.getLogger(PortUnificationHandler.class.getName()).fine("SSL request from " + ctx.getChannel().getRemoteAddress() + ".");
150+
148151
SSLEngine engine = WebsockifySslContext.getInstance(keystore, keystorePassword).getServerContext().createSSLEngine();
149152
engine.setUseClientMode(false);
150153

@@ -156,6 +159,8 @@ private void enableSsl(ChannelHandlerContext ctx) {
156159
private void switchToWebsocketProxy(ChannelHandlerContext ctx) {
157160
ChannelPipeline p = ctx.getPipeline();
158161

162+
Logger.getLogger(PortUnificationHandler.class.getName()).fine("Websocket proxy request from " + ctx.getChannel().getRemoteAddress() + ".");
163+
159164
p.addLast("decoder", new HttpRequestDecoder());
160165
p.addLast("aggregator", new HttpChunkAggregator(65536));
161166
p.addLast("encoder", new HttpResponseEncoder());
@@ -167,6 +172,8 @@ private void switchToWebsocketProxy(ChannelHandlerContext ctx) {
167172
private void switchToFlashPolicy(ChannelHandlerContext ctx) {
168173
ChannelPipeline p = ctx.getPipeline();
169174

175+
Logger.getLogger(PortUnificationHandler.class.getName()).fine("Flash policy request from " + ctx.getChannel().getRemoteAddress() + ".");
176+
170177
p.addLast("flash", new FlashPolicyHandler());
171178

172179
p.remove(this);
@@ -175,6 +182,8 @@ private void switchToFlashPolicy(ChannelHandlerContext ctx) {
175182
private void switchToDirectProxy(ChannelHandlerContext ctx) {
176183
ChannelPipeline p = ctx.getPipeline();
177184

185+
Logger.getLogger(PortUnificationHandler.class.getName()).fine("Direct proxy request from " + ctx.getChannel().getRemoteAddress() + ".");
186+
178187
p.addLast("proxy", new DirectProxyHandler( ctx.getChannel(), cf, resolver ));
179188

180189
p.remove(this);
@@ -192,5 +201,6 @@ public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)
192201
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
193202
throws Exception {
194203
cancelDirectionConnectionTimer ( );
204+
Logger.getLogger(PortUnificationHandler.class.getName()).severe("Exception on connection to " + ctx.getChannel().getRemoteAddress() + ": " + e.getCause().getMessage() );
195205
}
196206
}

src/main/java/com/netiq/websockify/WebsockifyProxyHandler.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,13 @@ private void ensureTargetConnection(ChannelEvent e, boolean websocket, final Obj
100100
// Suspend incoming traffic until connected to the remote host.
101101
final Channel inboundChannel = e.getChannel();
102102
inboundChannel.setReadable(false);
103+
Logger.getLogger(WebsockifyProxyHandler.class.getName()).info("Inbound proxy connection from " + inboundChannel.getRemoteAddress() + ".");
103104

104105
// resolve the target
105-
InetSocketAddress target = resolver.resolveTarget(e.getChannel());
106+
final InetSocketAddress target = resolver.resolveTarget(inboundChannel);
106107
if ( target == null )
107108
{
109+
Logger.getLogger(WebsockifyProxyHandler.class.getName()).severe("Connection from " + inboundChannel.getRemoteAddress() + " failed to resolve target.");
108110
// there is no target
109111
inboundChannel.close();
110112
return;
@@ -128,8 +130,10 @@ public void operationComplete(ChannelFuture future) throws Exception {
128130
if (future.isSuccess()) {
129131
// Connection attempt succeeded:
130132
// Begin to accept incoming traffic.
133+
Logger.getLogger(WebsockifyProxyHandler.class.getName()).info("Created outbound connection to " + target + ".");
131134
inboundChannel.setReadable(true);
132135
} else {
136+
Logger.getLogger(WebsockifyProxyHandler.class.getName()).severe("Failed to create outbound connection to " + target + ".");
133137
// Close the connection if the connection attempt has failed.
134138
inboundChannel.close();
135139
}
@@ -166,6 +170,7 @@ private void handleHttpRequest(ChannelHandlerContext ctx, HttpRequest req, final
166170

167171
String upgradeHeader = req.getHeader("Upgrade");
168172
if(upgradeHeader != null && upgradeHeader.toUpperCase().equals("WEBSOCKET")){
173+
Logger.getLogger(WebsockifyProxyHandler.class.getName()).fine("Websocket request from " + e.getRemoteAddress() + ".");
169174
// Handshake
170175
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
171176
this.getWebSocketLocation(req), "base64", false);
@@ -189,6 +194,7 @@ private void handleHttpRequest(ChannelHandlerContext ctx, HttpRequest req, final
189194
HttpRequest request = (HttpRequest) e.getMessage();
190195
String redirectUrl = isRedirect(request.getUri());
191196
if ( redirectUrl != null) {
197+
Logger.getLogger(WebsockifyProxyHandler.class.getName()).fine("Redirecting to " + redirectUrl + ".");
192198
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, TEMPORARY_REDIRECT);
193199
response.setHeader(HttpHeaders.Names.LOCATION, redirectUrl);
194200
sendHttpResponse(ctx, req, response);
@@ -238,6 +244,8 @@ private void handleWebRequest(ChannelHandlerContext ctx, final MessageEvent e) t
238244
sendError(ctx, METHOD_NOT_ALLOWED);
239245
return;
240246
}
247+
248+
Logger.getLogger(WebsockifyProxyHandler.class.getName()).info("Web request from " + e.getRemoteAddress() + " for " + request.getUri() + ".");
241249

242250
final String path = sanitizeUri(request.getUri());
243251
if (path == null) {

src/main/java/com/netiq/websockify/WebsockifySslContext.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.security.KeyStore;
55
import java.security.Security;
66
import java.util.HashMap;
7+
import java.util.logging.Logger;
78

89
import javax.net.ssl.KeyManagerFactory;
910
import javax.net.ssl.SSLContext;
@@ -70,6 +71,7 @@ private WebsockifySslContext(String keystore, String keystorePassword) {
7071
serverContext = SSLContext.getInstance(PROTOCOL);
7172
serverContext.init(kmf.getKeyManagers(), null, null);
7273
} catch (Exception e) {
74+
Logger.getLogger(WebsockifySslContext.class.getName()).severe("Error creating SSL context for keystore " + keystore + ": " + e.getMessage());
7375
throw new Error("Failed to initialize the server-side SSLContext", e);
7476
}
7577
_serverContext = serverContext;

0 commit comments

Comments
 (0)