⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vncviewer.java

📁 一个远程登陆器的原代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

//  Copyright (C) 2002-2005 Ultr@VNC Team.  All Rights Reserved.
//  Copyright (C) 2004 Kenn Min Chong, John Witchel.  All Rights Reserved.
//  Copyright (C) 2004 Alban Chazot.  All Rights Reserved.
//  Copyright (C) 2001,2002 HorizonLive.com, Inc.  All Rights Reserved.
//  Copyright (C) 2002 Constantin Kaplinsky.  All Rights Reserved.
//  Copyright (C) 1999 AT&T Laboratories Cambridge.  All Rights Reserved.
//
//  This is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This software is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this software; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
//  USA.
//

//
// VncViewer.java - the VNC viewer applet.  This class mainly just sets up the
// user interface, leaving it to the VncCanvas to do the actual rendering of
// a VNC desktop.
//

// Alban Chazot - Carmi Grenoble July 5th 2004 
// * Add support for Ultr@VNC mslogon feature.
//   You can now be connected to a Ultr@VNC box with mslogon required.
//   Thanks to Wim Vandersmissen, who provide a TightVNC viewer patch do to so.
//   That give me the idea to provide it in the java viewer too.
//
// * Add ScrollPanel to applet mode
//
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;

  // mslogon support

  boolean mslogon = false;

  // mslogon support end

  //
  // main() is called when run as a java program from the command line.
  // It simply runs the applet inside a newly-created frame.
  //

  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;
  AuthPanel authenticator;
  VncCanvas vc;
  OptionsFrame options;
  ClipboardFrame clipboard;
  RecordingFrame rec;
  FTPFrame ftp; // KMC: FTP Frame declaration

  // Control session recording.
  Object recordingSync;
  String sessionFileName;
  boolean recordingActive;
  boolean recordingStatusChanged;
  String cursorUpdatesDef;
  String eightBitColorsDef;

  // Variables read from parameter values.
  String host;
  int port;
  String passwordParam;
  String encPasswordParam;
  boolean showControls;
  boolean showOfflineDesktop;
  int deferScreenUpdates;
  int deferCursorUpdates;
  int deferUpdateRequests;

  // mslogon support 2
  String usernameParam;
  String encUsernameParam;
  String dm; 
  byte[] domain = new byte[256];
  byte[] user = new byte[256];
  byte[] passwd = new byte[32];
  int i;
  // mslogon support 2 end

  //
  // init()
  //

  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);
    // authenticator = new AuthPanel(false);   // mslogon support : go to connectAndAuthenticate()
    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); // KMC: FTPFrame creation
    rfbThread = new Thread(this);
    rfbThread.start();
  }

  public void update(Graphics g) {
  }

  //
  // run() - executed by the rfbThread to deal with the RFB socket.
  //

  public void run() {

    gridbag = new GridBagLayout();
    vncContainer.setLayout(gridbag);

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbc.anchor = GridBagConstraints.NORTHWEST;

    if (showControls) {
      buttonPanel = new ButtonPanel(this);
      gridbag.setConstraints(buttonPanel, gbc);
      vncContainer.add(buttonPanel);
    }

    try {
      connectAndAuthenticate();

      doProtocolInitialisation();

      vc = new VncCanvas(this);
      gbc.weightx = 1.0;
      gbc.weighty = 1.0;

	// Add ScrollPanel to applet mode

	// Create a panel which itself is resizeable and can hold
	// non-resizeable VncCanvas component at the top left corner.
	Panel canvasPanel = new Panel();
	canvasPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
	canvasPanel.add(vc);

	// Create a ScrollPane which will hold a panel with VncCanvas
	// inside.
	desktopScrollPane = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
	gbc.fill = GridBagConstraints.BOTH;
	gridbag.setConstraints(desktopScrollPane, gbc);
	desktopScrollPane.add(canvasPanel);

      if (inSeparateFrame) {
	// Finally, add our ScrollPane to the Frame window.
	vncFrame.add(desktopScrollPane);
	vncFrame.setTitle(rfb.desktopName);
	vncFrame.pack();
	vc.resizeDesktopFrame();
      } else {
	// Finally, add the scrollable panel component to the Applet.
        gridbag.setConstraints(desktopScrollPane, gbc);
	add(desktopScrollPane);

	// Add ScrollPanel to applet mode end
 
	validate();

      }

      if (showControls)
	buttonPanel.enableButtons();

      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());
      }
    }
    
  }


  //
  // Connect to the RFB server and authenticate the user.
  //

  void connectAndAuthenticate() throws Exception {

    // If "ENCPASSWORD" parameter is set, decrypt the password into
    // the passwordParam string.

    if (encPasswordParam != null) {
      // ENCPASSWORD is hexascii-encoded. Decode.
      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();
      }

      // Decrypt the password.
      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 a password parameter ("PASSWORD" or "ENCPASSWORD") is set,
    // don't ask user a password, get one from passwordParam instead.
    // Authentication failures would be fatal.

    if (passwordParam != null) {
      if (inSeparateFrame) {
	vncFrame.pack();
	vncFrame.show();
      } else {
	validate();
      }
      if (!tryAuthenticate(usernameParam,passwordParam)) {
	throw new Exception("VNC authentication failed");
      }
      return;
    }

    // There is no "PASSWORD" or "ENCPASSWORD" parameters -- ask user
    // for a password, try to authenticate, retry on authentication
    // failures.

    // mslogon support
    //
    // Detect Auth Protocol (Ultr@VNC or the standard One)
    // To know if we must show the username box
    //

    
    prologueDetectAuthProtocol() ;

    authenticator = new AuthPanel(mslogon);
    
    // mslogon support end
    
    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;
    
    gridbag.setConstraints(authenticator, gbc);
    vncContainer.add(authenticator);

    if (inSeparateFrame) {
      vncFrame.pack();
      vncFrame.show();
    } else {
      validate();
      // FIXME: here moveFocusToPasswordField() does not always work
      // under Netscape 4.7x/Java 1.1.5/Linux. It seems like this call
      // is being executed before the password field of the
      // authenticator is fully drawn and activated, therefore
      // requestFocus() does not work. Currently, I don't know how to
      // solve this problem.
      //   -- const
      
     //mslogon support 
      authenticator.moveFocusToUsernameField();
     //mslogon support end
    }

    while (true) {
      // Wait for user entering a password, or a username and a password
      synchronized(authenticator) {
	try {
	  authenticator.wait();
	} catch (InterruptedException e) {
	}
      }

      // Try to authenticate with a given password.
      //mslogon support 
      String us;
      if (mslogon) { us = authenticator.username.getText(); }
      else { us = "";}

      if (tryAuthenticate(us,authenticator.password.getText()))
	break;
      //mslogon support end

      // Retry on authentication failure.
      authenticator.retry();
    }

    vncContainer.remove(authenticator);
  }

  // mslogon support
  //
  // Detect Server rfb Protocol to know the auth Method
  // Perform a connexion to detect the Serverminor 
  //

  void prologueDetectAuthProtocol() throws Exception {

    rfb = new RfbProto(host, port, this);

    rfb.readVersionMsg();

    System.out.println("RFB server supports protocol version " +
		       rfb.serverMajor + "." + rfb.serverMinor);

    // Mslogon support 
    if (rfb.serverMinor == 4) {
	    mslogon = true;    
	    System.out.println("Ultr@VNC mslogon detected");
    }
    
    rfb.writeVersionMsg();

  }
  
  // mslogon support end


  //
  // Try to authenticate with a given password.
  //

  boolean tryAuthenticate(String us, String pw) throws Exception {
    
    rfb = new RfbProto(host, port, this);

    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");
          }
      }
      // mslogon support 
      byte[] challengems = new byte[64];
      if (mslogon) { 
        // copy the us (user) parameter into the user Byte formated variable 
	System.arraycopy(us.getBytes(), 0, user, 0, us.length() );
	// and pad it with Null
	if (us.length() < 256) {
	  for (i=us.length(); i<256; i++){ user[i]= 0; }
	}

	dm = ".";
        // copy the dm (domain) parameter into the domain Byte formated variable 
        System.arraycopy(dm.getBytes(), 0, domain, 0, dm.length() );
	// and pad it with Null
	if (dm.length() < 256) {
	  for (i=dm.length(); i<256; i++){ domain[i]= 0; }
	}
		
        // equivalent of vncEncryptPasswdMS
	 
        // copy the pw (password) parameter into the password Byte formated variable 
        System.arraycopy(pw.getBytes(), 0, passwd, 0, pw.length() );
	// and pad it with Null
	if (pw.length() < 32) {
	  for (i=pw.length(); i<32; i++){ passwd[i]= 0; }
	}

	// Encrypt the full given password
        byte[] fixedkey = {23, 82, 107, 6, 35, 78, 88, 7};
        DesCipher desme = new DesCipher(fixedkey);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -