Skip to content

Commit

Permalink
Okay that was pretty dumb... I was just trying to get myself a way to…
Browse files Browse the repository at this point in the history
… fix that SSL thing, so don't think I really need these parts anymore
  • Loading branch information
CatmanFan committed Feb 11, 2025
1 parent 1d931c7 commit fe3838f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 33 deletions.
2 changes: 1 addition & 1 deletion FriishProduce/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ static void Main(string[] args)
}
catch { }

Logger.Log("Opening FriishProduce.");
Config = new(Paths.Config);
Logger.Log("Opening FriishProduce.");
Lang = new Language();

System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(Lang.Current);
Expand Down
2 changes: 0 additions & 2 deletions FriishProduce/_classes/Program/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,9 @@ public class App
public string language { get; set; } = "sys";
#if DEBUG
public bool debug_mode { get; set; } = true;
public bool logger { get; set; } = true;
public bool force_update { get; set; } = false;
#else
public bool debug_mode { get; set; } = false;
public bool logger { get; set; } = false;
public bool force_update { get; set; } = false;
#endif
public bool auto_update { get; set; } = false;
Expand Down
99 changes: 69 additions & 30 deletions FriishProduce/_classes/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ public static void Log(string msg)
Text += Environment.NewLine;
Text += $"[{DateTime.Now.Year}-{DateTime.Now.Month:D2}-{DateTime.Now.Day:D2} {DateTime.Now.Hour:D2}:{DateTime.Now.Minute:D2}:{DateTime.Now.Second:D2}] {msg}";

if (Program.Config.application.logger)
File.WriteAllText(Paths.Log, Text);
// File.WriteAllText(Paths.Log, Text);
}
}

Expand Down Expand Up @@ -158,16 +157,42 @@ public static string MarkdownToHTML(string[] input)

public static class Web
{
public static bool InternetTest(string url = null, bool showDialog = true)
private static bool _compatibilityMode;
private static bool CompatibilityMode
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.SystemDefault;
string URL = url ?? "https://www.example.com/";
bool regionalized = false;
get => _compatibilityMode;
set
{
_compatibilityMode = value;

if (value)
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
}

else
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.SystemDefault;
}
}
}

public static bool InternetTest(string URL = null, bool showDialog = true)
{
if (showDialog)
Program.MainForm?.Wait(true, true, false, 0, 2);

Start:
int timeout = 30;
int region = System.Globalization.CultureInfo.InstalledUICulture.Name.StartsWith("fa") ? 2
: System.Globalization.CultureInfo.InstalledUICulture.Name.Contains("zh-CN") ? 1
: -1;
if (string.IsNullOrWhiteSpace(URL)) URL =
region == 2 ? "https://www.aparat.com/" :
region == 1 ? "http://www.baidu.com/" :
region == 0 ? "https://www.google.com/" :
"https://www.example.com/";

try
{
Expand All @@ -176,26 +201,7 @@ public static bool InternetTest(string url = null, bool showDialog = true)
if (!URL.EndsWith("/")) URL += "/";

Logger.Log($"Sending initial Web request to URL: {URL}");
HttpWebRequest request = null;
try { request = (HttpWebRequest)WebRequest.Create(URL); }
catch
{
Logger.Log("Failed to create request. Changing URL to local search engine.");

if (!regionalized)
{
int region = System.Globalization.CultureInfo.InstalledUICulture.Name.StartsWith("fa") ? 2
: System.Globalization.CultureInfo.InstalledUICulture.Name.Contains("zh-CN") ? 1
: 0;
URL = region == 2 ? "https://www.aparat.com/"
: region == 1 ? "http://www.baidu.com/"
: "https://www.google.com/";

goto Request;
}

else throw;
}
var request = (HttpWebRequest)WebRequest.Create(URL);

URL = request.Address.Authority;
request.Method = "HEAD";
Expand All @@ -211,7 +217,16 @@ public static bool InternetTest(string url = null, bool showDialog = true)
if (CheckDomain(URL, timeout))
{
WebResponse response = null;
response = request.GetResponse();
try { response = request.GetResponse(); }
catch (Exception ex)
{
if (region < 0 && region > 2)
{
region = 0;
goto Request;
}
else { throw ex; }
}

for (int i = 0; i < 2; i++)
{
Expand All @@ -228,14 +243,27 @@ public static bool InternetTest(string url = null, bool showDialog = true)

catch (Exception ex)
{
// Go back to beginning and set compatibility mode to true in event of SSL/TLS secure channel failure (Windows 7)
// ****************
if (ex.GetType() == typeof(WebException) && (ex as WebException).Status == WebExceptionStatus.SecureChannelFailure)
{
if (!CompatibilityMode)
{
Logger.Log("Failed to send initial Web request. Starting over in compatibility mode.");
CompatibilityMode = true;
goto Start;
}
}

if (showDialog)
Program.MainForm?.Wait(false, false, false);

// Automatically return true in event of 429: Too many requests
// ****************
if (ex.GetType() == typeof(WebException) && (ex as WebException).Status == WebExceptionStatus.ProtocolError)
return true;

Logger.Log("Failed to send request. Process halted.");
Logger.Log("Failed to send initial Web request. Process halted.");
throw new Exception(string.Format(Program.Lang.Msg(0, 1), Message(ex, URL)));
}
}
Expand Down Expand Up @@ -296,7 +324,8 @@ private static bool CheckDomain(string URL, int timeout)

public static byte[] Get(string URL, int timeout = 180)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.SystemDefault;
Start:
Logger.Log("Downloading from URL: " + URL);

// Actual web connection is done here
// ****************
Expand All @@ -318,8 +347,18 @@ public static byte[] Get(string URL, int timeout = 180)

catch (Exception ex)
{
if (ex.GetType() == typeof(FileNotFoundException))
// Go back to beginning and set compatibility mode to true in event of SSL/TLS secure channel failure (Windows 7)
// ****************
if (!CompatibilityMode && ex.GetType() == typeof(WebException) && (ex as WebException).Status == WebExceptionStatus.SecureChannelFailure)
{
Logger.Log("Failed to download from URL. Starting over in compatibility mode.");
CompatibilityMode = true;
goto Start;
}

else if (ex.GetType() == typeof(FileNotFoundException))
throw ex;

else
throw new Exception(string.Format(Program.Lang.Msg(0, 1), Message(ex, URL)));
}
Expand Down

0 comments on commit fe3838f

Please sign in to comment.