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
A common abstraction for protocols that establish blind forwarding proxy tunnels.
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable -
Field Summary
FieldsModifier and TypeFieldDescription(package private) static final StringA string that signifies 'no authentication' or 'anonymous'.private final ProxyHandler.LazyChannelPromiseprivate Future<?> private longprivate ChannelHandlerContextprivate static final longThe default connect timeout: 10 seconds.private SocketAddressprivate booleanprivate booleanprivate static final InternalLoggerprivate PendingWriteQueueprivate final SocketAddressprivate booleanprivate final ChannelFutureListener -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract voidAdds the codec handlers required to communicate with the proxy server.private voidaddPendingWrite(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) abstract StringReturns the name of the authentication scheme in use.private voidfinal voidCallsChannelHandlerContext.fireChannelActive()to forward to the nextChannelInboundHandlerin theChannelPipeline.final voidCallsChannelHandlerContext.fireChannelInactive()to forward to the nextChannelInboundHandlerin theChannelPipeline.final voidchannelRead(ChannelHandlerContext ctx, Object msg) CallsChannelHandlerContext.fireChannelRead(Object)to forward to the nextChannelInboundHandlerin theChannelPipeline.final voidCallsChannelHandlerContext.fireChannelReadComplete()to forward to the nextChannelInboundHandlerin theChannelPipeline.final voidconnect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) CallsChannelOutboundInvoker.connect(SocketAddress, SocketAddress, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.Returns aFuturethat is notified when the connection to the destination has been established or the connection attempt has failed.final longReturns the connect timeout in millis.final <T extends SocketAddress>
TReturns the address of the destination to connect to via the proxy server.final voidexceptionCaught(ChannelHandlerContext ctx, Throwable cause) CallsChannelHandlerContext.fireExceptionCaught(Throwable)to forward to the nextChannelHandlerin theChannelPipeline.protected final StringexceptionMessage(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(Throwable cause) private voidfinal voidCallsChannelHandlerContext.flush()to forward to the nextChannelOutboundHandlerin theChannelPipeline.final voidDo nothing by default, sub-classes may override this method.protected abstract booleanhandleResponse(ChannelHandlerContext ctx, Object response) Handles the message received from the proxy server.final booleanReturnstrueif and only if the connection to the destination has been established successfully.protected abstract ObjectReturns a new message that is sent at first time when the connection to the proxy server has been established.abstract Stringprotocol()Returns the name of the proxy protocol in use.final <T extends SocketAddress>
TReturns the address of the proxy server.private static voidprotected abstract voidRemoves the decoders added inaddCodec(ChannelHandlerContext).protected abstract voidRemoves the encoders added inaddCodec(ChannelHandlerContext).private booleanprivate booleanprivate voidSends the initial message to be sent to the proxy server.protected final voidsendToProxyServer(Object msg) Sends the specified message to the proxy server.private voidsetConnectFailure(Throwable cause) private voidfinal voidsetConnectTimeoutMillis(long connectTimeoutMillis) Sets the connect timeout in millis.final voidwrite(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) CallsChannelOutboundInvoker.write(Object, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.private voidMethods inherited from class io.netty.channel.ChannelDuplexHandler
bind, close, deregister, disconnect, readMethods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelRegistered, channelUnregistered, channelWritabilityChanged, userEventTriggeredMethods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, 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
handlerRemoved
-
Field Details
-
logger
-
DEFAULT_CONNECT_TIMEOUT_MILLIS
private static final long DEFAULT_CONNECT_TIMEOUT_MILLISThe default connect timeout: 10 seconds.- See Also:
-
AUTH_NONE
A string that signifies 'no authentication' or 'anonymous'.- See Also:
-
proxyAddress
-
destinationAddress
-
connectTimeoutMillis
private volatile long connectTimeoutMillis -
ctx
-
pendingWrites
-
finished
private boolean finished -
suppressChannelReadComplete
private boolean suppressChannelReadComplete -
flushedPrematurely
private boolean flushedPrematurely -
connectPromise
-
connectTimeoutFuture
-
writeListener
-
-
Constructor Details
-
ProxyHandler
-
-
Method Details
-
protocol
Returns the name of the proxy protocol in use. -
authScheme
Returns the name of the authentication scheme in use. -
proxyAddress
Returns the address of the proxy server. -
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
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
Description copied from class:ChannelHandlerAdapterDo nothing by default, sub-classes may override this method.- Specified by:
handlerAddedin interfaceChannelHandler- Overrides:
handlerAddedin classChannelHandlerAdapter- Throws:
Exception
-
addCodec
Adds the codec handlers required to communicate with the proxy server.- Throws:
Exception
-
removeEncoder
Removes the encoders added inaddCodec(ChannelHandlerContext).- Throws:
Exception
-
removeDecoder
Removes the decoders added inaddCodec(ChannelHandlerContext).- Throws:
Exception
-
connect
public final void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws 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:
Exception- thrown if an error occurs
-
channelActive
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:
Exception
-
sendInitialMessage
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:
Exception
-
newInitialMessage
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:
Exception
-
sendToProxyServer
Sends the specified message to the proxy server. Use this method to send a response to the proxy server inhandleResponse(ChannelHandlerContext, Object). -
channelInactive
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:
Exception
-
exceptionCaught
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:
Exception
-
channelRead
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:
Exception
-
handleResponse
protected abstract boolean handleResponse(ChannelHandlerContext ctx, Object response) throws 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:
Exception
-
setConnectSuccess
private void setConnectSuccess() -
safeRemoveDecoder
private boolean safeRemoveDecoder() -
safeRemoveEncoder
private boolean safeRemoveEncoder() -
setConnectFailure
-
failPendingWritesAndClose
-
cancelConnectTimeoutFuture
private void cancelConnectTimeoutFuture() -
exceptionMessage
Decorates the specified exception message with the common information such as the current protocol, authentication scheme, proxy address, and destination address. -
channelReadComplete
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:
Exception
-
write
public final void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws 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:
Exception- thrown if an error occurs
-
flush
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:
Exception- thrown if an error occurs
-
readIfNeeded
-
writePendingWrites
private void writePendingWrites() -
failPendingWrites
-
addPendingWrite
-