fix our copyright files
[invirt/packages/invirt-vnc-client.git] / code / VNCProxyConnectSocketFactory.java
1 //
2 //  Copyright (C) 2002 Constantin Kaplinsky, Inc.  All Rights Reserved.
3 //  Copyright 2007 MIT Student Information Processing Board
4 //
5 //  This is free software; you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation; either version 2 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This software is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this software; if not, write to the Free Software
17 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
18 //  USA.
19 //
20
21 //
22 // VNCProxyConnectSocketFactory.java together with VNCProxyConnectSocket.java
23 // implement an alternate way to connect to VNC servers via one or two
24 // VNCProxy proxies supporting the VNCProxy CONNECT method.
25 //
26
27 import java.applet.*;
28 import java.net.*;
29 import javax.net.ssl.*;
30 import java.io.*;
31
32 class VNCProxyConnectSocketFactory implements SocketFactory {
33
34     SSLSocketFactory factory;
35     
36     public VNCProxyConnectSocketFactory() {
37         try {
38             SSLContext c = SSLContext.getInstance("SSL");
39             c.init(null,
40                    new TrustManager[] { new SIPBTrustManager() },
41                    null);
42             factory =
43                 (SSLSocketFactory)c.getSocketFactory();
44         } catch (Exception e) {
45             e.printStackTrace();
46         }
47     }
48
49   public Socket createSocket(String host, int port, Applet applet)
50     throws IOException {
51
52     return createSocket(host, port,
53                         applet.getParameter("VMNAME"),
54                         applet.getParameter("AUTHTOKEN"));
55   }
56
57   public Socket createSocket(String host, int port, String[] args)
58     throws IOException {
59
60     return createSocket(host, port,
61                         readArg(args, "VMNAME"),
62                         readArg(args, "AUTHTOKEN"));
63   }
64
65   public Socket createSocket(String host, int port,
66                              String vmname, String authtoken)
67     throws IOException {
68
69     if (vmname == null || authtoken == null) {
70       System.out.println("Incomplete parameter list for VNCProxyConnectSocket");
71       return new Socket(host, port);
72     }
73
74     System.out.println("VNCProxy CONNECT via proxy " + host +
75                        " port " + port + " to vm " + vmname);
76     SSLSocket ssls = (SSLSocket)factory.createSocket(host, port);
77     ssls.startHandshake();
78     VNCProxyConnectSocketWrapper s =
79       new VNCProxyConnectSocketWrapper(ssls, vmname, authtoken);
80
81     return (Socket)s;
82   }
83
84   private String readArg(String[] args, String name) {
85
86     for (int i = 0; i < args.length; i += 2) {
87       if (args[i].equalsIgnoreCase(name)) {
88         try {
89           return args[i+1];
90         } catch (Exception e) {
91           return null;
92         }
93       }
94     }
95     return null;
96   }
97 }
98