12
|
1 package jp.ac.u_ryukyu.treevnc.server;
|
11
|
2
|
|
3 import static org.junit.Assert.*;
|
|
4
|
|
5 import java.awt.Graphics;
|
|
6 import java.awt.Image;
|
|
7 import java.awt.image.BufferedImage;
|
|
8 import java.io.BufferedOutputStream;
|
|
9 import java.io.BufferedReader;
|
|
10 import java.io.ByteArrayInputStream;
|
|
11 import java.io.ByteArrayOutputStream;
|
|
12 import java.io.IOException;
|
|
13 import java.io.InputStream;
|
|
14 import java.io.InputStreamReader;
|
|
15 import java.net.BindException;
|
|
16 import java.net.ServerSocket;
|
|
17 import java.net.Socket;
|
|
18 import java.nio.ByteBuffer;
|
|
19 import java.util.Iterator;
|
|
20 import java.util.LinkedList;
|
|
21
|
|
22 import javax.imageio.ImageIO;
|
|
23
|
|
24 import org.junit.Test;
|
|
25
|
15
|
26 import com.glavsoft.exceptions.TransportException;
|
|
27 import com.glavsoft.rfb.encoding.EncodingType;
|
|
28 import com.glavsoft.transport.Reader;
|
|
29
|
11
|
30
|
|
31 //import myVncProxy.MulticastQueue.Client;
|
|
32
|
|
33 import java.util.concurrent.ExecutorService;
|
|
34 import java.util.concurrent.atomic.AtomicInteger;
|
|
35 import java.util.zip.DataFormatException;
|
|
36 import java.util.zip.Deflater;
|
|
37 import java.util.zip.Inflater;
|
|
38 import java.io.OutputStream;
|
|
39
|
12
|
40 import jp.ac.u_ryukyu.treevnc.MulticastQueue;
|
|
41
|
11
|
42 public class MyRfbProtoProxy {
|
|
43 final static String versionMsg_3_855 = "RFB 003.855\n";
|
|
44 /**
|
|
45 * CheckMillis is one of new msgType for RFB 3.855.
|
|
46 */
|
|
47 final static byte SpeedCheckMillis = 4;
|
15
|
48 final static int FramebufferUpdate = 0;
|
|
49 final static int CheckDelay = 11;
|
11
|
50
|
|
51 // Secyrity type of OS X
|
|
52 final static int SecTypeReqAccess = 32;
|
|
53
|
|
54 // Supported authentication types
|
|
55 final static int AuthAccess = 32;
|
|
56
|
|
57 private static final int INFLATE_BUFSIZE = 1024 * 100;
|
|
58 boolean printStatusFlag = false;
|
|
59 long startCheckTime;
|
|
60 private int messageType;
|
|
61 private int rectangles;
|
|
62 private int rectX;
|
|
63 private int rectY;
|
|
64 private int rectW;
|
|
65 private int rectH;
|
|
66 private int encoding;
|
|
67 private int zLen;
|
|
68 private boolean clicomp = false;
|
15
|
69
|
11
|
70
|
|
71 private ServerSocket servSock;
|
|
72 protected int acceptPort;
|
|
73 // private byte initData[];
|
|
74 byte initData[];
|
|
75 private LinkedList<Socket> cliListTmp;
|
|
76 private LinkedList<Socket> cliList;
|
|
77 boolean createBimgFlag;
|
|
78 boolean proxyFlag = true;
|
15
|
79 private Reader reader;
|
11
|
80 ExecutorService executor;
|
|
81
|
|
82 byte[] pngBytes;
|
|
83
|
|
84 // private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new
|
|
85 // MostRecentMultiCast<LinkedList<ByteBuffer>>(10);
|
|
86 private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MulticastQueue<LinkedList<ByteBuffer>>();
|
|
87 private int clients = 0;
|
|
88 private Inflater inflater = new Inflater();
|
|
89 private Deflater deflater = new Deflater();
|
|
90 private CreateThread geth;
|
|
91 // private Thread requestThread;
|
|
92 private RequestScreenThread rThread;
|
|
93 private Thread requestThread;
|
|
94
|
15
|
95 public MyRfbProtoProxy() {
|
11
|
96 }
|
|
97
|
15
|
98 public void setReader(Reader _reader) {
|
|
99 reader = _reader;
|
|
100 }
|
11
|
101
|
|
102 void initServSock(int port) throws IOException {
|
|
103 servSock = new ServerSocket(port);
|
|
104 acceptPort = port;
|
|
105 }
|
|
106
|
|
107 /*
|
|
108 * default port number is 5999.
|
|
109 */
|
|
110 public void selectPort(int p) {
|
|
111 if (servSock != null)
|
|
112 return;
|
|
113 int port = p;
|
|
114 while (true) {
|
|
115 try {
|
|
116 initServSock(port);
|
|
117 break;
|
|
118 } catch (BindException e) {
|
|
119 port++;
|
|
120 continue;
|
|
121 } catch (IOException e) {
|
|
122
|
|
123 }
|
|
124 }
|
|
125 System.out.println("accept port = " + port);
|
|
126 }
|
|
127
|
|
128 int getAcceptPort() {
|
|
129 return acceptPort;
|
|
130 }
|
|
131
|
|
132 void setSoTimeout(int num) throws IOException {
|
|
133 servSock.setSoTimeout(num);
|
|
134 }
|
|
135
|
|
136 public Socket accept() throws IOException {
|
|
137 return servSock.accept();
|
|
138 }
|
|
139
|
|
140 void addSock(Socket sock) {
|
|
141 cliList.add(sock);
|
|
142 }
|
|
143
|
|
144 void addSockTmp(Socket sock) {
|
|
145 System.out.println("connected " + sock.getInetAddress());
|
|
146 cliListTmp.add(sock);
|
|
147 }
|
|
148
|
|
149 synchronized void changeStatusFlag() {
|
|
150 printStatusFlag = true;
|
|
151 }
|
|
152
|
|
153 void printMills() {
|
|
154 if (printStatusFlag) {
|
|
155
|
|
156 changeStatusFlag();
|
|
157 } else {
|
|
158 changeStatusFlag();
|
|
159 }
|
|
160 }
|
|
161
|
|
162
|
|
163 void requestThreadStart() {
|
|
164 requestThread.start();
|
|
165 }
|
|
166
|
|
167 public synchronized void requestThreadNotify() {
|
|
168 rThread.reStart();
|
|
169 }
|
|
170
|
|
171 /**
|
|
172 * gzip byte arrays
|
|
173 *
|
|
174 * @param deflater
|
|
175 * @param inputs
|
|
176 * byte data[]
|
|
177 * @param inputIndex
|
|
178 * @param outputs
|
|
179 * byte data[]
|
|
180 * @return byte length in last byte array
|
|
181 * @throws IOException
|
|
182 */
|
|
183 public int zip(Deflater deflater, LinkedList<ByteBuffer> inputs,
|
|
184 int inputIndex, LinkedList<ByteBuffer> outputs) throws IOException {
|
|
185 int len = 0;
|
|
186 ByteBuffer c1 = ByteBuffer.allocate(INFLATE_BUFSIZE);
|
|
187 while (inputIndex < inputs.size()) {
|
|
188 ByteBuffer b1 = inputs.get(inputIndex++);
|
|
189 deflater.setInput(b1.array(), b1.position(), b1.remaining());
|
|
190 /**
|
|
191 * If we finish() stream and reset() it, Deflater start new gzip
|
|
192 * stream, this makes continuous zlib reader unhappy. if we remove
|
|
193 * finish(), Deflater.deflate() never flushes its output. The
|
|
194 * original zlib deflate has flush flag. I'm pretty sure this a kind
|
|
195 * of bug of Java library.
|
|
196 */
|
|
197 if (inputIndex == inputs.size())
|
|
198 deflater.finish();
|
|
199 int len1 = 0;
|
|
200 do {
|
|
201 len1 = deflater.deflate(c1.array(), c1.position(),
|
|
202 c1.remaining());
|
|
203 if (len1 > 0) {
|
|
204 len += len1;
|
|
205 c1.position(c1.position() + len1);
|
|
206 if (c1.remaining() == 0) {
|
|
207 c1.flip();
|
|
208 outputs.addLast(c1);
|
|
209 c1 = ByteBuffer.allocate(INFLATE_BUFSIZE);
|
|
210 }
|
|
211 }
|
|
212 } while (len1 > 0 || !deflater.needsInput()); // &&!deflater.finished());
|
|
213 }
|
|
214 if (c1.position() != 0) {
|
|
215 c1.flip();
|
|
216 outputs.addLast(c1);
|
|
217 }
|
|
218 deflater.reset();
|
|
219 return len;
|
|
220 }
|
|
221
|
|
222 /**
|
|
223 * gunzip byte arrays
|
|
224 *
|
|
225 * @param inflater
|
|
226 * @param inputs
|
|
227 * byte data[]
|
|
228 * @param outputs
|
|
229 * byte data[]
|
|
230 * @return number of total bytes
|
|
231 * @throws IOException
|
|
232 */
|
|
233 public int unzip(Inflater inflater, LinkedList<ByteBuffer> inputs,
|
|
234 int inputIndex, LinkedList<ByteBuffer> outputs, int bufSize)
|
|
235 throws DataFormatException {
|
|
236 int len = 0;
|
|
237 ByteBuffer buf = ByteBuffer.allocate(bufSize);
|
|
238 while (inputIndex < inputs.size()) {
|
|
239 ByteBuffer input = inputs.get(inputIndex++);
|
|
240 inflater.setInput(input.array(), input.position(), input.limit());
|
|
241 // if (inputIndex==inputs.size()) if inflater/deflater has symmetry,
|
|
242 // we need this
|
|
243 // inflater.end(); but this won't work
|
|
244 do {
|
|
245 int len0 = inflater.inflate(buf.array(), buf.position(),
|
|
246 buf.remaining());
|
|
247 if (len0 > 0) {
|
|
248 buf.position(buf.position() + len0);
|
|
249 len += len0;
|
|
250 if (buf.remaining() == 0) {
|
|
251 buf.flip();
|
|
252 outputs.addLast(buf);
|
|
253 buf = ByteBuffer.allocate(bufSize);
|
|
254 }
|
|
255 }
|
|
256 } while (!inflater.needsInput());
|
|
257 }
|
|
258 if (buf.position() != 0) {
|
|
259 buf.flip();
|
|
260 outputs.addLast(buf);
|
|
261 }
|
|
262 return len;
|
|
263 }
|
|
264
|
|
265 float maxMag = 1;
|
|
266
|
|
267 /**
|
|
268 * send data to clients
|
|
269 *
|
|
270 * @param dataLen
|
|
271 * @throws IOException
|
|
272 * @throws DataFormatException
|
|
273 *
|
|
274 * Zlibed packet is compressed in context dependent way, that
|
|
275 * is, it have to send from the beginning. But this is
|
|
276 * impossible. So we have to compress it again for each clients.
|
|
277 * Separate deflater for each clients is necessary.
|
|
278 *
|
|
279 * Java's deflater does not support flush. This means to get the
|
|
280 * result, we have to finish the compression. Reseting start new
|
|
281 * compression, but it is not accepted well in zlib continuous
|
|
282 * reading. So we need new Encoding ZRLEE which reset decoder
|
|
283 * for each packet. ZRLEE can be invisible from user, but it
|
|
284 * have to be implemented in the clients. ZRLEE compression is
|
|
285 * not context dependent, so no recompression is necessary.
|
15
|
286 * @throws TransportException
|
11
|
287 */
|
|
288
|
15
|
289 void readSendData(int dataLen) throws IOException, DataFormatException, TransportException {
|
11
|
290 LinkedList<ByteBuffer> bufs = new LinkedList<ByteBuffer>();
|
|
291 ByteBuffer header = ByteBuffer.allocate(16);
|
15
|
292 reader.readBytes(header.array(), 0, 16);
|
11
|
293 header.limit(16);
|
15
|
294 if (header.get(0) == FramebufferUpdate) {
|
11
|
295 int encoding = header.getInt(12);
|
15
|
296 if (encoding == EncodingType.ZRLE.getId()
|
|
297 || encoding == EncodingType.ZLIB.getId()) { // ZRLEE is already
|
11
|
298 // recompressed
|
|
299 ByteBuffer len = ByteBuffer.allocate(4);
|
15
|
300 reader.readBytes(len.array(), 0, 4);
|
11
|
301 len.limit(4);
|
|
302 ByteBuffer inputData = ByteBuffer.allocate(dataLen - 20);
|
|
303
|
15
|
304 reader.readBytes(inputData.array(), 0, inputData.capacity());
|
11
|
305 // System.out.println(dataLen);
|
|
306 inputData.limit(dataLen - 20);
|
|
307
|
|
308 LinkedList<ByteBuffer> inputs = new LinkedList<ByteBuffer>();
|
|
309 inputs.add(inputData);
|
|
310
|
15
|
311 header.putInt(12, EncodingType.ZRLEE.getId()); // means recompress
|
11
|
312 // every time
|
|
313 // using new Deflecter every time is incompatible with the
|
|
314 // protocol, clients have to be modified.
|
|
315 Deflater nDeflater = deflater; // new Deflater();
|
|
316 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>();
|
|
317 unzip(inflater, inputs, 0, out, INFLATE_BUFSIZE);
|
|
318 // dump32(inputs);
|
|
319 int len2 = zip(nDeflater, out, 0, bufs);
|
|
320 ByteBuffer blen = ByteBuffer.allocate(4);
|
|
321 blen.putInt(len2);
|
|
322 blen.flip();
|
|
323 bufs.addFirst(blen);
|
|
324
|
|
325 bufs.addFirst(header);
|
|
326 // if(dataLen<=64000)
|
|
327 multicastqueue.put(bufs);
|
|
328 // is.reset();
|
|
329
|
|
330 /*
|
|
331 * System.out.println("ZRLE = "+dataLen);
|
|
332 * System.out.println("ZRLEE = "+(len2+20)); float mag =
|
|
333 * (float)dataLen / (float)(len2 + 20);
|
|
334 * System.out.println("ZRLE / ZRLEE = "+ mag); if(mag > maxMag)
|
|
335 * maxMag = mag; System.out.println("maxMag = "+maxMag);
|
|
336 */
|
|
337 return;
|
|
338 }
|
|
339 bufs.add(header);
|
|
340 if (dataLen > 16) {
|
|
341 ByteBuffer b = ByteBuffer.allocate(dataLen - 16);
|
15
|
342 reader.readBytes(b.array(), 0, dataLen - 16);
|
11
|
343 b.limit(dataLen - 16);
|
|
344 bufs.add(b);
|
|
345 }
|
|
346 multicastqueue.put(bufs);
|
|
347 // is.reset();
|
|
348 return;
|
|
349 }
|
15
|
350 reader.reset();
|
11
|
351
|
|
352 // It may be compressed. We can inflate here to avoid repeating clients
|
|
353 // decompressing here,
|
|
354 // but it may generate too many large data. It is better to do it in
|
|
355 // each client.
|
|
356 // But we have do inflation for all input data, so we have to do it
|
|
357 // here.
|
|
358 }
|
|
359
|
|
360 public void newClient(AcceptThread acceptThread, final Socket newCli,
|
|
361 final OutputStream os, final InputStream is) throws IOException {
|
|
362 // createBimgFlag = true;
|
|
363 // rfb.addSockTmp(newCli);
|
|
364 // addSock(newCli);
|
|
365 final int myId = clients;
|
|
366 final MulticastQueue.Client<LinkedList<ByteBuffer>> c = multicastqueue
|
|
367 .newClient();
|
|
368 final AtomicInteger writerRunning = new AtomicInteger();
|
|
369 writerRunning.set(1);
|
|
370 /**
|
|
371 * Timeout thread. If a client is suspended, it has top of queue
|
|
372 * indefinitely, which caused memory overflow. After the timeout, we
|
|
373 * poll the queue and discard it. Start long wait if writer is running.
|
|
374 */
|
|
375 final Runnable timer = new Runnable() {
|
|
376 public void run() {
|
|
377 int count = 0;
|
|
378 for (;;) {
|
|
379 long timeout = 50000 / 8;
|
|
380 try {
|
|
381 synchronized (this) {
|
|
382 int state, flag;
|
|
383 writerRunning.set(0);
|
|
384 wait(timeout);
|
|
385 flag = 0;
|
|
386 while ((state = writerRunning.get()) == 0) {
|
|
387 c.poll(); // discard, should be timeout
|
|
388 count++;
|
|
389 if (flag == 0) {
|
|
390 System.out.println("Discarding " + myId
|
|
391 + " count=" + count);
|
|
392 flag = 1;
|
|
393 }
|
|
394 wait(10); // if this is too short, writer cannot
|
|
395 // take the poll, if this is too
|
|
396 // long, memory will overflow...
|
|
397 }
|
|
398 if (flag == 1)
|
|
399 System.out.println("Resuming " + myId
|
|
400 + " count=" + count);
|
|
401 if (state != 1) {
|
|
402 System.out.println("Client died " + myId);
|
|
403 break;
|
|
404 }
|
|
405 }
|
|
406 } catch (InterruptedException e) {
|
|
407 }
|
|
408 }
|
|
409 }
|
|
410 };
|
|
411 new Thread(timer).start();
|
|
412 /**
|
|
413 * discard all incoming from clients
|
|
414 */
|
|
415 final Runnable reader = new Runnable() {
|
|
416 public void run() {
|
|
417 byte b[] = new byte[4096];
|
|
418 for (;;) {
|
|
419 try {
|
|
420 int c = is.read(b);
|
|
421 if (c <= 0)
|
|
422 throw new IOException();
|
|
423 // System.out.println("client read "+c);
|
|
424 } catch (IOException e) {
|
|
425 try {
|
|
426 writerRunning.set(2);
|
|
427 os.close();
|
|
428 is.close();
|
|
429 } catch (IOException e1) {
|
|
430 }
|
|
431 return;
|
|
432 }
|
|
433 }
|
|
434 }
|
|
435 };
|
|
436 /**
|
|
437 * send packets to a client
|
|
438 */
|
|
439 Runnable sender = new Runnable() {
|
|
440 public void run() {
|
|
441 writerRunning.set(1);
|
|
442 try {
|
|
443 requestThreadNotify();
|
|
444 // rThread.checkDelay();
|
|
445
|
|
446 /**
|
|
447 * initial connection of RFB protocol
|
|
448 */
|
|
449 sendRfbVersion(os);
|
|
450 // readVersionMsg(is);
|
|
451 int rfbMinor = readVersionMsg(is, os);
|
|
452 sendSecurityType(os);
|
|
453 readSecType(is);
|
|
454 sendSecResult(os);
|
|
455 readClientInit(is);
|
|
456 sendInitData(os);
|
|
457 new Thread(reader).start(); // discard incoming packet here
|
|
458 // after.
|
|
459 // writeFramebufferUpdateRequest(0,0, framebufferWidth,
|
|
460 // framebufferHeight, false );
|
|
461 int i = 0;
|
|
462 for (;;) {
|
|
463 LinkedList<ByteBuffer> bufs = c.poll();
|
|
464 int inputIndex = 0;
|
|
465 ByteBuffer header = bufs.get(inputIndex);
|
|
466 if (header == null)
|
|
467 continue;
|
15
|
468 else if (header.get(0) == CheckDelay) {
|
11
|
469 writeToClient(os, bufs, inputIndex);
|
|
470 continue;
|
15
|
471 } else if (header.get(0) == FramebufferUpdate) {
|
11
|
472 // System.out.println("client "+ myId);
|
|
473 }
|
|
474 /*
|
|
475 if(i%20==0){
|
|
476 sendDataCheckDelay();
|
|
477 }
|
|
478 i++;
|
|
479 */
|
|
480 writeToClient(os, bufs, inputIndex);
|
|
481 writerRunning.set(1); // yes my client is awaking.
|
|
482 }
|
|
483 } catch (IOException e) {
|
|
484 try {
|
|
485 writerRunning.set(2);
|
|
486 os.close();
|
|
487 } catch (IOException e1) {
|
|
488 }
|
|
489 /* if socket closed cliList.remove(newCli); */
|
|
490 }
|
|
491 }
|
|
492
|
|
493 public void writeToClient(final OutputStream os,
|
|
494 LinkedList<ByteBuffer> bufs, int inputIndex)
|
|
495 throws IOException {
|
|
496 while (inputIndex < bufs.size()) {
|
|
497 ByteBuffer b = bufs.get(inputIndex++);
|
|
498 os.write(b.array(), b.position(), b.limit());
|
|
499 }
|
|
500 os.flush();
|
|
501 }
|
|
502 };
|
|
503 clients++;
|
|
504 new Thread(sender).start();
|
|
505
|
|
506 }
|
|
507
|
|
508 public void dump32(LinkedList<ByteBuffer> bufs) {
|
|
509 int len = 0;
|
|
510 for (ByteBuffer b : bufs)
|
|
511 len += b.remaining();
|
|
512 ByteBuffer top = bufs.getFirst();
|
|
513 ByteBuffer end = bufs.getLast();
|
|
514 System.err.println("length: " + len);
|
|
515 System.err.print("head 0: ");
|
|
516 for (int i = 0; i < 16 && i < top.remaining(); i++) {
|
|
517 System.err.print(" " + top.get(i));
|
|
518 }
|
|
519 System.err.print("tail 0: ");
|
|
520 for (int i = 0; i < 16 && i < end.remaining(); i++) {
|
|
521 System.err.print(" " + end.get(i));
|
|
522 }
|
|
523 System.err.println();
|
|
524 }
|
|
525
|
|
526 @Test
|
|
527 public void test1() {
|
|
528 try {
|
|
529 LinkedList<ByteBuffer> in = new LinkedList<ByteBuffer>();
|
|
530 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>();
|
|
531 LinkedList<ByteBuffer> out2 = new LinkedList<ByteBuffer>();
|
|
532 // if (false) {
|
|
533 // for(int i=0;i<10;i++) {
|
|
534 // in.add(ByteBuffer.wrap("test1".getBytes()));
|
|
535 // in.add(ByteBuffer.wrap("test2".getBytes()));
|
|
536 // in.add(ByteBuffer.wrap("test3".getBytes()));
|
|
537 // in.add(ByteBuffer.wrap("test44".getBytes()));
|
|
538 // }
|
|
539 // } else
|
|
540 {
|
|
541 String t = "";
|
|
542 for (int i = 0; i < 10; i++) {
|
|
543 t += "test1";
|
|
544 t += "test2";
|
|
545 t += "test3";
|
|
546 t += "test44";
|
|
547 }
|
|
548 in.add(ByteBuffer.wrap(t.getBytes()));
|
|
549 }
|
|
550
|
|
551 LinkedList<ByteBuffer> in1 = clone(in);
|
|
552
|
|
553 Deflater deflater = new Deflater();
|
|
554 zip(deflater, in, 0, out);
|
|
555 // LinkedList<ByteBuffer> out3 = clone(out); zipped result is depend
|
|
556 // on deflator's state
|
|
557 unzip(inflater, out, 0, out2, INFLATE_BUFSIZE);
|
|
558 // inflater.reset();
|
|
559 equalByteBuffers(in1, out2);
|
|
560 LinkedList<ByteBuffer> out4 = new LinkedList<ByteBuffer>();
|
|
561 deflater = new Deflater();
|
|
562 zip(deflater, out2, 0, out4);
|
|
563 LinkedList<ByteBuffer> out5 = new LinkedList<ByteBuffer>();
|
|
564 unzip(inflater, out4, 0, out5, INFLATE_BUFSIZE);
|
|
565 int len = equalByteBuffers(in1, out5);
|
|
566
|
|
567 System.out.println("Test Ok. " + len);
|
|
568 } catch (Exception e) {
|
|
569 assertEquals(0, 1);
|
|
570 }
|
|
571 }
|
|
572
|
|
573 private LinkedList<ByteBuffer> clone(LinkedList<ByteBuffer> in) {
|
|
574 LinkedList<ByteBuffer> copy = new LinkedList<ByteBuffer>();
|
|
575 for (ByteBuffer b : in) {
|
|
576 ByteBuffer c = b.duplicate();
|
|
577 copy.add(c);
|
|
578 }
|
|
579 return copy;
|
|
580 }
|
|
581
|
|
582 public int equalByteBuffers(LinkedList<ByteBuffer> in,
|
|
583 LinkedList<ByteBuffer> out2) {
|
|
584 int len = 0;
|
|
585 Iterable<Byte> i = byteBufferIterator(in);
|
|
586 Iterator<Byte> o = byteBufferIterator(out2).iterator();
|
|
587
|
|
588 for (int b : i) {
|
|
589 len++;
|
|
590 if (o.hasNext()) {
|
|
591 int c = o.next();
|
|
592 assertEquals(b, c);
|
|
593 } else
|
|
594 assertEquals(0, 1);
|
|
595 }
|
|
596 if (o.hasNext())
|
|
597 assertEquals(0, 1);
|
|
598 // System.out.println();
|
|
599 return len;
|
|
600 }
|
|
601
|
|
602 private Iterable<Byte> byteBufferIterator(final LinkedList<ByteBuffer> in) {
|
|
603 return new Iterable<Byte>() {
|
|
604 public Iterator<Byte> iterator() {
|
|
605 return new Iterator<Byte>() {
|
|
606 int bytes = 0;
|
|
607 int buffers = 0;
|
|
608
|
|
609 public boolean hasNext() {
|
|
610 for (;;) {
|
|
611 if (buffers >= in.size())
|
|
612 return false;
|
|
613 ByteBuffer b = in.get(buffers);
|
|
614 if (!(bytes < b.remaining())) {
|
|
615 buffers++;
|
|
616 bytes = 0;
|
|
617 } else
|
|
618 return true;
|
|
619 }
|
|
620 }
|
|
621
|
|
622 public Byte next() {
|
|
623 ByteBuffer bf = in.get(buffers);
|
|
624 byte b = bf.get(bytes++);
|
|
625 if (bf.remaining() <= bytes) {
|
|
626 buffers++;
|
|
627 bytes = 0;
|
|
628 }
|
|
629 // System.out.print(b);
|
|
630 return b;
|
|
631 }
|
|
632
|
|
633 public void remove() {
|
|
634 }
|
|
635 };
|
|
636 }
|
|
637 };
|
|
638 }
|
15
|
639
|
|
640 void sendRfbVersion(OutputStream os) throws IOException {
|
|
641 // os.write(versionMsg_3_8.getBytes());
|
|
642 os.write(versionMsg_3_855.getBytes());
|
|
643 }
|
|
644
|
|
645 int readVersionMsg(InputStream is, OutputStream os) throws IOException {
|
11
|
646
|
15
|
647 byte[] b = new byte[12];
|
|
648
|
|
649 is.read(b);
|
|
650
|
|
651 if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ')
|
|
652 || (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9')
|
|
653 || (b[6] < '0') || (b[6] > '9') || (b[7] != '.')
|
|
654 || (b[8] < '0') || (b[8] > '9') || (b[9] < '0') || (b[9] > '9')
|
|
655 || (b[10] < '0') || (b[10] > '9') || (b[11] != '\n')) {
|
|
656 throw new IOException("this is not an RFB server");
|
|
657 }
|
|
658
|
|
659 int rfbMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0');
|
|
660 int rfbMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0');
|
|
661
|
|
662 if (rfbMajor < 3) {
|
|
663 throw new IOException(
|
|
664 "RFB server does not support protocol version 3");
|
|
665 }
|
|
666
|
|
667 if (rfbMinor == 855) {
|
|
668 sendProxyFlag(os);
|
|
669 if (proxyFlag)
|
|
670 sendPortNumber(os);
|
|
671 }
|
|
672 return rfbMinor;
|
|
673 }
|
|
674
|
|
675 void sendProxyFlag(OutputStream os) throws IOException {
|
|
676 if (proxyFlag)
|
|
677 os.write(1);
|
|
678 else
|
|
679 os.write(0);
|
|
680 }
|
|
681
|
|
682 void sendPortNumber(OutputStream os) throws IOException {
|
|
683 byte[] b = new byte[4];
|
|
684 b = castIntByte(geth.port);
|
|
685 os.write(b);
|
|
686 }
|
|
687
|
|
688 boolean readProxyFlag() throws IOException, TransportException {
|
|
689 int flag = reader.readUInt8();
|
|
690 if (flag == 1)
|
|
691 return true;
|
|
692 else
|
|
693 return false;
|
|
694 }
|
|
695
|
|
696 void sendSecurityType(OutputStream os) throws IOException {
|
|
697 // number-of-security-types
|
|
698 os.write(1);
|
|
699 // security-types
|
|
700 // 1:None
|
|
701 os.write(1);
|
|
702
|
|
703 /*
|
|
704 * os.write(4); os.write(30); os.write(31); os.write(32); os.write(35);
|
|
705 * os.flush();
|
|
706 */
|
|
707 }
|
|
708
|
|
709 void readSecType(InputStream is) throws IOException {
|
|
710 byte[] b = new byte[1];
|
|
711 is.read(b);
|
|
712 }
|
|
713
|
|
714 void readSecType(InputStream is, OutputStream os) throws IOException {
|
|
715 byte[] b = new byte[1];
|
|
716 is.read(b);
|
|
717
|
|
718 int count = 260;
|
|
719 int[] data = { 0, 2, 0, -128, -1, -1, -1, -1, -1, -1, -1, -1, -55, 15,
|
|
720 -38, -94, 33, 104, -62, 52, -60, -58, 98, -117, -128, -36, 28,
|
|
721 -47, 41, 2, 78, 8, -118, 103, -52, 116, 2, 11, -66, -90, 59,
|
|
722 19, -101, 34, 81, 74, 8, 121, -114, 52, 4, -35, -17, -107, 25,
|
|
723 -77, -51, 58, 67, 27, 48, 43, 10, 109, -14, 95, 20, 55, 79,
|
|
724 -31, 53, 109, 109, 81, -62, 69, -28, -123, -75, 118, 98, 94,
|
|
725 126, -58, -12, 76, 66, -23, -90, 55, -19, 107, 11, -1, 92, -74,
|
|
726 -12, 6, -73, -19, -18, 56, 107, -5, 90, -119, -97, -91, -82,
|
|
727 -97, 36, 17, 124, 75, 31, -26, 73, 40, 102, 81, -20, -26, 83,
|
|
728 -127, -1, -1, -1, -1, -1, -1, -1, -1, -111, 73, -29, 30, 57,
|
|
729 -67, -75, -77, -49, -50, -99, -76, -80, -80, 14, 65, 57, -105,
|
|
730 -103, -54, -102, 3, 39, -44, 39, 35, 118, -84, -64, 37, -117,
|
|
731 -21, 89, -31, -68, 70, 5, 122, -92, -119, 9, 121, 63, -112,
|
|
732 -60, 122, -46, -69, -36, 92, -103, -92, 74, 92, -73, 87, 120,
|
|
733 -8, 116, -47, 111, 20, -41, 110, 122, -3, -94, 14, 42, -51,
|
|
734 -59, 48, -54, -125, 117, 60, 77, -52, -31, 98, 32, -2, -102,
|
|
735 -15, -29, 58, -14, -106, -116, -32, -86, 50, -32, -16, -3,
|
|
736 -123, 87, 88, -118, 10, 120, -107, -37, 125, -110, 59, 87, 93,
|
|
737 -24, 124, -99, 18, 78, -13, -49, -34, -24, -27, 1, 114, -67,
|
|
738 -98, -56, -3, 85, -67, -126, 77 };
|
|
739 for (int i = 0; i < count; i++) {
|
|
740 os.write((byte) data[i]);
|
|
741 os.flush();
|
|
742 }
|
|
743
|
|
744 byte[] c = new byte[256];
|
|
745 is.read(c);
|
|
746
|
|
747 System.out.println(new String(c));
|
|
748
|
|
749 }
|
|
750
|
|
751 void sendSecResult(OutputStream os) throws IOException {
|
|
752 byte[] b = castIntByte(0);
|
|
753 os.write(b);
|
|
754 }
|
|
755
|
|
756 byte[] castIntByte(int len) {
|
|
757 byte[] b = new byte[4];
|
|
758 b[0] = (byte) ((len >>> 24) & 0xFF);
|
|
759 b[1] = (byte) ((len >>> 16) & 0xFF);
|
|
760 b[2] = (byte) ((len >>> 8) & 0xFF);
|
|
761 b[3] = (byte) ((len >>> 0) & 0xFF);
|
|
762 return b;
|
|
763 }
|
|
764
|
|
765 void readClientInit(InputStream in) throws IOException {
|
|
766 byte[] b = new byte[0];
|
|
767 in.read(b);
|
|
768 }
|
|
769
|
|
770 void sendInitData(OutputStream os) throws IOException {
|
|
771 os.write(initData);
|
|
772 }
|
|
773
|
11
|
774 }
|