Added GeoCountry
This commit is contained in:
@ -1,46 +1,71 @@
|
||||
#!/usr/bin/env python3
|
||||
import os
|
||||
import subprocess
|
||||
from enum import Enum
|
||||
import sys
|
||||
import requests
|
||||
|
||||
|
||||
class GeoCountry(Enum):
|
||||
IRAN = {
|
||||
'geosite': 'https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geosite.dat',
|
||||
'geoip': 'https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geoip.dat'
|
||||
}
|
||||
CHINA = {
|
||||
'geosite': 'https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat',
|
||||
'geoip': 'https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat'
|
||||
}
|
||||
RUSSIA = {
|
||||
'geosite': 'https://github.com/runetfreedom/russia-v2ray-rules-dat/releases/latest/download/geosite.dat',
|
||||
'geoip': 'https://github.com/runetfreedom/russia-v2ray-rules-dat/releases/latest/download/geoip.dat'
|
||||
}
|
||||
|
||||
GEOSITE_PATH = "/etc/hysteria/geosite.dat"
|
||||
GEOIP_PATH = "/etc/hysteria/geoip.dat"
|
||||
GEOSITE_URL = "https://raw.githubusercontent.com/Chocolate4U/Iran-v2ray-rules/release/geosite.dat"
|
||||
GEOIP_URL = "https://raw.githubusercontent.com/Chocolate4U/Iran-v2ray-rules/release/geoip.dat"
|
||||
|
||||
|
||||
def remove_file(file_path):
|
||||
try:
|
||||
if os.path.exists(file_path):
|
||||
os.remove(file_path)
|
||||
print(f"Removed existing file: {file_path}")
|
||||
except Exception as e:
|
||||
print(f"Error removing file {file_path}: {e}")
|
||||
|
||||
|
||||
def download_file(url, destination):
|
||||
def download_file(url, destination, chunk_size=8192):
|
||||
try:
|
||||
subprocess.run(
|
||||
["wget", "-O", destination, url],
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
check=True
|
||||
)
|
||||
print(f"Downloaded {url} to {destination}")
|
||||
except subprocess.CalledProcessError:
|
||||
print(f"Failed to download {url}")
|
||||
# Ensure the destination directory exists
|
||||
destination_dir = os.path.dirname(destination)
|
||||
if destination_dir and not os.path.exists(destination_dir):
|
||||
os.makedirs(destination_dir) # Create the directory if it doesn't exist
|
||||
|
||||
response = requests.get(url, stream=True)
|
||||
response.raise_for_status()
|
||||
|
||||
def update_geo_files():
|
||||
with open(destination, "wb") as file:
|
||||
for chunk in response.iter_content(chunk_size=chunk_size):
|
||||
file.write(chunk)
|
||||
|
||||
print(f"File successfully downloaded to: {destination}")
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"Error: Failed to download the file from '{url}'.\n{e}")
|
||||
except IOError as e:
|
||||
print(f"Error: Failed to save the file to '{destination}'.\n{e}")
|
||||
|
||||
def update_geo_files(country='iran'):
|
||||
try:
|
||||
print("Starting geo files update...")
|
||||
|
||||
print(f"Starting geo files update for {country.upper()}...")
|
||||
country_enum = GeoCountry[country.upper()]
|
||||
remove_file(GEOSITE_PATH)
|
||||
remove_file(GEOIP_PATH)
|
||||
download_file(GEOSITE_URL, GEOSITE_PATH)
|
||||
download_file(GEOIP_URL, GEOIP_PATH)
|
||||
|
||||
download_file(country_enum.value['geosite'], GEOSITE_PATH)
|
||||
download_file(country_enum.value['geoip'], GEOIP_PATH)
|
||||
print("Geo files update completed successfully.")
|
||||
|
||||
except KeyError:
|
||||
print(f"Invalid country selection. Available options: {', '.join([c.name.lower() for c in GeoCountry])}")
|
||||
except Exception as e:
|
||||
print(f"An error occurred during the update process: {e}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
update_geo_files()
|
||||
country = sys.argv[1] if len(sys.argv) > 1 else 'iran'
|
||||
update_geo_files(country)
|
||||
|
||||
Reference in New Issue
Block a user