⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socksconnection.java

📁 类似QQ的功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			{
				throw (new JimmException(118, 2));
			}

			// Read reply
			buf = new byte[is.available()];
			is.read(buf);

			int ver = Util.getByte(buf, 0);
			int meth = Util.getByte(buf, 1);

			// Plain text authorisation
			if (ver == 0x05 && meth == 0x02)
			{
				os.write(socks5_authorize_request(proxy_login, proxy_pass));
				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)
				{
					throw (new JimmException(118, 2));
				}

				// Read reply
				buf = new byte[is.available()];
				is.read(buf);

				meth = Util.getByte(buf, 1);

				if (meth == 0x00)
				{
					is_connected = true;
					is_socks5 = true;
				} else
				{
					// Unknown error (bad login or pass)
					throw (new JimmException(118, 3));
				}
			}
			// Proxy without authorisation
			else if (ver == 0x05 && meth == 0x00)
			{
				is_connected = true;
				is_socks5 = true;
			}
			// Something bad happened :'(
			else
			{
				throw (new JimmException(118, 2));
			}
			// If we got correct responce, send CONNECT
			if (is_connected == true)
			{
				os.write(socks5_connect_request(host, port));
				os.flush();
			}
		} 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));
		}
	}

	public void forceDisconnect()
	{
		setInputCloseFlag(true);
		closeStreams();
	}

	// Close input and output streams
	private synchronized void closeStreams()
	{
		try { is.close(); } catch (Exception e) {}
		is = null;

		try { os.close(); } catch (Exception e) {}
		os = null;

		try { sc.close(); } catch (Exception e) {}
		sc = null;
	}

	// Sends the specified packet
	public void sendPacket(Packet packet) throws JimmException
	{

		// Throw exception if output stream is not ready
		if (os == null)
		{
			throw (new JimmException(123, 0));
		}

		// Request lock on output stream
		synchronized (os)
		{

			// Set sequence numbers
			packet.setSequence(getFlapSequence());
			if (packet instanceof ToIcqSrvPacket)
			{
				((ToIcqSrvPacket) packet).setIcqSequence(nextIcqSequence++);
			}

			// Send packet and count the bytes
			try
			{
				byte[] outpack = packet.toByteArray();
				os.write(outpack);
				os.flush();
//#sijapp cond.if modules_TRAFFIC is "true" #
				Traffic.addOutTraffic(outpack.length + 51); // 51 is the overhead for each packet
				MainThread.updateContactListCaption();
//#sijapp cond.end#
			} catch (IOException e)
			{
				notifyToDisconnect();
			}

		}

	}

	//#sijapp cond.if target!="DEFAULT" & modules_FILES="true"#

	// Return the port this connection is running on
	public int getLocalPort()
	{
		try
		{
			return (this.sc.getLocalPort());
		} catch (IOException e)
		{
			return (0);
		}
	}

	// Return the ip this connection is running on
	public byte[] getLocalIP()
	{
		try
		{
			return (Util.ipToByteArray(this.sc.getLocalAddress()));
		} catch (IOException e)
		{
			return (new byte[4]);
		}
	}

	//#sijapp cond.end#

	// Main loop
	public void run()
	{

		// Required variables
		byte[] flapHeader = new byte[6];
		byte[] flapData;
		byte[] rcvdPacket;
		int bRead, bReadSum;

		// Reset packet buffer
		synchronized (this)
		{
			rcvdPackets = new Vector();
		}

		// Try
		try
		{

			// Check abort condition
			while (!getInputCloseFlag())
			{

				// Read flap header
				bReadSum = 0;
				if (Options.getInt(Options.OPTION_CONN_PROP) == 1)
				{
					while (is.available() == 0)
						Thread.sleep(250);
					if (is == null)
						break;
				}
				do
				{
					bRead = is.read(flapHeader, bReadSum, flapHeader.length
							- bReadSum);
					if (bRead == -1)
						break;
					bReadSum += bRead;
				} while (bReadSum < flapHeader.length);
				if (bRead == -1)
					break;
				// Verify and strip out proxy responce
				// Socks4 first
				if (Util.getByte(flapHeader, 0) == 0x00 && is_socks4)
				{
					// Strip only on first packet
					is_socks4 = false;
					int rep = Util.getByte(flapHeader, 1);
					if (rep != 0x5A)
					{
						// Something went wrong :(
						throw (new JimmException(118, 1));
					}

					is.skip(2);

					bReadSum = 0;
					do
					{
						bRead = is.read(flapHeader, bReadSum,
								flapHeader.length - bReadSum);
						if (bRead == -1)
							break;
						bReadSum += bRead;
					} while (bReadSum < flapHeader.length);
				}
				// Check for socks5
				else if (Util.getByte(flapHeader, 0) == 0x05 && is_socks5)
				{
					// Strip only on first packet
					is_socks5 = false;

					int rep = Util.getByte(flapHeader, 1);
					if (rep != 0x00)
					{
						// Something went wrong :(
						throw (new JimmException(118, 1));
					}
					// Check ATYP and skip BND.ADDR
					int atyp = Util.getByte(flapHeader, 3);

					if (atyp == 0x01)
					{
						is.skip(4);
					} else if (atyp == 0x03)
					{
						int size = Util.getByte(flapHeader, 4);
						is.skip(size + 1);
					} else
					{
						// Don't know what was that, but skip like
						// if it was an ip
						is.skip(4);
					}

					bReadSum = 0;
					do
					{
						bRead = is.read(flapHeader, bReadSum,
								flapHeader.length - bReadSum);
						if (bRead == -1)
							break;
						bReadSum += bRead;
					} while (bReadSum < flapHeader.length);
				}

				// Verify flap header
				if (Util.getByte(flapHeader, 0) != 0x2A)
				{
					throw (new JimmException(124, 0));
				}

				// Allocate memory for flap data
				flapData = new byte[Util.getWord(flapHeader, 4)];

				// Read flap data
				bReadSum = 0;
				do
				{
					bRead = is.read(flapData, bReadSum, flapData.length
							- bReadSum);
					if (bRead == -1)
						break;
					bReadSum += bRead;
				} while (bReadSum < flapData.length);
				if (bRead == -1)
					break;

				// Merge flap header and data and count the data
				rcvdPacket = new byte[flapHeader.length + flapData.length];
				System.arraycopy(flapHeader, 0, rcvdPacket, 0,
						flapHeader.length);
				System.arraycopy(flapData, 0, rcvdPacket,
						flapHeader.length, flapData.length);
//#sijapp cond.if modules_TRAFFIC is "true" #
				Traffic.addInTraffic(bReadSum + 57);
				MainThread.updateContactListCaption();
//#sijapp cond.end#

				// Lock object and add rcvd packet to vector
				synchronized (rcvdPackets)
				{
					rcvdPackets.addElement(rcvdPacket);
				}

				// Notify main loop
				synchronized (Icq.getWaitObj())
				{
					Icq.getWaitObj().notify();
				}
			}

		}
		// Catch communication exception
		catch (NullPointerException e)
		{

			// Construct and handle exception (only if input close flag has not been set)
			if (!getInputCloseFlag())
			{
				JimmException f = new JimmException(120, 3);
				JimmException.handleException(f);
			}

			// Reset input close flag
			setInputCloseFlag(false);

		}
		// Catch InterruptedException
		catch (InterruptedException e)
		{ /* Do nothing */
		}
		// Catch JimmException
		catch (JimmException e)
		{

			// Handle exception
			JimmException.handleException(e);

		}
		// Catch IO exception
		catch (IOException e)
		{
			// Construct and handle exception (only if input close flag has not been set)
			if (!getInputCloseFlag())
			{
				JimmException f = new JimmException(120, 1);
				JimmException.handleException(f);
			}

			// Reset input close flag
			setInputCloseFlag(false);

		}
		
		//closeStreams();
		//setNotConnected();
	}
	
	

}

//#sijapp cond.end #

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -