Skip to content

Commit

Permalink
Adding proxy check
Browse files Browse the repository at this point in the history
  • Loading branch information
grimmgringo committed Aug 9, 2016
1 parent 33ad129 commit 2163ab9
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 6 deletions.
8 changes: 6 additions & 2 deletions PoGo.NecroBot.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using PoGo.NecroBot.CLI.Resources;
using System.Reflection;
using PoGo.NecroBot.CLI.Plugin;
using System.Net.Http;

#endregion

Expand Down Expand Up @@ -188,7 +189,7 @@ private static void Main(string[] args)
ProgressBar.fill(100);

machine.AsyncStart(new VersionCheckState(), session);

if (settings.UseTelegramAPI)
{
session.Telegram = new Logic.Service.TelegramService(settings.TelegramAPIKey, session);
Expand All @@ -203,6 +204,9 @@ private static void Main(string[] args)
}
catch (IOException) { }

DelayingUtils.Delay(2000, 2000);
settings.checkProxy();

QuitEvent.WaitOne();
}

Expand All @@ -220,7 +224,7 @@ private static void SaveLocationToDisk(double lat, double lng)

private static bool CheckKillSwitch()
{
using (var wC = new WebClient())
using (var wC = new NecroWebClient())
{
try
{
Expand Down
3 changes: 3 additions & 0 deletions PoGo.NecroBot.Logic/PoGo.NecroBot.Logic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@
<Compile Include="Logging\ILogger.cs" />
<Compile Include="Utils\JitterUtils.cs" />
<Compile Include="Utils\LocationUtils.cs" />
<Compile Include="Utils\NecroWebClient.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Utils\Statistics.cs" />
<Compile Include="Utils\StringUtils.cs" />
<Compile Include="Utils\WebClientExtensions.cs" />
Expand Down
52 changes: 51 additions & 1 deletion PoGo.NecroBot.Logic/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Security.Cryptography;

Expand Down Expand Up @@ -177,6 +179,37 @@ public void Save()
}
}

public void checkProxy()
{
using (var tempWebClient = new NecroWebClient())
{
string unproxiedIP = WebClientExtensions.DownloadString(tempWebClient, new Uri("https://api.ipify.org/?format=text"));
if (UseProxy)
{
tempWebClient.Proxy = this.InitProxy();
string proxiedIPres = WebClientExtensions.DownloadString(tempWebClient, new Uri("https://api.ipify.org/?format=text"));
string proxiedIP = proxiedIPres == null?"INVALID PROXY": proxiedIPres;
Logger.Write(
$"Your IP is: {unproxiedIP} / Proxy IP is: {proxiedIP}",
LogLevel.Info, (unproxiedIP==proxiedIP)?ConsoleColor.Red:ConsoleColor.Green);

if (unproxiedIP == proxiedIP || proxiedIPres == null)
{
Logger.Write("Press any key to exit so you can fix your proxy settings...",
LogLevel.Info, ConsoleColor.Red);
Console.ReadKey();
Environment.Exit(0);
}
}
else
{
Logger.Write(
$"Your IP is: {unproxiedIP}",
LogLevel.Info, ConsoleColor.Red);
}
}
}

private string RandomString(int length, string alphabet = "abcdefghijklmnopqrstuvwxyz0123456789")
{
var outOfRange = Byte.MaxValue + 1 - (Byte.MaxValue + 1) % alphabet.Length;
Expand Down Expand Up @@ -224,6 +257,18 @@ private void SetDevInfoByKey(string devKey)
throw new ArgumentException("Invalid device info package! Check your auth.config file and make sure a valid DevicePackageName is set. For simple use set it to 'random'. If you have a custom device, then set it to 'custom'.");
}
}

private WebProxy InitProxy()
{
if (!UseProxy) return null;

WebProxy prox = new WebProxy(new System.Uri($"http://{UseProxyHost}:{UseProxyPort}"), false, null);

if (UseProxyAuthentication)
prox.Credentials = new NetworkCredential(UseProxyUsername, UseProxyPassword);

return prox;
}
}

public class GlobalSettings
Expand Down Expand Up @@ -786,10 +831,15 @@ public static GlobalSettings Load(string path, bool boolSkipSave = false)
settings.Save(configFile);
settings.Auth.Load(Path.Combine(profileConfigPath, "auth.json"));
}

return shouldExit ? null : settings;
}

public void checkProxy()
{
Auth.checkProxy();
}

public static bool PromptForSetup(ITranslation translator)
{
Logger.Write(translator.GetTranslation(TranslationString.FirstStartPrompt, "Y", "N"), LogLevel.Warning);
Expand Down
1 change: 0 additions & 1 deletion PoGo.NecroBot.Logic/State/FarmState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public class FarmState : IState
{
public async Task<IState> Execute(ISession session, CancellationToken cancellationToken)
{

if (session.LogicSettings.EvolveAllPokemonAboveIv || session.LogicSettings.EvolveAllPokemonWithEnoughCandy
|| session.LogicSettings.UseLuckyEggsWhileEvolving || session.LogicSettings.KeepPokemonsThatCanEvolve)
{
Expand Down
3 changes: 2 additions & 1 deletion PoGo.NecroBot.Logic/State/VersionCheckState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using PoGo.NecroBot.Logic.Event;
using PoGo.NecroBot.Logic.Logging;
using System.Collections.Generic;
using PoGo.NecroBot.Logic.Utils;

#endregion

Expand Down Expand Up @@ -174,7 +175,7 @@ public static bool DownloadFile(string url, string dest)

private static string DownloadServerVersion()
{
using (var wC = new WebClient())
using (var wC = new NecroWebClient())
{
return wC.DownloadString(VersionUri);
}
Expand Down
15 changes: 15 additions & 0 deletions PoGo.NecroBot.Logic/Utils/NecroWebClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Net;

namespace PoGo.NecroBot.Logic.Utils
{
public class NecroWebClient : WebClient
{
protected override WebRequest GetWebRequest(Uri uri)
{
WebRequest w = base.GetWebRequest(uri);
w.Timeout = 5000;
return w;
}
}
}
29 changes: 28 additions & 1 deletion PoGo.NecroBot.Logic/Utils/WebClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

Expand All @@ -15,7 +16,33 @@ public static string DownloadString(this WebClient webClient, Uri uri)
{
webClient.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
webClient.Encoding = Encoding.UTF8;
var rawData = webClient.DownloadData(uri);
byte[] rawData = null;
string error;
try
{
error = "loading";
rawData = webClient.DownloadData(uri);
}
catch (NullReferenceException)
{
error = null;
}
catch (ArgumentNullException)
{
error = null;
}
catch (WebException)
{
error = null;
}
catch (SocketException)
{
error = null;
}

if ( error == null || rawData == null )
return null;

var encoding = WebUtils.GetEncodingFrom(webClient.ResponseHeaders, Encoding.UTF8);
return encoding.GetString(rawData);
}
Expand Down

0 comments on commit 2163ab9

Please sign in to comment.