Package io.netty.handler.proxy
Class ProxyHandler
- java.lang.Object
-
- io.netty.channel.ChannelHandlerAdapter
-
- io.netty.channel.ChannelInboundHandlerAdapter
-
- io.netty.channel.ChannelDuplexHandler
-
- io.netty.handler.proxy.ProxyHandler
-
- All Implemented Interfaces:
ChannelHandler,ChannelInboundHandler,ChannelOutboundHandler
- Direct Known Subclasses:
HttpProxyHandler,Socks4ProxyHandler,Socks5ProxyHandler
public abstract class ProxyHandler extends ChannelDuplexHandler
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classProxyHandler.LazyChannelPromise-
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description (package private) static java.lang.StringAUTH_NONEA string that signifies 'no authentication' or 'anonymous'.private ProxyHandler.LazyChannelPromiseconnectPromiseprivate ScheduledFuture<?>connectTimeoutFutureprivate longconnectTimeoutMillisprivate ChannelHandlerContextctxprivate static longDEFAULT_CONNECT_TIMEOUT_MILLISThe default connect timeout: 10 seconds.private java.net.SocketAddressdestinationAddressprivate booleanfinishedprivate booleanflushedPrematurelyprivate static InternalLoggerloggerprivate PendingWriteQueuependingWritesprivate java.net.SocketAddressproxyAddressprivate booleansuppressChannelReadCompleteprivate ChannelFutureListenerwriteListener
-
Constructor Summary
Constructors Modifier Constructor Description protectedProxyHandler(java.net.SocketAddress proxyAddress)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract voidaddCodec(ChannelHandlerContext ctx)Adds the codec handlers required to communicate with the proxy server.private voidaddPendingWrite(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise)abstract java.lang.StringauthScheme()Returns the name of the authentication scheme in use.private voidcancelConnectTimeoutFuture()voidchannelActive(ChannelHandlerContext ctx)CallsChannelHandlerContext.fireChannelActive()to forward to the nextChannelInboundHandlerin theChannelPipeline.voidchannelInactive(ChannelHandlerContext ctx)CallsChannelHandlerContext.fireChannelInactive()to forward to the nextChannelInboundHandlerin theChannelPipeline.voidchannelRead(ChannelHandlerContext ctx, java.lang.Object msg)CallsChannelHandlerContext.fireChannelRead(Object)to forward to the nextChannelInboundHandlerin theChannelPipeline.voidchannelReadComplete(ChannelHandlerContext ctx)CallsChannelHandlerContext.fireChannelReadComplete()to forward to the nextChannelInboundHandlerin theChannelPipeline.voidconnect(ChannelHandlerContext ctx, java.net.SocketAddress remoteAddress, java.net.SocketAddress localAddress, ChannelPromise promise)CallsChannelOutboundInvoker.connect(SocketAddress, SocketAddress, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.Future<Channel>connectFuture()Returns aFuturethat is notified when the connection to the destination has been established or the connection attempt has failed.longconnectTimeoutMillis()Returns the connect timeout in millis.<T extends java.net.SocketAddress>
TdestinationAddress()Returns the address of the destination to connect to via the proxy server.voidexceptionCaught(ChannelHandlerContext ctx, java.lang.Throwable cause)CallsChannelHandlerContext.fireExceptionCaught(Throwable)to forward to the nextChannelHandlerin theChannelPipeline.protected java.lang.StringexceptionMessage(java.lang.String msg)Decorates the specified exception message with the common information such as the current protocol, authentication scheme, proxy address, and destination address.private voidfailPendingWrites(java.lang.Throwable cause)private voidfailPendingWritesAndClose(java.lang.Throwable cause)voidflush(ChannelHandlerContext ctx)CallsChannelHandlerContext.flush()to forward to the nextChannelOutboundHandlerin theChannelPipeline.voidhandlerAdded(ChannelHandlerContext ctx)Do nothing by default, sub-classes may override this method.protected abstract booleanhandleResponse(ChannelHandlerContext ctx, java.lang.Object response)Handles the message received from the proxy server.booleanisConnected()Returnstrueif and only if the connection to the destination has been established successfully.protected abstract java.lang.ObjectnewInitialMessage(ChannelHandlerContext ctx)Returns a new message that is sent at first time when the connection to the proxy server has been established.abstract java.lang.Stringprotocol()Returns the name of the proxy protocol in use.<T extends java.net.SocketAddress>
TproxyAddress()Returns the address of the proxy server.private static voidreadIfNeeded(ChannelHandlerContext ctx)protected abstract voidremoveDecoder(ChannelHandlerContext ctx)Removes the decoders added inaddCodec(ChannelHandlerContext).protected abstract voidremoveEncoder(ChannelHandlerContext ctx)Removes the encoders added inaddCodec(ChannelHandlerContext).private booleansafeRemoveDecoder()private booleansafeRemoveEncoder()private voidsendInitialMessage(ChannelHandlerContext ctx)Sends the initial message to be sent to the proxy server.protected voidsendToProxyServer(java.lang.Object msg)Sends the specified message to the proxy server.private voidsetConnectFailure(java.lang.Throwable cause)private voidsetConnectSuccess()voidsetConnectTimeoutMillis(long connectTimeoutMillis)Sets the connect timeout in millis.voidwrite(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise)CallsChannelOutboundInvoker.write(Object, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.private voidwritePendingWrites()-
Methods inherited from class io.netty.channel.ChannelDuplexHandler
bind, close, deregister, disconnect, read
-
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelRegistered, channelUnregistered, channelWritabilityChanged, userEventTriggered
-
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerRemoved, isSharable
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.netty.channel.ChannelHandler
handlerRemoved
-
-
-
-
Field Detail
-
logger
private static final InternalLogger logger
-
DEFAULT_CONNECT_TIMEOUT_MILLIS
private static final long DEFAULT_CONNECT_TIMEOUT_MILLIS
The default connect timeout: 10 seconds.- See Also:
- Constant Field Values
-
AUTH_NONE
static final java.lang.String AUTH_NONE
A string that signifies 'no authentication' or 'anonymous'.- See Also:
- Constant Field Values
-
proxyAddress
private final java.net.SocketAddress proxyAddress
-
destinationAddress
private volatile java.net.SocketAddress destinationAddress
-
connectTimeoutMillis
private volatile long connectTimeoutMillis
-
ctx
private volatile ChannelHandlerContext ctx
-
pendingWrites
private PendingWriteQueue pendingWrites
-
finished
private boolean finished
-
suppressChannelReadComplete
private boolean suppressChannelReadComplete
-
flushedPrematurely
private boolean flushedPrematurely
-
connectPromise
private final ProxyHandler.LazyChannelPromise connectPromise
-
connectTimeoutFuture
private ScheduledFuture<?> connectTimeoutFuture
-
writeListener
private final ChannelFutureListener writeListener
-
-
Method Detail
-
protocol
public abstract java.lang.String protocol()
Returns the name of the proxy protocol in use.
-
authScheme
public abstract java.lang.String authScheme()
Returns the name of the authentication scheme in use.
-
proxyAddress
public final <T extends java.net.SocketAddress> T proxyAddress()
Returns the address of the proxy server.
-
destinationAddress
public final <T extends java.net.SocketAddress> T destinationAddress()
Returns the address of the destination to connect to via the proxy server.
-
isConnected
public final boolean isConnected()
Returnstrueif and only if the connection to the destination has been established successfully.
-
connectFuture
public final Future<Channel> connectFuture()
Returns aFuturethat is notified when the connection to the destination has been established or the connection attempt has failed.
-
connectTimeoutMillis
public final long connectTimeoutMillis()
Returns the connect timeout in millis. If the connection attempt to the destination does not finish within the timeout, the connection attempt will be failed.
-
setConnectTimeoutMillis
public final void setConnectTimeoutMillis(long connectTimeoutMillis)
Sets the connect timeout in millis. If the connection attempt to the destination does not finish within the timeout, the connection attempt will be failed.
-
handlerAdded
public final void handlerAdded(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelHandlerAdapterDo nothing by default, sub-classes may override this method.- Specified by:
handlerAddedin interfaceChannelHandler- Overrides:
handlerAddedin classChannelHandlerAdapter- Throws:
java.lang.Exception
-
addCodec
protected abstract void addCodec(ChannelHandlerContext ctx) throws java.lang.Exception
Adds the codec handlers required to communicate with the proxy server.- Throws:
java.lang.Exception
-
removeEncoder
protected abstract void removeEncoder(ChannelHandlerContext ctx) throws java.lang.Exception
Removes the encoders added inaddCodec(ChannelHandlerContext).- Throws:
java.lang.Exception
-
removeDecoder
protected abstract void removeDecoder(ChannelHandlerContext ctx) throws java.lang.Exception
Removes the decoders added inaddCodec(ChannelHandlerContext).- Throws:
java.lang.Exception
-
connect
public final void connect(ChannelHandlerContext ctx, java.net.SocketAddress remoteAddress, java.net.SocketAddress localAddress, ChannelPromise promise) throws java.lang.Exception
Description copied from class:ChannelDuplexHandlerCallsChannelOutboundInvoker.connect(SocketAddress, SocketAddress, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
connectin interfaceChannelOutboundHandler- Overrides:
connectin classChannelDuplexHandler- Parameters:
ctx- theChannelHandlerContextfor which the connect operation is maderemoteAddress- theSocketAddressto which it should connectlocalAddress- theSocketAddresswhich is used as source on connectpromise- theChannelPromiseto notify once the operation completes- Throws:
java.lang.Exception- thrown if an error occurs
-
channelActive
public final void channelActive(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelInboundHandlerAdapterCallsChannelHandlerContext.fireChannelActive()to forward to the nextChannelInboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
channelActivein interfaceChannelInboundHandler- Overrides:
channelActivein classChannelInboundHandlerAdapter- Throws:
java.lang.Exception
-
sendInitialMessage
private void sendInitialMessage(ChannelHandlerContext ctx) throws java.lang.Exception
Sends the initial message to be sent to the proxy server. This method also starts a timeout task which marks theconnectPromiseas failure if the connection attempt does not success within the timeout.- Throws:
java.lang.Exception
-
newInitialMessage
protected abstract java.lang.Object newInitialMessage(ChannelHandlerContext ctx) throws java.lang.Exception
Returns a new message that is sent at first time when the connection to the proxy server has been established.- Returns:
- the initial message, or
nullif the proxy server is expected to send the first message instead - Throws:
java.lang.Exception
-
sendToProxyServer
protected final void sendToProxyServer(java.lang.Object msg)
Sends the specified message to the proxy server. Use this method to send a response to the proxy server inhandleResponse(ChannelHandlerContext, Object).
-
channelInactive
public final void channelInactive(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelInboundHandlerAdapterCallsChannelHandlerContext.fireChannelInactive()to forward to the nextChannelInboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
channelInactivein interfaceChannelInboundHandler- Overrides:
channelInactivein classChannelInboundHandlerAdapter- Throws:
java.lang.Exception
-
exceptionCaught
public final void exceptionCaught(ChannelHandlerContext ctx, java.lang.Throwable cause) throws java.lang.Exception
Description copied from class:ChannelInboundHandlerAdapterCallsChannelHandlerContext.fireExceptionCaught(Throwable)to forward to the nextChannelHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
exceptionCaughtin interfaceChannelHandler- Specified by:
exceptionCaughtin interfaceChannelInboundHandler- Overrides:
exceptionCaughtin classChannelInboundHandlerAdapter- Throws:
java.lang.Exception
-
channelRead
public final void channelRead(ChannelHandlerContext ctx, java.lang.Object msg) throws java.lang.Exception
Description copied from class:ChannelInboundHandlerAdapterCallsChannelHandlerContext.fireChannelRead(Object)to forward to the nextChannelInboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
channelReadin interfaceChannelInboundHandler- Overrides:
channelReadin classChannelInboundHandlerAdapter- Throws:
java.lang.Exception
-
handleResponse
protected abstract boolean handleResponse(ChannelHandlerContext ctx, java.lang.Object response) throws java.lang.Exception
Handles the message received from the proxy server.- Returns:
trueif the connection to the destination has been established,falseif the connection to the destination has not been established and more messages are expected from the proxy server- Throws:
java.lang.Exception
-
setConnectSuccess
private void setConnectSuccess()
-
safeRemoveDecoder
private boolean safeRemoveDecoder()
-
safeRemoveEncoder
private boolean safeRemoveEncoder()
-
setConnectFailure
private void setConnectFailure(java.lang.Throwable cause)
-
failPendingWritesAndClose
private void failPendingWritesAndClose(java.lang.Throwable cause)
-
cancelConnectTimeoutFuture
private void cancelConnectTimeoutFuture()
-
exceptionMessage
protected final java.lang.String exceptionMessage(java.lang.String msg)
Decorates the specified exception message with the common information such as the current protocol, authentication scheme, proxy address, and destination address.
-
channelReadComplete
public final void channelReadComplete(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelInboundHandlerAdapterCallsChannelHandlerContext.fireChannelReadComplete()to forward to the nextChannelInboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
channelReadCompletein interfaceChannelInboundHandler- Overrides:
channelReadCompletein classChannelInboundHandlerAdapter- Throws:
java.lang.Exception
-
write
public final void write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise) throws java.lang.Exception
Description copied from class:ChannelDuplexHandlerCallsChannelOutboundInvoker.write(Object, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
writein interfaceChannelOutboundHandler- Overrides:
writein classChannelDuplexHandler- Parameters:
ctx- theChannelHandlerContextfor which the write operation is mademsg- the message to writepromise- theChannelPromiseto notify once the operation completes- Throws:
java.lang.Exception- thrown if an error occurs
-
flush
public final void flush(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelDuplexHandlerCallsChannelHandlerContext.flush()to forward to the nextChannelOutboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
flushin interfaceChannelOutboundHandler- Overrides:
flushin classChannelDuplexHandler- Parameters:
ctx- theChannelHandlerContextfor which the flush operation is made- Throws:
java.lang.Exception- thrown if an error occurs
-
readIfNeeded
private static void readIfNeeded(ChannelHandlerContext ctx)
-
writePendingWrites
private void writePendingWrites()
-
failPendingWrites
private void failPendingWrites(java.lang.Throwable cause)
-
addPendingWrite
private void addPendingWrite(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise)
-
-