24
|
1 package myVncProxy;
|
25
|
2 import java.awt.Graphics;
|
|
3 import java.awt.Image;
|
|
4 import java.awt.image.BufferedImage;
|
|
5 import java.io.BufferedOutputStream;
|
15
|
6 import java.io.BufferedReader;
|
25
|
7 import java.io.ByteArrayInputStream;
|
|
8 import java.io.ByteArrayOutputStream;
|
10
|
9 import java.io.IOException;
|
43
|
10 import java.io.InputStream;
|
15
|
11 import java.io.InputStreamReader;
|
23
|
12 import java.net.BindException;
|
10
|
13 import java.net.ServerSocket;
|
|
14 import java.net.Socket;
|
|
15 import java.util.LinkedList;
|
|
16
|
25
|
17 import javax.imageio.ImageIO;
|
|
18
|
40
|
19 import java.util.concurrent.ExecutorService;
|
|
20 import java.util.concurrent.Executors;
|
|
21 import java.io.OutputStream;
|
10
|
22
|
|
23 class MyRfbProto extends RfbProto {
|
|
24
|
43
|
25 final static String versionMsg_3_998 = "RFB 003.998\n";
|
|
26
|
|
27
|
18
|
28 private int messageType;
|
|
29 private int rectangles;
|
23
|
30 private int rectX;
|
|
31 private int rectY;
|
|
32 private int rectW;
|
|
33 private int rectH;
|
18
|
34 private int encoding;
|
27
|
35 private int zLen;
|
39
|
36 private int dataLen;
|
18
|
37
|
23
|
38 private ServerSocket servSock;
|
|
39 private int acceptPort;
|
10
|
40 private byte initData[];
|
27
|
41 private LinkedList <Socket> cliListTmp;
|
10
|
42 private LinkedList <Socket> cliList;
|
39
|
43 private LinkedList <Thread> sendThreads;
|
27
|
44 boolean createBimgFlag;
|
10
|
45
|
40
|
46 ExecutorService executor;
|
|
47
|
|
48
|
25
|
49 byte[] pngBytes;
|
|
50
|
13
|
51 MyRfbProto(String h, int p, VncViewer v ) throws IOException {
|
10
|
52 super(h, p, v);
|
|
53 cliList = new LinkedList <Socket>();
|
27
|
54 cliListTmp = new LinkedList <Socket>();
|
|
55 createBimgFlag = false;
|
39
|
56 sendThreads = new LinkedList <Thread>();
|
40
|
57 // executor = Executors.newCachedThreadPool();
|
|
58 executor = Executors.newSingleThreadExecutor();
|
10
|
59 }
|
|
60
|
13
|
61 MyRfbProto(String h, int p) throws IOException {
|
|
62 super(h, p);
|
|
63 cliList = new LinkedList <Socket>();
|
27
|
64 cliListTmp = new LinkedList <Socket>();
|
|
65 createBimgFlag = false;
|
39
|
66 sendThreads = new LinkedList <Thread>();
|
40
|
67 // executor = Executors.newCachedThreadPool();
|
|
68 executor = Executors.newSingleThreadExecutor();
|
13
|
69 }
|
24
|
70
|
44
|
71 // over write
|
43
|
72 void writeVersionMsg() throws IOException {
|
|
73 clientMajor = 3;
|
|
74 if (serverMinor >= 9) {
|
|
75 clientMinor = 9;
|
|
76 os.write(versionMsg_3_998.getBytes());
|
|
77 } else if (serverMajor > 3 || serverMinor >= 8) {
|
|
78 clientMinor = 8;
|
|
79 os.write(versionMsg_3_8.getBytes());
|
|
80 } else if (serverMinor >= 9) {
|
|
81 clientMinor = 9;
|
|
82 os.write(versionMsg_3_998.getBytes());
|
|
83 } else if (serverMinor >= 7) {
|
|
84 clientMinor = 7;
|
|
85 os.write(versionMsg_3_7.getBytes());
|
|
86 } else {
|
|
87 clientMinor = 3;
|
|
88 os.write(versionMsg_3_3.getBytes());
|
|
89 }
|
|
90 protocolTightVNC = false;
|
|
91 initCapabilities();
|
|
92 }
|
|
93
|
|
94
|
|
95
|
10
|
96 void initServSock(int port) throws IOException{
|
|
97 servSock = new ServerSocket(port);
|
23
|
98 acceptPort = port;
|
10
|
99 }
|
23
|
100 void selectPort(){
|
|
101 int i = 5550;
|
|
102 while(true){
|
|
103 try{
|
|
104 initServSock(i);
|
|
105 break;
|
|
106 }catch(BindException e){
|
|
107 i++;
|
|
108 continue;
|
|
109 }catch(IOException e){
|
10
|
110
|
23
|
111 }
|
|
112 }
|
28
|
113 System.out.println("accept port = "+i);
|
23
|
114 }
|
|
115 int getAcceptPort(){
|
|
116 return acceptPort;
|
|
117 }
|
10
|
118 void setSoTimeout(int num) throws IOException {
|
|
119 servSock.setSoTimeout(num);
|
|
120 }
|
|
121
|
|
122 Socket accept() throws IOException {
|
|
123 return servSock.accept();
|
|
124 }
|
|
125
|
|
126 void addSock(Socket sock){
|
|
127 cliList.add(sock);
|
|
128 }
|
27
|
129 void addSockTmp(Socket sock){
|
30
|
130 System.out.println("connected "+sock.getInetAddress());
|
27
|
131 cliListTmp.add(sock);
|
|
132 }
|
10
|
133
|
|
134 void mark(int len) throws IOException {
|
|
135 is.mark(len);
|
|
136 }
|
|
137
|
|
138 void reset() throws IOException {
|
|
139 is.reset();
|
18
|
140 }
|
10
|
141
|
|
142 boolean markSupported() {
|
|
143 return is.markSupported();
|
|
144 }
|
|
145
|
|
146 void readServerInit() throws IOException {
|
|
147
|
|
148 mark(255);
|
|
149 skipBytes(20);
|
|
150 int nlen = readU32();
|
|
151 int blen = 20+4+nlen;
|
|
152 initData = new byte[blen];
|
|
153 reset();
|
|
154
|
|
155 mark(blen);
|
|
156 readFully(initData);
|
|
157 reset();
|
|
158
|
|
159 framebufferWidth = readU16();
|
|
160 framebufferHeight = readU16();
|
|
161 bitsPerPixel = readU8();
|
|
162 depth = readU8();
|
|
163 bigEndian = (readU8() != 0);
|
|
164 trueColour = (readU8() != 0);
|
|
165 redMax = readU16();
|
|
166 greenMax = readU16();
|
|
167 blueMax = readU16();
|
|
168 redShift = readU8();
|
|
169 greenShift = readU8();
|
|
170 blueShift = readU8();
|
|
171 byte[] pad = new byte[3];
|
|
172 readFully(pad);
|
|
173 int nameLength = readU32();
|
|
174 byte[] name = new byte[nameLength];
|
|
175 readFully(name);
|
|
176 desktopName = new String(name);
|
|
177
|
|
178 // Read interaction capabilities (TightVNC protocol extensions)
|
|
179 if (protocolTightVNC) {
|
|
180 int nServerMessageTypes = readU16();
|
|
181 int nClientMessageTypes = readU16();
|
|
182 int nEncodingTypes = readU16();
|
|
183 readU16();
|
|
184 readCapabilityList(serverMsgCaps, nServerMessageTypes);
|
|
185 readCapabilityList(clientMsgCaps, nClientMessageTypes);
|
|
186 readCapabilityList(encodingCaps, nEncodingTypes);
|
|
187 }
|
|
188
|
|
189 inNormalProtocol = true;
|
|
190 }
|
|
191
|
43
|
192 void sendRfbVersion(OutputStream os) throws IOException{
|
|
193 os.write(versionMsg_3_998.getBytes());
|
|
194 }
|
|
195 void sendSecurityType(OutputStream os) throws IOException {
|
|
196 // number-of-security-types
|
|
197 os.write(1);
|
|
198 // security-types
|
|
199 // 1:None
|
|
200 os.write(1);
|
|
201 }
|
|
202 void readClientInit(InputStream in) throws IOException {
|
|
203 byte[] b = new byte[0];
|
|
204 in.read(b);
|
|
205 }
|
|
206
|
|
207 void sendInitData(OutputStream os) throws IOException{
|
|
208 os.write(initData);
|
10
|
209 }
|
|
210
|
17
|
211 void sendData(byte b[]){
|
|
212 try{
|
|
213 for(Socket cli : cliList){
|
|
214 try{
|
|
215 cli.getOutputStream().write(b, 0, b.length);
|
|
216 }catch(IOException e){
|
|
217 // if socket closed
|
|
218 cliList.remove(cli);
|
|
219 }
|
|
220 }
|
20
|
221 // System.out.println("cliSize="+cliSize());
|
17
|
222 }catch(Exception e){
|
|
223 }
|
27
|
224 }
|
|
225
|
|
226 void sendPngImage(){
|
|
227 try{
|
|
228 for(Socket cli : cliListTmp){
|
|
229 try{
|
|
230 sendPngData(cli);
|
|
231 addSock(cli);
|
|
232 }catch(IOException e){
|
|
233 // if socket closed
|
|
234 cliListTmp.remove(cli);
|
|
235 }
|
|
236 }
|
|
237 // System.out.println("cliSize="+cliSize());
|
|
238 }catch(Exception e){
|
|
239 }
|
|
240 cliListTmp.clear();
|
|
241 }
|
|
242
|
|
243
|
|
244
|
15
|
245 boolean ready() throws IOException {
|
|
246 BufferedReader br = new BufferedReader(new InputStreamReader(is));
|
|
247 return br.ready();
|
|
248 }
|
10
|
249
|
|
250 int cliSize(){
|
|
251 return cliList.size();
|
|
252 }
|
15
|
253 void printNumBytesRead(){
|
|
254 System.out.println("numBytesRead="+numBytesRead);
|
|
255 }
|
|
256 void bufResetSend(int size) throws IOException {
|
|
257 reset();
|
|
258 int len = size;
|
|
259 if(available() < size )
|
|
260 len = available();
|
|
261 byte buffer[] = new byte[len];
|
|
262 readFully(buffer);
|
|
263 sendData(buffer);
|
|
264 }
|
40
|
265 void readSendData()throws IOException{
|
39
|
266 byte buffer[] = new byte[dataLen];
|
|
267 readFully(buffer);
|
|
268 reset();
|
|
269
|
|
270 for(Socket cli : cliList){
|
40
|
271 try{
|
|
272 OutputStream out = cli.getOutputStream();
|
|
273 executor.execute(new SendThread(out, buffer));
|
|
274 }catch(IOException e){
|
|
275 // if client socket closed
|
39
|
276 cliListTmp.remove(cli);
|
|
277 }catch(Exception e){
|
|
278
|
|
279 }
|
40
|
280
|
39
|
281 }
|
|
282 }
|
18
|
283 void regiFramebufferUpdate()throws IOException{
|
27
|
284 mark(20);
|
18
|
285 messageType = readU8();
|
|
286 skipBytes(1);
|
|
287 rectangles = readU16();
|
23
|
288 rectX = readU16();
|
|
289 rectY = readU16();
|
|
290 rectW = readU16();
|
|
291 rectH = readU16();
|
27
|
292 encoding = readU32();
|
|
293 if(encoding == 16)
|
|
294 zLen = readU32();
|
23
|
295 reset();
|
15
|
296 }
|
23
|
297 void checkAndMark() throws IOException{
|
|
298 switch(encoding){
|
|
299 case RfbProto.EncodingRaw:
|
39
|
300 dataLen = rectW * rectH * 4 + 16;
|
|
301 mark(dataLen);
|
23
|
302 break;
|
27
|
303 case RfbProto.EncodingZRLE:
|
39
|
304 dataLen = zLen+20;
|
|
305 mark(dataLen);
|
27
|
306 break;
|
23
|
307 default:
|
38
|
308 mark(1000000);
|
|
309 }
|
23
|
310 }
|
25
|
311 BufferedImage createBufferedImage(Image img){
|
|
312 BufferedImage bimg = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB );
|
27
|
313
|
25
|
314 Graphics g = bimg.getGraphics();
|
|
315 g.drawImage(img, 0, 0, null);
|
|
316 g.dispose();
|
|
317 return bimg;
|
|
318 }
|
|
319
|
|
320 void createPngBytes(BufferedImage bimg)throws IOException {
|
|
321 pngBytes = getImageBytes(bimg , "png");
|
|
322 }
|
|
323 byte[] getBytes(BufferedImage img)throws IOException {
|
|
324 byte[] b = getImageBytes(img, "png");
|
|
325 return b;
|
|
326 }
|
23
|
327
|
25
|
328 byte[] getImageBytes(BufferedImage image, String imageFormat) throws IOException {
|
|
329 ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
330 BufferedOutputStream os = new BufferedOutputStream(bos);
|
|
331 image.flush();
|
|
332 ImageIO.write(image, imageFormat, os);
|
|
333 os.flush();
|
|
334 os.close();
|
|
335 return bos.toByteArray();
|
|
336 }
|
|
337
|
|
338 void sendPngData(Socket sock)throws IOException{
|
26
|
339 byte[] dataLength = castIntByte(pngBytes.length);
|
|
340 sock.getOutputStream().write(dataLength);
|
25
|
341 sock.getOutputStream().write(pngBytes);
|
|
342 }
|
26
|
343 byte[] castIntByte(int len){
|
|
344 byte[] b = new byte[4];
|
|
345 b[0] = (byte)((len >>> 24 ) & 0xFF);
|
|
346 b[1] = (byte)((len >>> 16 ) & 0xFF);
|
|
347 b[2] = (byte)((len >>> 8 ) & 0xFF);
|
|
348 b[3] = (byte)((len >>> 0 ) & 0xFF);
|
|
349 return b;
|
|
350 }
|
25
|
351
|
|
352 BufferedImage createBimg()throws IOException{
|
|
353 BufferedImage bimg = ImageIO.read(new ByteArrayInputStream(pngBytes));
|
|
354 return bimg;
|
|
355 }
|
|
356 void readPngData()throws IOException{
|
|
357 pngBytes = new byte[is.available()];
|
|
358 readFully(pngBytes);
|
|
359 }
|
18
|
360 void printFramebufferUpdate(){
|
|
361
|
|
362 System.out.println("messageType=" + messageType);
|
|
363 System.out.println("rectangles="+rectangles);
|
|
364 System.out.println("encoding=" + encoding);
|
23
|
365 switch(encoding){
|
|
366 case RfbProto.EncodingRaw:
|
|
367 System.out.println("rectW * rectH * 4 + 16 =" + rectW * rectH * 4 + 16);
|
|
368 break;
|
|
369 default:
|
|
370 }
|
18
|
371 }
|
10
|
372 }
|
43
|
373
|
|
374
|