changeset 15:5d43194fdc51

add function
author Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
date Sun, 26 Aug 2012 12:51:54 +0900
parents ff01665d26b4
children e654b2e4de64
files src/main/java/com/glavsoft/transport/Reader.java src/main/java/jp/ac/u_ryukyu/treevnc/server/MyRfbProtoProxy.java src/main/java/jp/ac/u_ryukyu/treevnc/server/VncProxyService.java
diffstat 3 files changed, 170 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/com/glavsoft/transport/Reader.java	Thu Aug 23 20:22:32 2012 +0900
+++ b/src/main/java/com/glavsoft/transport/Reader.java	Sun Aug 26 12:51:54 2012 +0900
@@ -128,8 +128,17 @@
 			throw new TransportException("Cannot read " + length + " bytes array", e);
 		}
 	}
+	
 	public byte[] readBytes(byte[]b) throws TransportException {
 		byte[] result = readBytes(b,0,b.length);
 		return result;
 	}
+	
+	public void reset() throws IOException {
+		is.reset();
+	}
+	
+	public void mark(int readLimit) {
+		is.mark(readLimit);
+	}
 }
\ No newline at end of file
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/server/MyRfbProtoProxy.java	Thu Aug 23 20:22:32 2012 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/treevnc/server/MyRfbProtoProxy.java	Sun Aug 26 12:51:54 2012 +0900
@@ -23,6 +23,10 @@
 
 import org.junit.Test;
 
+import com.glavsoft.exceptions.TransportException;
+import com.glavsoft.rfb.encoding.EncodingType;
+import com.glavsoft.transport.Reader;
+
 
 //import myVncProxy.MulticastQueue.Client;
 
@@ -41,6 +45,8 @@
 	 * CheckMillis is one of new msgType for RFB 3.855.
 	 */
 	final static byte SpeedCheckMillis = 4;
+	final static int FramebufferUpdate = 0;
+	final static int CheckDelay = 11;
 
 	// Secyrity type of OS X
 	final static int SecTypeReqAccess = 32;
@@ -60,6 +66,7 @@
 	private int encoding;
 	private int zLen;
 	private boolean clicomp = false;
+	
 
 	private ServerSocket servSock;
 	protected int acceptPort;
@@ -69,7 +76,7 @@
 	private LinkedList<Socket> cliList;
 	boolean createBimgFlag;
 	boolean proxyFlag = true;
-
+	private Reader reader;
 	ExecutorService executor;
 
 	byte[] pngBytes;
@@ -85,9 +92,12 @@
 	private RequestScreenThread rThread;
 	private Thread requestThread;
 
-	public MyRfbProtoProxy() throws IOException {
+	public MyRfbProtoProxy() {
 	}
 
+	public void setReader(Reader _reader) {
+		reader = _reader;
+	}
 
 	void initServSock(int port) throws IOException {
 		servSock = new ServerSocket(port);
@@ -273,31 +283,32 @@
 	 *             for each packet. ZRLEE can be invisible from user, but it
 	 *             have to be implemented in the clients. ZRLEE compression is
 	 *             not context dependent, so no recompression is necessary.
+	 * @throws TransportException 
 	 */
 
-	void readSendData(int dataLen) throws IOException, DataFormatException {
+	void readSendData(int dataLen) throws IOException, DataFormatException, TransportException {
 		LinkedList<ByteBuffer> bufs = new LinkedList<ByteBuffer>();
 		ByteBuffer header = ByteBuffer.allocate(16);
-		readFully(header.array(), 0, 16);
+		reader.readBytes(header.array(), 0, 16);
 		header.limit(16);
-		if (header.get(0) == RfbProto.FramebufferUpdate) {
+		if (header.get(0) ==  FramebufferUpdate) {
 			int encoding = header.getInt(12);
-			if (encoding == RfbProto.EncodingZRLE
-					|| encoding == RfbProto.EncodingZlib) { // ZRLEE is already
+			if (encoding == EncodingType.ZRLE.getId()
+					|| encoding == EncodingType.ZLIB.getId()) { // ZRLEE is already
 															// recompressed
 				ByteBuffer len = ByteBuffer.allocate(4);
-				readFully(len.array(), 0, 4);
+				reader.readBytes(len.array(), 0, 4);
 				len.limit(4);
 				ByteBuffer inputData = ByteBuffer.allocate(dataLen - 20);
 
-				readFully(inputData.array(), 0, inputData.capacity());
+				reader.readBytes(inputData.array(), 0, inputData.capacity());
 //				System.out.println(dataLen);
 				inputData.limit(dataLen - 20);
 
 				LinkedList<ByteBuffer> inputs = new LinkedList<ByteBuffer>();
 				inputs.add(inputData);
 
-				header.putInt(12, RfbProto.EncodingZRLEE); // means recompress
+				header.putInt(12, EncodingType.ZRLEE.getId()); // means recompress
 															// every time
 				// using new Deflecter every time is incompatible with the
 				// protocol, clients have to be modified.
@@ -328,7 +339,7 @@
 			bufs.add(header);
 			if (dataLen > 16) {
 				ByteBuffer b = ByteBuffer.allocate(dataLen - 16);
-				readFully(b.array(), 0, dataLen - 16);
+				reader.readBytes(b.array(), 0, dataLen - 16);
 				b.limit(dataLen - 16);
 				bufs.add(b);
 			}
@@ -336,7 +347,7 @@
 			// is.reset();
 			return;
 		}
-		is.reset();
+		reader.reset();
 
 		// It may be compressed. We can inflate here to avoid repeating clients
 		// decompressing here,
@@ -454,10 +465,10 @@
 						ByteBuffer header = bufs.get(inputIndex);
 						if (header == null)
 							continue;
-						else if (header.get(0) == RfbProto.CheckDelay) {
+						else if (header.get(0) == CheckDelay) {
 							writeToClient(os, bufs, inputIndex);
 							continue;
-						} else if (header.get(0) == RfbProto.FramebufferUpdate) {
+						} else if (header.get(0) == FramebufferUpdate) {
 							// System.out.println("client "+ myId);
 						}
 						/*
@@ -625,5 +636,139 @@
 			}
 		};
 	}
+	
+	void sendRfbVersion(OutputStream os) throws IOException {
+		// os.write(versionMsg_3_8.getBytes());
+		os.write(versionMsg_3_855.getBytes());
+	}
+	
+	int readVersionMsg(InputStream is, OutputStream os) throws IOException {
 
+		byte[] b = new byte[12];
+
+		is.read(b);
+
+		if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ')
+				|| (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9')
+				|| (b[6] < '0') || (b[6] > '9') || (b[7] != '.')
+				|| (b[8] < '0') || (b[8] > '9') || (b[9] < '0') || (b[9] > '9')
+				|| (b[10] < '0') || (b[10] > '9') || (b[11] != '\n')) {
+			throw new IOException("this is not an RFB server");
+		}
+
+		int rfbMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0');
+		int rfbMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0');
+
+		if (rfbMajor < 3) {
+			throw new IOException(
+					"RFB server does not support protocol version 3");
+		}
+
+		if (rfbMinor == 855) {
+			sendProxyFlag(os);
+			if (proxyFlag)
+				sendPortNumber(os);
+		}
+		return rfbMinor;
+	}
+	
+	void sendProxyFlag(OutputStream os) throws IOException {
+		if (proxyFlag)
+			os.write(1);
+		else
+			os.write(0);
+	}
+	
+	void sendPortNumber(OutputStream os) throws IOException {
+		byte[] b = new byte[4];
+		b = castIntByte(geth.port);
+		os.write(b);
+	}
+
+	boolean readProxyFlag() throws IOException, TransportException {
+		int flag = reader.readUInt8();
+		if (flag == 1)
+			return true;
+		else
+			return false;
+	}
+	
+		void sendSecurityType(OutputStream os) throws IOException {
+		// number-of-security-types
+		os.write(1);
+		// security-types
+		// 1:None
+		os.write(1);
+
+		/*
+		 * os.write(4); os.write(30); os.write(31); os.write(32); os.write(35);
+		 * os.flush();
+		 */
+	}
+
+	void readSecType(InputStream is) throws IOException {
+		byte[] b = new byte[1];
+		is.read(b);
+	}
+
+	void readSecType(InputStream is, OutputStream os) throws IOException {
+		byte[] b = new byte[1];
+		is.read(b);
+
+		int count = 260;
+		int[] data = { 0, 2, 0, -128, -1, -1, -1, -1, -1, -1, -1, -1, -55, 15,
+				-38, -94, 33, 104, -62, 52, -60, -58, 98, -117, -128, -36, 28,
+				-47, 41, 2, 78, 8, -118, 103, -52, 116, 2, 11, -66, -90, 59,
+				19, -101, 34, 81, 74, 8, 121, -114, 52, 4, -35, -17, -107, 25,
+				-77, -51, 58, 67, 27, 48, 43, 10, 109, -14, 95, 20, 55, 79,
+				-31, 53, 109, 109, 81, -62, 69, -28, -123, -75, 118, 98, 94,
+				126, -58, -12, 76, 66, -23, -90, 55, -19, 107, 11, -1, 92, -74,
+				-12, 6, -73, -19, -18, 56, 107, -5, 90, -119, -97, -91, -82,
+				-97, 36, 17, 124, 75, 31, -26, 73, 40, 102, 81, -20, -26, 83,
+				-127, -1, -1, -1, -1, -1, -1, -1, -1, -111, 73, -29, 30, 57,
+				-67, -75, -77, -49, -50, -99, -76, -80, -80, 14, 65, 57, -105,
+				-103, -54, -102, 3, 39, -44, 39, 35, 118, -84, -64, 37, -117,
+				-21, 89, -31, -68, 70, 5, 122, -92, -119, 9, 121, 63, -112,
+				-60, 122, -46, -69, -36, 92, -103, -92, 74, 92, -73, 87, 120,
+				-8, 116, -47, 111, 20, -41, 110, 122, -3, -94, 14, 42, -51,
+				-59, 48, -54, -125, 117, 60, 77, -52, -31, 98, 32, -2, -102,
+				-15, -29, 58, -14, -106, -116, -32, -86, 50, -32, -16, -3,
+				-123, 87, 88, -118, 10, 120, -107, -37, 125, -110, 59, 87, 93,
+				-24, 124, -99, 18, 78, -13, -49, -34, -24, -27, 1, 114, -67,
+				-98, -56, -3, 85, -67, -126, 77 };
+		for (int i = 0; i < count; i++) {
+			os.write((byte) data[i]);
+			os.flush();
+		}
+
+		byte[] c = new byte[256];
+		is.read(c);
+
+		System.out.println(new String(c));
+
+	}
+
+	void sendSecResult(OutputStream os) throws IOException {
+		byte[] b = castIntByte(0);
+		os.write(b);
+	}
+	
+	byte[] castIntByte(int len) {
+		byte[] b = new byte[4];
+		b[0] = (byte) ((len >>> 24) & 0xFF);
+		b[1] = (byte) ((len >>> 16) & 0xFF);
+		b[2] = (byte) ((len >>> 8) & 0xFF);
+		b[3] = (byte) ((len >>> 0) & 0xFF);
+		return b;
+	}
+
+	void readClientInit(InputStream in) throws IOException {
+		byte[] b = new byte[0];
+		in.read(b);
+	}
+
+	void sendInitData(OutputStream os) throws IOException {
+		os.write(initData);
+	}
+	
 }
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/server/VncProxyService.java	Thu Aug 23 20:22:32 2012 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/treevnc/server/VncProxyService.java	Sun Aug 26 12:51:54 2012 +0900
@@ -37,11 +37,7 @@
 	// public AcceptClient acc;
 	public VncProxyService(Parser parser) {
 		this();
-		try {
-			rfb = new MyRfbProtoProxy();
-		} catch (IOException e) {
-			e.printStackTrace();
-		}
+		rfb = new MyRfbProtoProxy();
 		ParametersHandler.completeSettingsFromCLI(parser, connectionParams, settings, uiSettings);
 		showControls = ParametersHandler.showControls;
 		passwordFromParams = parser.getValueFor(ParametersHandler.ARG_PASSWORD);
@@ -132,6 +128,7 @@
 				workingSocket.setTcpNoDelay(true); // disable Nagle algorithm
 				Reader reader = new Reader(workingSocket.getInputStream());
 				Writer writer = new Writer(workingSocket.getOutputStream());
+				rfb.setReader(reader);
 				workingProtocol = new Protocol(reader, writer,
 						new PasswordChooser(passwordFromParams, connectionParams, containerFrame, this),
 						settings);