📄 socks5utils.java
字号:
package com.valhalla.jbother.jabber.smack;import java.nio.ByteBuffer;/** * Created by luke on Feb 8, 2005 11:11:13 AM *//** * helper class making it easier to deal with SOCKS5 connections and authentication * <b>NOTE:</b> not everything is in here. Right now only direct connections are supported * (ie. not using SOCKS5 proxies). */public class Socks5Utils{ private static String fId = "$Id$"; public static final int SOCKS_VERSION = 5; public static final int SOCKS_ATYP_IPV4 = 0x1; public static final int SOCKS_ATYP_DOMAINNAME = 0x3; public static final int SOCKS_ATYP_IPV6 = 0x4; public static final int SOCKS_COMMAND_CONNECT = 0x1; public static final int SOCKS_COMMAND_QUERY_AUTH_METHODS = 0x2; public static final int SOCKS_AUTH_NOAUTH = 0x0; public static final int SOCKS_AUTH_USERPASS = 0x2; /** * @return SOCKS5 CONNECT message with hostname and port as given */ public static byte[] createSOCKS5AuthenticationMessage(String hostname, int port) { byte[] data; byte[] addr; addr = hostname.getBytes(); data = new byte[7 + addr.length]; data[0] = (byte) SOCKS_VERSION; // SOCKS version data[1] = (byte) SOCKS_COMMAND_CONNECT; // command data[2] = (byte) 0; // reserved byte data[3] = (byte) SOCKS_ATYP_DOMAINNAME; // address type data[4] = (byte) addr.length; // length of the address // concat address System.arraycopy(addr,0,data,5,addr.length); // put port data[data.length-2] = (byte)(port >> 8); data[data.length-1] = (byte)(port); return data; } /** * @return SOCKS5 message with authentication methods */ public static byte[] createSOCKS5AuthenticationMethodsMessage() { byte[] authmethods = new byte[4]; authmethods[0] = (byte) SOCKS_VERSION; authmethods[1] = (byte) SOCKS_COMMAND_QUERY_AUTH_METHODS; authmethods[2] = (byte) SOCKS_AUTH_NOAUTH; authmethods[3] = (byte) SOCKS_AUTH_USERPASS; return authmethods; } public static byte[] createSOCKS5QueryAuthenticationMethodsMessage() { byte[] msg = new byte[2]; msg[0] = (byte) SOCKS_VERSION; msg[1] = (byte) SOCKS_COMMAND_QUERY_AUTH_METHODS; return msg; } public static byte[] createSOCKS5NoAuthMethodMessage() { byte[] msg = new byte[2]; msg[0] = (byte) SOCKS_VERSION; msg[1] = (byte) SOCKS_AUTH_NOAUTH; return msg; } public static boolean providesSOCKS5NoAuthMethod(ByteBuffer buffer) { int numberOfMethods = buffer.getInt(1); for(int i=2;i<numberOfMethods;i++) { if(buffer.get(i) == SOCKS_AUTH_NOAUTH) { return true; } } return false; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -