Debian Codegraph @

extra/haxelib_src/hx3compat/std/neko/net/ProxyDetect.hx

raw
1 /*
2 * Copyright (C)2005-2018 Haxe Foundation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22 package neko.net;
23
24 typedef ProxySettings = {
25 var host : String;
26 var port : Int;
27 var auth : {
28 var user : String;
29 var pass : String;
30 };
31 }
32
33 class ProxyDetect {
34
35 static function parseSettings( settings : String ) {
36 var r = ~/^([^:]+):([^@]*)@([^:]+):([0-9]+)$/;
37 if( r.match(settings) )
38 return {
39 auth : {
40 user : r.matched(1),
41 pass : r.matched(2),
42 },
43 host : r.matched(3),
44 port : Std.parseInt(r.matched(4)),
45 };
46 var r = ~/^([^:]+):([0-9]+)$/;
47 if( !r.match(settings) ) {
48 var r2 = ~/http=([^:]+):([0-9]+)/;
49 if( !r2.match(settings) )
50 throw "Invalid settings '"+settings+"'";
51 r = r2;
52 }
53 return {
54 host : r.matched(1),
55 port : Std.parseInt(r.matched(2)),
56 auth : null,
57 };
58 }
59
60 static function detectFF( basedir : String ) {
61 var files = try sys.FileSystem.readDirectory(basedir) catch( e : Dynamic ) return null;
62 var profile = null;
63 for( f in files )
64 if( f.substr(-8) == ".default" ) {
65 profile = f;
66 break;
67 }
68 if( profile == null )
69 return null;
70 var prefs = sys.io.File.getContent(basedir+"/"+profile+"/prefs.js");
71 // enabled ?
72 var r = ~/user_pref\("network\.proxy\.type", 1\);/;
73 if( !r.match(prefs) )
74 return null;
75 // prefs
76 var r = ~/user_pref\("network\.proxy\.http", "([^"]+)"\);/;
77 if( !r.match(prefs) )
78 return null;
79 var host = r.matched(1);
80 var r = ~/user_pref\("network\.proxy\.http_port", ([0-9]+)\);/;
81 if( !r.match(prefs) )
82 return null;
83 var port = r.matched(1);
84 return parseSettings(host+":"+port);
85 }
86
87 static function detectIE() {
88 var dir = Sys.getEnv("TMP");
89 if( dir == null )
90 dir = ".";
91 var temp = dir + "/proxy.txt";
92 if( Sys.command('regedit /E "'+temp+'" "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"') != 0 ) {
93 // might fail without appropriate rights
94 return null;
95 }
96 // it's possible that if registry access was disabled the proxy file is not created
97 var content = try sys.io.File.getContent(temp) catch( e : Dynamic ) return null;
98 sys.FileSystem.deleteFile(temp);
99 // turn 16-bit string into 8-bit one
100 var b = new StringBuf();
101 var p = 0;
102 while( p < content.length ) {
103 b.addChar(content.charCodeAt(p));
104 p += 2;
105 }
106 content = b.toString();
107 // enabled ?
108 var renabled = ~/"ProxyEnable"=dword:0000000(0|1)/;
109 if( !renabled.match(content) )
110 return null;
111 if( renabled.matched(1) == "0" )
112 return null;
113 // value ?
114 var rproxy = ~/"ProxyServer"="([^"]+)"/;
115 if( !rproxy.match(content) )
116 return null;
117 return parseSettings(rproxy.matched(1));
118 }
119
120 static function parseOSXConfiguration(xml : Xml) : Dynamic {
121 switch( xml.nodeName ) {
122 case "dict":
123 var o = {};
124 var it = xml.elements();
125 for( x in it ) {
126 if( x.nodeName != "key" ) throw "Missing key";
127 var v = x.firstChild().nodeValue;
128 var r = parseOSXConfiguration(it.next());
129 Reflect.setField(o,v,r);
130 }
131 return o;
132 case "string":
133 return xml.firstChild().nodeValue;
134 case "integer":
135 return Std.parseInt(xml.firstChild().nodeValue);
136 case "array":
137 var a = new Array();
138 for( x in xml.elements() )
139 a.push(parseOSXConfiguration(x));
140 return a;
141 case "true":
142 return true;
143 case "false":
144 return false;
145 case "data":
146 return xml.firstChild().nodeValue;
147 default:
148 throw "Invalid value type '"+xml.nodeName+"'";
149 }
150 }
151
152 static function detectOSX() {
153 var prefs = sys.io.File.getContent("/Library/Preferences/SystemConfiguration/preferences.plist");
154 var xml = Xml.parse(prefs).firstElement().firstElement(); // plist/dict
155 var data : Dynamic = parseOSXConfiguration(xml);
156 for( nsname in Reflect.fields(data.NetworkServices) ) {
157 var ns : Dynamic = Reflect.field(data.NetworkServices,nsname);
158 if( ns.Proxies != null && ns.Proxies.HTTPEnable == 1 )
159 return { host : ns.Proxies.HTTPProxy, port : ns.Proxies.HTTPPort, auth : null };
160 }
161 return null;
162 }
163
164 static function detectAll() : ProxySettings {
165 switch( Sys.systemName() ) {
166 case "Windows":
167 try {
168 var ffdir = Sys.getEnv("APPDATA")+"/Mozilla/Firefox/Profiles";
169 var p = detectFF(ffdir);
170 if( p == null )
171 throw "No Firefox proxy";
172 return p;
173 } catch( e : Dynamic ) {
174 return detectIE();
175 }
176 case "Mac":
177 var p = detectOSX();
178 if( p != null )
179 return p;
180 var ffdir = Sys.getEnv("HOME")+"/Library/Application Support/Firefox/Profiles";
181 return detectFF(ffdir);
182 case "Linux":
183 var ffdir = Sys.getEnv("HOME")+"/.mozilla/firefox";
184 return detectFF(ffdir);
185 default:
186 throw "This system is not supported";
187 }
188 }
189
190 static var save : { r : ProxySettings } = null;
191
192 public static function detect() {
193 if( save == null )
194 save = { r : try detectAll() catch( e : Dynamic ) null };
195 return save.r;
196 }
197
198 static function main() {
199 trace(detect());
200 }
201
202 }