/
geodata.py
173 lines (137 loc) · 5.03 KB
/
geodata.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/bin/env python3
# Geodata Creation
# ------------------------------------------
# Author : SonyaCore
# Github : https://github.com/SonyaCore
# Licence : https://www.gnu.org/licenses/gpl-3.0.en.html
import re
import json
import ipaddress
import os, sys
from urllib.request import urlopen, Request
from typing import Iterable
class DataSet:
CIDR = Request("https://cdn-lite.ip2location.com/datasets/IR.json")
ADS = Request("https://raw.githubusercontent.com/MasterKia/PersianBlocker/main/PersianBlockerHosts.txt")
sets = []
ads = []
cidrs = []
name = ""
URL_REGEX = re.compile(
r"\b((?:https?://)"
r"?(?:(?:www\.)"
r"?(?:[\da-z\.-]+)\.(?:[a-z]{2,6})|(?:(?:25[0-5]"
r"(?!(?:10|127)(?:\.\d{1,3}){3})"
r"(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})"
r"(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})"
r"[0-4][0-9][01]?[0-9][0-9]?)\.)(?:25"
r"|"
r"2[0-4][0-9]|[01]?[0-9][0-9]?)|(?:(?:[0-9a-fA-F]{1,4}:)"
r"{7,7}[0-9a-fA-F]{1,4}|(?:[0-9a-fA-F]{1,4}:){1,7}:|(?:"
r"[0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|(?:[0-9a-fA-F]"
r"{1,4}:){1,5}(?::[0-9a-fA-F]{1,4}){1,2}|(?:[0-9a-fA-F]"
r"{1,4}:){1,4}(?::[0-9a-fA-F]{1,4}){1,3}|(?:[0-9a-fA-F]"
r"{1,4}:){1,3}(?::[0-9a-fA-F]{1,4}){1,4}|(?:[0-9a-fA-F]"
r"{1,4}:){1,2}(?::[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]"
r"{1,4}:(?:(?::[0-9a-fA-F]{1,4}){1,6})|:(?:(?::[0-9a-fA-F]{1,4})"
r"{1,7}|:)|fe80:(?::[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}"
r"|"
r"::(?:ffff(?::0{1,4}){0,1}:){0,1}(?:(?:25[0-5]"
r"|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(?:25[0-5]"
r"|"
r"(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])|(?:[0-9a-fA-F]{1,4}:)"
r"{1,4}:(?:(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.)"
r"{3,3}(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])))"
r"(?::[0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]"
r"{2}|655[0-2][0-9]|6553[0-5])?(?:/[\w\.-]*)*/?)\b",
re.UNICODE | re.IGNORECASE
)
def load_dataset():
with urlopen(DataSet.CIDR) as respone:
data = respone.read()
dat = json.loads(data)
for item in dat["data"]:
start = ipaddress.IPv4Address(item[:2][0])
end = ipaddress.IPv4Address(item[:2][1])
DataSet.cidrs.append(next(ipaddress.summarize_address_range(start, end)))
for _, datasets in enumerate(DataSet.cidrs):
with open("/tmp/dump", "a") as file:
file.write(str(datasets) + "\n")
def url(text: str) -> bool:
return bool(URL_REGEX.search(text))
def load_ads() -> Iterable[str]:
with urlopen(DataSet.ADS) as respone:
data = respone.read().decode("utf-8")
ads = re.sub(r"(?m)^\s*#.*\n?", "", data)
ads = data.splitlines()[1:]
ads = filter(url, ads)
DataSet.ads = [domain for domain in ads if URL_REGEX.match(domain)]
return sorted(DataSet.ads)
def qv2rayrouting(cidr: list, ads: list):
schema = {
"description": "List of Iranian IP's",
"domainStrategy": "IPIfNonMatch",
"domains": {"direct": ["regexp:^.+\.ir$"], "block": ["geosite:category-ads-all"] + list(ads)},
"ips": {"direct": cidr},
"name": "IR_IPS",
}
return json.dumps(schema, indent=2)
def clash(cidr: list, ads: list) -> str:
config = (
"# Clash\n"
"# Wiki: https://github.com/Dreamacro/clash/wiki/premium-core-features#rule-providers\n"
"payload:\n"
)
# ads
config += "".join(f" - DOMAIN-SUFFIX,{adslist}\n" for adslist in ads)
# cidr
config += "".join(f" - IP-CIDR,{cidrlist}\n" for cidrlist in cidr)
return config
def loadsets():
global cidrs, ads
with open("/tmp/dump", "r") as cidr:
cidrs = [line.strip() for line in cidr]
with open("/tmp/ads", "r") as ads:
ads = [line.strip() for line in ads]
def writerouting(name: json):
loadsets()
with open(name, "w") as file:
file.write(qv2rayrouting(cidrs, ads))
def writeclash(name: json):
loadsets()
with open(name, "w") as file:
file.write(clash(cidrs, ads))
def writeraw(cidr,ads):
with open("/tmp/ads", "r") as file:
data = file.read()
with open(ads, "w") as ads:
ads.write(data)
with open("/tmp/dump", "r") as file:
data = file.read()
with open(cidr, "w") as cidr:
cidr.write(data)
try:
load_dataset()
load_ads()
with open("/tmp/dump", "w") as file:
for v, datasets in enumerate(DataSet.cidrs):
file.write(str(datasets) + "\n")
with open("/tmp/ads", "w") as file:
for v, ads in enumerate(DataSet.ads):
file.write(str(ads) + "\n")
if sys.argv[1] in ("qv2ray", "q2ray"):
DataSet.name = "qv2ray-client.json"
writerouting(DataSet.name)
elif sys.argv[1] in ("raw", "ip", "ips"):
writeraw("IranIPs.txt","ads.txt")
elif sys.argv[1] in ("clash", "clashyaml", "c"):
DataSet.name = "clash_rules.yaml"
writeclash(DataSet.name)
except IndexError:
sys.exit("No Option Selected")
finally:
for paths in ["/tmp/dump", "/tmp/ads"]:
os.remove(paths)
print("Generated {}".format(DataSet.name))
print("Total CIDR'S : {}".format(len(DataSet.cidrs)))
print("Total Domain ADS : {}".format(len(DataSet.ads)))