📄 csocks5.java
字号:
/*************************************************************************
FILE : CSock5.java
Author : Svetoslav Tchekanov (swetoslav@iname.com)
Description: CSock5 class definition.
CSock5.class is the implementation of Socks5 copmmands
Copyright notice:
Written by Svetoslav Tchekanov (swetoslav@iname.com)
Copyright(c) 2000
This code may be used in compiled form in any way you desire. This
file may be redistributed unmodified by any means PROVIDING it is
not sold for profit without the authors written consent, and
providing that this notice and the authors name is included. If
the source code in this file is used in any commercial application
then a simple email would be nice.
This file is provided "as is" with no expressed or implied warranty.
The author accepts no liability if it causes any damage to your
computer.
*************************************************************************/
package socksshttp;
///////////////////////////////////////////////
import java.math.*;
import java.io.*;
import java.net.*;
///////////////////////////////////////////////
public class CSocks5 extends CSocks4
{
final byte SOCKS5_Version = 0x05;
static final int MaxAddrLen = 255;
static final byte SC_UDP = 0x03;
protected DatagramSocket DGSocket= null;
protected DatagramPacket DGPack = null;
private InetAddress UDP_IA = null;
private int UDP_port = 0;
//--- Reply Codes ---
protected byte getSuccessCode() { return 00; }
protected byte getFailCode() { return 04; }
//-------------------
// public byte SOCKS_Version; // Version of SOCKS
// public byte Command; // Command code
public byte RSV; // Reserved.Must be'00'
public byte ATYP; // Address Type
// public byte[] DST_Addr; // Destination Address
// public byte[] DST_Port; // Destination Port
// in Network order
static final int ADDR_Size[]={ -1, //'00' No such AType
4, //'01' IP v4 - 4Bytes
-1, //'02' No such AType
-1, //'03' First Byte is Len
16 //'04' IP v6 - 16bytes
};
static final byte SRE_Accept[] = { (byte)0x05, (byte)0x00 };
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
public CSocks5( CProxy Parent ) {
super( Parent );
DST_Addr = new byte[MaxAddrLen]; // 20 za vseki sluchay.
}
/////////////////////////////////////////////////////////////////
public InetAddress calcInetAddress( byte AType, byte[] addr ) {
InetAddress IA = null;
switch( AType ) {
// Version IP 4
case 0x01: IA = super.calcInetAddress( addr );
break;
// Version IP DOMAIN NAME
case 0x03: if( addr[0] <= 0 ) {
Log.Error( "SOCKS 5 - calcInetAddress() : BAD IP in command - size : " + addr[0] );
return null;
}
String sIA= "";
for( int i = 1; i <= addr[0]; i++ ) {
sIA += (char)addr[i];
}
try {
IA = InetAddress.getByName( sIA );
}
catch( UnknownHostException e ) {
return null;
}
break;
default: return null;
}
return IA;
} // calcInetAddress()
/////////////////////////////////////////////////////////////////
public boolean Calculate_Address() {
m_ServerIP = calcInetAddress( ATYP, DST_Addr );
m_nServerPort = calcPort( DST_Port[0], DST_Port[1] );
m_ClientIP = m_Parent.m_ClientSocket.getInetAddress();
m_nClientPort = m_Parent.m_ClientSocket.getPort();
return ( (m_ServerIP != null) && (m_nServerPort >= 0) );
}
/////////////////////////////////////////////////////////////////
public void Authenticate( byte SOCKS_Ver )
throws Exception {
super.Authenticate( SOCKS_Ver ); // Sets SOCKS Version...
if( SOCKS_Version == SOCKS5_Version ) {
if( !CheckAuthentication() ) {// It reads whole Cli Request
Refuse_Authentication("SOCKS 5 - Not Supported Authentication!");
throw new Exception("SOCKS 5 - Not Supported Authentication.");
}
Accept_Authentication();
}// if( SOCKS_Version...
else {
Refuse_Authentication( "Incorrect SOCKS version : "+SOCKS_Version );
throw new Exception( "Not Supported SOCKS Version -'"+
SOCKS_Version + "'");
}
} // Authenticate()
/////////////////////////////////////////////////////////////
public void Refuse_Authentication( String msg ) {
Log.Println( "SOCKS 5 - Refuse Authentication: '"+msg+"'" );
m_Parent.SendToClient( SRE_Refuse );
}
/////////////////////////////////////////////////////////////
public void Accept_Authentication() {
Log.Println( "SOCKS 5 - Accepts Auth. method 'NO_AUTH'" );
SRE_Accept[0] = SOCKS_Version;
m_Parent.SendToClient( SRE_Accept );
}
/////////////////////////////////////////////////////////////
public boolean CheckAuthentication()
throws Exception {
boolean Have_NoAuthentication = false;
byte Methods_Num = GetByte();
String Methods = "";
for( int i=0; i<Methods_Num; i++ ) {
Methods += ",-" + GetByte() + '-';
}
return ( (Methods.indexOf( "-0-" ) != -1) ||
(Methods.indexOf( "-00-" ) != -1) );
}
/////////////////////////////////////////////////////////////
public void GetClientCommand()
throws Exception
{
int Addr_Len;
SOCKS_Version = GetByte();
Command = GetByte();
RSV = GetByte();
ATYP = GetByte();
Addr_Len = ADDR_Size[ATYP];
DST_Addr[0] = GetByte();
if( ATYP==0x03 ) {
Addr_Len = DST_Addr[0]+1;
}
for( int i=1; i<Addr_Len; i++ ) {
DST_Addr[i]= GetByte();
}
DST_Port[0] = GetByte();
DST_Port[1] = GetByte();
if( SOCKS_Version != SOCKS5_Version ) {
Log.Println( "SOCKS 5 - Incorrect SOCKS Version of Command: "+
SOCKS_Version );
Refuse_Command( (byte)0xFF );
throw new Exception("Incorrect SOCKS Version of Command: "+
SOCKS_Version);
}
if( (Command < SC_CONNECT) || (Command > SC_UDP) ) {
Log.Error( "SOCKS 5 - GetClientCommand() - Unsupported Command : \"" + commName( Command )+"\"" );
Refuse_Command( (byte)0x07 );
throw new Exception("SOCKS 5 - Unsupported Command: \"" + Command +"\"" );
}
if( ATYP == 0x04 ) {
Log.Error( "SOCKS 5 - GetClientCommand() - Unsupported Address Type - IP v6" );
Refuse_Command( (byte)0x08 );
throw new Exception( "Unsupported Address Type - IP v6" );
}
if( (ATYP >= 0x04) || (ATYP <=0) ) {
Log.Error( "SOCKS 5 - GetClientCommand() - Unsupported Address Type: " + ATYP );
Refuse_Command( (byte)0x08 );
throw new Exception( "SOCKS 5 - Unsupported Address Type: " + ATYP );
}
if( !Calculate_Address() ) { // Gets the IP Address
Refuse_Command( (byte)0x04 );// Host Not Exists...
throw new Exception( "SOCKS 5 - Unknown Host/IP address '" + m_ServerIP.toString()+"'" );
}
Log.Println( "SOCKS 5 - Accepted SOCKS5 Command: \""+commName(Command)+"\"" );
} // GetClientCommand()
/////////////////////////////////////////////////////////////
public void Reply_Command( byte ReplyCode ) {
Log.Println( "SOCKS 5 - Reply to Client \"" + ReplyName(ReplyCode)+"\"" );
int pt = 0;
String DN = "0.0.0.0";
InetAddress IA = null;
byte[] REPLY = new byte[10];
byte IP[] = new byte[4];
if( m_Parent.m_ServerSocket != null ) {
IA = m_Parent.m_ServerSocket.getInetAddress();
DN = IA.toString();
pt = m_Parent.m_ServerSocket.getLocalPort();
}
else {
IP[0]=0;
IP[1]=0;
IP[2]=0;
IP[3]=0;
pt = 0;
}
REPLY[0] = SOCKS5_Version;
REPLY[1] = ReplyCode; // Reply Code;
REPLY[2] = 0x00; // Reserved '00'
REPLY[3] = 0x01; // DOMAIN NAME Type IP ver.4
REPLY[4]= IP[0];
REPLY[5]= IP[1];
REPLY[6]= IP[2];
REPLY[7]= IP[3];
REPLY[8] = (byte)((pt & 0xFF00) >> 8);// Port High
REPLY[9] = (byte) (pt & 0x00FF); // Port Low
m_Parent.SendToClient( REPLY );// BND.PORT
} // Reply_Command()
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
public void BIND_Reply( byte ReplyCode, InetAddress IA, int PT )
{
byte IP[] = {0,0,0,0};
Log.Println( "BIND Reply to Client \"" + ReplyName( ReplyCode )+"\"" );
byte[] REPLY = new byte[10];
if( IA != null ) IP = IA.getAddress();
REPLY[0] = SOCKS5_Version;
REPLY[1] = (byte)((int)ReplyCode - 90); // Reply Code;
REPLY[2] = 0x00; // Reserved '00'
REPLY[3] = 0x01; // IP ver.4 Type
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -