Class ProxyConnection<I extends HttpObject>
- Type Parameters:
I- the type of "initial" message. This will be eitherHttpResponseorHttpRequest.
- All Implemented Interfaces:
ChannelHandler,ChannelInboundHandler
- Direct Known Subclasses:
ClientToProxyConnection,ProxyToServerConnection
Base class for objects that represent a connection to/from our proxy.
A ProxyConnection models a bidirectional message flow on top of a Netty
Channel.
The read(Object) method is called whenever a new message arrives on
the underlying socket.
The write(Object) method can be called by anyone wanting to write
data out of the connection.
ProxyConnection has a lifecycle and its current state within that lifecycle
is recorded as a ConnectionState. The allowed states and transitions
vary a little depending on the concrete implementation of ProxyConnection.
However, all ProxyConnections share the following lifecycle events:
connected()- Once the underlying channel is active, the ProxyConnection is considered connected and moves intoConnectionState.AWAITING_INITIAL. The Channel is recorded at this time for later referencing.disconnected()- When the underlying channel goes inactive, the ProxyConnection moves intoConnectionState.DISCONNECTEDbecameWritable()- When the underlying channel becomes writeable, this callback is invoked.
By default, incoming data on the underlying channel is automatically read and
passed to the read(Object) method. Reading can be stopped and
resumed using stopReading() and resumeReading().
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classUtility handler for monitoring bytes read on this connection.protected classUtility handler for monitoring bytes written on this connection.protected classUtility handler for monitoring requests read on this connection.protected static classUtility handler for monitoring requests written on this connection.protected classUtility handler for monitoring responses read on this connection.protected classUtility handler for monitoring responses written on this connection.Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Channelprotected ChannelHandlerContextprivate ConnectionStateprotected longprotected final ProxyConnectionLoggerprotected final DefaultHttpProxyServerprotected final booleanprotected SSLEngineIf using encryption, this holds ourSSLEngine.protected ConnectionFlowStepEnables tunneling on this connection by dropping the HTTP related encoders and decoders, as well as idle timers.protected boolean -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedProxyConnection(ConnectionState initialState, DefaultHttpProxyServer proxyServer, boolean runsAsSslClient) Construct a new ProxyConnection. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaggregateContentForFiltering(ChannelPipeline pipeline, int numberOfBytesToBuffer) Enables decompression and aggregation of content, which is useful for certain types of filtering activity.protected voidCallback that's invoked if this connection becomes saturated.protected voidCallback that's invoked when this connection becomes writeable again.protected voidbecome(ConnectionState state) Updates the current state to the given value.final voidOnly once the Netty Channel is active to we recognize the ProxyConnection as connected.voidAs soon as the Netty Channel is inactive, we recognize the ProxyConnection as disconnected.protected final voidchannelRead0(ChannelHandlerContext ctx, Object msg) voidvoidfinal voidprivate voidcloseChannel(Promise<Void> promise) protected voidThis method is called as soon as the underlyingChannelis connected.Disconnects.protected voidThis method is called as soon as the underlyingChannelbecomes disconnected.(package private) ChannelFutureencrypt(ChannelPipeline pipeline, SSLEngine sslEngine, boolean authenticateClients) Encrypts traffic on this connection with SSL/TLS.Encrypts traffic on this connection with SSL/TLS.protected ConnectionFlowStepEncryptChannel(SSLEngine sslEngine) Encrypts the channel using the providedSSLEngine.final voidexceptionCaught(ChannelHandlerContext ctx, Throwable cause) protected voidexceptionCaught(Throwable cause) Override this to handle exceptions that occurred during asynchronous processing on theChannel.protected ConnectionStateprotected HttpFiltersgetHttpFiltersFromProxyServer(HttpRequest httpRequest) Request the ProxyServer for Filters.(package private) ProxyConnectionLoggergetLOG()protected booleanis(ConnectionState state) Utility for checking current state.protected booleanIf this connection is currently in the process of going through aConnectionFlow, this will return true.protected booleanIndicates whether or not this connection is saturated (i.e.booleanprotected voidRead is invoked automatically by Netty as messages arrive on the socket.protected abstract voidRead anHAProxyMessageprivate voidreadHTTP(HttpObject httpObject) Handles readingHttpObjects.protected abstract voidreadHTTPChunk(HttpContent chunk) Implement this to handle reading a chunk in a chunked transfer.protected abstract ConnectionStatereadHTTPInitial(I httpObject) Implement this to handle reading the initial object (e.g.protected abstract voidImplement this to handle reading a raw buffer as they are used in HTTP tunneling.protected booleanremoveHandlerIfPresent(ChannelPipeline pipeline, String handlerName) Removes the handler with the given name if it is present in the pipeline.protected voidCall this to resume reading.protected voidCall this to stop reading.protected voidtimedOut()This method is called when the underlyingChanneltimes out due to an idle timeout.final voiduserEventTriggered(ChannelHandlerContext ctx, Object evt) We're looking forIdleStateEvents to see if we need to disconnect.(package private) ChannelFutureThis method is called by users of the ProxyConnection to send stuff out over the socket.protected ChannelFuturewriteHttp(HttpObject httpObject) Writes HttpObjects to the connection asynchronously.protected ChannelFutureWrites raw buffers to the connection.protected ChannelFuturewriteToChannel(Object msg) Methods inherited from class io.netty.channel.SimpleChannelInboundHandler
acceptInboundMessage, channelReadMethods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelReadCompleteMethods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerAdded, handlerRemoved, isSharableMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.netty.channel.ChannelHandler
handlerAdded, handlerRemoved
-
Field Details
-
LOG
-
proxyServer
-
runsAsSslClient
protected final boolean runsAsSslClient -
ctx
-
channel
-
currentState
-
tunneling
protected volatile boolean tunneling -
lastReadTime
protected volatile long lastReadTime -
sslEngine
If using encryption, this holds ourSSLEngine. -
StartTunneling
Enables tunneling on this connection by dropping the HTTP related encoders and decoders, as well as idle timers.
Note - the work is done on the
ChannelHandlerContext's executor becauseChannelPipeline.remove(String)can deadlock if called directly.
-
-
Constructor Details
-
ProxyConnection
protected ProxyConnection(ConnectionState initialState, DefaultHttpProxyServer proxyServer, boolean runsAsSslClient) Construct a new ProxyConnection.- Parameters:
initialState- the state in which this connection starts outproxyServer- theDefaultHttpProxyServerin which we're runningrunsAsSslClient- determines whether this connection acts as an SSL client or server (determines who does the handshake)
-
-
Method Details
-
read
Read is invoked automatically by Netty as messages arrive on the socket. -
readHAProxyMessage
Read anHAProxyMessage- Parameters:
msg-HAProxyMessage
-
readHTTP
Handles readingHttpObjects. -
readHTTPInitial
Implement this to handle reading the initial object (e.g.HttpRequestorHttpResponse). -
readHTTPChunk
Implement this to handle reading a chunk in a chunked transfer. -
readRaw
Implement this to handle reading a raw buffer as they are used in HTTP tunneling. -
write
This method is called by users of the ProxyConnection to send stuff out over the socket. -
doWrite
-
writeHttp
Writes HttpObjects to the connection asynchronously. -
writeRaw
Writes raw buffers to the connection. -
writeToChannel
-
connected
protected void connected()This method is called as soon as the underlyingChannelis connected. Note that for proxies with complexConnectionFlows that include SSL handshaking and other such things, just because theChannelis connected doesn't mean that our connection is fully established. -
disconnected
protected void disconnected()This method is called as soon as the underlyingChannelbecomes disconnected. -
timedOut
protected void timedOut()This method is called when the underlyingChanneltimes out due to an idle timeout. -
encrypt
Encrypts traffic on this connection with SSL/TLS.- Parameters:
sslEngine- theSSLEnginefor doing the encryptionauthenticateClients- determines whether to authenticate clients or not- Returns:
- a Future for when the SSL handshake has completed
-
encrypt
protected Future<Channel> encrypt(ChannelPipeline pipeline, SSLEngine sslEngine, boolean authenticateClients) Encrypts traffic on this connection with SSL/TLS.- Parameters:
pipeline- the ChannelPipeline on which to enable encryptionsslEngine- theSSLEnginefor doing the encryptionauthenticateClients- determines whether to authenticate clients or not- Returns:
- a Future for when the SSL handshake has completed
-
EncryptChannel
Encrypts the channel using the providedSSLEngine.- Parameters:
sslEngine- theSSLEnginefor doing the encryption
-
aggregateContentForFiltering
Enables decompression and aggregation of content, which is useful for certain types of filtering activity. -
becameSaturated
protected void becameSaturated()Callback that's invoked if this connection becomes saturated. -
becameWritable
protected void becameWritable()Callback that's invoked when this connection becomes writeable again. -
exceptionCaught
Override this to handle exceptions that occurred during asynchronous processing on theChannel. -
removeHandlerIfPresent
Removes the handler with the given name if it is present in the pipeline.- Parameters:
pipeline- the pipeline from which to remove the handler.handlerName- the name of the handler to remove.- Returns:
- true if the handler was found and removed; false otherwise.
-
disconnect
Disconnects. This will wait for pending writes to be flushed before disconnecting.- Returns:
Future<Void>for when we're done disconnecting. If we weren't connected, this returns null.
-
closeChannel
-
isSaturated
protected boolean isSaturated()Indicates whether or not this connection is saturated (i.e. not writeable). -
is
Utility for checking current state. -
isConnecting
protected boolean isConnecting()If this connection is currently in the process of going through aConnectionFlow, this will return true. -
become
Updates the current state to the given value. -
getCurrentState
-
isTunneling
public boolean isTunneling() -
getSslEngine
-
stopReading
protected void stopReading()Call this to stop reading. -
resumeReading
protected void resumeReading()Call this to resume reading. -
getHttpFiltersFromProxyServer
Request the ProxyServer for Filters. By default, no-op filters are returned by DefaultHttpProxyServer. Subclasses of ProxyConnection can change this behaviour.- Parameters:
httpRequest- Filter attached to the give HttpRequest (if any)
-
getLOG
ProxyConnectionLogger getLOG() -
channelRead0
- Specified by:
channelRead0in classSimpleChannelInboundHandler<Object>
-
channelRegistered
- Specified by:
channelRegisteredin interfaceChannelInboundHandler- Overrides:
channelRegisteredin classChannelInboundHandlerAdapter- Throws:
Exception
-
channelUnregistered
- Specified by:
channelUnregisteredin interfaceChannelInboundHandler- Overrides:
channelUnregisteredin classChannelInboundHandlerAdapter- Throws:
Exception
-
channelActive
Only once the Netty Channel is active to we recognize the ProxyConnection as connected.- Specified by:
channelActivein interfaceChannelInboundHandler- Overrides:
channelActivein classChannelInboundHandlerAdapter- Throws:
Exception
-
channelInactive
As soon as the Netty Channel is inactive, we recognize the ProxyConnection as disconnected.- Specified by:
channelInactivein interfaceChannelInboundHandler- Overrides:
channelInactivein classChannelInboundHandlerAdapter- Throws:
Exception
-
channelWritabilityChanged
- Specified by:
channelWritabilityChangedin interfaceChannelInboundHandler- Overrides:
channelWritabilityChangedin classChannelInboundHandlerAdapter- Throws:
Exception
-
exceptionCaught
- Specified by:
exceptionCaughtin interfaceChannelHandler- Specified by:
exceptionCaughtin interfaceChannelInboundHandler- Overrides:
exceptionCaughtin classChannelInboundHandlerAdapter
-
userEventTriggered
We're looking for
IdleStateEvents to see if we need to disconnect.Note - we don't care what kind of IdleState we got. Thanks to qbast for pointing this out.
- Specified by:
userEventTriggeredin interfaceChannelInboundHandler- Overrides:
userEventTriggeredin classChannelInboundHandlerAdapter- Throws:
Exception
-