|
| 1 | +using System.Runtime.InteropServices; |
| 2 | +using System.Text; |
| 3 | + |
| 4 | +namespace LibCurlImpersonate; |
| 5 | + |
| 6 | +public class CurlHttp : IDisposable |
| 7 | +{ |
| 8 | + static CurlHttp() => LibCurl.curl_global_init(LibCurl.CURL_GLOBAL_DEFAULT); |
| 9 | + |
| 10 | + public static IEnumerable<string> FetchLines( |
| 11 | + string url, |
| 12 | + Action<double>? onSpeed = null, |
| 13 | + bool http3 = false, |
| 14 | + Action<string>? onHttpVersion = null, |
| 15 | + CancellationToken ct = default |
| 16 | + ) |
| 17 | + { |
| 18 | + using var reader = new StringReader(Fetch(url, onSpeed, http3, onHttpVersion, ct)); |
| 19 | + while (reader.ReadLine() is { } line) |
| 20 | + yield return line; |
| 21 | + } |
| 22 | + |
| 23 | + public static string Fetch( |
| 24 | + string url, |
| 25 | + Action<double>? onSpeed = null, |
| 26 | + bool http3 = false, |
| 27 | + Action<string>? onHttpVersion = null, |
| 28 | + CancellationToken ct = default |
| 29 | + ) |
| 30 | + { |
| 31 | + IntPtr handle = LibCurl.curl_easy_init(); |
| 32 | + if (handle == IntPtr.Zero) |
| 33 | + throw new InvalidOperationException("curl_easy_init failed"); |
| 34 | + |
| 35 | + var sb = new StringBuilder(); |
| 36 | + var writePin = GCHandle.Alloc(sb); |
| 37 | + var xferPin = InstallXferCallback(handle, ct, onSpeed: onSpeed); |
| 38 | + try |
| 39 | + { |
| 40 | + LibCurl.curl_easy_impersonate(handle, Impersonation, 1); |
| 41 | + LibCurl.curl_easy_setopt_str(handle, LibCurl.CURLOPT_ACCEPT_ENCODING, ""); |
| 42 | + LibCurl.curl_easy_setopt_str(handle, LibCurl.CURLOPT_URL, url); |
| 43 | + LibCurl.curl_easy_setopt_cb(handle, LibCurl.CURLOPT_WRITEFUNCTION, OnWrite); |
| 44 | + LibCurl.curl_easy_setopt_ptr( |
| 45 | + handle, |
| 46 | + LibCurl.CURLOPT_WRITEDATA, |
| 47 | + GCHandle.ToIntPtr(writePin) |
| 48 | + ); |
| 49 | + LibCurl.curl_easy_setopt_long(handle, LibCurl.CURLOPT_FOLLOWLOCATION, 1L); |
| 50 | + LibCurl.curl_easy_setopt_str(handle, LibCurl.CURLOPT_CAINFO, "cacert.pem"); |
| 51 | + if (http3) |
| 52 | + LibCurl.curl_easy_setopt_long( |
| 53 | + handle, |
| 54 | + LibCurl.CURLOPT_HTTP_VERSION, |
| 55 | + LibCurl.CURL_HTTP_VERSION_3 |
| 56 | + ); |
| 57 | + |
| 58 | + int code = LibCurl.curl_easy_perform(handle); |
| 59 | + if (code == LibCurl.CURLE_ABORTED_BY_CALLBACK) |
| 60 | + ct.ThrowIfCancellationRequested(); |
| 61 | + if (code != LibCurl.CURLE_OK) |
| 62 | + throw new InvalidOperationException( |
| 63 | + $"curl_easy_perform returned error code {code}" |
| 64 | + ); |
| 65 | + |
| 66 | + ReportHttpVersion(handle, onHttpVersion); |
| 67 | + return sb.ToString(); |
| 68 | + } |
| 69 | + finally |
| 70 | + { |
| 71 | + writePin.Free(); |
| 72 | + if (xferPin.IsAllocated) |
| 73 | + xferPin.Free(); |
| 74 | + LibCurl.curl_easy_cleanup(handle); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public static Dictionary<string, string> GetHeaders( |
| 79 | + string url, |
| 80 | + bool http3 = false, |
| 81 | + Action<string>? onHttpVersion = null, |
| 82 | + CancellationToken ct = default |
| 83 | + ) |
| 84 | + { |
| 85 | + IntPtr handle = LibCurl.curl_easy_init(); |
| 86 | + if (handle == IntPtr.Zero) |
| 87 | + throw new InvalidOperationException("curl_easy_init failed"); |
| 88 | + |
| 89 | + var headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
| 90 | + var headerPin = GCHandle.Alloc(headers); |
| 91 | + var xferPin = InstallXferCallback(handle, ct); |
| 92 | + try |
| 93 | + { |
| 94 | + LibCurl.curl_easy_impersonate(handle, Impersonation, 1); |
| 95 | + LibCurl.curl_easy_setopt_str(handle, LibCurl.CURLOPT_URL, url); |
| 96 | + LibCurl.curl_easy_setopt_cb(handle, LibCurl.CURLOPT_HEADERFUNCTION, OnHeader); |
| 97 | + LibCurl.curl_easy_setopt_ptr( |
| 98 | + handle, |
| 99 | + LibCurl.CURLOPT_HEADERDATA, |
| 100 | + GCHandle.ToIntPtr(headerPin) |
| 101 | + ); |
| 102 | + LibCurl.curl_easy_setopt_long(handle, LibCurl.CURLOPT_NOBODY, 1L); |
| 103 | + LibCurl.curl_easy_setopt_str(handle, LibCurl.CURLOPT_CAINFO, "cacert.pem"); |
| 104 | + if (http3) |
| 105 | + LibCurl.curl_easy_setopt_long( |
| 106 | + handle, |
| 107 | + LibCurl.CURLOPT_HTTP_VERSION, |
| 108 | + LibCurl.CURL_HTTP_VERSION_3 |
| 109 | + ); |
| 110 | + |
| 111 | + int code = LibCurl.curl_easy_perform(handle); |
| 112 | + if (code == LibCurl.CURLE_ABORTED_BY_CALLBACK) |
| 113 | + ct.ThrowIfCancellationRequested(); |
| 114 | + if (code != LibCurl.CURLE_OK) |
| 115 | + throw new InvalidOperationException( |
| 116 | + $"curl_easy_perform returned error code {code}" |
| 117 | + ); |
| 118 | + |
| 119 | + ReportHttpVersion(handle, onHttpVersion); |
| 120 | + return headers; |
| 121 | + } |
| 122 | + finally |
| 123 | + { |
| 124 | + headerPin.Free(); |
| 125 | + if (xferPin.IsAllocated) |
| 126 | + xferPin.Free(); |
| 127 | + LibCurl.curl_easy_cleanup(handle); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private static void ReportHttpVersion(IntPtr handle, Action<string>? cb) |
| 132 | + { |
| 133 | + if (cb is null) |
| 134 | + return; |
| 135 | + LibCurl.curl_easy_getinfo_long(handle, LibCurl.CURLINFO_HTTP_VERSION, out long v); |
| 136 | + cb( |
| 137 | + v switch |
| 138 | + { |
| 139 | + 10 => "HTTP/1.0", |
| 140 | + 11 => "HTTP/1.1", |
| 141 | + 20 => "HTTP/2", |
| 142 | + 30 => "HTTP/3", |
| 143 | + _ => $"HTTP/?", |
| 144 | + } |
| 145 | + ); |
| 146 | + } |
| 147 | + |
| 148 | + // Installs CURLOPT_XFERINFOFUNCTION for cancellation and optional progress reporting. |
| 149 | + // Skipped entirely when neither is needed to avoid the NOPROGRESS overhead. |
| 150 | + private static GCHandle InstallXferCallback( |
| 151 | + IntPtr handle, |
| 152 | + CancellationToken ct, |
| 153 | + Action<double>? onProgress = null, |
| 154 | + Action<double>? onSpeed = null |
| 155 | + ) |
| 156 | + { |
| 157 | + if (!ct.CanBeCanceled && onProgress is null && onSpeed is null) |
| 158 | + return default; |
| 159 | + |
| 160 | + LibCurl.XferInfoCallback cb = (_, total, now, _, _) => |
| 161 | + { |
| 162 | + if (ct.IsCancellationRequested) |
| 163 | + return 1; |
| 164 | + if (onProgress is not null && total > 0) |
| 165 | + onProgress(now / (double)total); |
| 166 | + if (onSpeed is not null) |
| 167 | + { |
| 168 | + LibCurl.curl_easy_getinfo_long( |
| 169 | + handle, |
| 170 | + LibCurl.CURLINFO_SPEED_DOWNLOAD_T, |
| 171 | + out long bps |
| 172 | + ); |
| 173 | + onSpeed(bps); |
| 174 | + } |
| 175 | + return 0; |
| 176 | + }; |
| 177 | + var pin = GCHandle.Alloc(cb); |
| 178 | + LibCurl.curl_easy_setopt_long(handle, LibCurl.CURLOPT_NOPROGRESS, 0L); |
| 179 | + LibCurl.curl_easy_setopt_xcb(handle, LibCurl.CURLOPT_XFERINFOFUNCTION, cb); |
| 180 | + return pin; |
| 181 | + } |
| 182 | + |
| 183 | + private static nuint OnWrite(IntPtr data, nuint size, nuint nmemb, IntPtr userdata) |
| 184 | + { |
| 185 | + nuint total = size * nmemb; |
| 186 | + var sb = (StringBuilder)GCHandle.FromIntPtr(userdata).Target!; |
| 187 | + sb.Append(Marshal.PtrToStringUTF8(data, (int)total)); |
| 188 | + return total; |
| 189 | + } |
| 190 | + |
| 191 | + private static unsafe nuint OnWriteFile(IntPtr data, nuint size, nuint nmemb, IntPtr userdata) |
| 192 | + { |
| 193 | + nuint total = size * nmemb; |
| 194 | + var fs = (FileStream)GCHandle.FromIntPtr(userdata).Target!; |
| 195 | + fs.Write(new ReadOnlySpan<byte>((void*)data, (int)total)); |
| 196 | + return total; |
| 197 | + } |
| 198 | + |
| 199 | + private static nuint OnHeader(IntPtr data, nuint size, nuint nmemb, IntPtr userdata) |
| 200 | + { |
| 201 | + nuint total = size * nmemb; |
| 202 | + var line = Marshal.PtrToStringUTF8(data, (int)total)?.Trim(); |
| 203 | + if (string.IsNullOrEmpty(line) || line.StartsWith("HTTP/")) |
| 204 | + return total; |
| 205 | + |
| 206 | + int colon = line.IndexOf(':'); |
| 207 | + if (colon > 0) |
| 208 | + { |
| 209 | + var headers = (Dictionary<string, string>)GCHandle.FromIntPtr(userdata).Target!; |
| 210 | + headers[line[..colon].Trim()] = line[(colon + 1)..].Trim(); |
| 211 | + } |
| 212 | + return total; |
| 213 | + } |
| 214 | + |
| 215 | + public void Dispose() |
| 216 | + { |
| 217 | + LibCurl.curl_global_cleanup(); |
| 218 | + } |
| 219 | + |
| 220 | + private static readonly HashSet<string> Impersonations = |
| 221 | + [ |
| 222 | + "chrome145", |
| 223 | + "chrome142", |
| 224 | + // "firefox147", |
| 225 | + "safari2601", |
| 226 | + ]; |
| 227 | + |
| 228 | + // choose an impersonation randomly to be used for this session's requests to moddb |
| 229 | + private static readonly string Impersonation = Impersonations.ElementAt( |
| 230 | + Random.Shared.Next(Impersonations.Count) |
| 231 | + ); |
| 232 | +} |
0 commit comments