home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 April / PCO0499.ISO / filesbbs / mac / vnc-mac.hqx / binary / Server / VNCServer.rsrc / JAVA_135_vncViewer.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-01-25  |  7.3 KB  |  347 lines

  1. import java.applet.Applet;
  2. import java.awt.Button;
  3. import java.awt.Container;
  4. import java.awt.Event;
  5. import java.awt.FlowLayout;
  6. import java.awt.Frame;
  7. import java.awt.Graphics;
  8. import java.awt.GridBagConstraints;
  9. import java.awt.GridBagLayout;
  10. import java.awt.Label;
  11. import java.awt.Panel;
  12. import java.io.IOException;
  13.  
  14. public class vncviewer extends Applet implements Runnable {
  15.    boolean inAnApplet = true;
  16.    // $FF: renamed from: f java.awt.Frame
  17.    Frame field_0;
  18.    String[] mainArgs;
  19.    String host;
  20.    int port;
  21.    rfbProto rfb;
  22.    Thread rfbThread;
  23.    GridBagLayout gridbag;
  24.    Panel buttonPanel;
  25.    Button disconnectButton;
  26.    Button optionsButton;
  27.    Button clipboardButton;
  28.    Button ctrlAltDelButton;
  29.    optionsFrame options;
  30.    clipboardFrame clipboard;
  31.    authenticationPanel authenticator;
  32.    boolean gotFocus = false;
  33.  
  34.    public static void main(String[] var0) {
  35.       vncviewer var1 = new vncviewer();
  36.       var1.mainArgs = var0;
  37.       var1.inAnApplet = false;
  38.       var1.field_0 = new Frame("VNC");
  39.       var1.field_0.add("Center", var1);
  40.       var1.init();
  41.       ((Applet)var1).start();
  42.    }
  43.  
  44.    public void init() {
  45.       this.readParameters();
  46.       this.options = new optionsFrame(this);
  47.       this.clipboard = new clipboardFrame(this);
  48.       this.authenticator = new authenticationPanel();
  49.       this.rfbThread = new Thread(this);
  50.       this.rfbThread.start();
  51.    }
  52.  
  53.    public void update(Graphics var1) {
  54.    }
  55.  
  56.    public void run() {
  57.       this.gridbag = new GridBagLayout();
  58.       ((Container)this).setLayout(this.gridbag);
  59.       this.buttonPanel = new Panel();
  60.       this.buttonPanel.setLayout(new FlowLayout(0, 0, 0));
  61.       this.disconnectButton = new Button("Disconnect");
  62.       this.disconnectButton.disable();
  63.       this.buttonPanel.add(this.disconnectButton);
  64.       this.optionsButton = new Button("Options");
  65.       this.buttonPanel.add(this.optionsButton);
  66.       this.clipboardButton = new Button("Clipboard");
  67.       this.clipboardButton.disable();
  68.       this.buttonPanel.add(this.clipboardButton);
  69.       this.ctrlAltDelButton = new Button("Send Ctrl-Alt-Del");
  70.       this.ctrlAltDelButton.disable();
  71.       this.buttonPanel.add(this.ctrlAltDelButton);
  72.       GridBagConstraints var1 = new GridBagConstraints();
  73.       var1.gridwidth = 0;
  74.       var1.anchor = 18;
  75.       this.gridbag.setConstraints(this.buttonPanel, var1);
  76.       ((Container)this).add(this.buttonPanel);
  77.  
  78.       try {
  79.          this.connectAndAuthenticate();
  80.          this.doProtocolInitialisation();
  81.          vncCanvas var2 = new vncCanvas(this);
  82.          var1.weightx = (double)1.0F;
  83.          var1.weighty = (double)1.0F;
  84.          this.gridbag.setConstraints(var2, var1);
  85.          ((Container)this).add(var2);
  86.          if (!this.inAnApplet) {
  87.             this.field_0.setTitle(this.rfb.desktopName);
  88.             this.field_0.pack();
  89.          } else {
  90.             ((Container)this).validate();
  91.          }
  92.  
  93.          this.disconnectButton.enable();
  94.          this.clipboardButton.enable();
  95.          this.ctrlAltDelButton.enable();
  96.          var2.processNormalProtocol();
  97.       } catch (Exception var3) {
  98.          ((Throwable)var3).printStackTrace();
  99.          this.fatalError(((Throwable)var3).toString());
  100.       }
  101.    }
  102.  
  103.    void connectAndAuthenticate() throws IOException {
  104.       GridBagConstraints var1 = new GridBagConstraints();
  105.       var1.gridwidth = 0;
  106.       var1.anchor = 18;
  107.       var1.weightx = (double)1.0F;
  108.       var1.weighty = (double)1.0F;
  109.       var1.ipadx = 100;
  110.       var1.ipady = 50;
  111.       this.gridbag.setConstraints(this.authenticator, var1);
  112.       ((Container)this).add(this.authenticator);
  113.       ((Container)this).validate();
  114.       if (!this.inAnApplet) {
  115.          this.field_0.pack();
  116.          this.field_0.show();
  117.       }
  118.  
  119.       boolean var2 = false;
  120.  
  121.       while(!var2) {
  122.          authenticationPanel var3 = this.authenticator;
  123.          synchronized(var3){}
  124.  
  125.          try {
  126.             try {
  127.                this.authenticator.wait();
  128.             } catch (InterruptedException var10) {
  129.             }
  130.          } catch (Throwable var11) {
  131.             throw var11;
  132.          }
  133.  
  134.          this.rfb = new rfbProto(this.host, this.port, this);
  135.          this.rfb.readVersionMsg();
  136.          System.out.println("RFB server supports protocol version " + this.rfb.serverMajor + "." + this.rfb.serverMinor);
  137.          this.rfb.writeVersionMsg();
  138.          switch (this.rfb.readAuthScheme()) {
  139.             case 1:
  140.                System.out.println("No authentication needed");
  141.                var2 = true;
  142.                break;
  143.             case 2:
  144.                byte[] var12 = new byte[16];
  145.                this.rfb.is.readFully(var12);
  146.                String var4 = this.authenticator.password.getText();
  147.                if (var4.length() > 8) {
  148.                   var4 = var4.substring(0, 8);
  149.                }
  150.  
  151.                if (var4.length() != 0) {
  152.                   byte[] var5 = new byte[8];
  153.                   var4.getBytes(0, var4.length(), var5, 0);
  154.  
  155.                   for(int var6 = var4.length(); var6 < 8; ++var6) {
  156.                      var5[var6] = 0;
  157.                   }
  158.  
  159.                   DesCipher var13 = new DesCipher(var5);
  160.                   var13.encrypt(var12, 0, var12, 0);
  161.                   var13.encrypt(var12, 8, var12, 8);
  162.                   this.rfb.os.write(var12);
  163.                   int var7 = this.rfb.is.readInt();
  164.                   switch (var7) {
  165.                      case 0:
  166.                         System.out.println("VNC authentication succeeded");
  167.                         var2 = true;
  168.                         break;
  169.                      case 1:
  170.                         System.out.println("VNC authentication failed");
  171.                         this.authenticator.retry();
  172.                         break;
  173.                      case 2:
  174.                         throw new IOException("VNC authentication failed - too many tries");
  175.                      default:
  176.                         throw new IOException("Unknown VNC authentication result " + var7);
  177.                   }
  178.                } else {
  179.                   this.authenticator.retry();
  180.                }
  181.          }
  182.       }
  183.  
  184.       ((Container)this).remove(this.authenticator);
  185.    }
  186.  
  187.    void doProtocolInitialisation() throws IOException {
  188.       System.out.println("sending client init");
  189.       this.rfb.writeClientInit();
  190.       this.rfb.readServerInit();
  191.       System.out.println("Desktop name is " + this.rfb.desktopName);
  192.       System.out.println("Desktop size is " + this.rfb.framebufferWidth + " x " + this.rfb.framebufferHeight);
  193.       this.setEncodings();
  194.    }
  195.  
  196.    void setEncodings() {
  197.       try {
  198.          if (this.rfb != null && this.rfb.inNormalProtocol) {
  199.             this.rfb.writeSetEncodings(this.options.encodings, this.options.nEncodings);
  200.             return;
  201.          }
  202.       } catch (Exception var2) {
  203.          ((Throwable)var2).printStackTrace();
  204.       }
  205.  
  206.    }
  207.  
  208.    void setCutText(String var1) {
  209.       try {
  210.          if (this.rfb != null && this.rfb.inNormalProtocol) {
  211.             this.rfb.writeClientCutText(var1);
  212.             return;
  213.          }
  214.       } catch (Exception var3) {
  215.          ((Throwable)var3).printStackTrace();
  216.       }
  217.  
  218.    }
  219.  
  220.    public synchronized boolean action(Event var1, Object var2) {
  221.       if (var1.target == this.optionsButton) {
  222.          if (this.options.isVisible()) {
  223.             this.options.hide();
  224.          } else {
  225.             this.options.show();
  226.          }
  227.       } else if (var1.target == this.disconnectButton) {
  228.          System.out.println("disconnect");
  229.          this.options.dispose();
  230.          this.clipboard.dispose();
  231.          if (this.inAnApplet) {
  232.             ((Container)this).removeAll();
  233.             this.rfb.close();
  234.             this.rfb = null;
  235.             Label var3 = new Label("Disconnected");
  236.             ((Container)this).setLayout(new FlowLayout(0, 30, 30));
  237.             ((Container)this).add(var3);
  238.             ((Container)this).validate();
  239.             this.rfbThread.stop();
  240.          } else {
  241.             System.exit(1);
  242.          }
  243.       } else if (var1.target == this.clipboardButton) {
  244.          if (this.clipboard.isVisible()) {
  245.             this.clipboard.hide();
  246.          } else {
  247.             this.clipboard.show();
  248.          }
  249.       } else if (var1.target == this.ctrlAltDelButton) {
  250.          try {
  251.             Event var5 = new Event((Object)null, 0, (Object)null);
  252.             var5.key = 127;
  253.             var5.modifiers = 10;
  254.             var5.id = 401;
  255.             this.rfb.writeKeyEvent(var5);
  256.             var5.id = 402;
  257.             this.rfb.writeKeyEvent(var5);
  258.          } catch (Exception var4) {
  259.             ((Throwable)var4).printStackTrace();
  260.          }
  261.       }
  262.  
  263.       return false;
  264.    }
  265.  
  266.    public boolean gotFocus(Event var1, Object var2) {
  267.       this.gotFocus = true;
  268.       return true;
  269.    }
  270.  
  271.    public boolean lostFocus(Event var1, Object var2) {
  272.       this.gotFocus = false;
  273.       return true;
  274.    }
  275.  
  276.    void encryptBytes(byte[] var1, String var2) {
  277.       byte[] var3 = new byte[8];
  278.       var2.getBytes(0, var2.length(), var3, 0);
  279.  
  280.       for(int var4 = var2.length(); var4 < 8; ++var4) {
  281.          var3[var4] = 0;
  282.       }
  283.  
  284.       DesCipher var5 = new DesCipher(var3);
  285.       var5.encrypt(var1, 0, var1, 0);
  286.       var5.encrypt(var1, 8, var1, 8);
  287.    }
  288.  
  289.    public void readParameters() {
  290.       this.host = this.readParameter("HOST", !this.inAnApplet);
  291.       if (this.host == null) {
  292.          this.host = ((Applet)this).getCodeBase().getHost();
  293.          if (this.host.equals("")) {
  294.             this.fatalError("HOST parameter not specified");
  295.          }
  296.       }
  297.  
  298.       String var1 = this.readParameter("PORT", true);
  299.       this.port = Integer.parseInt(var1);
  300.    }
  301.  
  302.    public String readParameter(String var1, boolean var2) {
  303.       if (this.inAnApplet) {
  304.          String var5 = ((Applet)this).getParameter(var1);
  305.          if (var5 == null && var2) {
  306.             this.fatalError(var1 + " parameter not specified");
  307.          }
  308.  
  309.          return var5;
  310.       } else {
  311.          for(int var3 = 0; var3 < this.mainArgs.length; var3 += 2) {
  312.             if (this.mainArgs[var3].equalsIgnoreCase(var1)) {
  313.                try {
  314.                   return this.mainArgs[var3 + 1];
  315.                } catch (Exception var4) {
  316.                   if (var2) {
  317.                      this.fatalError(var1 + " parameter not specified");
  318.                   }
  319.  
  320.                   return null;
  321.                }
  322.             }
  323.          }
  324.  
  325.          if (var2) {
  326.             this.fatalError(var1 + " parameter not specified");
  327.          }
  328.  
  329.          return null;
  330.       }
  331.    }
  332.  
  333.    public void fatalError(String var1) {
  334.       System.out.println(var1);
  335.       if (this.inAnApplet) {
  336.          ((Container)this).removeAll();
  337.          Label var2 = new Label(var1);
  338.          ((Container)this).setLayout(new FlowLayout(0, 30, 30));
  339.          ((Container)this).add(var2);
  340.          ((Container)this).validate();
  341.          Thread.currentThread().stop();
  342.       } else {
  343.          System.exit(1);
  344.       }
  345.    }
  346. }
  347.