001: // Copyright 07/16/01 Sun Microsystems, Inc. All Rights Reserved.
002:
003: package com.sun.portal.netlet.client.applet;
004:
005: import com.ms.lang.*;
006: import com.ms.security.*;
007: import com.sun.portal.netlet.client.common.LocalhostWarning;
008: import com.sun.portal.netlet.client.common.ProxyInfo;
009: import com.sun.portal.netlet.client.common.ClientUtil;
010: import com.sun.portal.netlet.client.common.ResourceProperties;
011:
012: import java.awt.*;
013: import java.awt.event.ActionEvent;
014: import java.awt.event.ActionListener;
015: import java.util.Enumeration;
016: import java.util.StringTokenizer;
017: import java.util.Vector;
018:
019: public class Win32Reg implements ActionListener {
020:
021: String ProxyString = "notStarted";
022: boolean AutoConfig = false;
023:
024: private String over = null;
025:
026: private Frame f = new Frame();
027: private LocalhostWarning pw;
028:
029: private Vector noProxiesFor = new Vector();
030:
031: //added only for using the parsed PAC file result
032: //if these two following values are null and AutoConfig = true, it failed in parsing the PAC file
033: String autoProxyHost = null;
034: String autoProxyPort = null;
035: String proxySSL = null;
036: String proxySSLPort = null;
037: ProxyInfo pi;
038:
039: public Win32Reg(ProxyInfo pi) {
040: this .pi = pi;
041: }
042:
043: public void startWin32Reg(boolean displayMsg) {
044:
045: pw = new LocalhostWarning(f, this );
046:
047: String keyName = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
048:
049: //System.out.println("Netlet IE Proxy Setup");
050: try {
051: PolicyEngine.assertPermission(PermissionID.REGISTRY);
052:
053: RegKey rk = new RegKey(RegKey.USER_ROOT, keyName,
054: RegKey.KEYOPEN_ALL);
055: boolean proxyEnable = rk.getIntValue("ProxyEnable") == 1 ? true
056: : false;
057:
058: if (proxyEnable) {
059:
060: PolicyEngine.assertPermission(PermissionID.CLIENTSTORE);
061: ProxyString = rk.getStringValue("ProxyServer");
062: System.out.println("Netlet IE Proxy String: "
063: + ProxyString);
064: /*
065: * Added to fix Bug - 4625690
066: * Parse the proxy string, get proxy host and port
067: */
068: String tmp = null;
069: if (ProxyString.indexOf("https") == -1) { // Same proxy for all protocols
070: tmp = ProxyString;
071: } else {
072: tmp = ProxyString.substring(ProxyString
073: .indexOf("https") + 6);
074: }
075: if (tmp.indexOf(';') != -1) {
076: tmp = tmp.substring(0, tmp.indexOf(';'));
077: }
078: proxySSL = tmp.substring(0, tmp.indexOf(':'));
079: proxySSLPort = tmp.substring(tmp.indexOf(':') + 1);
080:
081: PolicyEngine.assertPermission(PermissionID.REGISTRY);
082:
083: //StringTokenizer st = new StringTokenizer(ProxyString, ";");
084: //if the ProxyOverride is blank it fails in IE 5
085: try {
086: over = rk.getStringValue("ProxyOverride");
087: } catch (Exception ex) {
088: ex.printStackTrace();
089: }
090: if (over == null)
091: over = new String("");
092: System.out.println("Netlet IE ProxyOverride: " + over);
093: if (over.indexOf("localhost") < 0) {
094: rk.setValue("ProxyOverride", over + ";localhost");
095: if (over.indexOf("<local>") < 0) {
096: // String os = System.getProperty("os.name");
097: // System.out.println("os version = " + os);
098: // if (os.equals("Windows NT")) {
099: pw.setVisible(true);
100: // }
101: }
102: }
103: StringTokenizer nst = new StringTokenizer(over, ";");
104: while (nst.hasMoreTokens()) {
105: String s = nst.nextToken().toLowerCase().trim();
106: if (!s.equals("")) {
107: noProxiesFor.addElement(s);
108: //System.out.println("no proxy for putting " + s);
109: }
110: }
111: System.out.println("Proxy override - "
112: + noProxiesFor.toString());
113: } else {
114: ProxyString = "0";
115: }
116: String autoConfigURL = rk.getStringValue("AutoConfigURL");
117: if (autoConfigURL.equals("") || autoConfigURL == null) {
118: AutoConfig = false;
119: } else {
120: //give it the permission to connect to the server where the pac file resides
121: try {
122: //if the file is on a webserver
123: com.ms.security.PolicyEngine
124: .assertPermission(com.ms.security.PermissionID.NETIO);
125: //if the file is on a local machine
126: com.ms.security.PolicyEngine
127: .assertPermission(com.ms.security.PermissionID.FILEIO);
128: } catch (Exception e) {
129: e.printStackTrace();
130: }
131:
132: //try to parse the PAC file
133: String inputLine = ClientUtil
134: .parsePACFile(autoConfigURL);
135:
136: if (inputLine.equalsIgnoreCase("DIRECT")) {
137: //no proxy required
138: AutoConfig = false;
139: } else {
140: if (inputLine.startsWith("PROXY")) {
141: /*
142: * @ modified by bshankar@sun.com for Bug - 4625682.
143: * Instead of taking the first proxy, use the first valid proxy
144: */
145: boolean isValid = false;
146: StringTokenizer tok = new StringTokenizer(
147: inputLine, ";");
148: while (tok.hasMoreTokens() && !isValid) {
149: String str = tok.nextToken().trim();
150: String proxyString = (str.indexOf("PROXY") == 0) ? str
151: .substring("PROXY".length()).trim()
152: : str;
153: int index = proxyString.indexOf(":");
154: if (index == -1) {
155: autoProxyHost = proxyString;
156: autoProxyPort = new String("80");
157: } else {
158: autoProxyHost = proxyString.substring(
159: 0, index);
160: autoProxyPort = proxyString
161: .substring(index + 1);
162: }
163: isValid = ClientUtil.isProxySettingsValid(
164: autoProxyHost, Integer
165: .parseInt(autoProxyPort));
166: }
167: if (!isValid) {
168: pi.getProxyWarning().setMessage(
169: ResourceProperties
170: .getString("pwarn.4"));
171: autoProxyHost = "";
172: }
173: AutoConfig = true;
174: } else {
175: //else it would have failed,
176: //donot do anything , so that it will show the message.
177: AutoConfig = true;
178: }
179: }
180: }
181: } catch (RegKeyException e) {
182: //System.out.println("Netlet IE key exception: "+e);
183: }
184: }
185:
186: public void actionPerformed(ActionEvent evt) {
187: if ("OK".equals(evt.getActionCommand())) {
188: pw.setVisible(false);
189: }
190: }
191:
192: public boolean autoConfig() {
193: return (AutoConfig);
194: }
195:
196: public boolean inLocalhostNoProxy(String host) {
197: int i = 0;
198: boolean match = false;
199: boolean done = false;
200: for (Enumeration e = noProxiesFor.elements(); e
201: .hasMoreElements();) {
202: String leftover = host;
203: String s = (String) e.nextElement();
204: StringTokenizer token = new StringTokenizer(s, "*");
205: match = true;
206: while (token.hasMoreTokens()) {
207: String stk = token.nextToken().toLowerCase().trim();
208: // System.out.println("checking " + leftover + " against " + stk);
209: i = leftover.indexOf(stk);
210: if (i == -1) {
211: match = false;
212: break;
213: } else {
214: leftover = leftover.substring(i + stk.length());
215: }
216: }
217: if (match) {
218: break;
219: }
220: }
221: return (match);
222: }
223:
224: public String getLocalhostNoProxy() {
225: return (over);
226: }
227:
228: public String getProxyHost() {
229: if (autoConfig())
230: return autoProxyHost;
231: //return System.getProperty("http.proxyHost");
232: return proxySSL;
233: }
234:
235: public int getProxyPort() {
236: String pport_s = null;
237: int pport = 0;
238: if (autoConfig()) {
239: pport_s = autoProxyPort;
240: } else {
241: //pport_s = System.getProperty("http.proxyPort");
242: pport_s = proxySSLPort;
243: }
244: try {
245: pport = Integer.parseInt(pport_s);
246: } catch (NumberFormatException e) {
247: pport = 0;
248: }
249: return pport;
250: }
251:
252: }
|