📄 csocks5.java
字号:
REPLY[4] = IP[0];
REPLY[5] = IP[1];
REPLY[6] = IP[2];
REPLY[7] = IP[3];
REPLY[8] = (byte)((PT & 0xFF00) >> 8);
REPLY[9] = (byte) (PT & 0x00FF);
if( m_Parent.isActive() ) {
m_Parent.SendToClient( REPLY );
}
else {
Log.Println( "BIND - Closed Client Connection" );
}
} // BIND_Reply()
/////////////////////////////////////////////////////////////
/* protected void Bind() throws EXNetServer, IOException {
super.Bind();
} // BIND ...
*/
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
public void UDP_Reply( byte ReplyCode, InetAddress IA, int PT )
throws IOException {
Log.Println( "Reply to Client \"" + ReplyName( ReplyCode )+"\"" );
if( m_Parent.m_ClientSocket == null ) {
Log.Println( "Error in UDP_Reply() - Client socket is NULL" );
}
byte[] IP = IA.getAddress();
byte[] REPLY = new byte[10];
REPLY[0] = SOCKS5_Version;
REPLY[1] = ReplyCode; // Reply Code;
REPLY[2] = 0x00; // Reserved '00'
REPLY[3] = 0x01; // Address Type IP v4
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 UDP() throws IOException {
// Connect to the Remote Host
try {
DGSocket = new DatagramSocket();
Init_UDP_InOut();
}
catch( IOException e ) {
Refuse_Command( (byte)0x05 ); // Connection Refused
throw new IOException( "Connection Refused - FAILED TO INITIALIZE UDP Association." );
}
InetAddress MyIP = m_Parent.m_ClientSocket.getLocalAddress();
int MyPort = DGSocket.getLocalPort();
// Return response to the Client
// Code '00' - Connection Succeeded,
// IP/Port where Server will listen
UDP_Reply( (byte)0, MyIP, MyPort );
Log.Println( "UDP Listen at: <"+MyIP.toString()+":"+MyPort+">" );
while( m_Parent.CheckClientData() >= 0 )
{
ProcessUDP();
Thread.yield();
}
Log.Println( "UDP - Closed TCP Master of UDP Association" );
} // UDP ...
/////////////////////////////////////////////////////////////
private void Init_UDP_InOut() throws IOException {
DGSocket.setSoTimeout ( m_Parent.DEFAULT_TIMEOUT );
m_Parent.m_Buffer = new byte[ m_Parent.m_BufLen ];
DGPack = new DatagramPacket( m_Parent.m_Buffer, m_Parent.m_BufLen );
}
/////////////////////////////////////////////////////////////
private byte[] AddDGPhead( byte[] Buffer ) {
int bl = Buffer.length;
byte IABuf[] = DGPack.getAddress().getAddress();
int DGport = DGPack.getPort();
int HeaderLen = 6 + IABuf.length;
int DataLen = DGPack.getLength();
int NewPackLen = HeaderLen + DataLen;
byte UB[] = new byte[ NewPackLen ];
UB[0] = (byte)0x00; // Reserved 0x00
UB[1] = (byte)0x00; // Reserved 0x00
UB[2] = (byte)0x00; // FRAG '00' - Standalone DataGram
UB[3] = (byte)0x01; // Address Type -->'01'-IP v4
System.arraycopy( IABuf,0, UB,4, IABuf.length );
UB[4+IABuf.length] = (byte)((DGport >> 8) & 0xFF);
UB[5+IABuf.length] = (byte)((DGport ) & 0xFF);
System.arraycopy( Buffer,0, UB, 6+IABuf.length, DataLen );
System.arraycopy( UB,0, Buffer,0, NewPackLen );
return UB;
} // AddDGPhead()
/////////////////////////////////////////////////////////////
private byte[] ClearDGPhead( byte[] Buffer ) {
int IAlen = 0;
int bl = Buffer.length;
int p = 4; // First byte of IP Address
byte AType = Buffer[3]; // IP Address Type
switch( AType ) {
case 0x01: IAlen = 4; break;
case 0x03: IAlen = Buffer[p]+1; break; // One for Size Byte
default : Log.Println( "Error in ClearDGPhead() - Invalid Destination IP Addres type " + AType );
return null;
}
byte IABuf[] = new byte[IAlen];
System.arraycopy( Buffer, p, IABuf, 0, IAlen );
p += IAlen;
UDP_IA = calcInetAddress( AType , IABuf );
UDP_port = calcPort( Buffer[p++], Buffer[p++] );
if( UDP_IA == null ) {
Log.Println( "Error in ClearDGPHead() - Invalid UDP dest IP address: NULL" );
return null;
}
int DataLen = DGPack.getLength();
DataLen -= p; // <p> is length of UDP Header
byte UB[] = new byte[ DataLen ];
System.arraycopy( Buffer,p, UB,0, DataLen );
System.arraycopy( UB,0, Buffer,0, DataLen );
return UB;
} // ClearDGPhead()
/////////////////////////////////////////////////////////////
protected void UDPSend( DatagramPacket DGP ) {
if( DGP == null ) return;
String LogString = DGP.getAddress()+ ":" +
DGP.getPort() + "> : " +
DGP.getLength() + " bytes";
try {
DGSocket.send( DGP );
}
catch( IOException e ) {
Log.Println( "Error in ProcessUDPClient() - Failed to Send DGP to "+ LogString );
return;
}
}
/////////////////////////////////////////////////////////////
public void ProcessUDP() {
// Trying to Receive DataGram
try {
DGSocket.receive( DGPack );
}
catch( InterruptedIOException e ) {
return; // Time Out
}
catch( IOException e ) {
Log.Println( "Error in ProcessUDP() - "+ e.toString() );
return;
}
if( m_ClientIP.equals( DGPack.getAddress() ) ) {
ProcessUDPClient();
}
else {
ProcessUDPRemote();
}
try {
Init_UDP_InOut(); // Clean DGPack & Buffer
}
catch( IOException e ) {
Log.Println( "IOError in Init_UDP_IO() - "+ e.toString() );
m_Parent.Close();
}
} // ProcessUDP()...
/////////////////////////////////////////////////////////////
/** Processing Client's datagram
* This Method must be called only from <ProcessUDP()>
*/
public void ProcessUDPClient() {
m_nClientPort = DGPack.getPort();
// Also calculates UDP_IA & UDP_port ...
byte[] Buf = ClearDGPhead( DGPack.getData() );
if( Buf == null ) return;
if( Buf.length <= 0 ) return;
if( UDP_IA == null ) {
Log.Println( "Error in ProcessUDPClient() - Invalid Destination IP - NULL" );
return;
}
if( UDP_port == 0 ) {
Log.Println( "Error in ProcessUDPClient() - Invalid Destination Port - 0" );
return;
}
if( m_ServerIP != UDP_IA || m_nServerPort != UDP_port ) {
m_ServerIP = UDP_IA;
m_nServerPort = UDP_port;
}
Log.Println( "Datagram : "+ Buf.length + " bytes : "+Log.getSocketInfo( DGPack )+
" >> <" + Log.IP2Str( m_ServerIP )+":"+m_nServerPort+">" );
DatagramPacket DGPSend = new DatagramPacket( Buf, Buf.length,
UDP_IA, UDP_port );
UDPSend( DGPSend );
}
/////////////////////////////////////////////////////////////
public void ProcessUDPRemote() {
Log.Println( "Datagram : "+ DGPack.getLength()+" bytes : "+
"<"+Log.IP2Str( m_ClientIP )+":"+m_nClientPort+"> << " +
Log.getSocketInfo( DGPack ) );
// This Method must be CALL only from <ProcessUDP()>
// ProcessUDP() Reads a Datagram packet <DGPack>
InetAddress DGP_IP = DGPack.getAddress();
int DGP_Port= DGPack.getPort();
byte[] Buf;
Buf = AddDGPhead( m_Parent.m_Buffer );
if( Buf == null ) return;
// SendTo Client
DatagramPacket DGPSend = new DatagramPacket( Buf, Buf.length,
m_ClientIP, m_nClientPort );
UDPSend( DGPSend );
if( DGP_IP != UDP_IA || DGP_Port != UDP_port ) {
m_ServerIP = DGP_IP;
m_nServerPort = DGP_Port;
}
}
/////////////////////////////////////////////////////////////
}
/////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -