ProxyDetect.hx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. package neko.net;
  26. typedef ProxySettings = {
  27. var host : String;
  28. var port : Int;
  29. var auth : {
  30. var user : String;
  31. var pass : String;
  32. };
  33. }
  34. class ProxyDetect {
  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. static function detectFF( basedir : String ) {
  60. var files = try neko.FileSystem.readDirectory(basedir) catch( e : Dynamic ) return null;
  61. var profile = null;
  62. for( f in files )
  63. if( f.substr(-8) == ".default" ) {
  64. profile = f;
  65. break;
  66. }
  67. if( profile == null )
  68. return null;
  69. var prefs = neko.io.File.getContent(basedir+"/"+profile+"/prefs.js");
  70. // enabled ?
  71. var r = ~/user_pref\("network\.proxy\.type", 1\);/;
  72. if( !r.match(prefs) )
  73. return null;
  74. // prefs
  75. var r = ~/user_pref\("network\.proxy\.http", "([^"]+)"\);/;
  76. if( !r.match(prefs) )
  77. return null;
  78. var host = r.matched(1);
  79. var r = ~/user_pref\("network\.proxy\.http_port", ([0-9]+)\);/;
  80. if( !r.match(prefs) )
  81. return null;
  82. var port = r.matched(1);
  83. return parseSettings(host+":"+port);
  84. }
  85. static function detectIE() {
  86. var dir = neko.Sys.getEnv("TMP");
  87. if( dir == null )
  88. dir = ".";
  89. var temp = dir + "/proxy.txt";
  90. if( neko.Sys.command('regedit /E "'+temp+'" "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"') != 0 ) {
  91. // might fail without appropriate rights
  92. return null;
  93. }
  94. // it's possible that if registry access was disabled the proxy file is not created
  95. var content = try neko.io.File.getContent(temp) catch( e : Dynamic ) return null;
  96. neko.FileSystem.deleteFile(temp);
  97. // turn 16-bit string into 8-bit one
  98. var b = new StringBuf();
  99. var p = 0;
  100. while( p < content.length ) {
  101. b.addChar(content.charCodeAt(p));
  102. p += 2;
  103. }
  104. content = b.toString();
  105. // enabled ?
  106. var renabled = ~/"ProxyEnable"=dword:0000000(0|1)/;
  107. if( !renabled.match(content) )
  108. return null;
  109. if( renabled.matched(1) == "0" )
  110. return null;
  111. // value ?
  112. var rproxy = ~/"ProxyServer"="([^"]+)"/;
  113. if( !rproxy.match(content) )
  114. return null;
  115. return parseSettings(rproxy.matched(1));
  116. }
  117. static function parseOSXConfiguration(xml : Xml) : Dynamic {
  118. switch( xml.nodeName ) {
  119. case "dict":
  120. var o = {};
  121. var it = xml.elements();
  122. for( x in it ) {
  123. if( x.nodeName != "key" ) throw "Missing key";
  124. var v = x.firstChild().nodeValue;
  125. var r = parseOSXConfiguration(it.next());
  126. Reflect.setField(o,v,r);
  127. }
  128. return o;
  129. case "string":
  130. return xml.firstChild().nodeValue;
  131. case "integer":
  132. return Std.parseInt(xml.firstChild().nodeValue);
  133. case "array":
  134. var a = new Array();
  135. for( x in xml.elements() )
  136. a.push(parseOSXConfiguration(x));
  137. return a;
  138. case "true":
  139. return true;
  140. case "false":
  141. return false;
  142. case "data":
  143. return xml.firstChild().nodeValue;
  144. default:
  145. throw "Invalid value type '"+xml.nodeName+"'";
  146. }
  147. }
  148. static function detectOSX() {
  149. var prefs = neko.io.File.getContent("/Library/Preferences/SystemConfiguration/preferences.plist");
  150. var xml = Xml.parse(prefs).firstElement().firstElement(); // plist/dict
  151. var data : Dynamic = parseOSXConfiguration(xml);
  152. for( nsname in Reflect.fields(data.NetworkServices) ) {
  153. var ns : Dynamic = Reflect.field(data.NetworkServices,nsname);
  154. if( ns.Proxies != null && ns.Proxies.HTTPEnable == 1 )
  155. return { host : ns.Proxies.HTTPProxy, port : ns.Proxies.HTTPPort, auth : null };
  156. }
  157. return null;
  158. }
  159. static function detectAll() : ProxySettings {
  160. switch( neko.Sys.systemName() ) {
  161. case "Windows":
  162. try {
  163. var ffdir = neko.Sys.getEnv("APPDATA")+"/Mozilla/Firefox/Profiles";
  164. var p = detectFF(ffdir);
  165. if( p == null )
  166. throw "No Firefox proxy";
  167. return p;
  168. } catch( e : Dynamic ) {
  169. return detectIE();
  170. }
  171. case "Mac":
  172. var p = detectOSX();
  173. if( p != null )
  174. return p;
  175. var ffdir = neko.Sys.getEnv("HOME")+"/Library/Application Support/Firefox/Profiles";
  176. return detectFF(ffdir);
  177. case "Linux":
  178. var ffdir = neko.Sys.getEnv("HOME")+"/.mozilla/firefox";
  179. return detectFF(ffdir);
  180. default:
  181. throw "This system is not supported";
  182. }
  183. }
  184. static var save : { r : ProxySettings } = null;
  185. public static function detect() {
  186. if( save == null )
  187. save = { r : detectAll() };
  188. return save.r;
  189. }
  190. static function main() {
  191. trace(detect());
  192. }
  193. }