Skip to content

Commit

Permalink
Merge pull request #171 from MortezaBashsiz/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
goingfine authored Mar 3, 2023
2 parents 02eba42 + d7c3c1c commit a73d686
Show file tree
Hide file tree
Showing 15 changed files with 576 additions and 333 deletions.
135 changes: 135 additions & 0 deletions windows/Classes/AppUpdateChecker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WinCFScan.Classes.Config;

namespace WinCFScan.Classes
{
internal class AppUpdateChecker
{
private string localVersion;
private string remoteVersion;

private string remoteVersionUrl = "https://raw.githubusercontent.com/MortezaBashsiz/CFScanner/main/windows/Properties/latest-version";
private string localVersionFileName = "local-version";
private bool foundNewVersion = false;
public UpdateCheckResult updateCheckResult;

public static Version getCurrentVersion()
{
return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
}

public AppUpdateChecker() {
localVersion = getCurrentVersion().ToString();
}

// check for update
public UpdateCheckResult check()
{
if(getRemoteVersion())
{
foundNewVersion = isNewVersionAvailable();
updateCheckResult = foundNewVersion ? UpdateCheckResult.NewVersionAvailable : UpdateCheckResult.NewVersionAvailable;
}
else
{
updateCheckResult = UpdateCheckResult.HasError;
}

return updateCheckResult;
}

public bool isFoundNewVersion()
{
return foundNewVersion;
}

public string getUpdateVersion()
{
return remoteVersion;
}

public bool shouldCheck()
{
return isFileOld(localVersionFileName);
}

// is there new version?
private bool isNewVersionAvailable()
{
return isRemoteVersionValid() && compareVersions();
}

// is valid string with format v:1.0.xxx.xxx
private bool isRemoteVersionValid()
{
return remoteVersion != null && remoteVersion.StartsWith("v:") && remoteVersion.Split(".").Length == 4;
}

// return true if remote version is higher
private bool compareVersions() {

var remoteV = new Version(remoteVersion.Substring(2));
var localV = new Version(localVersion);

return remoteV.CompareTo(localV) == 1;
}

private bool isFileOld(string file)
{
return !File.Exists(file) || (DateTime.Now - File.GetLastWriteTime(file)).TotalDays > 1;
}

private bool saveCurrentLocalVersion()
{
try
{
File.WriteAllText(localVersionFileName, localVersion);
return true;
}
catch (Exception) { }
return false;
}

private bool getRemoteVersion()
{
HttpClient client = new HttpClient();
client.Timeout = TimeSpan.FromSeconds(5);

try
{
remoteVersion = client.GetStringAsync(remoteVersionUrl).Result;

if (isRemoteVersionValid())
{
// whenever check remote version then update timestamp of local version file
// to prevent frequent remote checks
saveCurrentLocalVersion();

return true;
}
}
catch (Exception ex)
{
Tools.logStep($"getRemoteVersion had exception: {ex.Message}");
}
finally
{
client.Dispose();
}

return false;
}
}

public enum UpdateCheckResult {
NewVersionAvailable = 0,
NewVersionNotAvailable = 1,
HasError = 3
}
}
32 changes: 21 additions & 11 deletions windows/Classes/CheckIPWorking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ internal class CheckIPWorking
private string port;
private string v2rayConfigPath;
public long downloadDuration { get; private set; }
private ScanSpeed targetSpeed;

public CheckIPWorking(string ip)
public CheckIPWorking(string ip, ScanSpeed targetSpeed)
{
this.ip = ip;
this.port = getPortByIP();
v2rayConfigPath = $"v2ray-config/generated/config.{ip}.json";
this.targetSpeed = targetSpeed;
}

public CheckIPWorking()
Expand Down Expand Up @@ -116,19 +118,20 @@ private bool checkDownloadSpeed()
Proxy = proxy
};

int timeout = 2;
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");
client.Timeout = TimeSpan.FromSeconds(timeout); // 2 seconds
Tools.logStep($"Start check dl speed, proxy port: {port}, timeout: {timeout} sec, target speed: {targetSpeed.getTargetSpeed():n0} b/s");
Stopwatch sw = new Stopwatch();
try
{
sw.Start();
string dlUrl = "https://" + ConfigManager.Instance.getAppConfig().scanDomain + "/data.100k";
string dlUrl = "https://" + ConfigManager.Instance.getAppConfig().scanDomain + targetSpeed.getTargetFileSize(timeout);
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;
return data.Length == targetSpeed.getTargetSpeed() * timeout;
}
catch (Exception ex)
{
Expand All @@ -148,16 +151,16 @@ private bool createV2rayConfigFile()
try
{
var configTemplate = ConfigManager.Instance.v2rayConfigTemplate;
RealConfig realConfig = ConfigManager.Instance.getRealConfig();
ClientConfig clientConfig = ConfigManager.Instance.getClientConfig();

configTemplate = configTemplate
.Replace("IDID", realConfig.id)
.Replace("IDID", clientConfig.id)
.Replace("PORTPORT", port)
.Replace("HOSTHOST", realConfig.host)
.Replace("CFPORTCFPORT", realConfig.port)
.Replace("RANDOMHOST", realConfig.serverName)
.Replace("HOSTHOST", clientConfig.host)
.Replace("CFPORTCFPORT", clientConfig.port)
.Replace("RANDOMHOST", clientConfig.serverName)
.Replace("IP.IP.IP.IP", this.ip)
.Replace("ENDPOINTENDPOINT", realConfig.path);
.Replace("ENDPOINTENDPOINT", clientConfig.path);

File.WriteAllText(v2rayConfigPath, configTemplate);

Expand Down Expand Up @@ -197,6 +200,13 @@ private bool runV2rayProcess()
Thread.Sleep(1500);
bool wasSuccess = process.Responding && !process.HasExited;
Tools.logStep($"v2ray.exe executed success: {wasSuccess}");

// log error
if (!wasSuccess)
{
Tools.logStep($"v2ray.exe Error: {process.StandardError.ReadToEnd()}");
}

return wasSuccess;
}

Expand Down
4 changes: 2 additions & 2 deletions windows/Classes/Config/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal class AppConfig : ConfigInterface

public string frontDomain { get; set; }
public string scanDomain { get; set; }
public string configRealUrl { get; set; }
public string clientConfigUrl { get; set; }

// load app config
public bool load()
Expand All @@ -41,7 +41,7 @@ public bool load()

public bool isConfigValid()
{
return frontDomain != null && scanDomain != null && configRealUrl != null;
return frontDomain != null && scanDomain != null && clientConfigUrl != null;
}

public AppConfig? getLoadedInstance()
Expand Down
16 changes: 10 additions & 6 deletions windows/Classes/Config/ConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal class ConfigManager
protected string[] mandatoryDirectories = { "v2ray-config", "v2ray-config/generated", "results" }; //this dirs must be existed

protected AppConfig? appConfig;
protected RealConfig? realConfig;
protected ClientConfig? clientConfig;

public string errorMessage { get; set; } = "";
protected bool loadedOK = true;
Expand All @@ -28,7 +28,7 @@ public ConfigManager()
{
if (this.load() && appConfig != null)
{
realConfig = new RealConfig(appConfig);
clientConfig = (new ClientConfig(appConfig)).getLoadedInstance();
}

// set static instance for later access of this instance
Expand Down Expand Up @@ -81,12 +81,16 @@ protected bool load()
return false;
}

return isConfigValid();
return true;
}

public bool isConfigValid()
{
if(appConfig != null && appConfig.isConfigValid() && v2rayConfigTemplate != null && loadedOK)
if(
appConfig != null && appConfig.isConfigValid() &&
v2rayConfigTemplate != null && loadedOK &&
clientConfig != null && clientConfig.isConfigValid()
)
{
return true;
}
Expand All @@ -106,9 +110,9 @@ private void checkDebugEnable()
return this.appConfig;
}

public RealConfig? getRealConfig()
public ClientConfig? getClientConfig()
{
return this.realConfig;
return this.clientConfig;
}
}
}
Loading

0 comments on commit a73d686

Please sign in to comment.