3.5.2 commit
This commit is contained in:
214
Web.cs
214
Web.cs
@@ -13,13 +13,14 @@ using System.Text;
|
||||
|
||||
//using System.Configuration;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Net.Security;
|
||||
|
||||
using System.Net.Security;
|
||||
using Org.BouncyCastle.Asn1.Ocsp;
|
||||
|
||||
namespace bdf
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides wrapper around HttpWebRequest, handling 302 redir, binary and certificate issues
|
||||
/// </summary>
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides wrapper around HttpWebRequest, handling 302 redir, binary and certificate issues
|
||||
/// </summary>
|
||||
public class Web
|
||||
{
|
||||
public Web()
|
||||
@@ -111,10 +112,7 @@ namespace bdf
|
||||
bool success = false;
|
||||
HttpWebRequest webRequest = null;
|
||||
|
||||
// Consider setting webRequest.KeepAlive = false; for the logout call to shut down the TCP resources.
|
||||
|
||||
// Set the 'Timeout' property in Milliseconds.
|
||||
//webRequest.Timeout = 1000 * 60 * 3; // 3min
|
||||
// Consider setting webRequest.KeepAlive = false; for the logout call to shut down the TCP resources.
|
||||
|
||||
if (method == "GET")
|
||||
{
|
||||
@@ -122,7 +120,13 @@ namespace bdf
|
||||
webRequest.Method = "GET";
|
||||
webRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36 OPR/76.0.4017.123";
|
||||
|
||||
if (token != "")
|
||||
{ // Try to outsmart Win11/Net481...keep NTLM auth alive
|
||||
webRequest.Credentials = bdf.cache;
|
||||
webRequest.PreAuthenticate = true; //added for Win11 .NET which fails as it does not auto-redirect
|
||||
webRequest.KeepAlive = true;
|
||||
}
|
||||
|
||||
if (token != "")
|
||||
{
|
||||
webRequest.PreAuthenticate = true;
|
||||
//webRequest.Headers.Add("Authorization", "Bearer " + token);
|
||||
@@ -140,7 +144,6 @@ namespace bdf
|
||||
webRequest.Method = method;
|
||||
webRequest.CookieContainer = cookie;
|
||||
|
||||
//webRequest.Timeout = 180000; // 3min
|
||||
webRequest.Timeout = bdf.webMaxWait * 60 * 1000; // convert minutes of wait into milliseconds
|
||||
|
||||
if (proxy != null)
|
||||
@@ -152,6 +155,14 @@ namespace bdf
|
||||
{
|
||||
byte[] buffer = Encoding.ASCII.GetBytes(vars);
|
||||
webRequest = (HttpWebRequest)WebRequest.Create(uri);
|
||||
|
||||
{
|
||||
webRequest.Credentials = bdf.cache;
|
||||
webRequest.PreAuthenticate = true; //added for Win11 .NET which fails as it does not auto-redirect
|
||||
//webRequest.UnsafeAuthenticatedConnectionSharing = true;
|
||||
webRequest.KeepAlive = true;
|
||||
}
|
||||
|
||||
if (proxy != null)
|
||||
{
|
||||
webRequest.Proxy = proxy;
|
||||
@@ -208,7 +219,7 @@ namespace bdf
|
||||
try
|
||||
{
|
||||
//ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072 | (SecurityProtocolType)768 | (SecurityProtocolType)192; //3072
|
||||
//ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls | System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12;
|
||||
//ServicePointManager.SecurityProtocol |= System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12 | System.Net.SecurityProtocolType.Tls13;
|
||||
try
|
||||
{
|
||||
response = (HttpWebResponse)webRequest.GetResponse();
|
||||
@@ -263,7 +274,176 @@ namespace bdf
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
// this version worked for Mono and with a proxy like Fiddler, but not instraight up .NET481 on Win11
|
||||
public static bool working_MakeByteRequest(string method, bool redir, string uri, string vars, ref byte[] data, bool json, ref string token, ref CookieContainer cookie, WebProxy proxy)
|
||||
{
|
||||
bool success = false;
|
||||
HttpWebRequest webRequest = null;
|
||||
|
||||
// Consider setting webRequest.KeepAlive = false; for the logout call to shut down the TCP resources.
|
||||
|
||||
if (method == "GET")
|
||||
{
|
||||
webRequest = (HttpWebRequest)WebRequest.Create(string.Format("{0}{1}", uri, vars));
|
||||
webRequest.Method = "GET";
|
||||
webRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36 OPR/76.0.4017.123";
|
||||
|
||||
{ // Try to outsmart Win11/Net481...
|
||||
webRequest.Credentials = CredentialCache.DefaultNetworkCredentials; //cache;
|
||||
webRequest.UnsafeAuthenticatedConnectionSharing = true;
|
||||
webRequest.UseDefaultCredentials = true;
|
||||
webRequest.PreAuthenticate = true; //added for Win11 .NET which fails as it does not auto-redirect
|
||||
webRequest.KeepAlive = true;
|
||||
}
|
||||
|
||||
if (token != "")
|
||||
{
|
||||
webRequest.PreAuthenticate = true;
|
||||
//webRequest.Headers.Add("Authorization", "Bearer " + token);
|
||||
//webRequest.Headers.Add("Origin", uri); //Feb2023
|
||||
//webRequest.Headers.Add("Authorization", "Negotiate " + token);
|
||||
//webRequest.Headers.Add("Authorization", "Negotiate " + token);
|
||||
//webRequest.Headers.Add("Upgrade-Insecure-Requests", "1");
|
||||
}
|
||||
if (json)
|
||||
{
|
||||
webRequest.Accept = "application/json";
|
||||
}
|
||||
|
||||
webRequest.AllowAutoRedirect = redir;
|
||||
webRequest.Method = method;
|
||||
webRequest.CookieContainer = cookie;
|
||||
|
||||
webRequest.Timeout = bdf.webMaxWait * 60 * 1000; // convert minutes of wait into milliseconds
|
||||
|
||||
if (proxy != null)
|
||||
{
|
||||
webRequest.Proxy = proxy;
|
||||
}
|
||||
}
|
||||
else if (method == "POST")
|
||||
{
|
||||
byte[] buffer = Encoding.ASCII.GetBytes(vars);
|
||||
webRequest = (HttpWebRequest)WebRequest.Create(uri);
|
||||
|
||||
{
|
||||
webRequest.PreAuthenticate = true; //added for Win11 .NET which fails as it does not auto-redirect
|
||||
webRequest.UnsafeAuthenticatedConnectionSharing = true;
|
||||
webRequest.KeepAlive = true;
|
||||
}
|
||||
|
||||
if (proxy != null)
|
||||
{
|
||||
webRequest.Proxy = proxy;
|
||||
}
|
||||
webRequest.Method = "POST";
|
||||
webRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36 OPR/76.0.4017.123";
|
||||
|
||||
if (token != "") // basically is this a JSON request
|
||||
{
|
||||
webRequest.PreAuthenticate = true;
|
||||
webRequest.Headers.Add("Authorization", "Bearer " + token);
|
||||
}
|
||||
if (json)
|
||||
{
|
||||
webRequest.Accept = "application/json";
|
||||
webRequest.ContentType = "application/json";
|
||||
}
|
||||
else
|
||||
{
|
||||
webRequest.ContentType = "application/x-www-form-urlencoded";
|
||||
}
|
||||
|
||||
webRequest.ContentLength = buffer.Length;
|
||||
webRequest.AllowAutoRedirect = redir;
|
||||
webRequest.Method = method;
|
||||
webRequest.CookieContainer = cookie;
|
||||
webRequest.Timeout = bdf.webMaxWait * 60 * 1000; // convert minutes of wait into milliseconds
|
||||
|
||||
if (proxy != null)
|
||||
{
|
||||
webRequest.Proxy = proxy;
|
||||
}
|
||||
|
||||
//We open a stream for writing the postvars
|
||||
Stream PostData = webRequest.GetRequestStream();
|
||||
//Now we write, and afterwards, we close. Closing is always important!
|
||||
PostData.Write(buffer, 0, buffer.Length);
|
||||
PostData.Close();
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.Log("Request not understood");
|
||||
Logger.Log(3, "FAIL - UNKNOWN METHOD");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// allows for validation of SSL conversations
|
||||
ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(
|
||||
ValidateRemoteCertificate
|
||||
);
|
||||
|
||||
HttpWebResponse response = null;
|
||||
try
|
||||
{
|
||||
//ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072 | (SecurityProtocolType)768 | (SecurityProtocolType)192; //3072
|
||||
//ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls | System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12;
|
||||
try
|
||||
{
|
||||
response = (HttpWebResponse)webRequest.GetResponse();
|
||||
}
|
||||
catch (WebException ex)
|
||||
{
|
||||
//string authresp = Web_Auth.HttpAuthHandler.ExtractAuthenticateHeader(ex);
|
||||
//token = authresp.Split(new char[] { ' ' })[1];
|
||||
Logger.Log(0, "webRequest failed. {0}", ex.Message);
|
||||
}
|
||||
//Let's show some information about the response
|
||||
//Logger.Log(6, "YY {0}", response.StatusCode);
|
||||
//Logger.Log(6, "ZZ {0}", response.GetResponseHeader("error"));
|
||||
|
||||
foreach (Cookie cook in response.Cookies)
|
||||
{
|
||||
cookie.Add(cook);
|
||||
}
|
||||
|
||||
//DumpCookies(response.Cookies);
|
||||
|
||||
if ((response.StatusCode == HttpStatusCode.Found) ||
|
||||
(response.StatusCode == HttpStatusCode.OK))
|
||||
{
|
||||
success = true;
|
||||
}
|
||||
|
||||
using (Stream s = response.GetResponseStream())
|
||||
{
|
||||
//byte[] data = ReadStreamFully(s);
|
||||
data = ReadStreamFully(s);
|
||||
s.Close();
|
||||
//System.IO.StreamWriter file = new System.IO.StreamWriter("webcs.txt"); file.BaseStream.Write(data,0,data.Length); file.Close();
|
||||
//object[] ret = { success, Encoding.ASCII.GetString(data) , data };
|
||||
//object[] ret = { success, data };
|
||||
//return true;
|
||||
}
|
||||
|
||||
return success;
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Log(0, "WebRequest error: {0}", ex.Message);
|
||||
//object[] ret = { false, null };
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (response != null)
|
||||
response.Close();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
public static object[] zMakeByteRequest(string method, bool redir, string uri, string vars, ref CookieContainer cookie, WebProxy proxy)
|
||||
{
|
||||
bool success = false;
|
||||
@@ -398,9 +578,9 @@ namespace bdf
|
||||
response.Close();
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
public static byte[] ReadStreamFully (Stream stream)
|
||||
*/
|
||||
|
||||
public static byte[] ReadStreamFully (Stream stream)
|
||||
{
|
||||
byte[] buffer = new byte[32768];
|
||||
using (MemoryStream ms = new MemoryStream())
|
||||
|
||||
Reference in New Issue
Block a user