You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by ed...@apache.org on 2009/04/17 15:28:00 UTC
svn commit: r765991 - in
/mina/trunk/core/src/main/java/org/apache/mina/proxy/handlers/socks:
Socks5LogicHandler.java SocksProxyRequest.java
Author: edeoliveira
Date: Fri Apr 17 13:28:00 2009
New Revision: 765991
URL: http://svn.apache.org/viewvc?rev=765991&view=rev
Log:
Added javadoc & minor fixes to the handling of InetSocketAddress vars
Modified:
mina/trunk/core/src/main/java/org/apache/mina/proxy/handlers/socks/Socks5LogicHandler.java
mina/trunk/core/src/main/java/org/apache/mina/proxy/handlers/socks/SocksProxyRequest.java
Modified: mina/trunk/core/src/main/java/org/apache/mina/proxy/handlers/socks/Socks5LogicHandler.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/proxy/handlers/socks/Socks5LogicHandler.java?rev=765991&r1=765990&r2=765991&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/proxy/handlers/socks/Socks5LogicHandler.java (original)
+++ mina/trunk/core/src/main/java/org/apache/mina/proxy/handlers/socks/Socks5LogicHandler.java Fri Apr 17 13:28:00 2009
@@ -85,7 +85,9 @@
}
/**
- * Perform the handshake process.
+ * Performs the handshake process.
+ *
+ * @param nextFilter the next filter
*/
public synchronized void doHandshake(final NextFilter nextFilter) {
logger.debug(" doHandshake()");
@@ -96,7 +98,7 @@
}
/**
- * Encode the initial greeting packet.
+ * Encodes the initial greeting packet.
*
* @param request the socks proxy request data
* @return the encoded buffer
@@ -113,7 +115,7 @@
}
/**
- * Encode the proxy authorization request packet.
+ * Encodes the proxy authorization request packet.
*
* @param request the socks proxy request data
* @return the encoded buffer
@@ -123,12 +125,10 @@
private IoBuffer encodeProxyRequestPacket(final SocksProxyRequest request)
throws UnsupportedEncodingException {
int len = 6;
- byte[] host = request.getHost() != null ? request.getHost().getBytes(
- "ASCII") : null;
-
InetSocketAddress adr = request.getEndpointAddress();
byte addressType = 0;
-
+ byte[] host = null;
+
if (adr != null && !adr.isUnresolved()) {
if (adr.getAddress() instanceof Inet6Address) {
len += 16;
@@ -138,10 +138,18 @@
addressType = SocksProxyConstants.IPV4_ADDRESS_TYPE;
}
} else {
- len += 1 + host.length;
- addressType = SocksProxyConstants.DOMAIN_NAME_ADDRESS_TYPE;
- }
+ host = request.getHost() != null ?
+ request.getHost().getBytes("ASCII") : null;
+ if (host != null) {
+ len += 1 + host.length;
+ addressType = SocksProxyConstants.DOMAIN_NAME_ADDRESS_TYPE;
+ } else {
+ throw new IllegalArgumentException("SocksProxyRequest object " +
+ "has no suitable endpoint information");
+ }
+ }
+
IoBuffer buf = IoBuffer.allocate(len);
buf.put(request.getProtocolVersion());
@@ -149,11 +157,11 @@
buf.put((byte) 0x00); // Reserved
buf.put(addressType);
- if (addressType == SocksProxyConstants.DOMAIN_NAME_ADDRESS_TYPE) {
- buf.put((byte) host.length);
- buf.put(host);
+ if (host == null) {
+ buf.put(request.getIpAddress());
} else {
- buf.put(request.getIpAddress());
+ buf.put((byte) host.length);
+ buf.put(host);
}
buf.put(request.getPort());
@@ -162,13 +170,13 @@
}
/**
- * Encode the authentication packet for supported authentication methods.
+ * Encodes the authentication packet for supported authentication methods.
*
* @param request the socks proxy request data
* @return the encoded buffer, if null then authentication step is over
* and handshake process can jump immediately to the next step without waiting
* for a server reply.
- * @throws UnsupportedEncodingException
+ * @throws UnsupportedEncodingException if some string charset convertion fails
* @throws GSSException when something fails while using GSSAPI
*/
private IoBuffer encodeAuthenticationPacket(final SocksProxyRequest request)
@@ -177,36 +185,36 @@
Socks5LogicHandler.SELECTED_AUTH_METHOD)).byteValue();
switch (method) {
- case SocksProxyConstants.NO_AUTH:
- // In this case authentication is immediately considered as successfull
- // Next writeRequest() call will send the proxy request
- getSession().setAttribute(HANDSHAKE_STEP,
- SocksProxyConstants.SOCKS5_REQUEST_STEP);
- break;
-
- case SocksProxyConstants.GSSAPI_AUTH:
- return encodeGSSAPIAuthenticationPacket(request);
-
- case SocksProxyConstants.BASIC_AUTH:
- // The basic auth scheme packet is sent
- byte[] user = request.getUserName().getBytes("ASCII");
- byte[] pwd = request.getPassword().getBytes("ASCII");
- IoBuffer buf = IoBuffer.allocate(3 + user.length + pwd.length);
-
- buf.put(SocksProxyConstants.BASIC_AUTH_SUBNEGOTIATION_VERSION);
- buf.put((byte) user.length);
- buf.put(user);
- buf.put((byte) pwd.length);
- buf.put(pwd);
-
- return buf;
+ case SocksProxyConstants.NO_AUTH:
+ // In this case authentication is immediately considered as successfull
+ // Next writeRequest() call will send the proxy request
+ getSession().setAttribute(HANDSHAKE_STEP,
+ SocksProxyConstants.SOCKS5_REQUEST_STEP);
+ break;
+
+ case SocksProxyConstants.GSSAPI_AUTH:
+ return encodeGSSAPIAuthenticationPacket(request);
+
+ case SocksProxyConstants.BASIC_AUTH:
+ // The basic auth scheme packet is sent
+ byte[] user = request.getUserName().getBytes("ASCII");
+ byte[] pwd = request.getPassword().getBytes("ASCII");
+ IoBuffer buf = IoBuffer.allocate(3 + user.length + pwd.length);
+
+ buf.put(SocksProxyConstants.BASIC_AUTH_SUBNEGOTIATION_VERSION);
+ buf.put((byte) user.length);
+ buf.put(user);
+ buf.put((byte) pwd.length);
+ buf.put(pwd);
+
+ return buf;
}
return null;
}
/**
- * Encode the authentication packet for supported authentication methods.
+ * Encodes the authentication packet for supported authentication methods.
*
* @param request the socks proxy request data
* @return the encoded buffer
@@ -216,6 +224,7 @@
final SocksProxyRequest request) throws GSSException {
GSSContext ctx = (GSSContext) getSession().getAttribute(GSS_CONTEXT);
if (ctx == null) {
+ // first step in the authentication process
GSSManager manager = GSSManager.getInstance();
GSSName serverName = manager.createName(request
.getServiceKerberosName(), null);
@@ -269,8 +278,7 @@
SocksProxyConstants.GSSAPI_AUTH_SUBNEGOTIATION_VERSION,
SocksProxyConstants.GSSAPI_MSG_TYPE });
- buf.put(ByteUtilities.intToNetworkByteOrder(token.length,
- new byte[2], 0, 2));
+ buf.put(ByteUtilities.intToNetworkByteOrder(token.length, 2));
buf.put(token);
}
}
@@ -279,7 +287,7 @@
}
/**
- * Encode a SOCKS5 request and writes it to the next filter
+ * Encodes a SOCKS5 request and writes it to the next filter
* so it can be sent to the proxy server.
*
* @param nextFilter the next filter
@@ -315,8 +323,11 @@
}
/**
- * Handle incoming data during the handshake process. Should consume only the
+ * Handles incoming data during the handshake process. Should consume only the
* handshake data from the buffer, leaving any extra data in place.
+ *
+ * @param nextFilter the next filter
+ * @param buf the buffered data received
*/
public synchronized void messageReceived(final NextFilter nextFilter,
final IoBuffer buf) {
@@ -330,7 +341,8 @@
"Wrong socks version running on server");
}
- if ((step == SocksProxyConstants.SOCKS5_GREETING_STEP || step == SocksProxyConstants.SOCKS5_AUTH_STEP)
+ if ((step == SocksProxyConstants.SOCKS5_GREETING_STEP ||
+ step == SocksProxyConstants.SOCKS5_AUTH_STEP)
&& buf.remaining() >= 2) {
handleResponse(nextFilter, buf, step);
} else if (step == SocksProxyConstants.SOCKS5_REQUEST_STEP
@@ -344,6 +356,10 @@
/**
* Handle a SOCKS v5 response from the proxy server.
+ *
+ * @param nextFilter the next filter
+ * @param buf the buffered data received
+ * @param step the current step in the authentication process
*/
protected void handleResponse(final NextFilter nextFilter,
final IoBuffer buf, int step) throws Exception {
@@ -354,7 +370,8 @@
if (method == SocksProxyConstants.NO_ACCEPTABLE_AUTH_METHOD) {
throw new IllegalStateException(
- "No acceptable authentication method to use the socks proxy server");
+ "No acceptable authentication method to use with " +
+ "the socks proxy server");
}
getSession().setAttribute(SELECTED_AUTH_METHOD, new Byte(method));
@@ -454,8 +471,11 @@
}
/**
- * {@inheritDoc}
- */
+ * Closes the session. If any {@link GSSContext} is present in the session
+ * then it is closed.
+ *
+ * @param message the error message
+ */
@Override
protected void closeSession(String message) {
GSSContext ctx = (GSSContext) getSession().getAttribute(GSS_CONTEXT);
Modified: mina/trunk/core/src/main/java/org/apache/mina/proxy/handlers/socks/SocksProxyRequest.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/proxy/handlers/socks/SocksProxyRequest.java?rev=765991&r1=765990&r2=765991&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/proxy/handlers/socks/SocksProxyRequest.java (original)
+++ mina/trunk/core/src/main/java/org/apache/mina/proxy/handlers/socks/SocksProxyRequest.java Fri Apr 17 13:28:00 2009
@@ -101,7 +101,7 @@
}
/**
- * Return the endpoint address resulting from the {@link #getEndpointAddress()}.
+ * Returns the endpoint address resulting from the {@link #getEndpointAddress()}.
* If not set, it will return the {@link SocksProxyConstants#FAKE_IP} constant
* value which will be ignored in a SOCKS v4 request.
*
@@ -162,7 +162,8 @@
*/
public synchronized final String getHost() {
if (host == null) {
- if (getEndpointAddress() != null) {
+ InetSocketAddress adr = getEndpointAddress();
+ if ( adr != null && !adr.isUnresolved()) {
host = getEndpointAddress().getHostName();
}
}