📄 socks5datagramsocket.java
字号:
super.receive(dp);
}
//Restore timeout settings
if(initTimeout != 0) setSoTimeout(initTimeout);
}else if(!relayIP.equals(dp.getAddress()) ||
relayPort != dp.getPort())
return; // Recieved direct packet
//If the datagram is not from the relay server, return it it as is.
byte[] data;
data = dp.getData();
if(encapsulation != null)
data = encapsulation.udpEncapsulate(data,false);
int offset = 0; //Java 1.1
//int offset = dp.getOffset(); //Java 1.2
ByteArrayInputStream bIn = new ByteArrayInputStream(data,offset,
dp.getLength());
ProxyMessage msg = new Socks5Message(bIn);
dp.setPort(msg.port);
dp.setAddress(msg.getInetAddress());
//what wasn't read by the Message is the data
int data_length = bIn.available();
//Shift data to the left
System.arraycopy(data,offset+dp.getLength()-data_length,
data,offset,data_length);
dp.setLength(data_length);
}
/**
* Returns port assigned by the proxy, to which datagrams are relayed.
* It is not the same port to which other party should send datagrams.
@return Port assigned by socks server to which datagrams are send
for association.
*/
public int getLocalPort(){
if(server_mode) return super.getLocalPort();
return relayPort;
}
/**
* Address assigned by the proxy, to which datagrams are send for relay.
* It is not necesseraly the same address, to which other party should send
* datagrams.
@return Address to which datagrams are send for association.
*/
public InetAddress getLocalAddress(){
if(server_mode) return super.getLocalAddress();
return relayIP;
}
/**
* Closes datagram socket, and proxy connection.
*/
public void close(){
if(!server_mode) proxy.endSession();
super.close();
}
/**
This method checks wether proxy still runs udp forwarding service
for this socket.
<p>
This methods checks wether the primary connection to proxy server
is active. If it is, chances are that proxy continues to forward
datagrams being send from this socket. If it was closed, most likely
datagrams are no longer being forwarded by the server.
<p>
Proxy might decide to stop forwarding datagrams, in which case it
should close primary connection. This method allows to check, wether
this have been done.
<p>
You can specify timeout for which we should be checking EOF condition
on the primary connection. Timeout is in milliseconds. Specifying 0 as
timeout implies infinity, in which case method will block, until
connection to proxy is closed or an error happens, and then return false.
<p>
One possible scenario is to call isProxyactive(0) in separate thread,
and once it returned notify other threads about this event.
@param timeout For how long this method should block, before returning.
@return true if connection to proxy is active, false if eof or error
condition have been encountered on the connection.
*/
public boolean isProxyAlive(int timeout){
if(server_mode) return false;
if(proxy != null){
try{
proxy.proxySocket.setSoTimeout(timeout);
int eof = proxy.in.read();
if(eof < 0) return false; // EOF encountered.
else return true; // This really should not happen
}catch(InterruptedIOException iioe){
return true; // read timed out.
}catch(IOException ioe){
return false;
}
}
return false;
}
//PRIVATE METHODS
//////////////////
private byte[] formHeader(InetAddress ip, int port){
Socks5Message request = new Socks5Message(0,ip,port);
request.data[0] = 0;
return request.data;
}
private byte[] formHeader(String host,int port){
Socks5Message request = new Socks5Message(0,host,port);
request.data[0] = 0;
return request.data;
}
/*======================================================================
//Mainly Test functions
//////////////////////
private String bytes2String(byte[] b){
String s="";
char[] hex_digit = { '0','1','2','3','4','5','6','7','8','9',
'A','B','C','D','E','F'};
for(int i=0;i<b.length;++i){
int i1 = (b[i] & 0xF0) >> 4;
int i2 = b[i] & 0xF;
s+=hex_digit[i1];
s+=hex_digit[i2];
s+=" ";
}
return s;
}
private static final void debug(String s){
if(DEBUG)
System.out.print(s);
}
private static final boolean DEBUG = true;
public static void usage(){
System.err.print(
"Usage: java Socks.SocksDatagramSocket host port [socksHost socksPort]\n");
}
static final int defaultProxyPort = 1080; //Default Port
static final String defaultProxyHost = "www-proxy"; //Default proxy
public static void main(String args[]){
int port;
String host;
int proxyPort;
String proxyHost;
InetAddress ip;
if(args.length > 1 && args.length < 5){
try{
host = args[0];
port = Integer.parseInt(args[1]);
proxyPort =(args.length > 3)? Integer.parseInt(args[3])
: defaultProxyPort;
host = args[0];
ip = InetAddress.getByName(host);
proxyHost =(args.length > 2)? args[2]
: defaultProxyHost;
Proxy.setDefaultProxy(proxyHost,proxyPort);
Proxy p = Proxy.getDefaultProxy();
p.addDirect("lux");
DatagramSocket ds = new Socks5DatagramSocket();
BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
String s;
System.out.print("Enter line:");
s = in.readLine();
while(s != null){
byte[] data = (s+"\r\n").getBytes();
DatagramPacket dp = new DatagramPacket(data,0,data.length,
ip,port);
System.out.println("Sending to: "+ip+":"+port);
ds.send(dp);
dp = new DatagramPacket(new byte[1024],1024);
System.out.println("Trying to recieve on port:"+
ds.getLocalPort());
ds.receive(dp);
System.out.print("Recieved:\n"+
"From:"+dp.getAddress()+":"+dp.getPort()+
"\n\n"+
new String(dp.getData(),dp.getOffset(),dp.getLength())+"\n"
);
System.out.print("Enter line:");
s = in.readLine();
}
ds.close();
System.exit(1);
}catch(SocksException s_ex){
System.err.println("SocksException:"+s_ex);
s_ex.printStackTrace();
System.exit(1);
}catch(IOException io_ex){
io_ex.printStackTrace();
System.exit(1);
}catch(NumberFormatException num_ex){
usage();
num_ex.printStackTrace();
System.exit(1);
}
}else{
usage();
}
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -