12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247 |
- import java.awt.event.InputEvent;
- import java.awt.event.KeyEvent;
- import java.awt.event.MouseEvent;
- import java.io.BufferedInputStream;
- import java.io.DataInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.net.Socket;
- import java.util.ArrayList;
- import java.util.zip.DataFormatException;
- import java.util.zip.Deflater;
- import java.util.zip.Inflater;
- import javax.swing.JOptionPane;
- class NoctisRfbProto {
- final String versionMsg = "RFB 003.003\n";
- final static int ConnFailed = 0, NoAuth = 1, VncAuth = 2, MsLogon = 0xfffffffa;
- final static int VncAuthOK = 0, VncAuthFailed = 1, VncAuthTooMany = 2;
- final static int FramebufferUpdate = 0, SetColourMapEntries = 1, Bell = 2, ServerCutText = 3, rfbFileTransfer = 7;
- final int SetPixelFormat = 0, FixColourMapEntries = 1, SetEncodings = 2, FramebufferUpdateRequest = 3, KeyboardEvent = 4, PointerEvent = 5, ClientCutText = 6;
- final static int EncodingRaw = 0, EncodingCopyRect = 1, EncodingRRE = 2, EncodingCoRRE = 4, EncodingHextile = 5, EncodingZlib = 6, EncodingTight = 7, EncodingCompressLevel0 = 0xFFFFFF00,
- EncodingQualityLevel0 = 0xFFFFFFE0, EncodingXCursor = 0xFFFFFF10, EncodingRichCursor = 0xFFFFFF11, EncodingPointerPos = 0xFFFFFF18,
- EncodingLastRect = 0xFFFFFF20, EncodingNewFBSize = 0xFFFFFF21;
- final int HextileRaw = (1 << 0);
- final int HextileBackgroundSpecified = (1 << 1);
- final int HextileForegroundSpecified = (1 << 2);
- final int HextileAnySubrects = (1 << 3);
- final int HextileSubrectsColoured = (1 << 4);
- final static int TightExplicitFilter = 0x04;
- final static int TightFill = 0x08;
- final static int TightJpeg = 0x09;
- final static int TightMaxSubencoding = 0x09;
- final static int TightFilterCopy = 0x00;
- final static int TightFilterPalette = 0x01;
- final static int TightFilterGradient = 0x02;
- final static int TightMinToCompress = 12;
-
- ArrayList remoteDirsList;
- ArrayList remoteFilesList;
- ArrayList a;
- boolean fFTInit = true;
- boolean fFTAllowed = true;
- boolean fAbort = false;
- boolean fFileReceptionError = false;
- boolean fFileReceptionRunning = false;
- boolean inDirectory2;
- FileOutputStream fos;
- FileInputStream fis;
- String sendFileSource;
- String receivePath;
- long fileSize;
- long receiveFileSize;
- long fileChunkCounter;
- final static int sz_rfbFileTransferMsg = 12,
-
- rfbDirContentRequest = 1,
-
- rfbDirPacket = 2,
-
- rfbFileTransferRequest = 3,
-
- rfbFileHeader = 4,
-
- rfbFilePacket = 5,
- rfbEndOfFile = 6,
-
- rfbAbortFileTransfer = 7,
-
- rfbFileTransferOffer = 8,
-
- rfbFileAcceptHeader = 9,
- rfbCommand = 10,
-
- rfbCommandReturn = 11,
-
- rfbFileChecksums = 12,
-
-
- rfbRDirContent = 1,
- rfbRDrivesList = 2,
-
- rfbADirectory = 1,
- rfbAFile = 2,
- rfbADrivesList = 3,
- rfbADirCreate = 4,
- rfbADirDelete = 5,
- rfbAFileCreate = 6,
- rfbAFileDelete = 7,
-
- rfbCDirCreate = 1,
- rfbCDirDelete = 2,
- rfbCFileCreate = 3,
- rfbCFileDelete = 4,
-
- rfbRErrorUnknownCmd = 1,
- rfbRErrorCmd = 0xFFFFFFFF,
-
- sz_rfbBlockSize = 8192,
-
- sz_rfbZipDirectoryPrefix = 9;
- String rfbZipDirectoryPrefix = "!UVNCDIR-\0";
-
-
- String host;
- int port;
- Socket sock;
- DataInputStream is;
- OutputStream os;
- OutputStreamWriter osw;
- SessionRecorder rec;
- boolean inNormalProtocol = false;
-
- ConfigClientBean config;
-
-
-
-
- boolean brokenKeyPressed = false;
-
-
- boolean wereZlibUpdates = false;
-
-
-
- boolean recordFromBeginning = true;
-
-
- boolean zlibWarningShown;
- boolean tightWarningShown;
-
-
-
-
-
-
- int numUpdatesInSession;
-
-
-
- NoctisRfbProto(String h, int p, ConfigClientBean c) throws IOException {
- config = c;
- host = h;
- port = p;
- sock = new Socket(host, port);
- is = new DataInputStream(new BufferedInputStream(sock.getInputStream(), 16384));
- os = sock.getOutputStream();
- osw = new OutputStreamWriter(sock.getOutputStream());
- inDirectory2 = false;
- a = new ArrayList();
-
- remoteDirsList = new ArrayList();
- remoteFilesList = new ArrayList();
- sendFileSource = "";
- }
- void close() {
- try {
- sock.close();
- if (rec != null) {
- rec.close();
- rec = null;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
-
- int serverMajor, serverMinor;
- void readVersionMsg() throws Exception {
- byte[] b = new byte[12];
- is.readFully(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 Exception("Host " + host + " port " + port + " is not an RFB server");
- }
- serverMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0');
- serverMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0');
- }
-
-
-
- void writeVersionMsg() throws IOException {
- os.write(versionMsg.getBytes());
- }
-
-
-
- int readAuthScheme() throws Exception {
- int authScheme = is.readInt();
- switch (authScheme) {
- case ConnFailed:
- int reasonLen = is.readInt();
- byte[] reason = new byte[reasonLen];
- is.readFully(reason);
- throw new Exception(new String(reason));
- case NoAuth:
- case VncAuth:
- case MsLogon:
- return authScheme;
- default:
- throw new Exception("Unknown authentication scheme from RFB server: " + authScheme);
- }
- }
-
-
-
- void writeClientInit() throws IOException {
- if (config.isShareDesktop()) {
- os.write(1);
- } else {
- os.write(0);
- }
- config.setShareDesktop(false);
- }
-
-
-
- String desktopName;
- int framebufferWidth, framebufferHeight;
- int bitsPerPixel, depth;
- boolean bigEndian, trueColour;
- int redMax, greenMax, blueMax, redShift, greenShift, blueShift;
- void readServerInit() throws IOException {
- framebufferWidth = is.readUnsignedShort();
- framebufferHeight = is.readUnsignedShort();
- bitsPerPixel = is.readUnsignedByte();
- depth = is.readUnsignedByte();
- bigEndian = (is.readUnsignedByte() != 0);
- trueColour = (is.readUnsignedByte() != 0);
- redMax = is.readUnsignedShort();
- greenMax = is.readUnsignedShort();
- blueMax = is.readUnsignedShort();
- redShift = is.readUnsignedByte();
- greenShift = is.readUnsignedByte();
- blueShift = is.readUnsignedByte();
- byte[] pad = new byte[3];
- is.readFully(pad);
- int nameLength = is.readInt();
- byte[] name = new byte[nameLength];
- is.readFully(name);
- desktopName = new String(name);
- inNormalProtocol = true;
- }
-
-
-
- void startSession(String fname) throws IOException {
- rec = new SessionRecorder(fname);
- rec.writeHeader();
- rec.write(versionMsg.getBytes());
- rec.writeIntBE(NoAuth);
- rec.writeShortBE(framebufferWidth);
- rec.writeShortBE(framebufferHeight);
- byte[] fbsServerInitMsg = { 32, 24, 0, 1, 0, (byte) 0xFF, 0, (byte) 0xFF, 0, (byte) 0xFF, 16, 8, 0, 0, 0, 0 };
- rec.write(fbsServerInitMsg);
- rec.writeIntBE(desktopName.length());
- rec.write(desktopName.getBytes());
- numUpdatesInSession = 0;
- if (wereZlibUpdates)
- recordFromBeginning = false;
- zlibWarningShown = false;
- tightWarningShown = false;
- }
-
-
-
- void closeSession() throws IOException {
- if (rec != null) {
- rec.close();
- rec = null;
- }
- }
-
-
-
- void setFramebufferSize(int width, int height) {
- framebufferWidth = width;
- framebufferHeight = height;
- }
-
-
-
- int readServerMessageType() throws IOException {
- int msgType = is.readUnsignedByte();
-
- if (rec != null) {
- if (msgType == Bell) {
- rec.writeByte(msgType);
- if (numUpdatesInSession > 0)
- rec.flush();
- }
- }
- return msgType;
- }
-
-
-
- int updateNRects;
- void readFramebufferUpdate() throws IOException {
- is.readByte();
- updateNRects = is.readUnsignedShort();
-
- if (rec != null) {
- rec.writeByte(FramebufferUpdate);
- rec.writeByte(0);
- rec.writeShortBE(updateNRects);
- }
- numUpdatesInSession++;
- }
-
- int updateRectX, updateRectY, updateRectW, updateRectH, updateRectEncoding;
- void readFramebufferUpdateRectHdr() throws Exception {
- updateRectX = is.readUnsignedShort();
- updateRectY = is.readUnsignedShort();
- updateRectW = is.readUnsignedShort();
- updateRectH = is.readUnsignedShort();
- updateRectEncoding = is.readInt();
- if (updateRectEncoding == EncodingZlib || updateRectEncoding == EncodingTight)
- wereZlibUpdates = true;
-
- if (rec != null) {
- if (numUpdatesInSession > 1)
- rec.flush();
- rec.writeShortBE(updateRectX);
- rec.writeShortBE(updateRectY);
- rec.writeShortBE(updateRectW);
- rec.writeShortBE(updateRectH);
- if (updateRectEncoding == EncodingZlib && !recordFromBeginning) {
-
-
- if (!zlibWarningShown) {
- System.out.println("Warning: Raw encoding will be used " + "instead of Zlib in recorded session.");
- zlibWarningShown = true;
- }
- rec.writeIntBE(EncodingRaw);
- } else {
- rec.writeIntBE(updateRectEncoding);
- if (updateRectEncoding == EncodingTight && !recordFromBeginning && !tightWarningShown) {
- System.out.println("Warning: Re-compressing Tight-encoded " + "updates for session recording.");
- tightWarningShown = true;
- }
- }
- }
- if (updateRectEncoding == EncodingLastRect || updateRectEncoding == EncodingNewFBSize)
- return;
- if (updateRectX + updateRectW > framebufferWidth || updateRectY + updateRectH > framebufferHeight) {
- throw new Exception("Framebuffer update rectangle too large: " + updateRectW + "x" + updateRectH + " at (" + updateRectX + "," + updateRectY + ")");
- }
- }
-
- int copyRectSrcX, copyRectSrcY;
- void readCopyRect() throws IOException {
- copyRectSrcX = is.readUnsignedShort();
- copyRectSrcY = is.readUnsignedShort();
-
- if (rec != null) {
- rec.writeShortBE(copyRectSrcX);
- rec.writeShortBE(copyRectSrcY);
- }
- }
-
-
-
- String readServerCutText() throws IOException {
- byte[] pad = new byte[3];
- is.readFully(pad);
- int len = is.readInt();
- byte[] text = new byte[len];
- is.readFully(text);
- return new String(text);
- }
-
-
-
-
-
-
- int readCompactLen() throws IOException {
- int[] portion = new int[3];
- portion[0] = is.readUnsignedByte();
- int byteCount = 1;
- int len = portion[0] & 0x7F;
- if ((portion[0] & 0x80) != 0) {
- portion[1] = is.readUnsignedByte();
- byteCount++;
- len |= (portion[1] & 0x7F) << 7;
- if ((portion[1] & 0x80) != 0) {
- portion[2] = is.readUnsignedByte();
- byteCount++;
- len |= (portion[2] & 0xFF) << 14;
- }
- }
- if (rec != null && recordFromBeginning)
- for (int i = 0; i < byteCount; i++)
- rec.writeByte(portion[i]);
- return len;
- }
-
-
-
-
- void readRfbFileTransferMsg() throws IOException {
- int contentType = is.readUnsignedByte();
- int contentParamT = is.readUnsignedByte();
- int contentParam = contentParamT;
- contentParamT = is.readUnsignedByte();
- contentParamT = contentParamT << 8;
- contentParam = contentParam | contentParamT;
- if (contentType == rfbRDrivesList || contentType == rfbDirPacket) {
- readDriveOrDirectory(contentParam);
- } else if (contentType == rfbFileHeader) {
- receiveFileHeader();
- } else if (contentType == rfbFilePacket) {
- receiveFileChunk();
- } else if (contentType == rfbEndOfFile) {
- endOfReceiveFile(true);
- } else if (contentType == rfbAbortFileTransfer) {
- if (fFileReceptionRunning) {
- endOfReceiveFile(false);
- } else {
-
-
- }
- } else if (contentType == rfbFileChecksums) {
- ReceiveDestinationFileChecksums();
- } else {
- System.out.println("ContentType: " + contentType);
- }
- }
-
- public void readDriveOrDirectory(int contentParam) throws IOException {
- if (contentParam == rfbADrivesList) {
- readFTPMsgDriveList();
- } else if (contentParam == rfbADirectory && !inDirectory2) {
- inDirectory2 = true;
- readFTPMsgDirectoryList();
- } else if (contentParam == rfbADirectory && inDirectory2) {
- readFTPMsgDirectoryListContent();
- } else if (contentParam == 0) {
- readFTPMsgDirectoryListEndContent();
- inDirectory2 = false;
- } else {
- System.out.println("ContentParam: " + contentParam);
- }
- }
-
- void writeRfbFileTransferMsg(int contentType, int contentParam, long size, // 0 : compression not supported - 1 : compression supported
- long length, String text) throws IOException {
- byte b[] = new byte[12];
- b[0] = (byte) rfbFileTransfer;
- b[1] = (byte) contentType;
- b[2] = (byte) contentParam;
- byte by = 0;
- long c = 0;
- length++;
- c = size & 0xFF000000;
- by = (byte) (c >>> 24);
- b[4] = by;
- c = size & 0xFF0000;
- by = (byte) (c >>> 16);
- b[5] = by;
- c = size & 0xFF00;
- by = (byte) (c >>> 8);
- b[6] = by;
- c = size & 0xFF;
- by = (byte) c;
- b[7] = by;
- c = length & 0xFF000000;
- by = (byte) (c >>> 24);
- b[8] = by;
- c = length & 0xFF0000;
- by = (byte) (c >>> 16);
- b[9] = by;
- c = length & 0xFF00;
- by = (byte) (c >>> 8);
- b[10] = by;
- c = length & 0xFF;
- by = (byte) c;
- b[11] = by;
- os.write(b);
- if (text != null) {
- byte byteArray[] = text.getBytes();
- byte byteArray2[] = new byte[byteArray.length + 1];
- for (int i = 0; i < byteArray.length; i++) {
- byteArray2[i] = byteArray[i];
- }
- byteArray2[byteArray2.length - 1] = 0;
- os.write(byteArray2);
- }
- }
-
- void offerLocalFile(String source, String destinationPath) {
- try {
- sendFileSource = source;
- File f = new File(source);
-
- long lSize = f.length();
- int iLowSize = (int) (lSize & 0x00000000FFFFFFFF);
- int iHighSize = (int) (lSize >> 32);
- String temp = destinationPath + f.getName();
- writeRfbFileTransferMsg(rfbFileTransferOffer, 0, iLowSize,
- temp.length(), temp);
-
- byte b[] = new byte[4];
- byte by = 0;
- long c = 0;
- c = iHighSize & 0xFF000000;
- by = (byte) (c >>> 24);
- b[0] = by;
- c = iHighSize & 0xFF0000;
- by = (byte) (c >>> 16);
- b[1] = by;
- c = iHighSize & 0xFF00;
- by = (byte) (c >>> 8);
- b[2] = by;
- c = iHighSize & 0xFF;
- by = (byte) c;
- b[3] = by;
- os.write(b);
- } catch (IOException e) {
- System.err.println(e);
- }
- }
-
- void deleteRemoteFile(String text) {
- try {
- String temp = text;
- writeRfbFileTransferMsg(rfbCommand, rfbCFileDelete, 0, temp.length(), temp);
- } catch (IOException e) {
- System.err.println(e);
- }
- }
-
- void createRemoteDirectory(String text) {
- try {
- String temp = text;
- writeRfbFileTransferMsg(rfbCommand, rfbCDirCreate, 0, temp.length(), temp);
- } catch (IOException e) {
- System.err.println(e);
- }
- }
-
- void requestRemoteFile(String text, String localPath) {
- try {
- String temp = text;
- receivePath = localPath;
- writeRfbFileTransferMsg(rfbFileTransferRequest, 0, 1,
- temp.length(), temp);
- } catch (IOException e) {
- System.err.println(e);
- }
- }
-
-
- void receiveFileHeader() throws IOException {
- fFileReceptionRunning = true;
- fFileReceptionError = false;
- int size = is.readInt();
- int length = is.readInt();
- String tempName = "";
- for (int i = 0; i < length; i++) {
- tempName += (char) is.readUnsignedByte();
- }
-
-
- int sizeH = is.readInt();
- long lSize = ((long) (sizeH) << 32) + size;
- receiveFileSize = lSize;
- fileSize = 0;
- fileChunkCounter = 0;
- String fileName = receivePath;
- fos = new FileOutputStream(fileName);
- writeRfbFileTransferMsg(rfbFileHeader, 0, 0, 0, null);
- }
-
-
- void receiveFileChunk() throws IOException {
-
- int size = is.readInt();
- boolean fCompressed = (size != 0);
- int length = is.readInt();
- fileChunkCounter++;
-
- byte[] ReceptionBuffer = new byte[length + 32];
-
-
- is.readFully(ReceptionBuffer, 0, length);
- if (fCompressed) {
- int bufSize = sz_rfbBlockSize + 1024;
- int decompressedSize = 0;
- byte[] DecompressionBuffer = new byte[bufSize];
- Inflater myInflater = new Inflater();
- myInflater.setInput(ReceptionBuffer);
- try {
- decompressedSize = myInflater.inflate(DecompressionBuffer);
- } catch (DataFormatException e) {
- System.err.println(e);
- }
-
- fos.write(DecompressionBuffer, 0, decompressedSize);
- fileSize += decompressedSize;
- } else {
-
- fos.write(ReceptionBuffer, 0, length);
- fileSize += length;
- }
-
- if (fAbort == true) {
- fAbort = false;
- fFileReceptionError = true;
- writeRfbFileTransferMsg(rfbAbortFileTransfer, 0, 0, 0, null);
- }
-
-
- }
-
- void endOfReceiveFile(boolean fReceptionOk) throws IOException {
- int size = is.readInt();
- int length = is.readInt();
- fileSize = 0;
- fos.close();
- if (fReceptionOk && !fFileReceptionError) {
- } else {
-
- File f = new File(receivePath);
- f.delete();
- }
- fFileReceptionError = false;
- fFileReceptionRunning = false;
- }
-
- void readServerDirectory(String text) {
- try {
- String temp = text;
- writeRfbFileTransferMsg(rfbDirContentRequest, rfbRDirContent, 0, temp.length(), temp);
- } catch (IOException e) {
- System.err.println(e);
- }
- }
-
- void readFTPMsgDriveList() throws IOException {
- String str = "";
- for (int i = 0; i < 4; i++) {
- is.readUnsignedByte();
- }
- int length = is.readInt();
- for (int i = 0; i < length; i++) {
- char temp = (char) is.readUnsignedByte();
- if (temp != '\0') {
- str += temp;
- }
- }
- }
-
-
- void readFTPMsgDirectoryList() throws IOException {
- is.readInt();
- int length = is.readInt();
- if (length == 0) {
- inDirectory2 = false;
- } else {
-
- String str = "";
- for (int i = 0; i < length; i++) {
- char temp = (char) is.readUnsignedByte();
- if (temp != '\0') {
- str += temp;
- }
- }
-
- }
- }
-
-
- void readFTPMsgDirectoryListContent() throws IOException {
- String fileName = "", alternateFileName = "";
- byte contentType = 0;
- int contentParamT = 0;
- int contentParam = 0;
- byte temp = 0;
- int dwFileAttributes, nFileSizeHigh, nFileSizeLow, dwReserved0, dwReserved1;
- long ftCreationTime, ftLastAccessTime, ftLastWriteTime;
- char cFileName, cAlternateFileName;
- int length = 0;
- is.readInt();
- length = is.readInt();
- dwFileAttributes = is.readInt();
- length -= 4;
- ftCreationTime = is.readLong();
- length -= 8;
- ftLastAccessTime = is.readLong();
- length -= 8;
- ftLastWriteTime = is.readLong();
- length -= 8;
- nFileSizeHigh = is.readInt();
- length -= 4;
- nFileSizeLow = is.readInt();
- length -= 4;
- dwReserved0 = is.readInt();
- length -= 4;
- dwReserved1 = is.readInt();
- length -= 4;
- cFileName = (char) is.readUnsignedByte();
- length--;
- while (cFileName != '\0') {
- fileName += cFileName;
- cFileName = (char) is.readUnsignedByte();
- length--;
- }
- cAlternateFileName = (char) is.readByte();
- length--;
- while (length != 0) {
- alternateFileName += cAlternateFileName;
- cAlternateFileName = (char) is.readUnsignedByte();
- length--;
- }
- if (dwFileAttributes == 268435456 || dwFileAttributes == 369098752 || dwFileAttributes == 285212672 || dwFileAttributes == 271056896 || dwFileAttributes == 824705024
- || dwFileAttributes == 807927808 || dwFileAttributes == 371720192 || dwFileAttributes == 369623040) {
- fileName = " [" + fileName + "]";
- remoteDirsList.add(fileName);
- } else {
- remoteFilesList.add(" " + fileName);
- }
-
- }
-
-
- void readFTPMsgDirectoryListEndContent() throws IOException {
- is.readInt();
- int length = is.readInt();
-
- a.clear();
- for (int i = 0; i < remoteDirsList.size(); i++)
- a.add(remoteDirsList.get(i));
- for (int i = 0; i < remoteFilesList.size(); i++)
- a.add(remoteFilesList.get(i));
- remoteDirsList.clear();
- remoteFilesList.clear();
- }
-
-
- void ReceiveDestinationFileChecksums() throws IOException {
- int size = is.readInt();
- int length = is.readInt();
- byte[] ReceptionBuffer = new byte[length + 32];
-
- is.readFully(ReceptionBuffer, 0, length);
-
-
- }
-
-
-
- void writeSetPixelFormat(int bitsPerPixel, int depth, boolean bigEndian, boolean trueColour, int redMax, int greenMax, int blueMax, int redShift, int greenShift, int blueShift, boolean fGreyScale)
- throws IOException {
- byte[] b = new byte[20];
- b[0] = (byte) SetPixelFormat;
- b[4] = (byte) bitsPerPixel;
- b[5] = (byte) depth;
- b[6] = (byte) (bigEndian ? 1 : 0);
- b[7] = (byte) (trueColour ? 1 : 0);
- b[8] = (byte) ((redMax >> 8) & 0xff);
- b[9] = (byte) (redMax & 0xff);
- b[10] = (byte) ((greenMax >> 8) & 0xff);
- b[11] = (byte) (greenMax & 0xff);
- b[12] = (byte) ((blueMax >> 8) & 0xff);
- b[13] = (byte) (blueMax & 0xff);
- b[14] = (byte) redShift;
- b[15] = (byte) greenShift;
- b[16] = (byte) blueShift;
- b[17] = (byte) (fGreyScale ? 1 : 0);
- os.write(b);
- }
-
-
-
-
- void writeFixColourMapEntries(int firstColour, int nColours, int[] red, int[] green, int[] blue) throws IOException {
- byte[] b = new byte[6 + nColours * 6];
- b[0] = (byte) FixColourMapEntries;
- b[2] = (byte) ((firstColour >> 8) & 0xff);
- b[3] = (byte) (firstColour & 0xff);
- b[4] = (byte) ((nColours >> 8) & 0xff);
- b[5] = (byte) (nColours & 0xff);
- for (int i = 0; i < nColours; i++) {
- b[6 + i * 6] = (byte) ((red[i] >> 8) & 0xff);
- b[6 + i * 6 + 1] = (byte) (red[i] & 0xff);
- b[6 + i * 6 + 2] = (byte) ((green[i] >> 8) & 0xff);
- b[6 + i * 6 + 3] = (byte) (green[i] & 0xff);
- b[6 + i * 6 + 4] = (byte) ((blue[i] >> 8) & 0xff);
- b[6 + i * 6 + 5] = (byte) (blue[i] & 0xff);
- }
- os.write(b);
- }
-
-
-
- void writeSetEncodings(int[] encs, int len) throws IOException {
- byte[] b = new byte[4 + 4 * len];
- b[0] = (byte) SetEncodings;
- b[2] = (byte) ((len >> 8) & 0xff);
- b[3] = (byte) (len & 0xff);
- for (int i = 0; i < len; i++) {
- b[4 + 4 * i] = (byte) ((encs[i] >> 24) & 0xff);
- b[5 + 4 * i] = (byte) ((encs[i] >> 16) & 0xff);
- b[6 + 4 * i] = (byte) ((encs[i] >> 8) & 0xff);
- b[7 + 4 * i] = (byte) (encs[i] & 0xff);
- }
- os.write(b);
- }
-
-
-
- void writeClientCutText(String text) throws IOException {
-
- byte[] b = new byte[8 + text.length()];
- b[0] = (byte) ClientCutText;
- b[4] = (byte) ((text.length() >> 24) & 0xff);
- b[5] = (byte) ((text.length() >> 16) & 0xff);
- b[6] = (byte) ((text.length() >> 8) & 0xff);
- b[7] = (byte) (text.length() & 0xff);
- System.arraycopy(text.getBytes(), 0, b, 8, text.length());
- os.write(b);
-
- }
-
-
-
-
-
-
-
- byte[] eventBuf = new byte[72];
- int eventBufLen;
-
- final static int CTRL_MASK = InputEvent.CTRL_MASK;
- final static int SHIFT_MASK = InputEvent.SHIFT_MASK;
- final static int META_MASK = InputEvent.META_MASK;
- final static int ALT_MASK = InputEvent.ALT_MASK;
-
-
-
-
- int pointerMask = 0;
-
-
-
- void writeKeyEvent(int keysym, boolean down) {
- eventBuf[eventBufLen++] = (byte) KeyboardEvent;
- eventBuf[eventBufLen++] = (byte) (down ? 1 : 0);
- eventBuf[eventBufLen++] = (byte) 0;
- eventBuf[eventBufLen++] = (byte) 0;
- eventBuf[eventBufLen++] = (byte) ((keysym >> 24) & 0xff);
- eventBuf[eventBufLen++] = (byte) ((keysym >> 16) & 0xff);
- eventBuf[eventBufLen++] = (byte) ((keysym >> 8) & 0xff);
- eventBuf[eventBufLen++] = (byte) (keysym & 0xff);
- }
-
-
-
- int oldModifiers = 0;
- void writeModifierKeyEvents(int newModifiers) {
- if ((newModifiers & CTRL_MASK) != (oldModifiers & CTRL_MASK))
- writeKeyEvent(0xffe3, (newModifiers & CTRL_MASK) != 0);
- if ((newModifiers & SHIFT_MASK) != (oldModifiers & SHIFT_MASK))
- writeKeyEvent(0xffe1, (newModifiers & SHIFT_MASK) != 0);
- if ((newModifiers & META_MASK) != (oldModifiers & META_MASK))
- writeKeyEvent(0xffe7, (newModifiers & META_MASK) != 0);
- if ((newModifiers & ALT_MASK) != (oldModifiers & ALT_MASK))
- writeKeyEvent(0xffe9, (newModifiers & ALT_MASK) != 0);
- oldModifiers = newModifiers;
- }
-
-
-
-
- void recordCompressedData(byte[] data, int off, int len) throws IOException {
- Deflater deflater = new Deflater();
- deflater.setInput(data, off, len);
- int bufSize = len + len / 100 + 12;
- byte[] buf = new byte[bufSize];
- deflater.finish();
- int compressedSize = deflater.deflate(buf);
- recordCompactLen(compressedSize);
- rec.write(buf, 0, compressedSize);
- }
- void recordCompressedData(byte[] data) throws IOException {
- recordCompressedData(data, 0, data.length);
- }
-
-
-
-
-
- void recordCompactLen(int len) throws IOException {
- byte[] buf = new byte[3];
- int bytes = 0;
- buf[bytes++] = (byte) (len & 0x7F);
- if (len > 0x7F) {
- buf[bytes - 1] |= 0x80;
- buf[bytes++] = (byte) (len >> 7 & 0x7F);
- if (len > 0x3FFF) {
- buf[bytes - 1] |= 0x80;
- buf[bytes++] = (byte) (len >> 14 & 0xFF);
- }
- }
- rec.write(buf, 0, bytes);
- }
-
- public boolean tryAuthenticate(String us, String pw) throws Exception {
- readVersionMsg();
- System.out.println("RFB server supports protocol version " + serverMajor + "." + serverMinor);
- writeVersionMsg();
- int authScheme = readAuthScheme();
- switch (authScheme) {
- case RfbProto.NoAuth:
- System.out.println("No authentication needed");
- return true;
- case RfbProto.VncAuth:
- byte[] challenge = new byte[16];
- is.readFully(challenge);
- if (pw.length() > 8)
- pw = pw.substring(0, 8);
-
-
- int firstZero = pw.indexOf(0);
- if (firstZero != -1) {
- pw = pw.substring(0, firstZero);
- }
- byte[] key = { 0, 0, 0, 0, 0, 0, 0, 0 };
- System.arraycopy(pw.getBytes(), 0, key, 0, pw.length());
- DesCipher des = new DesCipher(key);
- des.encrypt(challenge, 0, challenge, 0);
- des.encrypt(challenge, 8, challenge, 8);
- os.write(challenge);
- int authResult = is.readInt();
- switch (authResult) {
- case RfbProto.VncAuthOK:
- System.out.println("VNC authentication succeeded");
- return true;
- case RfbProto.VncAuthFailed:
- System.out.println("VNC authentication failed");
- break;
- case RfbProto.VncAuthTooMany:
- throw new Exception("VNC authentication failed - too many tries");
- default:
- throw new Exception("Unknown VNC authentication result " + authResult);
- }
- break;
- case RfbProto.MsLogon:
- System.out.println("MS-Logon (DH) detected");
- break;
- default:
- throw new Exception("Unknown VNC authentication scheme " + authScheme);
- }
- return false;
- }
-
- void doProtocolInitialisation() throws IOException {
- writeClientInit();
- readServerInit();
- System.out.println("Desktop name is " + desktopName);
- System.out.println("Desktop size is " + framebufferWidth + " x " + framebufferHeight);
- }
-
- }
|