Skip to content

Commit

Permalink
Merge pull request MortezaBashsiz#146 from MortezaBashsiz/dev
Browse files Browse the repository at this point in the history
Added some new features to Windows app
  • Loading branch information
goingfine authored Mar 1, 2023
2 parents 12d8711 + 57ec47f commit 9595f47
Show file tree
Hide file tree
Showing 16 changed files with 972 additions and 160 deletions.
45 changes: 40 additions & 5 deletions windows/Classes/CFIPList.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,78 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using WinCFScan.Classes.IP;


namespace WinCFScan.Classes
{
// load cloudflare ip ranges from 'cf.local.iplist' file
internal class CFIPList
{
protected string[] ipList { get; set; }
public List<RangeInfo> validIPRanges = new();
public uint totalIPs = 0;

public CFIPList(string fileName = "cf.local.iplist")
public CFIPList(string fileName)
{
loadList(fileName);
}

private bool loadList(string fileName)
{
if (!File.Exists(fileName))
if (!File.Exists(fileName) || (new FileInfo(fileName)).Length > 2 * 1_000_000)
{
return false;
}

string fileData = File.ReadAllText(fileName);

ipList = fileData.Split();
validateIPList(fileData.Split());

return true;
}

private void validateIPList(string[] loadedIPList)
{
validIPRanges.Clear();
totalIPs = 0;
foreach (var ipRange in loadedIPList)
{
if (IPAddressExtensions.isValidIPRange(ipRange))
{
var rangeTotalIPs = IPAddressExtensions.getIPRangeTotalIPs(ipRange);
if (rangeTotalIPs > 0)
{
this.validIPRanges.Add(new RangeInfo(ipRange, rangeTotalIPs));
totalIPs += rangeTotalIPs;
}
}
}
}

public string[] getIPList()
{
return ipList;
return validIPRanges.Select(x => x.rangeText).ToArray();
}

public bool isIPListValid()
{
return ipList != null && ipList.Length > 0;
return validIPRanges.Count > 0 && totalIPs > 0;
}
}

public class RangeInfo{
public string rangeText;
public uint totalIps;

public RangeInfo(string rangeText, uint totalIps)
{
this.rangeText = rangeText;
this.totalIps = totalIps;
}
}

}
106 changes: 65 additions & 41 deletions windows/Classes/CheckIPWorking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,22 @@ public CheckIPWorking()
{
}

public bool check()
public bool check()
{
bool success = false;
Tools.logStep("\n ------- Start IP Check ------- ");

// first of all quick test on fronting domain through cloudflare
if(! checkFronting())
return false;
if(checkFronting())
{
// then test quality of connection by downloading small file through v2ray vpn
success = checkV2ray();

}

Tools.logStep("\n------- End IP Check -------\n");
return success;

// then test quality of connection by downloading small file through v2ray vpn
return checkV2ray();
}

public bool checkV2ray() {
Expand All @@ -59,6 +67,46 @@ public bool checkV2ray() {
return success;
}

public bool checkFronting(bool withCustumDNSResolver = true, int timeout = 1)
{
DnsHandler dnsHandler;
HttpClient client;
if (withCustumDNSResolver)
{
dnsHandler = new DnsHandler(new CustomDnsResolver(ip));
client = new HttpClient(dnsHandler);
}
else
{
client = new HttpClient();
}

//ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
client.Timeout = TimeSpan.FromSeconds(timeout);

Tools.logStep($"Start fronting check, timeout: {timeout}, Resolver IP: {ip}, withCustumDNSResolver: {withCustumDNSResolver.ToString()}");

try
{
Stopwatch sw = new Stopwatch();
string frUrl = "https://" + ConfigManager.Instance.getAppConfig()?.frontDomain;
Tools.logStep($"Starting fronting check with url: {frUrl}");
var html = client.GetStringAsync(frUrl).Result;
Tools.logStep($"Fronting check done in {sw.ElapsedMilliseconds:n0} ms, content: '{html.Substring(0, 50)}'");
return true;
}
catch (Exception ex)
{
Tools.logStep($"Fronting check had exception: {ex.Message}");
return false;
}
finally
{
client.Dispose();
}

}

private bool checkDownloadSpeed()
{
var proxy = new WebProxy();
Expand All @@ -70,17 +118,21 @@ private bool checkDownloadSpeed()

var client = new HttpClient(handler);
client.Timeout = TimeSpan.FromSeconds(2); // 2 seconds
Tools.logStep($"Start check dl speed, proxy port: {port}, timeout: {client.Timeout.TotalSeconds} sec");
Stopwatch sw = new Stopwatch();
try
{
sw.Start();
string dlUrl = "https://" + ConfigManager.Instance.getAppConfig().scanDomain + "/data.100k";
Tools.logStep($"Starting dl url: {dlUrl}");
var data = client.GetStringAsync(dlUrl).Result;

Tools.logStep($"*** Download success in {sw.ElapsedMilliseconds:n0} ms, dl size: {data.Length:n0} bytes for IP {ip}");

return data.Length > 90_000;
}
catch (Exception ex)
{
Tools.logStep($"dl had exception: {ex.Message}");
return false;
}
finally
Expand Down Expand Up @@ -111,8 +163,9 @@ private bool createV2rayConfigFile()

return true;
}
catch (Exception)
catch (Exception ex)
{
Tools.logStep($"createV2rayConfigFile has exception: {ex.Message}");
return false;
}

Expand All @@ -139,43 +192,14 @@ private bool runV2rayProcess()
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.Arguments = $"run -config=\"{v2rayConfigPath}\"";
Tools.logStep($"Starting v2ray.exe with arg: {startInfo.Arguments}");
process = Process.Start(startInfo);
Thread.Sleep(1500);
return process.Responding && ! process.HasExited;
bool wasSuccess = process.Responding && !process.HasExited;
Tools.logStep($"v2ray.exe executed success: {wasSuccess}");
return wasSuccess;
}

public bool checkFronting(bool withCstumDNSResolver = true, int timeout = 1)
{
DnsHandler dnsHandler;
HttpClient client;
if (withCstumDNSResolver)
{
dnsHandler = new DnsHandler(new CustomDnsResolver(ip));
client = new HttpClient(dnsHandler);
}
else
{
client = new HttpClient();
}

//ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
client.Timeout = TimeSpan.FromSeconds(timeout);

try
{
string frUrl = "https://" + ConfigManager.Instance.getAppConfig()?.frontDomain;
var html = client.GetStringAsync(frUrl).Result;

return true;
}
catch (Exception ex)
{
return false;
}
finally {
client.Dispose();
}

}

}
}
1 change: 1 addition & 0 deletions windows/Classes/Config/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public bool load()
}
catch (Exception ex)
{
Tools.logStep($"AppConfig.load() had exception: {ex.Message}");
return false;
}

Expand Down
11 changes: 11 additions & 0 deletions windows/Classes/Config/ConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace WinCFScan.Classes.Config
internal class ConfigManager
{
protected string v2rayTemplateConfigFileName = "v2ray-config/config.json.template";
protected string enableDebugFileName = "enable-debug"; // debug enabler file
public string? v2rayConfigTemplate { get; private set; }
protected string[] mandatoryDirectories = { "v2ray-config", "v2ray-config/generated", "results" }; //this dirs must be existed

Expand All @@ -21,6 +22,7 @@ internal class ConfigManager
protected bool loadedOK = true;

public static ConfigManager? Instance { get; private set; }
public bool enableDebug = false;

public ConfigManager()
{
Expand All @@ -31,6 +33,8 @@ public ConfigManager()

// set static instance for later access of this instance
Instance = this;

checkDebugEnable();
}

protected bool load()
Expand Down Expand Up @@ -72,6 +76,7 @@ protected bool load()
}
catch(Exception ex)
{
Tools.logStep($"ConfigManager.load() had exception: {ex.Message}");
errorMessage = ex.Message;
return false;
}
Expand All @@ -89,6 +94,12 @@ public bool isConfigValid()
return false;
}

// debugging enabled from outside?
private void checkDebugEnable()
{
enableDebug = File.Exists(enableDebugFileName) || File.Exists(enableDebugFileName + ".txt");
}


public AppConfig? getAppConfig()
{
Expand Down
1 change: 1 addition & 0 deletions windows/Classes/Config/RealConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public bool remoteUpdateConfigReal()
}
catch (Exception ex)
{
Tools.logStep($"remoteUpdateConfigReal() had exception: {ex.Message}");
return false;
}
finally
Expand Down
Loading

0 comments on commit 9595f47

Please sign in to comment.