?? socksconnection.java.svn-base
字號:
/*******************************************************************************
Library of additional graphical screens for J2ME applications
Copyright (C) 2003-08 Jimm Project
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
********************************************************************************
File: src/jimm/comm/connections/SOCKSConnection.java
Version: ###VERSION### Date: ###DATE###
Author(s): Andreas Rossbacher, Perminov Andrey
*******************************************************************************/
package jimm.comm.connections;
//#sijapp cond.if modules_PROXY is "true"#
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Vector;
//#sijapp cond.if target!="DEFAULT"#
import javax.microedition.io.SocketConnection;
//#sijapp cond.else#
//# import javax.microedition.io.StreamConnection;
//#sijapp cond.end#
import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.io.Connector;
import jimm.JimmException;
import jimm.Options;
import jimm.MainThread;
import jimm.comm.Icq;
import jimm.comm.Packet;
import jimm.comm.ToIcqSrvPacket;
import jimm.comm.Util;
//#sijapp cond.if modules_TRAFFIC is "true" #
import jimm.Traffic;
//#sijapp cond.end#
// SOCKSConnection
public class SOCKSConnection extends Connection implements Runnable
{
private final byte[] SOCKS4_CMD_CONNECT =
{ (byte) 0x04, (byte) 0x01, (byte) 0x14, (byte) 0x46, // Port 5190
(byte) 0x40, (byte) 0x0C, (byte) 0xA1, (byte) 0xB9, (byte) 0x00 // IP 64.12.161.185 (default login.icq.com)
};
private final byte[] SOCKS5_HELLO =
{ (byte) 0x05, (byte) 0x02, (byte) 0x00, (byte) 0x02 };
private final byte[] SOCKS5_CMD_CONNECT =
{ (byte) 0x05, (byte) 0x01, (byte) 0x00, (byte) 0x03 };
// Connection variables
//#sijapp cond.if target is "MIDP2" | target is "MOTOROLA" | target is "SIEMENS2" | target is "RIM"#
private SocketConnection sc;
//#sijapp cond.else#
//# private StreamConnection sc;
//#sijapp cond.end#
private InputStream is;
private OutputStream os;
private boolean is_socks4 = false;
private boolean is_socks5 = false;
private boolean is_connected = false;
// ICQ sequence number counter
private int nextIcqSequence;
public boolean haveToSetNullAfterDisconnect()
{
return false;
}
// Tries to resolve given host IP
private synchronized String ResolveIP(String host, String port)
{
//#sijapp cond.if target!="DEFAULT"#
if (Util.isIP(host))
return host;
SocketConnection c;
try
{
c = (SocketConnection) Connector.open("socket://" + host + ":"
+ port, Connector.READ_WRITE);
String ip = c.getAddress();
try
{
c.close();
} catch (Exception e)
{ /* Do nothing */
} finally
{
c = null;
}
return ip;
} catch (Exception e)
{
return "0.0.0.0";
}
//#sijapp cond.else#
//# if (Util.isIP(host))
//# return host;
//# else
//# return "0.0.0.0";
//#sijapp cond.end#
}
// Build socks4 CONNECT request
private byte[] socks4_connect_request(String ip, String port)
{
byte[] buf = new byte[9];
System.arraycopy(SOCKS4_CMD_CONNECT, 0, buf, 0, 9);
Util.putWord(buf, 2, Integer.parseInt(port));
byte[] bip = Util.ipToByteArray(ip);
System.arraycopy(bip, 0, buf, 4, 4);
return buf;
}
// Build socks5 AUTHORIZE request
private byte[] socks5_authorize_request(String login, String pass)
{
byte[] buf = new byte[3 + login.length() + pass.length()];
Util.putByte(buf, 0, 0x01);
Util.putByte(buf, 1, login.length());
Util.putByte(buf, login.length() + 2, pass.length());
byte[] blogin = Util.stringToByteArray(login);
byte[] bpass = Util.stringToByteArray(pass);
System.arraycopy(blogin, 0, buf, 2, blogin.length);
System.arraycopy(bpass, 0, buf, blogin.length + 3, bpass.length);
return buf;
}
// Build socks5 CONNECT request
private byte[] socks5_connect_request(String host, String port)
{
byte[] buf = new byte[7 + host.length()];
System.arraycopy(SOCKS5_CMD_CONNECT, 0, buf, 0, 4);
Util.putByte(buf, 4, host.length());
byte[] bhost = Util.stringToByteArray(host);
System.arraycopy(bhost, 0, buf, 5, bhost.length);
Util.putWord(buf, 5 + bhost.length, Integer.parseInt(port));
return buf;
}
// Opens a connection to the specified host and starts the receiver
// thread
public synchronized void connect(String hostAndPort)
throws JimmException
{
int mode = Options.getInt(Options.OPTION_PRX_TYPE);
is_connected = false;
is_socks4 = false;
is_socks5 = false;
String host = "";
String port = "";
if (mode != 0)
{
int sep = 0;
for (int i = 0; i < hostAndPort.length(); i++)
{
if (hostAndPort.charAt(i) == ':')
{
sep = i;
break;
}
}
// Get Host and Port
host = hostAndPort.substring(0, sep);
port = hostAndPort.substring(sep + 1);
}
try
{
switch (mode)
{
case 0:
connect_socks4(host, port);
break;
case 1:
connect_socks5(host, port);
break;
case 2:
// Try better first
try
{
connect_socks5(host, port);
} catch (Exception e)
{
// Do nothing
}
// If not succeeded, then try socks4
if (!is_connected)
{
closeStreams();
try
{
// Wait the given time
Thread.sleep(2000);
} catch (InterruptedException e)
{
// Do nothing
}
connect_socks4(host, port);
}
break;
}
setInputCloseFlag(false);
rcvThread = new Thread(this);
rcvThread.start();
// Set starting point for seq numbers (not bigger then 0x8000)
flapSEQ = getSeqValue();
nextIcqSequence = 2;
} catch (JimmException e)
{
throw (e);
}
}
// Attempts to connect through socks4
private synchronized void connect_socks4(String host, String port)
throws JimmException
{
is_socks4 = false;
String proxy_host = Options.getString(Options.OPTION_PRX_SERV);
String proxy_port = Options.getString(Options.OPTION_PRX_PORT);
int i = 0;
byte[] buf;
try
{
//#sijapp cond.if target!="DEFAULT"#
sc = (SocketConnection) Connector.open("socket://" + proxy_host
+ ":" + proxy_port, Connector.READ_WRITE);
//#sijapp cond.else#
//# sc = (StreamConnection) Connector.open("socket://" + proxy_host + ":" + proxy_port, Connector.READ_WRITE);
//#sijapp cond.end#
is = sc.openInputStream();
os = sc.openOutputStream();
String ip = ResolveIP(host, port);
os.write(socks4_connect_request(ip, port));
os.flush();
// Wait for responce
while (is.available() == 0 && i < 50)
{
try
{
// Wait the given time
i++;
Thread.sleep(100);
} catch (InterruptedException e)
{
// Do nothing
}
}
// Read packet
// If only got proxy responce packet, parse it
if (is.available() == 8)
{
// Read reply
buf = new byte[is.available()];
is.read(buf);
int ver = Util.getByte(buf, 0);
int meth = Util.getByte(buf, 1);
// All we need
if (ver == 0x00 && meth == 0x5A)
{
is_connected = true;
is_socks4 = true;
} else
{
is_connected = false;
throw (new JimmException(118, 2));
}
}
// If we got responce packet bigger than mere proxy responce,
// we might got destination server responce in tail of proxy
// responce
else if (is.available() > 8)
{
is_connected = true;
is_socks4 = true;
} else
{
throw (new JimmException(118, 2));
}
} catch (ConnectionNotFoundException e)
{
if (!getInputCloseFlag()) throw (new JimmException(121, 0));
} catch (IllegalArgumentException e)
{
throw (new JimmException(122, 0));
} catch (IOException e)
{
throw (new JimmException(120, 0));
} catch (SecurityException e)
{
throw (new JimmException(119, 0));
}
}
// Attempts to connect through socks5
private synchronized void connect_socks5(String host, String port)
throws JimmException
{
is_socks5 = false;
String proxy_host = Options.getString(Options.OPTION_PRX_SERV);
String proxy_port = Options.getString(Options.OPTION_PRX_PORT);
String proxy_login = Options.getString(Options.OPTION_PRX_NAME);
String proxy_pass = Options.getString(Options.OPTION_PRX_PASS);
int i = 0;
byte[] buf;
try
{
//#sijapp cond.if target!="DEFAULT"#
sc = (SocketConnection) Connector.open("socket://" + proxy_host
+ ":" + proxy_port, Connector.READ_WRITE);
//#sijapp cond.else#
//# sc = (StreamConnection) Connector.open("socket://" + proxy_host + ":" + proxy_port, Connector.READ_WRITE);
//#sijapp cond.end#
is = sc.openInputStream();
os = sc.openOutputStream();
os.write(SOCKS5_HELLO);
os.flush();
// Wait for responce
while (is.available() == 0 && i < 50)
{
try
{
// Wait the given time
i++;
Thread.sleep(100);
} catch (InterruptedException e)
{
// Do nothing
}
}
if (is.available() == 0)
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -