12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019 |
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import java.net.*;
- import javax.swing.*;
- public class VncViewer extends java.applet.Applet implements java.lang.Runnable, WindowListener {
- boolean inAnApplet = true;
- boolean inSeparateFrame = false;
-
- boolean mslogon = false;
-
-
-
-
-
- public static void main(String[] argv) {
- VncViewer v = new VncViewer();
- v.mainArgs = argv;
- v.inAnApplet = false;
- v.inSeparateFrame = true;
- v.init();
- v.start();
- }
- String[] mainArgs;
- RfbProto rfb;
- Thread rfbThread;
- Frame vncFrame;
- Container vncContainer;
- ScrollPane desktopScrollPane;
- GridBagLayout gridbag;
- ButtonPanel buttonPanel;
-
- VncCanvas vc;
- OptionsFrame options;
- ClipboardFrame clipboard;
- RecordingFrame rec;
- FTPFrame ftp;
-
- Object recordingSync;
- String sessionFileName;
- boolean recordingActive;
- boolean recordingStatusChanged;
- String cursorUpdatesDef;
- String eightBitColorsDef;
-
- String host;
- int port;
- String passwordParam;
- String encPasswordParam;
- boolean showControls;
- boolean showOfflineDesktop;
- int deferScreenUpdates;
- int deferCursorUpdates;
- int deferUpdateRequests;
-
- String serverID;
-
- String usernameParam;
- String encUsernameParam;
- String dm;
- byte[] domain = new byte[256];
- byte[] user = new byte[256];
- byte[] passwd = new byte[32];
- int i;
-
-
-
-
- public void init() {
- readParameters();
- if (inSeparateFrame) {
- vncFrame = new Frame("Ultr@VNC");
- if (!inAnApplet) {
- vncFrame.add("Center", this);
- }
- vncContainer = vncFrame;
- } else {
- vncContainer = this;
- }
- recordingSync = new Object();
- options = new OptionsFrame(this);
- clipboard = new ClipboardFrame(this);
-
- if (RecordingFrame.checkSecurity())
- rec = new RecordingFrame(this);
- sessionFileName = null;
- recordingActive = false;
- recordingStatusChanged = false;
- cursorUpdatesDef = null;
- eightBitColorsDef = null;
- if (inSeparateFrame)
- vncFrame.addWindowListener(this);
- ftp = new FTPFrame(this);
- rfbThread = new Thread(this);
- rfbThread.start();
- }
- public void update(Graphics g) {
- }
-
-
-
- public void run() {
- gridbag = new GridBagLayout();
- vncContainer.setLayout(gridbag);
- GridBagConstraints gbc = new GridBagConstraints();
- gbc.gridwidth = GridBagConstraints.REMAINDER;
- gbc.anchor = GridBagConstraints.NORTHWEST;
-
-
- try {
- connectAndAuthenticate();
- doProtocolInitialisation();
- vc = new VncCanvas(this);
- gbc.weightx = 1.0;
- gbc.weighty = 1.0;
-
-
-
- Panel canvasPanel = new Panel();
- canvasPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
- canvasPanel.add(vc);
-
-
- desktopScrollPane = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
- gbc.fill = GridBagConstraints.BOTH;
- gridbag.setConstraints(desktopScrollPane, gbc);
- desktopScrollPane.add(canvasPanel);
- if (inSeparateFrame) {
-
- vncFrame.add(desktopScrollPane);
- vncFrame.setTitle(rfb.desktopName);
- vncFrame.pack();
- vc.resizeDesktopFrame();
- } else {
-
- gridbag.setConstraints(desktopScrollPane, gbc);
- add(desktopScrollPane);
-
- validate();
- }
- moveFocusToDesktop();
-
-
- vc.processNormalProtocol();
- } catch (NoRouteToHostException e) {
- e.printStackTrace();
- fatalError("Network error: no route to server: " + host);
- } catch (UnknownHostException e) {
- e.printStackTrace();
- fatalError("Network error: server name unknown: " + host);
- } catch (ConnectException e) {
- e.printStackTrace();
- fatalError("Network error: could not connect to server: " + host + ":" + port);
- } catch (EOFException e) {
- e.printStackTrace();
- if (showOfflineDesktop) {
- System.out.println("Network error: remote side closed connection");
- if (vc != null) {
- vc.enableInput(false);
- }
- if (inSeparateFrame) {
- vncFrame.setTitle(rfb.desktopName + " [disconnected]");
- }
- if (rfb != null) {
- rfb.close();
- rfb = null;
- }
- if (showControls && buttonPanel != null) {
- buttonPanel.disableButtonsOnDisconnect();
- if (inSeparateFrame) {
- vncFrame.pack();
- } else {
- validate();
- }
- }
- } else {
- fatalError("Network error: remote side closed connection");
- }
- } catch (IOException e) {
- String str = e.getMessage();
- e.printStackTrace();
- if (str != null && str.length() != 0) {
- fatalError("Network Error: " + str);
- } else {
- fatalError(e.toString());
- }
- } catch (Exception e) {
- String str = e.getMessage();
- e.printStackTrace();
- if (str != null && str.length() != 0) {
- fatalError("Error: " + str);
- } else {
- fatalError(e.toString());
- }
- }
- }
-
-
-
- void connectAndAuthenticate() throws Exception {
-
-
- if (encPasswordParam != null) {
-
- byte[] pw = { 0, 0, 0, 0, 0, 0, 0, 0 };
- int len = encPasswordParam.length() / 2;
- if (len > 8)
- len = 8;
- for (int i = 0; i < len; i++) {
- String hex = encPasswordParam.substring(i * 2, i * 2 + 2);
- Integer x = new Integer(Integer.parseInt(hex, 16));
- pw[i] = x.byteValue();
- }
-
- byte[] key = { 23, 82, 107, 6, 35, 78, 88, 7 };
- DesCipher des = new DesCipher(key);
- des.decrypt(pw, 0, pw, 0);
- passwordParam = new String(pw);
- }
-
-
-
- if (passwordParam != null) {
-
- if (inSeparateFrame) {
- vncFrame.pack();
- vncFrame.show();
- } else {
- validate();
- }
-
-
-
-
-
-
- if (!tryAuthenticate(usernameParam, passwordParam)) {
- throw new Exception("VNC authentication failed");
- }
- return;
- }
-
-
-
-
-
-
-
-
- prologueDetectAuthProtocol();
-
-
- GridBagConstraints gbc = new GridBagConstraints();
- gbc.gridwidth = GridBagConstraints.REMAINDER;
- gbc.anchor = GridBagConstraints.NORTHWEST;
- gbc.weightx = 1.0;
- gbc.weighty = 1.0;
- gbc.ipadx = 100;
- gbc.ipady = 50;
-
-
- if (inSeparateFrame) {
- vncFrame.pack();
- vncFrame.show();
- } else {
- validate();
-
-
-
-
-
-
-
-
-
-
- }
- while (true) {
-
-
- String us="";
- if (mslogon) {
-
- } else {
- us = "";
- }
-
-
- if (tryAuthenticate(us, "dokeos"))
- break;
-
-
- }
-
- }
-
-
-
-
-
- void prologueDetectAuthProtocol() throws Exception {
- rfb = new RfbProto(host, port, this, serverID);
- rfb.readVersionMsg();
- System.out.println("RFB server supports protocol version " + rfb.serverMajor + "." + rfb.serverMinor);
-
- if (rfb.serverMinor == 4) {
- mslogon = true;
- System.out.println("Ultr@VNC mslogon detected");
- }
- rfb.writeVersionMsg();
- }
-
-
-
-
- boolean tryAuthenticate(String us, String pw) throws Exception {
- rfb = new RfbProto(host, port, this, serverID);
- rfb.readVersionMsg();
- System.out.println("RFB server supports protocol version " + rfb.serverMajor + "." + rfb.serverMinor);
- rfb.writeVersionMsg();
- int authScheme = rfb.readAuthScheme();
- switch (authScheme) {
- case RfbProto.NoAuth:
- System.out.println("No authentication needed");
- return true;
- case RfbProto.VncAuth:
- if (mslogon) {
- System.out.println("showing JOptionPane warning.");
- int n = JOptionPane.showConfirmDialog(vncFrame, "The current authentication method does not transfer your password securely." + "Do you want to continue?", "Warning",
- JOptionPane.YES_NO_OPTION);
- if (n != JOptionPane.YES_OPTION) {
- throw new Exception("User cancelled insecure MS-Logon");
- }
- }
-
- byte[] challengems = new byte[64];
- if (mslogon) {
-
- System.arraycopy(us.getBytes(), 0, user, 0, us.length());
-
- if (us.length() < 256) {
- for (i = us.length(); i < 256; i++) {
- user[i] = 0;
- }
- }
- dm = ".";
-
- System.arraycopy(dm.getBytes(), 0, domain, 0, dm.length());
-
- if (dm.length() < 256) {
- for (i = dm.length(); i < 256; i++) {
- domain[i] = 0;
- }
- }
-
-
- System.arraycopy(pw.getBytes(), 0, passwd, 0, pw.length());
-
- if (pw.length() < 32) {
- for (i = pw.length(); i < 32; i++) {
- passwd[i] = 0;
- }
- }
-
- byte[] fixedkey = { 23, 82, 107, 6, 35, 78, 88, 7 };
- DesCipher desme = new DesCipher(fixedkey);
- desme.encrypt(passwd, 0, passwd, 0);
-
-
- rfb.is.readFully(challengems);
- }
-
- byte[] challenge = new byte[16];
- rfb.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);
-
- if (mslogon) {
- for (i = 0; i < 32; i++) {
- challengems[i] = (byte) (passwd[i] ^ challengems[i]);
- }
- rfb.os.write(user);
- rfb.os.write(domain);
- rfb.os.write(challengems);
- }
-
- 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);
- rfb.os.write(challenge);
- int authResult = rfb.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");
- if (AuthMsLogon(us, pw)) {
- return true;
- }
- break;
- default:
- throw new Exception("Unknown VNC authentication scheme " + authScheme);
- }
- return false;
- }
-
-
-
- boolean AuthMsLogon(String us, String pw) throws Exception {
- byte user[] = new byte[256];
- byte passwd[] = new byte[64];
- long gen = rfb.is.readLong();
- long mod = rfb.is.readLong();
- long resp = rfb.is.readLong();
- DH dh = new DH(gen, mod);
- long pub = dh.createInterKey();
- rfb.os.write(DH.longToBytes(pub));
- long key = dh.createEncryptionKey(resp);
- System.out.println("gen=" + gen + ", mod=" + mod + ", pub=" + pub + ", key=" + key);
- DesCipher des = new DesCipher(DH.longToBytes(key));
- System.arraycopy(us.getBytes(), 0, user, 0, us.length());
-
- if (us.length() < 256) {
- for (i = us.length(); i < 256; i++) {
- user[i] = 0;
- }
- }
-
- System.arraycopy(pw.getBytes(), 0, passwd, 0, pw.length());
-
- if (pw.length() < 32) {
- for (i = pw.length(); i < 32; i++) {
- passwd[i] = 0;
- }
- }
-
- des.encryptText(user, user, DH.longToBytes(key));
- des.encryptText(passwd, passwd, DH.longToBytes(key));
- rfb.os.write(user);
- rfb.os.write(passwd);
- int authResult = rfb.is.readInt();
- switch (authResult) {
- case RfbProto.VncAuthOK:
- System.out.println("MS-Logon (DH) authentication succeeded");
- return true;
- case RfbProto.VncAuthFailed:
- System.out.println("MS-Logon (DH) authentication failed");
- break;
- case RfbProto.VncAuthTooMany:
- throw new Exception("MS-Logon (DH) authentication failed - too many tries");
- default:
- throw new Exception("Unknown MS-Logon (DH) authentication result " + authResult);
- }
- return false;
- }
-
-
-
- void doProtocolInitialisation() throws IOException {
- rfb.writeClientInit();
- rfb.readServerInit();
- System.out.println("Desktop name is " + rfb.desktopName);
- System.out.println("Desktop size is " + rfb.framebufferWidth + " x " + rfb.framebufferHeight);
- setEncodings();
- }
-
-
-
- void setEncodings() {
- try {
- if (rfb != null && rfb.inNormalProtocol) {
- rfb.writeSetEncodings(options.encodings, options.nEncodings);
- if (vc != null) {
- vc.softCursorFree();
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
-
- void setCutText(String text) {
- try {
- if (rfb != null && rfb.inNormalProtocol) {
- rfb.writeClientCutText(text);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
-
-
- void setRecordingStatus(String fname) {
- synchronized (recordingSync) {
- sessionFileName = fname;
- recordingStatusChanged = true;
- }
- }
-
-
-
-
- boolean checkRecordingStatus() throws IOException {
- synchronized (recordingSync) {
- if (recordingStatusChanged) {
- recordingStatusChanged = false;
- if (sessionFileName != null) {
- startRecording();
- return true;
- } else {
- stopRecording();
- }
- }
- }
- return false;
- }
-
-
-
- protected void startRecording() throws IOException {
- synchronized (recordingSync) {
- if (!recordingActive) {
-
- cursorUpdatesDef = options.choices[options.cursorUpdatesIndex].getSelectedItem();
- eightBitColorsDef = options.choices[options.eightBitColorsIndex].getSelectedItem();
-
- options.choices[options.cursorUpdatesIndex].select("Disable");
- options.choices[options.cursorUpdatesIndex].setEnabled(false);
- options.setEncodings();
- options.choices[options.eightBitColorsIndex].select("Full");
- options.choices[options.eightBitColorsIndex].setEnabled(false);
- options.setColorFormat();
- } else {
- rfb.closeSession();
- }
- System.out.println("Recording the session in " + sessionFileName);
- rfb.startSession(sessionFileName);
- recordingActive = true;
- }
- }
-
-
-
- protected void stopRecording() throws IOException {
- synchronized (recordingSync) {
- if (recordingActive) {
-
- options.choices[options.cursorUpdatesIndex].select(cursorUpdatesDef);
- options.choices[options.cursorUpdatesIndex].setEnabled(true);
- options.setEncodings();
- options.choices[options.eightBitColorsIndex].select(eightBitColorsDef);
- options.choices[options.eightBitColorsIndex].setEnabled(true);
- options.setColorFormat();
- rfb.closeSession();
- System.out.println("Session recording stopped.");
- }
- sessionFileName = null;
- recordingActive = false;
- }
- }
-
-
-
-
-
-
- public void readParameters() {
- host = readParameter("HOST", !inAnApplet);
- if (host == null) {
- host = getCodeBase().getHost();
- if (host.equals("")) {
- fatalError("HOST parameter not specified");
- }
- }
-
- serverID = readParameter("SERVERID", true);
- String str = readParameter("PORT", true);
- port = Integer.parseInt(str);
- if (inAnApplet) {
- str = readParameter("Open New Window", false);
- if (str != null && str.equalsIgnoreCase("Yes"))
- inSeparateFrame = true;
- }
- encPasswordParam = readParameter("ENCPASSWORD", false);
- if (encPasswordParam == null)
- passwordParam = readParameter("PASSWORD", false);
-
- showControls = true;
- str = readParameter("Show Controls", false);
- if (str != null && str.equalsIgnoreCase("No"))
- showControls = false;
-
- showOfflineDesktop = false;
- str = readParameter("Show Offline Desktop", false);
- if (str != null && str.equalsIgnoreCase("Yes"))
- showOfflineDesktop = true;
-
- deferScreenUpdates = readIntParameter("Defer screen updates", 20);
- deferCursorUpdates = readIntParameter("Defer cursor updates", 10);
- deferUpdateRequests = readIntParameter("Defer update requests", 50);
- }
- public String readParameter(String name, boolean required) {
- if (inAnApplet) {
- String s = getParameter(name);
- if ((s == null) && required) {
- fatalError(name + " parameter not specified");
- }
- return s;
- }
- for (int i = 0; i < mainArgs.length; i += 2) {
- if (mainArgs[i].equalsIgnoreCase(name)) {
- try {
- return mainArgs[i + 1];
- } catch (Exception e) {
- if (required) {
- fatalError(name + " parameter not specified");
- }
- return null;
- }
- }
- }
- if (required) {
- fatalError(name + " parameter not specified");
- }
- return null;
- }
- int readIntParameter(String name, int defaultValue) {
- String str = readParameter(name, false);
- int result = defaultValue;
- if (str != null) {
- try {
- result = Integer.parseInt(str);
- } catch (NumberFormatException e) {
- }
- }
- return result;
- }
-
-
-
-
- void moveFocusToDesktop() {
- if (vncContainer != null) {
- if (vc != null && vncContainer.isAncestorOf(vc)) {
- vc.requestFocus();
- }
- }
- }
-
-
-
- boolean disconnectRequested = false;
- synchronized public void disconnect() {
- disconnectRequested = true;
- if (rfb != null) {
- rfb.close();
- rfb = null;
- }
- System.out.println("Disconnect");
- options.dispose();
- clipboard.dispose();
- if (rec != null)
- rec.dispose();
- if (inAnApplet) {
- vncContainer.removeAll();
- Label errLabel = new Label("Disconnected");
- errLabel.setFont(new Font("Helvetica", Font.PLAIN, 12));
- vncContainer.setLayout(new FlowLayout(FlowLayout.LEFT, 30, 30));
- vncContainer.add(errLabel);
- if (inSeparateFrame) {
- vncFrame.pack();
- } else {
- validate();
- }
- rfbThread.stop();
- } else {
- System.exit(0);
- }
- }
-
-
-
- synchronized public void fatalError(String str) {
- if (rfb != null) {
- rfb.close();
- rfb = null;
- }
- System.out.println(str);
- if (disconnectRequested) {
-
-
- disconnectRequested = false;
- return;
- }
- if (inAnApplet) {
- vncContainer.removeAll();
- Label errLabel = new Label(str);
- errLabel.setFont(new Font("Helvetica", Font.PLAIN, 12));
- vncContainer.setLayout(new FlowLayout(FlowLayout.LEFT, 30, 30));
- vncContainer.add(errLabel);
- if (inSeparateFrame) {
- vncFrame.pack();
- } else {
- validate();
- }
- Thread.currentThread().stop();
- } else {
- System.exit(1);
- }
- }
-
-
-
- public void destroy() {
- vncContainer.removeAll();
- options.dispose();
- clipboard.dispose();
- if (ftp != null)
- ftp.dispose();
- if (rec != null)
- rec.dispose();
- if (rfb != null)
- rfb.close();
- if (inSeparateFrame)
- vncFrame.dispose();
- }
-
-
-
- public void windowClosing(WindowEvent evt) {
- if (rfb != null)
- disconnect();
- vncFrame.dispose();
- if (!inAnApplet) {
- System.exit(0);
- }
- }
-
-
-
- public void windowActivated(WindowEvent evt) {
- }
-
-
-
- public void windowDeactivated(WindowEvent evt) {
- }
- public void windowOpened(WindowEvent evt) {
- }
- public void windowClosed(WindowEvent evt) {
- }
- public void windowIconified(WindowEvent evt) {
- }
- public void windowDeiconified(WindowEvent evt) {
- }
- }
|