Skip to content

Commit b9421e6

Browse files
committed
Rewrite everything and make debian package
1 parent cee25cc commit b9421e6

File tree

8 files changed

+285
-306
lines changed

8 files changed

+285
-306
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
better-mirror.deb
2+
better-mirror/usr

README.md

Lines changed: 85 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,94 @@
1-
# Mirrorscript-v2
2-
Script to change official Kali repository to mirrors. This helps increase packages update and downloading for some user. Please visit my blog [post](https://www.metahackers.pro/speed-kali-linux-update/).
1+
# Better Mirror
32

4-
## Requirements
5-
Kali Linux,
6-
Python3
3+
```text
4+
___ _____ _ ____ __ __ _ ___ ___ __ ___
5+
| _ )__ / |_| |_|__ /_ _| \/ / | _ \ _ \/ \| _ \
6+
| _ \|_ \ _| _||_ \ '_| |\/| | | / / () | /
7+
|___/___/\__|\__|___/_| |_| |_|_|_|_\_|_\\__/|_|_\
8+
```
79

8-
## Usage
9-
Run the script in privilege mode, such that `sources.list` could be edited.
10+
A Linux package that find the fastest mirror and configure apt to use that.
1011

11-
## Help
12+
Just a fork of [IceM4nn/mirrorscript-v2](https://github.com/IceM4nn/mirrorscript-v2)
13+
14+
## Installation
15+
16+
```shell
17+
$ git clone https://github.com/heinthanth/better-mirror
18+
$ cd better-mirror
19+
$ chmod +x build.sh
20+
$ ./build.sh
21+
$ sudo dpkg -i better-mirror.deb
1222
```
13-
# python3 mirrorscript-v2.py -h
14-
usage: mirrorscript-v2.py [-h] [-v] [-https] [-src]
1523

16-
Kali Mirrorscripts-v2 by IceM4nn automatically select the best kali mirror
17-
server and apply the configuration
24+
## Help
25+
26+
```shell
27+
$ sudo better-mirror -h
28+
29+
___ _____ _ ____ __ __ _ ___ ___ __ ___
30+
| _ )__ / |_| |_|__ /_ _| \/ / | _ \ _ \/ \| _ \
31+
| _ \|_ \ _| _||_ \ '_| |\/| | | / / () | /
32+
|___/___/\__|\__|___/_| |_| |_|_|_|_\_|_\\__/|_|_\
33+
34+
better-mirror 1.0.0 - H31iUMx49
35+
https://github.com/heinthanth/better-mirror
1836
19-
optional arguments:
20-
-h, --help show this help message and exit
21-
-v, --verbose enable verbose output
22-
-https use HTTPS in apt transport (default HTTP)
23-
-src enable sources packages (default disable)
37+
38+
usage: better-mirror [option] [mode]
39+
40+
options:
41+
-c, --choose : perform mirror choosing
42+
-h, --help : display help message
43+
-s, --src : enable source repository
44+
45+
modes:
46+
-v, --verbose : enable verbose mode
2447
```
2548
26-
## Sample output:
49+
## Sample Output
50+
51+
```shell
52+
$ sudo better-mirror -v -c
53+
___ _____ _ ____ __ __ _ ___ ___ __ ___
54+
| _ )__ / |_| |_|__ /_ _| \/ / | _ \ _ \/ \| _ \
55+
| _ \|_ \ _| _||_ \ '_| |\/| | | / / () | /
56+
|___/___/\__|\__|___/_| |_| |_|_|_|_\_|_\\__/|_|_\
57+
58+
better-mirror 1.0.0 - H31iUMx49
59+
https://github.com/heinthanth/better-mirror
60+
61+
62+
[*] calculating the mirror latency ...
63+
[*] pinging kali.download ... latency 23.0 ms
64+
[*] pinging ftp.jaist.ac.jp ... latency 117.5 ms
65+
[*] pinging ftp.free.fr ... latency 213.5 ms
66+
[*] pinging ftp.belnet.be ... latency 222.5 ms
67+
[*] pinging mirror.neostrada.nl ... latency 223.75 ms
68+
[*] pinging mirror.serverius.net ... latency 218.25 ms
69+
[*] pinging ftp2.nluug.nl ... latency 232.75 ms
70+
[*] pinging mirrors.dotsrc.org ... latency 216.75 ms
71+
[*] pinging ftp1.nluug.nl ... latency 235.5 ms
72+
[*] pinging ftp.acc.umu.se ... latency 231.0 ms
73+
[*] pinging archive.linux.duke.edu ... latency 304.25 ms
74+
[*] pinging mirror.pwnieexpress.com ... latency 341.25 ms
75+
[*] pinging mirror-1.truenetwork.ru ... latency 322.5 ms
76+
[*] pinging mirror.karneval.cz ... latency 336.5 ms
77+
[*] pinging hlzmel.fsmg.org.nz ... latency 324.5 ms
78+
[*] pinging wlglam.fsmg.org.nz ... latency 330.75 ms
79+
[*] pinging archive-4.kali.org ... latency 252.0 ms
80+
[*] pinging mirrors.ocf.berkeley.edu ... latency 251.0 ms
81+
[*] pinging ftp.halifax.rwth-aachen.de ... latency 257.25 ms
82+
[*] pinging ftp.hands.com ... latency 197.0 ms
83+
[*] finding the best mirror ...
84+
[*] found! the selected mirror: kali.download ... latency 23.0 ms
85+
[*] backuping original /etc/apt/sources.list to /etc/apt/sources.list.bak
86+
[*] updating /etc/apt/sources.list
87+
[*] performing 'apt-get update' for you
88+
Hit:1 http://kali.download/kali kali-rolling InRelease
89+
Ign:2 http://dl.google.com/linux/chrome/deb stable InRelease
90+
Hit:3 http://dl.google.com/linux/chrome/deb stable Release
91+
Hit:4 http://packages.microsoft.com/repos/vscode stable InRelease
92+
Reading package lists... Done
93+
[*] Done!
2794
```
28-
# python3 mirrorscript-v2.py -v -https -src
29-
30-
#
31-
# Mirrorscripts-v2 - By Hazmirul Afiq
32-
# Automatically select the best Kali mirror and apply the configuration
33-
# https://github.com/IceM4nn/mirrorscript-v2
34-
# https://www.metahackers.pro/speed-kali-linux-update/
35-
#
36-
37-
[-] Checking if 'apt-transport-https' package is installed.
38-
- apt-transport-https is installed
39-
40-
[+] Getting mirror list ...
41-
[+] Found a lists of mirrors:
42-
- https://hlzmel.fsmg.org.nz/kali
43-
- https://wlglam.fsmg.org.nz/kali
44-
- https://mirror.karneval.cz/pub/linux/kali
45-
- https://ftp.acc.umu.se/mirror/kali.org/kali
46-
- https://mirrors.dotsrc.org/kali
47-
- https://ftp.halifax.rwth-aachen.de/kali
48-
- https://ftp2.nluug.nl/os/Linux/distr/kali
49-
- https://ftp1.nluug.nl/os/Linux/distr/kali
50-
- https://mirror.neostrada.nl/kali
51-
- https://kali.download/kali
52-
- https://mirrors.ocf.berkeley.edu/kali
53-
54-
[+] Checking mirrors ... This could take some times.
55-
[+] Finding the best latency
56-
- hlzmel.fsmg.org.nz : 416.890
57-
- wlglam.fsmg.org.nz : 388.998
58-
- mirror.karneval.cz : 391.414
59-
- ftp.acc.umu.se : 313.722
60-
- mirrors.dotsrc.org : 314.235
61-
- ftp.halifax.rwth-aachen.de : 291.252
62-
- ftp2.nluug.nl : 297.838
63-
- ftp1.nluug.nl : 302.336
64-
- mirror.neostrada.nl : 294.256
65-
- kali.download : 40.478
66-
- mirrors.ocf.berkeley.edu : 348.848
67-
68-
[+] Fastest mirror: ('kali.download', '040.478')
69-
[+] Preparing ...
70-
- Making a backup file /etc/apt/sources.list.bk ...
71-
- Checking sources.list for older entries ...
72-
- Commenting older entries ...
73-
- Done
74-
75-
[+] Updating sources.list with new entry ...
76-
- Your new mirror: https://kali.download/kali
77-
78-
[+] Done!
79-
- Run 'apt clean; apt update' for the changes to load.
80-
81-
```

better-mirror.py

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
#!/usr/bin/env python3
2+
3+
import re
4+
import os
5+
import urllib
6+
import xmltodict
7+
import subprocess
8+
import sys
9+
from threading import Thread
10+
11+
# check if we have privileges
12+
if not os.getuid() == 0:
13+
sys.exit("\n\033[31mcan you run as root or sudo, please?\033[0m\n")
14+
15+
# check if supported distros
16+
supported_distros = ['kali-rolling']
17+
with open("/etc/os-release") as release:
18+
version = re.search("VERSION_CODENAME=\"(.*)\"", release.read())
19+
if not version:
20+
sys.exit(
21+
"\n\033[31mI don't know what distros you are using :(\033[0m\n")
22+
else:
23+
if not version[1] in supported_distros:
24+
sys.exit(
25+
"\n\033[31m{} is not supported in these days\033[0m\n".format(version[1]))
26+
27+
verboseMode = False
28+
enableSource = False
29+
30+
threads = list()
31+
results = dict()
32+
hostlist = dict()
33+
selectedMirror = ""
34+
35+
36+
def ascii():
37+
print("""\
38+
___ _____ _ ____ __ __ _ ___ ___ __ ___
39+
| _ )__ / |_| |_|__ /_ _| \/ / | _ \ _ \/ \| _ \\
40+
| _ \|_ \ _| _||_ \ '_| |\/| | | / / () | /
41+
|___/___/\__|\__|___/_| |_| |_|_|_|_\_|_\\\\__/|_|_\\
42+
43+
better-mirror 1.0.0 - H31iUMx49
44+
https://github.com/heinthanth/better-mirror
45+
46+
""")
47+
48+
49+
def help():
50+
ascii()
51+
print("usage: better-mirror [option] [mode]")
52+
print("\noptions:")
53+
print("\t-c, --choose\t: perform mirror choosing")
54+
print("\t-h, --help\t: display help message")
55+
print("\t-s, --src\t: enable source repository")
56+
print()
57+
print("modes:")
58+
print("\t-v, --verbose\t: enable verbose mode")
59+
print()
60+
61+
62+
def measurelatency(hostname):
63+
res = ""
64+
p = subprocess.Popen(['ping', '-c 4', hostname],
65+
stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
66+
res = [x.decode('utf-8') for x in p]
67+
if not res[0]:
68+
results[hostname] = 9999
69+
res = "\033[31merror\033[0m"
70+
else:
71+
latencies = re.findall(r'time=([0-9\.]*)\sms', res[0])
72+
latencies = list(map(float, latencies))
73+
average = sum(latencies) / len(latencies)
74+
results[hostname] = average
75+
res = "\033[32m" + str(round(average, 2)) + " ms\033[0m"
76+
77+
if verboseMode:
78+
print("\033[33m[*]\033[0m pinging \033[32m{hostname}\033[0m ... latency {latency}".format(
79+
hostname=hostname, latency=res))
80+
81+
82+
def main():
83+
# get list of mirror from meta4 file
84+
meta_xml = urllib.request.urlopen("http://http.kali.org/README.meta4")
85+
lists = xmltodict.parse(meta_xml)
86+
87+
print("\033[33m[*]\033[0m calculating the mirror latency ...")
88+
for info in lists["metalink"]["file"]["url"]:
89+
url = urllib.parse.urlparse(info["#text"])
90+
hostlist[url.netloc] = str(url.path).replace("/README", "")
91+
thread = Thread(target=measurelatency, args=[url.netloc])
92+
thread.start()
93+
threads.append(thread)
94+
95+
for thread in threads:
96+
thread.join()
97+
98+
if verboseMode:
99+
print("\033[33m[*]\033[0m finding the best mirror ...")
100+
result = sorted(results.items(), key=lambda item: item[1])
101+
102+
selected = result[0]
103+
print("\033[33m[*]\033[0m found! the selected mirror: \033[32m{}\033[0m ... latency \033[32m{} ms\033[0m".format(
104+
selected[0], round(selected[1], 3)))
105+
selectedMirror = selected[0]
106+
107+
if verboseMode:
108+
print(
109+
"\033[33m[*]\033[0m backuping original /etc/apt/sources.list to /etc/apt/sources.list.bak")
110+
os.popen('cp /etc/apt/sources.list /etc/apt/sources.list.bak')
111+
112+
print("\033[33m[*]\033[0m updating /etc/apt/sources.list")
113+
newcontent = []
114+
try:
115+
read_only_sources_list = open("/etc/apt/sources.list", "r")
116+
for line in read_only_sources_list:
117+
if(re.search(r'# add with better-mirror', line)):
118+
# will skip this line
119+
pass
120+
elif re.search(r'^deb http(?:s|)://(.*)/kali kali-rolling', line):
121+
newcontent.append("\n# " + line)
122+
elif re.search(r'^deb-src http(?:s|)://(.*)/kali kali-rolling', line):
123+
newcontent.append("# " + line + "\n")
124+
elif re.search(r'^#\s*deb http(?:s|)://(.*)/kali kali-rolling', line):
125+
newcontent.append("\n" + line)
126+
elif re.search(r'^#\s*deb-src http(?:s|)://(.*)/kali kali-rolling', line):
127+
newcontent.append(line + "\n")
128+
elif line.strip() == "":
129+
pass
130+
else:
131+
newcontent.append(line)
132+
newcontent.append("\ndeb http://{hostname}{path} kali-rolling main non-free contrib # add with better-mirror\n".format(
133+
hostname=selectedMirror, path=hostlist[selectedMirror]))
134+
if enableSource:
135+
newcontent.append("deb-src http://{hostname}{path} kali-rolling main non-free contrib # add with better-mirror\n".format(
136+
hostname=selectedMirror, path=hostlist[selectedMirror]))
137+
except PermissionError:
138+
sys.exit(
139+
"\033[31m[*] unsufficient permission ... cannot open /etc/apt/sources.list\033[0m")
140+
finally:
141+
read_only_sources_list.close()
142+
143+
try:
144+
source_list = open("/etc/apt/sources.list", "w")
145+
for line in newcontent:
146+
source_list.writelines(line)
147+
except PermissionError:
148+
sys.exit(
149+
"\033[31m[*] unsufficient permission ... cannot write /etc/apt/sources.list\033[0m")
150+
finally:
151+
source_list.close()
152+
153+
print("\033[33m[*]\033[0m performing 'apt-get update' for you")
154+
if verboseMode:
155+
os.system("apt-get update")
156+
else:
157+
os.system("apt-get update -qq")
158+
print("\033[33m[*]\033[0m Done!")
159+
160+
161+
if len(sys.argv) == 1:
162+
help()
163+
exit(1)
164+
else:
165+
# parse the argument
166+
if "-h" in sys.argv or "--help" in sys.argv:
167+
help()
168+
exit(0)
169+
if "-v" in sys.argv or "--verbose" in sys.argv:
170+
verboseMode = True
171+
if "-s" in sys.argv or "--src" in sys.argv:
172+
enableSource = True
173+
if "-c" in sys.argv or "--choose" in sys.argv:
174+
ascii()
175+
main()
176+
else:
177+
help()
178+
exit(1)

better-mirror/DEBIAN/changelog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
better-mirror (1.0.0) heinthanth; urgency=low
2+
3+
* initial release - with Kali Linux supported
4+
5+
-- Hein Thanth <contact@heinthanth.com> Wed, 12 Feb 2020 13:34:00

better-mirror/DEBIAN/control

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Package: better-mirror
2+
Version: 1.0.0
3+
Maintainer: Hein Thanth <contact@heinthanth.com>
4+
Description: A package that select the fastest for debian-like distros like Kali Linux, Parrot Security Linux
5+
Homepage: https://github.com/heinthanth/better-mirror
6+
Architecture: all
7+
Depends: python3, python3-pip

better-mirror/DEBIAN/postinst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pip3 install xmltodict
2+
cp /etc/apt/sources.list /etc/apt/sources.list.origin
3+
chmod +x /usr/bin/better-mirror

build.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
mkdir -p ./better-mirror/usr/bin
3+
cp better-mirror.py ./better-mirror/usr/bin/better-mirror
4+
5+
dpkg -b ./better-mirror ./better-mirror.deb

0 commit comments

Comments
 (0)