11/*
2- * Copyright 2011 Miguel Mendoza -
[email protected] , PapaCharlie9, Singh400, EBastard
2+ * Copyright 2011 Miguel Mendoza -
[email protected] , PapaCharlie9, Singh400, EBastard
, Hedius 33 *
44 * Insane Balancer is free software: you can redistribute it and/or modify it under the terms of the
55 * GNU General Public License as published by the Free Software Foundation, either version 3 of the License,
2929using System.Reflection;
3030using Microsoft.CSharp;
3131using System.CodeDom;
32-
33-
32+ using System.IO.Compression;
3433using PRoCon.Core;
3534using PRoCon.Core.Plugin;
3635using PRoCon.Core.Plugin.Commands;
@@ -1214,6 +1213,7 @@ public InsaneLimits()
12141213 this.booleanVariables.Add("use_white_list", false);
12151214 this.booleanVariables.Add("use_direct_fetch", true);
12161215 this.booleanVariables.Add("use_weapon_stats", false);
1216+ this.booleanVariables.Add("use_battlelog_proxy", false);
12171217 this.booleanVariables.Add("use_slow_weapon_stats", false);
12181218 this.booleanVariables.Add("use_stats_log", false);
12191219 this.booleanVariables.Add("use_custom_lists", false);
@@ -1229,7 +1229,7 @@ public InsaneLimits()
12291229 this.booleanVariables.Add("tweet_my_plugin_state", true);
12301230 this.booleanVariables.Add("auto_hide_sections", true);
12311231 this.booleanVariables.Add("smtp_ssl", true);
1232-
1232+
12331233 this.hidden_variables.Add("use_weapon_stats", true);
12341234
12351235 this.booleanVarValidators = new Dictionary<string, booleanVariableValidator>();
@@ -1240,6 +1240,7 @@ public InsaneLimits()
12401240 this.booleanVarValidators.Add("use_white_list", booleanValidator);
12411241 this.booleanVarValidators.Add("use_direct_fetch", booleanValidator);
12421242 this.booleanVarValidators.Add("use_weapon_stats", booleanValidator);
1243+ this.booleanVarValidators.Add("use_battlelog_proxy", booleanValidator);
12431244 this.booleanVarValidators.Add("use_slow_weapon_stats", booleanValidator);
12441245 this.booleanVarValidators.Add("use_stats_log", booleanValidator);
12451246 this.booleanVarValidators.Add("use_custom_lists", booleanValidator);
@@ -1284,6 +1285,8 @@ public InsaneLimits()
12841285 this.stringVariables.Add("smtp_mail", "
[email protected] ");
12851286 this.stringVariables.Add("smtp_password", Decode("dG90YWxseWluc2FuZQ=="));
12861287
1288+ this.stringVariables.Add("proxy_url", "http://127.0.0.1:3128");
1289+
12871290
12881291 this.stringVariables.Add("twitter_verifier_pin", default_PIN_message);
12891292 this.stringVariables.Add("twitter_consumer_key", default_twitter_consumer_key);
@@ -1347,8 +1350,6 @@ public InsaneLimits()
13471350 custom_stmp_group.Add("smtp_ssl");
13481351 settings_group.Add(MailG, custom_stmp_group);
13491352
1350-
1351-
13521353 List<String> custom_twitter_group = new List<string>();
13531354 custom_twitter_group.Add("twitter_setup_account");
13541355 custom_twitter_group.Add("twitter_reset_defaults");
@@ -1359,6 +1360,10 @@ public InsaneLimits()
13591360 custom_twitter_group.Add("twitter_consumer_secret");
13601361 settings_group.Add(TwitterG, custom_twitter_group);
13611362
1363+ List<String> proxy_group = new List<string>();
1364+ proxy_group.Add("proxy_url");
1365+ settings_group.Add(ProxyG, proxy_group);
1366+
13621367 List<String> privacy_policy = new List<string>();
13631368 privacy_policy.Add("tweet_my_server_bans");
13641369 privacy_policy.Add("tweet_my_server_kicks");
@@ -1374,8 +1379,9 @@ public InsaneLimits()
13741379 settings_group_order.Add(MailG, 4);
13751380 settings_group_order.Add(TwitterG, 5);
13761381 settings_group_order.Add(StorageG, 6);
1377- settings_group_order.Add(ListManagerG, 7);
1378- settings_group_order.Add(LimitManagerG, 8);
1382+ settings_group_order.Add(ProxyG, 7);
1383+ settings_group_order.Add(ListManagerG, 8);
1384+ settings_group_order.Add(LimitManagerG, 9);
13791385
13801386 /* Exported Variables are those that should live in the *conf file */
13811387 exported_variables.Add("tweet_my_server_bans");
@@ -3757,7 +3763,7 @@ public string GetPluginName()
37573763
37583764 public string GetPluginVersion()
37593765 {
3760- return "0.9.17.0 ";
3766+ return "0.9.18.1 ";
37613767 }
37623768
37633769 public string GetPluginAuthor()
@@ -4679,6 +4685,18 @@ option. If the <b>Battlelog Cache</b> plugin
46794685 fail since the cache is not available and this setting is False.
46804686 </blockquote>
46814687 </li>
4688+ <li><blockquote><b>use_battlelog_proxy</b><br />
4689+ <i>True</i> - Send requests to web services over a proxy.<br />
4690+ <i>False</i> - Do not use a proxy.<br />
4691+ <br />
4692+ </blockquote>
4693+ </li>
4694+ <li><blockquote><b>proxy_url</b><br />
4695+ <i>(string, url)</i> - Format: http://IP:PORT - http://user:password@IP:PORT<br />
4696+ <br />
4697+ The URL of the proxy server.
4698+ </blockquote>
4699+ </li>
46824700 <li><blockquote><b>use_slow_weapon_stats</b><br />
46834701 <i>False</i> - skip fetching weapon stats for new players<br />
46844702 <i>True</i> - fetch weapon stats for new players<br />
@@ -7389,6 +7407,7 @@ public bool Agreement
73897407 public const String StorageG = "Custom Storage";
73907408 public const String TwitterG = "Custom Twitter";
73917409 public const String SettingsG = "Settings";
7410+ public const String ProxyG = "Proxy for HTTP Requests";
73927411
73937412 public bool shouldSkipGroup(String name)
73947413 {
@@ -7413,6 +7432,9 @@ public bool shouldSkipGroup(String name)
74137432
74147433 if (name.StartsWith(TwitterG) && !getBooleanVarValue("use_custom_twitter"))
74157434 return true;
7435+
7436+ if (name.StartsWith(ProxyG) && !getBooleanVarValue("use_battlelog_proxy"))
7437+ return true;
74167438
74177439
74187440 if (name.StartsWith(PrivacyPolicyG) && !getBooleanVarValue("use_custom_privacy_policy"))
@@ -13654,7 +13676,75 @@ public class BattleLog
1365413676 //private HttpWebRequest req = null;
1365513677 //private CookieContainer cookies = null;
1365613678
13657- WebClient client = null;
13679+ private GZipWebClient client = null;
13680+ private String curAddress = "";
13681+
13682+ public class GZipWebClient : WebClient {
13683+ private String ua;
13684+ private bool compress;
13685+
13686+ public GZipWebClient() {
13687+ this.ua = "Mozilla/5.0 (compatible; PRoCon 1; Insane Limits)";
13688+ base.Headers["User-Agent"] = ua;
13689+ compress = true;
13690+ }
13691+
13692+ public GZipWebClient(bool compress) : this() {
13693+ this.compress = compress;
13694+ }
13695+
13696+ public string GZipDownloadString(string address) {
13697+ return this.GZipDownloadString(new Uri(address));
13698+ }
13699+
13700+ public string GZipDownloadString(Uri address) {
13701+ base.Headers[HttpRequestHeader.UserAgent] = ua;
13702+
13703+ if (compress == false)
13704+ return base.DownloadString(address);
13705+
13706+ base.Headers[HttpRequestHeader.AcceptEncoding] = "gzip";
13707+ var stream = this.OpenRead(address);
13708+ if (stream == null)
13709+ return "";
13710+
13711+ var contentEncoding = ResponseHeaders[HttpResponseHeader.ContentEncoding];
13712+ base.Headers.Remove(HttpRequestHeader.AcceptEncoding);
13713+
13714+ Stream decompressedStream = null;
13715+ StreamReader reader = null;
13716+ if (!string.IsNullOrEmpty(contentEncoding) && contentEncoding.ToLower().Contains("gzip")) {
13717+ decompressedStream = new GZipStream(stream, CompressionMode.Decompress);
13718+ reader = new StreamReader(decompressedStream);
13719+ }
13720+ else {
13721+ reader = new StreamReader(stream);
13722+ }
13723+ var data = reader.ReadToEnd();
13724+ reader.Close();
13725+ decompressedStream?.Close();
13726+ stream.Close();
13727+ return data;
13728+ }
13729+
13730+ public void SetProxy(String proxyURL)
13731+ {
13732+ if(!String.IsNullOrEmpty(proxyURL))
13733+ {
13734+ Uri uri = new Uri(proxyURL);
13735+ this.Proxy = new WebProxy(proxyURL, true);
13736+ if (!String.IsNullOrEmpty(uri.UserInfo))
13737+ {
13738+ string[] parameters = uri.UserInfo.Split(':');
13739+ if (parameters.Length < 2)
13740+ {
13741+ return;
13742+ }
13743+ this.Proxy.Credentials = new NetworkCredential(parameters[0], parameters[1]);
13744+ }
13745+ }
13746+ }
13747+ }
1365813748
1365913749 public BattleLog(InsaneLimits plugin)
1366013750 {
@@ -13665,24 +13755,39 @@ public BattleLog(InsaneLimits plugin)
1366513755 public void CleanUp()
1366613756 {
1366713757 client = null; // Release WebClient to avoid re-use error
13758+ curAddress = "";
1366813759 }
1366913760
13670-
1367113761 private String fetchWebPage(ref String html_data, String url)
1367213762 {
1367313763 try
1367413764 {
1367513765 if (client == null) {
13676- client = new WebClient() ;
13766+ curAddress = null ;
1367713767 String ua = "Mozilla/5.0 (compatible; PRoCon 1; Insane Limits)";
13768+ client = new GZipWebClient(ua);
1367813769 // XXX String ua = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; .NET CLR 3.5.30729)";
1367913770 plugin.DebugWrite("Using user-agent: " + ua, 4);
13680- client.Headers.Add("user-agent", ua);
13771+ }
13772+
13773+ // proxy support
13774+ if (plugin.getBooleanVarValue("use_battlelog_proxy")) {
13775+ // set proxy
13776+ try {
13777+ var address = plugin.getStringVarValue("proxy_url");
13778+ if (curAddress == null || client.Proxy == null || !curAddress.Equals(address)) {
13779+ client.SetProxy(address);
13780+ curAddress = address;
13781+ }
13782+ }
13783+ catch (UriFormatException) {
13784+ plugin.ConsoleError("Invalid Proxy URL set!");
13785+ }
1368113786 }
1368213787
1368313788 DateTime since = DateTime.Now;
1368413789
13685- html_data = client.DownloadString (url);
13790+ html_data = client.GZipDownloadString (url);
1368613791
1368713792 /* TESTS
1368813793 String testUrl = "http://status.savanttools.com/?code=";
0 commit comments