|
| 1 | +// Solve {CAPTCHA_TYPE} using the CaptchaAI API. |
| 2 | +// |
| 3 | +// Usage: |
| 4 | +// dotnet run |
| 5 | +// |
| 6 | +// Requires .env file in parent directory with: |
| 7 | +// CAPTCHAAI_API_KEY and CAPTCHA-specific variables |
| 8 | + |
| 9 | +using System; |
| 10 | +using System.Collections.Generic; |
| 11 | +using System.IO; |
| 12 | +using System.Net.Http; |
| 13 | +using System.Text.Json; |
| 14 | +using System.Threading.Tasks; |
| 15 | + |
| 16 | +class Solve |
| 17 | +{ |
| 18 | + private const string SubmitUrl = "https://ocr.captchaai.com/in.php"; |
| 19 | + private const string ResultUrl = "https://ocr.captchaai.com/res.php"; |
| 20 | + |
| 21 | + private static readonly HashSet<string> AuthErrors = new() |
| 22 | + { "ERROR_WRONG_USER_KEY", "ERROR_KEY_DOES_NOT_EXIST", "IP_BANNED" }; |
| 23 | + private static readonly HashSet<string> BalanceErrors = new() |
| 24 | + { "ERROR_ZERO_BALANCE" }; |
| 25 | + private static readonly HashSet<string> InputErrors = new() |
| 26 | + { "ERROR_PAGEURL", "ERROR_WRONG_GOOGLEKEY", "ERROR_BAD_PARAMETERS", "ERROR_BAD_TOKEN_OR_PAGEURL" }; |
| 27 | + private static readonly HashSet<string> TransientErrors = new() |
| 28 | + { "ERROR_SERVER_ERROR", "ERROR_INTERNAL_SERVER_ERROR" }; |
| 29 | + private static readonly HashSet<string> SolveErrors = new() |
| 30 | + { "ERROR_CAPTCHA_UNSOLVABLE" }; |
| 31 | + private static readonly HashSet<string> ProxyErrors = new() |
| 32 | + { "ERROR_BAD_PROXY", "ERROR_PROXY_CONNECTION_FAILED" }; |
| 33 | + |
| 34 | + private static readonly HttpClient Client = new() { Timeout = TimeSpan.FromSeconds(30) }; |
| 35 | + |
| 36 | + private readonly string _apiKey; |
| 37 | + private readonly int _pollInterval; |
| 38 | + private readonly int _maxTimeout; |
| 39 | + // TODO: Add CAPTCHA-specific fields |
| 40 | + |
| 41 | + public Solve(Dictionary<string, string> env) |
| 42 | + { |
| 43 | + _apiKey = GetEnv(env, "CAPTCHAAI_API_KEY", ""); |
| 44 | + // TODO: Add CAPTCHA-specific environment variables |
| 45 | + _pollInterval = int.Parse(GetEnv(env, "POLL_INTERVAL", "5")); |
| 46 | + _maxTimeout = int.Parse(GetEnv(env, "MAX_TIMEOUT", "120")); |
| 47 | + } |
| 48 | + |
| 49 | + private static string GetEnv(Dictionary<string, string> env, string key, string fallback) |
| 50 | + { |
| 51 | + if (env.TryGetValue(key, out var val) && !string.IsNullOrEmpty(val)) return val; |
| 52 | + var sysVal = Environment.GetEnvironmentVariable(key); |
| 53 | + return !string.IsNullOrEmpty(sysVal) ? sysVal : fallback; |
| 54 | + } |
| 55 | + |
| 56 | + private void ValidateConfig() |
| 57 | + { |
| 58 | + if (string.IsNullOrEmpty(_apiKey) || _apiKey == "YOUR_API_KEY") |
| 59 | + { |
| 60 | + Console.WriteLine("[!] ERROR: CAPTCHAAI_API_KEY is not set."); |
| 61 | + Console.WriteLine(" Copy .env.example to .env and add your real API key."); |
| 62 | + Environment.Exit(1); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private async Task<string> SubmitTaskAsync() |
| 67 | + { |
| 68 | + Console.WriteLine("[*] Submitting CAPTCHA task..."); |
| 69 | + var query = $"key={Uri.EscapeDataString(_apiKey)}" + |
| 70 | + $"&method=userrecaptcha" + // TODO: Change method for your CAPTCHA type |
| 71 | + // TODO: Add CAPTCHA-specific parameters |
| 72 | + $"&json=1"; |
| 73 | + |
| 74 | + string body; |
| 75 | + try { body = await Client.GetStringAsync($"{SubmitUrl}?{query}"); } |
| 76 | + catch (Exception ex) |
| 77 | + { |
| 78 | + Console.WriteLine($"[!] Network error during submission: {ex.Message}"); |
| 79 | + Environment.Exit(1); return ""; |
| 80 | + } |
| 81 | + |
| 82 | + using var doc = JsonDocument.Parse(body); |
| 83 | + var root = doc.RootElement; |
| 84 | + var status = root.GetProperty("status").GetInt32(); |
| 85 | + var request = root.GetProperty("request").ToString(); |
| 86 | + |
| 87 | + if (status != 1) { ClassifyError(request); Environment.Exit(1); } |
| 88 | + |
| 89 | + Console.WriteLine($"[+] Task submitted. ID: {request}"); |
| 90 | + return request; |
| 91 | + } |
| 92 | + |
| 93 | + private async Task<string> PollResultAsync(string taskId) |
| 94 | + { |
| 95 | + Console.WriteLine("[*] Waiting 15s before first poll..."); |
| 96 | + await Task.Delay(15000); |
| 97 | + |
| 98 | + var query = $"key={Uri.EscapeDataString(_apiKey)}&action=get&id={Uri.EscapeDataString(taskId)}&json=1"; |
| 99 | + var elapsed = 15; var attempt = 0; var backoff = _pollInterval; |
| 100 | + |
| 101 | + while (elapsed < _maxTimeout) |
| 102 | + { |
| 103 | + attempt++; |
| 104 | + Console.WriteLine($"[*] Polling for result (attempt {attempt})..."); |
| 105 | + |
| 106 | + string body; |
| 107 | + try { body = await Client.GetStringAsync($"{ResultUrl}?{query}"); } |
| 108 | + catch (Exception ex) |
| 109 | + { |
| 110 | + Console.WriteLine($"[!] Network error during polling: {ex.Message}"); |
| 111 | + await Task.Delay(backoff * 1000); |
| 112 | + elapsed += backoff; backoff = Math.Min(backoff * 2, 30); continue; |
| 113 | + } |
| 114 | + |
| 115 | + using var doc = JsonDocument.Parse(body); |
| 116 | + var root = doc.RootElement; |
| 117 | + var status = root.GetProperty("status").GetInt32(); |
| 118 | + var request = root.GetProperty("request").ToString(); |
| 119 | + |
| 120 | + if (status == 1) return request; |
| 121 | + if (request == "CAPCHA_NOT_READY") |
| 122 | + { |
| 123 | + Console.WriteLine($"[*] Not ready yet, waiting {_pollInterval}s..."); |
| 124 | + await Task.Delay(_pollInterval * 1000); |
| 125 | + elapsed += _pollInterval; backoff = _pollInterval; continue; |
| 126 | + } |
| 127 | + if (TransientErrors.Contains(request)) |
| 128 | + { |
| 129 | + Console.WriteLine($"[!] Transient error: {request}, retrying in {backoff}s..."); |
| 130 | + await Task.Delay(backoff * 1000); |
| 131 | + elapsed += backoff; backoff = Math.Min(backoff * 2, 30); continue; |
| 132 | + } |
| 133 | + if (SolveErrors.Contains(request)) |
| 134 | + { |
| 135 | + Console.WriteLine($"[!] Solve error: {request}"); |
| 136 | + Console.WriteLine(" The CAPTCHA could not be solved. Verify parameters and retry."); |
| 137 | + Environment.Exit(1); |
| 138 | + } |
| 139 | + if (ProxyErrors.Contains(request)) |
| 140 | + { |
| 141 | + Console.WriteLine($"[!] Proxy error: {request}"); |
| 142 | + Console.WriteLine(" Check your proxy configuration or try a different proxy."); |
| 143 | + Environment.Exit(1); |
| 144 | + } |
| 145 | + Console.WriteLine($"[!] Unexpected error: {request}"); |
| 146 | + Environment.Exit(1); |
| 147 | + } |
| 148 | + |
| 149 | + Console.WriteLine($"[!] Timeout: no solution received within {_maxTimeout} seconds."); |
| 150 | + Environment.Exit(1); return ""; |
| 151 | + } |
| 152 | + |
| 153 | + private static void ClassifyError(string error) |
| 154 | + { |
| 155 | + if (AuthErrors.Contains(error)) { Console.WriteLine($"[!] Authentication error: {error}"); Console.WriteLine(" Check your API key at https://captchaai.com/dashboard"); } |
| 156 | + else if (BalanceErrors.Contains(error)) { Console.WriteLine($"[!] Balance error: {error}"); Console.WriteLine(" Top up your account at https://captchaai.com"); } |
| 157 | + else if (InputErrors.Contains(error)) { Console.WriteLine($"[!] Input error: {error}"); Console.WriteLine(" Verify your sitekey and page URL are correct."); } |
| 158 | + else if (ProxyErrors.Contains(error)) { Console.WriteLine($"[!] Proxy error: {error}"); Console.WriteLine(" Check your proxy configuration or try a different proxy."); } |
| 159 | + else Console.WriteLine($"[!] Submission failed: {error}"); |
| 160 | + } |
| 161 | + |
| 162 | + private static Dictionary<string, string> LoadEnv(string path) |
| 163 | + { |
| 164 | + var env = new Dictionary<string, string>(); |
| 165 | + if (!File.Exists(path)) return env; |
| 166 | + foreach (var line in File.ReadAllLines(path)) |
| 167 | + { |
| 168 | + var trimmed = line.Trim(); |
| 169 | + if (string.IsNullOrEmpty(trimmed) || trimmed.StartsWith('#')) continue; |
| 170 | + var idx = trimmed.IndexOf('='); |
| 171 | + if (idx > 0) env[trimmed[..idx].Trim()] = trimmed[(idx + 1)..].Trim(); |
| 172 | + } |
| 173 | + return env; |
| 174 | + } |
| 175 | + |
| 176 | + public static async Task Main(string[] args) |
| 177 | + { |
| 178 | + var env = LoadEnv(Path.Combine("..", ".env")); |
| 179 | + var solver = new Solve(env); |
| 180 | + solver.ValidateConfig(); |
| 181 | + var taskId = await solver.SubmitTaskAsync(); |
| 182 | + var token = await solver.PollResultAsync(taskId); |
| 183 | + var truncated = token.Length > 50 ? token[..50] : token; |
| 184 | + Console.WriteLine($"[+] Solved! Token: {truncated}..."); |
| 185 | + Console.WriteLine($"[+] Full token length: {token.Length} characters"); |
| 186 | + } |
| 187 | +} |
0 commit comments