-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaptdevproxy.py
More file actions
106 lines (89 loc) · 3.15 KB
/
aptdevproxy.py
File metadata and controls
106 lines (89 loc) · 3.15 KB
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
from http.server import HTTPServer, BaseHTTPRequestHandler
import requests
#from requests.auth import HTTPDigestAuth
#import pprint
#pp = pprint.PrettyPrinter(indent=1)
#import re
#rexDebPack = re.compile(r'^.*[.]deb$')
import os, sys, getopt
#todo
#prendre un fichier avec toutes les confs, multi repos
#faire marcher repo security
#tester avec mdm
#sources
#n'a pas marché (j'ai autorisé mon IP sur APT) : https://requests.readthedocs.io/en/master/user/authentication/
#problème NODATA : https://askubuntu.com/questions/474549/got-nodata-issue-nodata-does-the-network-require-authentication
#request : https://www.tutorialspoint.com/downloading-files-from-web-using-python
#serveur HTTP : https://blog.anvileight.com/posts/simple-python-http-server/
#cli args : https://www.tutorialspoint.com/python/python_command_line_arguments.htm
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def answer(self, code, content, length):
self.send_response(code)
#self.send_header('content-type', r.headers['content-type'])
self.send_header('content-length', length)
self.end_headers()
self.wfile.write(content)
def fromdisk(self, path):
f = open(path, "rb")
self.answer(200, f.read(), os.path.getsize(path))
f.close()
def passthru(self, uri, store):
finished=False
if store is not None:
sourcepath=self.server._pathSource+'/'+os.path.basename(store)
if os.path.isfile(sourcepath):
print('fromsource '+sourcepath)
self.fromdisk(sourcepath)
finished=True
elif os.path.isfile(store):
print('fromcache '+store)
self.fromdisk(store)
finished=True
if not finished:
url = self.server._urlSrc + '/' + uri
print('download '+url)
r = requests.get(url, allow_redirects=True)
if store is not None and r.status_code == 200:
print('store '+store)
dir=os.path.dirname(store)
if not os.path.isdir(dir): os.makedirs(dir)
f = open(store, "wb")
f.write(r.content)
f.close()
self.answer(r.status_code, r.content, r.headers['content-length'])
def do_GET(self):
#if rexDebPack.match(self.path):
self.passthru(uri=self.path, store=self.server._pathBase + self.path)
#else:
# self.passthru(uri=self.path, store=None)
class MyHTTPServer(HTTPServer):
def setPathBase(self, path):
self._pathBase = path
self._pathSource = path + '/sources'
if not os.path.isdir(self._pathSource): os.makedirs(self._pathSource)
def setUrlSrc(self, url):
self._urlSrc = url
def main(argv):
port=8000
pathbase='/home/cedric/aptdevproxy/'
urlapt='https://apt.epiconcept.fr'
help='aptdevproxy.py -p <port> -d <path cache> -u https://apt.epiconcept.fr'
try:
opts, args = getopt.getopt(argv,"hp:d:u:",["port=","dir=","url="])
except getopt.GetoptError:
print(help)
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print(help)
sys.exit()
elif opt in ("-p", "--port"): port = arg
elif opt in ("-d", "--dir"): pathbase = arg
elif opt in ("-u", "--url"): urlapt = arg
print('port '+str(port)+' dir '+pathbase+' url '+urlapt)
httpd = MyHTTPServer(('', int(port)), SimpleHTTPRequestHandler)
httpd.setPathBase(pathbase)
httpd.setUrlSrc(urlapt)
httpd.serve_forever()
if __name__ == "__main__":
main(sys.argv[1:])