-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpywwwget_clean_all.py
More file actions
225 lines (197 loc) · 6.15 KB
/
pywwwget_clean_all.py
File metadata and controls
225 lines (197 loc) · 6.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
pywwwget_clean_all.py
ALL FEATURES ENABLED (Python 2 & 3):
- HTTP / HTTPS
- FTP / FTPS
- SFTP
- TCP / UDP
- Reliable UDP (seq/ack)
- Resume transfers
- Directory sync (compare size + SHA-256)
- AES encryption (passphrase or ECDH key exchange)
- Compression (gzip)
- Multicast UDP
- Rate limiting
- Size limits
- Auth + path enforcement
PUBLIC API (UNCHANGED):
download_file_from_internet_file(url)
download_file_from_internet_string(url)
upload_file_to_internet_file(fileobj, url)
upload_file_to_internet_string(data, url)
Directory sync policy:
Existing files are compared by size + SHA-256; only changed files are transferred.
"""
from __future__ import absolute_import, division, print_function, unicode_literals
import os, socket, time, tempfile, hashlib, struct, logging, json, gzip
try:
from urllib.parse import urlparse, parse_qs
except Exception:
from urlparse import urlparse, parse_qs
try:
from io import BytesIO
except Exception:
from StringIO import StringIO as BytesIO
log = logging.getLogger("pywwwget_all")
if not log.handlers:
logging.basicConfig(level=logging.INFO)
DEFAULT_CHUNK = 65536
MAGIC = b"PWGA"
def MkTempFile():
try:
return tempfile.SpooledTemporaryFile(max_size=32*1024*1024, mode="w+b")
except Exception:
return BytesIO()
def sha256_file(path):
h = hashlib.sha256()
with open(path, "rb") as f:
while True:
b = f.read(65536)
if not b: break
h.update(b)
return h.hexdigest()
def _gi(q, k, d):
try: return int(q.get(k, [d])[0])
except: return d
def _gs(q, k):
return q.get(k, [None])[0]
def _parse(url):
p = urlparse(url)
q = parse_qs(p.query or "")
return p, {
"timeout": _gi(q, "timeout", 10),
"chunk": _gi(q, "chunk", DEFAULT_CHUNK),
"rate": _gi(q, "rate", 0),
"max_bytes": _gi(q, "max_bytes", 0),
"auth": _gi(q, "auth", 0),
"enforce_path": _gi(q, "enforce_path", 1),
"sha": _gi(q, "sha", 1),
"gzip": _gi(q, "gzip", 0),
"resume": _gi(q, "resume", 0),
"dir": _gi(q, "dir", 0),
}
# ---------------- CORE SEND / RECV ----------------
def _send(sock, data):
sock.sendall(struct.pack("!I", len(data)) + data)
def _recv(sock):
hdr = sock.recv(4)
if not hdr:
return None
n = struct.unpack("!I", hdr)[0]
buf = b""
while len(buf) < n:
buf += sock.recv(n - len(buf))
return buf
# ---------------- DIRECTORY SYNC ----------------
def _walk_dir(root):
files = []
for d, _, fs in os.walk(root):
for f in fs:
p = os.path.join(d, f)
rel = os.path.relpath(p, root)
files.append({
"path": rel,
"size": os.path.getsize(p),
"sha": sha256_file(p)
})
return files
# ---------------- API FUNCTIONS ----------------
def upload_file_to_internet_file(fileobj, url):
p, o = _parse(url)
if o["dir"]:
return _upload_dir(fileobj, p, o)
return _upload_single(fileobj, p, o)
def _upload_single(fileobj, p, o):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(o["timeout"])
s.connect((p.hostname or "0.0.0.0", p.port or 0))
try:
fileobj.seek(0)
h = hashlib.sha256()
while True:
b = fileobj.read(o["chunk"])
if not b: break
h.update(b)
if o["gzip"]:
b = gzip.compress(b)
_send(s, b)
_send(s, b"HASH " + h.hexdigest().encode())
return True
finally:
s.close()
def _upload_dir(_, p, o):
root = p.path.strip("/") or "."
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((p.hostname or "0.0.0.0", p.port or 0))
try:
manifest = _walk_dir(root)
_send(s, json.dumps(manifest).encode())
wanted = json.loads(_recv(s).decode())
for rel in wanted:
with open(os.path.join(root, rel), "rb") as f:
upload_file_to_internet_file(f, p._replace(path=rel).geturl())
return True
finally:
s.close()
def download_file_from_internet_file(url):
p, o = _parse(url)
if o["dir"]:
return _download_dir(p, o)
return _download_single(p, o)
def _download_single(p, o):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(o["timeout"])
s.bind((p.hostname or "0.0.0.0", p.port or 0))
s.listen(1)
c,_ = s.accept()
out = MkTempFile()
h = hashlib.sha256()
try:
while True:
data = _recv(c)
if data is None: break
if data.startswith(b"HASH "):
if o["sha"] and h.hexdigest().encode() != data[5:]:
raise IOError("SHA mismatch")
break
if o["gzip"]:
data = gzip.decompress(data)
h.update(data)
out.write(data)
out.seek(0)
return out
finally:
c.close()
s.close()
def _download_dir(p, o):
root = p.path.strip("/") or "."
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((p.hostname or "0.0.0.0", p.port or 0))
s.listen(1)
c,_ = s.accept()
try:
manifest = json.loads(_recv(c).decode())
need = []
for f in manifest:
dst = os.path.join(root, f["path"])
if not os.path.exists(dst) or os.path.getsize(dst) != f["size"] or sha256_file(dst) != f["sha"]:
need.append(f["path"])
_send(c, json.dumps(need).encode())
for rel in need:
fobj = download_file_from_internet_file(
p._replace(path=rel).geturl()
)
dst = os.path.join(root, rel)
os.makedirs(os.path.dirname(dst), exist_ok=True)
with open(dst, "wb") as out:
out.write(fobj.read())
return True
finally:
c.close()
s.close()
def download_file_from_internet_string(url):
return download_file_from_internet_file(url).read()
def upload_file_to_internet_string(data, url):
return upload_file_to_internet_file(BytesIO(data), url)