Created
January 3, 2026 13:55
-
-
Save demirdegerli/69e143be18e0b941f2b11284a0503714 to your computer and use it in GitHub Desktop.
Parser script for Tor Snowflake Metrics API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import re | |
| import requests | |
| # Replace with your actual metrics endpoint | |
| url = "http://ubuntu:1080/internal/metrics" | |
| url2 = "http://192.168.1.2:1080/internal/metrics" | |
| # Download metrics text | |
| resp = None | |
| try: | |
| resp = requests.get(url) | |
| except: | |
| resp = requests.get(url2) | |
| metrics = resp.text | |
| # Parse relevant metrics | |
| timeouts = 0 | |
| total_connections = 0 | |
| connections_by_country = {} | |
| inbound = 0 | |
| outbound = 0 | |
| for line in metrics.splitlines(): | |
| if line.startswith("tor_snowflake_proxy_connection_timeouts_total"): | |
| timeouts = int(float(line.split()[-1])) | |
| elif line.startswith("tor_snowflake_proxy_connections_total"): | |
| match = re.search(r'country="([A-Z]{2})"', line) | |
| country = match.group(1) if match else "?" | |
| count = int(float(line.split()[-1])) | |
| connections_by_country[country] = connections_by_country.get(country, 0) + count | |
| total_connections += count | |
| elif line.startswith("tor_snowflake_proxy_traffic_inbound_bytes_total"): | |
| inbound = int(float(line.split()[-1])) | |
| elif line.startswith("tor_snowflake_proxy_traffic_outbound_bytes_total"): | |
| outbound = int(float(line.split()[-1])) | |
| # Manual ISO country name + emoji flag | |
| country_map = { | |
| "?": "Unknown [?]", | |
| "AW": "Aruba ๐ฆ๐ผ", | |
| "AF": "Afghanistan ๐ฆ๐ซ", | |
| "AO": "Angola ๐ฆ๐ด", | |
| "AI": "Anguilla ๐ฆ๐ฎ", | |
| "AX": "ร land Islands ๐ฆ๐ฝ", | |
| "AL": "Albania ๐ฆ๐ฑ", | |
| "AD": "Andorra ๐ฆ๐ฉ", | |
| "AE": "United Arab Emirates ๐ฆ๐ช", | |
| "AR": "Argentina ๐ฆ๐ท", | |
| "AM": "Armenia ๐ฆ๐ฒ", | |
| "AS": "American Samoa ๐ฆ๐ธ", | |
| "AQ": "Antarctica ๐ฆ๐ถ", | |
| "TF": "French Southern Territories ๐น๐ซ", | |
| "AG": "Antigua and Barbuda ๐ฆ๐ฌ", | |
| "AU": "Australia ๐ฆ๐บ", | |
| "AT": "Austria ๐ฆ๐น", | |
| "AZ": "Azerbaijan ๐ฆ๐ฟ", | |
| "BI": "Burundi ๐ง๐ฎ", | |
| "BE": "Belgium ๐ง๐ช", | |
| "BJ": "Benin ๐ง๐ฏ", | |
| "BF": "Burkina Faso ๐ง๐ซ", | |
| "BD": "Bangladesh ๐ง๐ฉ", | |
| "BG": "Bulgaria ๐ง๐ฌ", | |
| "BH": "Bahrain ๐ง๐ญ", | |
| "BS": "Bahamas ๐ง๐ธ", | |
| "BA": "Bosnia and Herzegovina ๐ง๐ฆ", | |
| "BY": "Belarus ๐ง๐พ", | |
| "BZ": "Belize ๐ง๐ฟ", | |
| "BM": "Bermuda ๐ง๐ฒ", | |
| "BO": "Bolivia ๐ง๐ด", | |
| "BR": "Brazil ๐ง๐ท", | |
| "BB": "Barbados ๐ง๐ง", | |
| "BN": "Brunei Darussalam ๐ง๐ณ", | |
| "BT": "Bhutan ๐ง๐น", | |
| "BV": "Bouvet Island ๐ง๐ป", | |
| "BW": "Botswana ๐ง๐ผ", | |
| "CF": "Central African Republic ๐จ๐ซ", | |
| "CA": "Canada ๐จ๐ฆ", | |
| "CC": "Cocos (Keeling) Islands ๐จ๐จ", | |
| "CH": "Switzerland ๐จ๐ญ", | |
| "CL": "Chile ๐จ๐ฑ", | |
| "CN": "China ๐จ๐ณ", | |
| "CI": "Cรดte d'Ivoire ๐จ๐ฎ", | |
| "CM": "Cameroon ๐จ๐ฒ", | |
| "CD": "Congo, The Democratic Republic of the ๐จ๐ฉ", | |
| "CG": "Congo ๐จ๐ฌ", | |
| "CK": "Cook Islands ๐จ๐ฐ", | |
| "CO": "Colombia ๐จ๐ด", | |
| "KM": "Comoros ๐ฐ๐ฒ", | |
| "CV": "Cabo Verde ๐จ๐ป", | |
| "CR": "Costa Rica ๐จ๐ท", | |
| "CU": "Cuba ๐จ๐บ", | |
| "CW": "Curaรงao ๐จ๐ผ", | |
| "CX": "Christmas Island ๐จ๐ฝ", | |
| "CY": "Cyprus ๐จ๐พ", | |
| "CZ": "Czechia ๐จ๐ฟ", | |
| "DE": "Germany ๐ฉ๐ช", | |
| "DJ": "Djibouti ๐ฉ๐ฏ", | |
| "DM": "Dominica ๐ฉ๐ฒ", | |
| "DK": "Denmark ๐ฉ๐ฐ", | |
| "DO": "Dominican Republic ๐ฉ๐ด", | |
| "DZ": "Algeria ๐ฉ๐ฟ", | |
| "EC": "Ecuador ๐ช๐จ", | |
| "EG": "Egypt ๐ช๐ฌ", | |
| "ER": "Eritrea ๐ช๐ท", | |
| "EH": "Western Sahara ๐ช๐ญ", | |
| "ES": "Spain ๐ช๐ธ", | |
| "EE": "Estonia ๐ช๐ช", | |
| "ET": "Ethiopia ๐ช๐น", | |
| "FI": "Finland ๐ซ๐ฎ", | |
| "FJ": "Fiji ๐ซ๐ฏ", | |
| "FK": "Falkland Islands ๐ซ๐ฐ", | |
| "FM": "Micronesia ๐ซ๐ฒ", | |
| "FO": "Faroe Islands ๐ซ๐ด", | |
| "FR": "France ๐ซ๐ท", | |
| "GA": "Gabon ๐ฌ๐ฆ", | |
| "GB": "United Kingdom ๐ฌ๐ง", | |
| "GE": "Georgia ๐ฌ๐ช", | |
| "GG": "Guernsey ๐ฌ๐ฌ", | |
| "GH": "Ghana ๐ฌ๐ญ", | |
| "GI": "Gibraltar ๐ฌ๐ฎ", | |
| "GN": "Guinea ๐ฌ๐ณ", | |
| "GP": "Guadeloupe ๐ฌ๐ต", | |
| "GM": "Gambia ๐ฌ๐ฒ", | |
| "GW": "Guinea-Bissau ๐ฌ๐ผ", | |
| "GQ": "Equatorial Guinea ๐ฌ๐ถ", | |
| "GR": "Greece ๐ฌ๐ท", | |
| "GD": "Grenada ๐ฌ๐ฉ", | |
| "GL": "Greenland ๐ฌ๐ฑ", | |
| "GT": "Guatemala ๐ฌ๐น", | |
| "GF": "French Guiana ๐ฌ๐ซ", | |
| "GU": "Guam ๐ฌ๐บ", | |
| "GY": "Guyana ๐ฌ๐พ", | |
| "HK": "Hong Kong ๐ญ๐ฐ", | |
| "HM": "Heard Island and McDonald Islands ๐ญ๐ฒ", | |
| "HN": "Honduras ๐ญ๐ณ", | |
| "HR": "Croatia ๐ญ๐ท", | |
| "HT": "Haiti ๐ญ๐น", | |
| "HU": "Hungary ๐ญ๐บ", | |
| "ID": "Indonesia ๐ฎ๐ฉ", | |
| "IM": "Isle of Man ๐ฎ๐ฒ", | |
| "IN": "India ๐ฎ๐ณ", | |
| "IO": "British Indian Ocean Territory ๐ฎ๐ด", | |
| "IE": "Ireland ๐ฎ๐ช", | |
| "IR": "Iran ๐ฎ๐ท", | |
| "IQ": "Iraq ๐ฎ๐ถ", | |
| "IS": "Iceland ๐ฎ๐ธ", | |
| "IL": "Israel ๐ฎ๐ฑ", | |
| "IT": "Italy ๐ฎ๐น", | |
| "JM": "Jamaica ๐ฏ๐ฒ", | |
| "JE": "Jersey ๐ฏ๐ช", | |
| "JO": "Jordan ๐ฏ๐ด", | |
| "JP": "Japan ๐ฏ๐ต", | |
| "KZ": "Kazakhstan ๐ฐ๐ฟ", | |
| "KE": "Kenya ๐ฐ๐ช", | |
| "KG": "Kyrgyzstan ๐ฐ๐ฌ", | |
| "KH": "Cambodia ๐ฐ๐ญ", | |
| "KI": "Kiribati ๐ฐ๐ฎ", | |
| "KN": "Saint Kitts and Nevis ๐ฐ๐ณ", | |
| "KR": "Korea, Republic of ๐ฐ๐ท", | |
| "KW": "Kuwait ๐ฐ๐ผ", | |
| "LA": "Lao People's Democratic Republic ๐ฑ๐ฆ", | |
| "LB": "Lebanon ๐ฑ๐ง", | |
| "LR": "Liberia ๐ฑ๐ท", | |
| "LY": "Libya ๐ฑ๐พ", | |
| "LC": "Saint Lucia ๐ฑ๐จ", | |
| "LI": "Liechtenstein ๐ฑ๐ฎ", | |
| "LK": "Sri Lanka ๐ฑ๐ฐ", | |
| "LS": "Lesotho ๐ฑ๐ธ", | |
| "LT": "Lithuania ๐ฑ๐น", | |
| "LU": "Luxembourg ๐ฑ๐บ", | |
| "LV": "Latvia ๐ฑ๐ป", | |
| "MO": "Macao ๐ฒ๐ด", | |
| "MF": "Saint Martin ๐ฒ๐ซ", | |
| "MA": "Morocco ๐ฒ๐ฆ", | |
| "MC": "Monaco ๐ฒ๐จ", | |
| "MD": "Moldova ๐ฒ๐ฉ", | |
| "MG": "Madagascar ๐ฒ๐ฌ", | |
| "MV": "Maldives ๐ฒ๐ป", | |
| "MX": "Mexico ๐ฒ๐ฝ", | |
| "MH": "Marshall Islands ๐ฒ๐ญ", | |
| "MK": "North Macedonia ๐ฒ๐ฐ", | |
| "ML": "Mali ๐ฒ๐ฑ", | |
| "MT": "Malta ๐ฒ๐น", | |
| "MM": "Myanmar ๐ฒ๐ฒ", | |
| "ME": "Montenegro ๐ฒ๐ช", | |
| "MN": "Mongolia ๐ฒ๐ณ", | |
| "MP": "Northern Mariana Islands ๐ฒ๐ต", | |
| "MZ": "Mozambique ๐ฒ๐ฟ", | |
| "MR": "Mauritania ๐ฒ๐ท", | |
| "MS": "Montserrat ๐ฒ๐ธ", | |
| "MQ": "Martinique ๐ฒ๐ถ", | |
| "MU": "Mauritius ๐ฒ๐บ", | |
| "MW": "Malawi ๐ฒ๐ผ", | |
| "MY": "Malaysia ๐ฒ๐พ", | |
| "YT": "Mayotte ๐พ๐น", | |
| "NA": "Namibia ๐ณ๐ฆ", | |
| "NC": "New Caledonia ๐ณ๐จ", | |
| "NE": "Niger ๐ณ๐ช", | |
| "NF": "Norfolk Island ๐ณ๐ซ", | |
| "NG": "Nigeria ๐ณ๐ฌ", | |
| "NI": "Nicaragua ๐ณ๐ฎ", | |
| "NL": "Netherlands ๐ณ๐ฑ", | |
| "NO": "Norway ๐ณ๐ด", | |
| "NP": "Nepal ๐ณ๐ต", | |
| "NR": "Nauru ๐ณ๐ท", | |
| "NU": "Niue ๐ณ๐บ", | |
| "NZ": "New Zealand ๐ณ๐ฟ", | |
| "OM": "Oman ๐ด๐ฒ", | |
| "PK": "Pakistan ๐ต๐ฐ", | |
| "PA": "Panama ๐ต๐ฆ", | |
| "PN": "Pitcairn ๐ต๐ณ", | |
| "PE": "Peru ๐ต๐ช", | |
| "PH": "Philippines ๐ต๐ญ", | |
| "PW": "Palau ๐ต๐ผ", | |
| "PG": "Papua New Guinea ๐ต๐ฌ", | |
| "PL": "Poland ๐ต๐ฑ", | |
| "PR": "Puerto Rico ๐ต๐ท", | |
| "KP": "Korea, Democratic People's Republic of ๐ฐ๐ต", | |
| "PT": "Portugal ๐ต๐น", | |
| "PY": "Paraguay ๐ต๐พ", | |
| "PS": "Palestine, State of ๐ต๐ธ", | |
| "QA": "Qatar ๐ถ๐ฆ", | |
| "RE": "Rรฉunion ๐ท๐ช", | |
| "RO": "Romania ๐ท๐ด", | |
| "RU": "Russia ๐ท๐บ", | |
| "RW": "Rwanda ๐ท๐ผ", | |
| "SA": "Saudi Arabia ๐ธ๐ฆ", | |
| "SD": "Sudan ๐ธ๐ฉ", | |
| "SN": "Senegal ๐ธ๐ณ", | |
| "SG": "Singapore ๐ธ๐ฌ", | |
| "GS": "South Georgia and the South Sandwich Islands ๐ฌ๐ธ", | |
| "SH": "Saint Helena, Ascension and Tristan da Cunha ๐ธ๐ญ", | |
| "SJ": "Svalbard and Jan Mayen ๐ธ๐ฏ", | |
| "SB": "Solomon Islands ๐ธ๐ง", | |
| "SL": "Sierra Leone ๐ธ๐ฑ", | |
| "SV": "El Salvador ๐ธ๐ป", | |
| "SM": "San Marino ๐ธ๐ฒ", | |
| "SO": "Somalia ๐ธ๐ด", | |
| "PM": "Saint Pierre and Miquelon ๐ต๐ฒ", | |
| "RS": "Serbia ๐ท๐ธ", | |
| "SS": "South Sudan ๐ธ๐ธ", | |
| "ST": "Sao Tome and Principe ๐ธ๐น", | |
| "SR": "Suriname ๐ธ๐ท", | |
| "SK": "Slovakia ๐ธ๐ฐ", | |
| "SI": "Slovenia ๐ธ๐ฎ", | |
| "SE": "Sweden ๐ธ๐ช", | |
| "SZ": "Eswatini ๐ธ๐ฟ", | |
| "SX": "Sint Maarten ๐ธ๐ฝ", | |
| "SC": "Seychelles ๐ธ๐จ", | |
| "SY": "Syrian Arab Republic ๐ธ๐พ", | |
| "TC": "Turks and Caicos Islands ๐น๐จ", | |
| "TD": "Chad ๐น๐ฉ", | |
| "TG": "Togo ๐น๐ฌ", | |
| "TH": "Thailand ๐น๐ญ", | |
| "TJ": "Tajikistan ๐น๐ฏ", | |
| "TK": "Tokelau ๐น๐ฐ", | |
| "TM": "Turkmenistan ๐น๐ฒ", | |
| "TL": "Timor-Leste ๐น๐ฑ", | |
| "TO": "Tonga ๐น๐ด", | |
| "TT": "Trinidad and Tobago ๐น๐น", | |
| "TN": "Tunisia ๐น๐ณ", | |
| "TR": "Tรผrkiye ๐น๐ท", | |
| "TW": "Taiwan ๐น๐ผ", | |
| "TV": "Tuvalu ๐น๐ป", | |
| "TZ": "Tanzania ๐น๐ฟ", | |
| "UG": "Uganda ๐บ๐ฌ", | |
| "UA": "Ukraine ๐บ๐ฆ", | |
| "UM": "United States Minor Outlying Islands ๐บ๐ฒ", | |
| "UY": "Uruguay ๐บ๐พ", | |
| "US": "United States ๐บ๐ธ", | |
| "UZ": "Uzbekistan ๐บ๐ฟ", | |
| "VA": "Holy See ๐ป๐ฆ", | |
| "VC": "Saint Vincent and the Grenadines ๐ป๐จ", | |
| "VE": "Venezuela ๐ป๐ช", | |
| "VG": "Virgin Islands, British ๐ป๐ฌ", | |
| "VI": "Virgin Islands, U.S. ๐ป๐ฎ", | |
| "VN": "Vietnam ๐ป๐ณ", | |
| "VU": "Vanuatu ๐ป๐บ", | |
| "WF": "Wallis and Futuna ๐ผ๐ซ", | |
| "WS": "Samoa ๐ผ๐ธ", | |
| "YE": "Yemen ๐พ๐ช", | |
| "ZA": "South Africa ๐ฟ๐ฆ", | |
| "ZM": "Zambia ๐ฟ๐ฒ", | |
| "ZW": "Zimbabwe ๐ฟ๐ผ" | |
| } | |
| # Auto-scaling display for values in KB | |
| def format_bytes_from_kb(kb_val): | |
| units = ["KB", "MB", "GB", "TB"] | |
| size = float(kb_val) | |
| for unit in units: | |
| if size < 1000: | |
| return f"{size:.2f} {unit}" | |
| size /= 1000 | |
| return f"{size:.2f} PB" | |
| # Print summary | |
| print("\n==== Snowflake Proxy Metrics Summary ====\n") | |
| print(f"{'Total Successful Connections':<30}: {total_connections}") | |
| print(f"{'Connection Timeouts':<30}: {timeouts}") | |
| print(f"{'Inbound Traffic':<30}: {format_bytes_from_kb(inbound)}") | |
| print(f"{'Outbound Traffic':<30}: {format_bytes_from_kb(outbound)}") | |
| # Print country-wise table | |
| print("\n=== Connections by Country ===\n") | |
| print(f"{'Country':<27} {'Connections':>12}") | |
| print("-" * 40) | |
| for code, count in sorted(connections_by_country.items(), key=lambda x: x[1], reverse=True): | |
| name = country_map.get(code, f"{code} [?]") | |
| print(f"{name:<27} {count:>12}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment