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:
io.netty.channel.ChannelHandler
,io.netty.channel.ChannelInboundHandler
,io.netty.channel.ChannelOutboundHandler
- Direct Known Subclasses:
HttpProxyHandler
,Socks4ProxyHandler
,Socks5ProxyHandler
public abstract class ProxyHandler extends io.netty.channel.ChannelDuplexHandler
A common abstraction for protocols that establish blind forwarding proxy tunnels.
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
ProxyHandler(SocketAddress proxyAddress)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract void
addCodec(io.netty.channel.ChannelHandlerContext ctx)
Adds the codec handlers required to communicate with the proxy server.abstract String
authScheme()
Returns the name of the authentication scheme in use.void
channelActive(io.netty.channel.ChannelHandlerContext ctx)
void
channelInactive(io.netty.channel.ChannelHandlerContext ctx)
void
channelRead(io.netty.channel.ChannelHandlerContext ctx, Object msg)
void
channelReadComplete(io.netty.channel.ChannelHandlerContext ctx)
void
connect(io.netty.channel.ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, io.netty.channel.ChannelPromise promise)
io.netty.util.concurrent.Future<io.netty.channel.Channel>
connectFuture()
Returns aFuture
that is notified when the connection to the destination has been established or the connection attempt has failed.long
connectTimeoutMillis()
Returns the connect timeout in millis.<T extends SocketAddress>
TdestinationAddress()
Returns the address of the destination to connect to via the proxy server.void
exceptionCaught(io.netty.channel.ChannelHandlerContext ctx, Throwable cause)
protected String
exceptionMessage(String msg)
Decorates the specified exception message with the common information such as the current protocol, authentication scheme, proxy address, and destination address.void
flush(io.netty.channel.ChannelHandlerContext ctx)
void
handlerAdded(io.netty.channel.ChannelHandlerContext ctx)
protected abstract boolean
handleResponse(io.netty.channel.ChannelHandlerContext ctx, Object response)
Handles the message received from the proxy server.boolean
isConnected()
Returnstrue
if and only if the connection to the destination has been established successfully.protected abstract Object
newInitialMessage(io.netty.channel.ChannelHandlerContext ctx)
Returns a new message that is sent at first time when the connection to the proxy server has been established.abstract String
protocol()
Returns the name of the proxy protocol in use.<T extends SocketAddress>
TproxyAddress()
Returns the address of the proxy server.protected abstract void
removeDecoder(io.netty.channel.ChannelHandlerContext ctx)
Removes the decoders added inaddCodec(ChannelHandlerContext)
.protected abstract void
removeEncoder(io.netty.channel.ChannelHandlerContext ctx)
Removes the encoders added inaddCodec(ChannelHandlerContext)
.protected void
sendToProxyServer(Object msg)
Sends the specified message to the proxy server.void
setConnectTimeoutMillis(long connectTimeoutMillis)
Sets the connect timeout in millis.void
write(io.netty.channel.ChannelHandlerContext ctx, Object msg, io.netty.channel.ChannelPromise promise)
-
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
-
-
-
-
Constructor Detail
-
ProxyHandler
protected ProxyHandler(SocketAddress proxyAddress)
-
-
Method Detail
-
protocol
public abstract String protocol()
Returns the name of the proxy protocol in use.
-
authScheme
public abstract String authScheme()
Returns the name of the authentication scheme in use.
-
proxyAddress
public final <T extends SocketAddress> T proxyAddress()
Returns the address of the proxy server.
-
destinationAddress
public final <T extends SocketAddress> T destinationAddress()
Returns the address of the destination to connect to via the proxy server.
-
isConnected
public final boolean isConnected()
Returnstrue
if and only if the connection to the destination has been established successfully.
-
connectFuture
public final io.netty.util.concurrent.Future<io.netty.channel.Channel> connectFuture()
Returns aFuture
that 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(io.netty.channel.ChannelHandlerContext ctx) throws Exception
- Specified by:
handlerAdded
in interfaceio.netty.channel.ChannelHandler
- Overrides:
handlerAdded
in classio.netty.channel.ChannelHandlerAdapter
- Throws:
Exception
-
addCodec
protected abstract void addCodec(io.netty.channel.ChannelHandlerContext ctx) throws Exception
Adds the codec handlers required to communicate with the proxy server.- Throws:
Exception
-
removeEncoder
protected abstract void removeEncoder(io.netty.channel.ChannelHandlerContext ctx) throws Exception
Removes the encoders added inaddCodec(ChannelHandlerContext)
.- Throws:
Exception
-
removeDecoder
protected abstract void removeDecoder(io.netty.channel.ChannelHandlerContext ctx) throws Exception
Removes the decoders added inaddCodec(ChannelHandlerContext)
.- Throws:
Exception
-
connect
public final void connect(io.netty.channel.ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, io.netty.channel.ChannelPromise promise) throws Exception
- Specified by:
connect
in interfaceio.netty.channel.ChannelOutboundHandler
- Overrides:
connect
in classio.netty.channel.ChannelDuplexHandler
- Throws:
Exception
-
channelActive
public final void channelActive(io.netty.channel.ChannelHandlerContext ctx) throws Exception
- Specified by:
channelActive
in interfaceio.netty.channel.ChannelInboundHandler
- Overrides:
channelActive
in classio.netty.channel.ChannelInboundHandlerAdapter
- Throws:
Exception
-
newInitialMessage
protected abstract Object newInitialMessage(io.netty.channel.ChannelHandlerContext ctx) throws 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
null
if the proxy server is expected to send the first message instead - Throws:
Exception
-
sendToProxyServer
protected final void sendToProxyServer(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(io.netty.channel.ChannelHandlerContext ctx) throws Exception
- Specified by:
channelInactive
in interfaceio.netty.channel.ChannelInboundHandler
- Overrides:
channelInactive
in classio.netty.channel.ChannelInboundHandlerAdapter
- Throws:
Exception
-
exceptionCaught
public final void exceptionCaught(io.netty.channel.ChannelHandlerContext ctx, Throwable cause) throws Exception
- Specified by:
exceptionCaught
in interfaceio.netty.channel.ChannelHandler
- Specified by:
exceptionCaught
in interfaceio.netty.channel.ChannelInboundHandler
- Overrides:
exceptionCaught
in classio.netty.channel.ChannelInboundHandlerAdapter
- Throws:
Exception
-
channelRead
public final void channelRead(io.netty.channel.ChannelHandlerContext ctx, Object msg) throws Exception
- Specified by:
channelRead
in interfaceio.netty.channel.ChannelInboundHandler
- Overrides:
channelRead
in classio.netty.channel.ChannelInboundHandlerAdapter
- Throws:
Exception
-
handleResponse
protected abstract boolean handleResponse(io.netty.channel.ChannelHandlerContext ctx, Object response) throws Exception
Handles the message received from the proxy server.- Returns:
true
if the connection to the destination has been established,false
if the connection to the destination has not been established and more messages are expected from the proxy server- Throws:
Exception
-
exceptionMessage
protected final String exceptionMessage(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(io.netty.channel.ChannelHandlerContext ctx) throws Exception
- Specified by:
channelReadComplete
in interfaceio.netty.channel.ChannelInboundHandler
- Overrides:
channelReadComplete
in classio.netty.channel.ChannelInboundHandlerAdapter
- Throws:
Exception
-
write
public final void write(io.netty.channel.ChannelHandlerContext ctx, Object msg, io.netty.channel.ChannelPromise promise) throws Exception
- Specified by:
write
in interfaceio.netty.channel.ChannelOutboundHandler
- Overrides:
write
in classio.netty.channel.ChannelDuplexHandler
- Throws:
Exception
-
-