From b8b15814cf33d2e433b8a980288fb929c6106f20 Mon Sep 17 00:00:00 2001 From: ed Date: Fri, 12 Nov 2021 01:34:56 +0100 Subject: [PATCH] add traffic shaping, bump speeds on https/windows --- copyparty/__main__.py | 4 +++- copyparty/httpcli.py | 8 ++++---- copyparty/util.py | 10 ++++++---- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/copyparty/__main__.py b/copyparty/__main__.py index c7ec8c4f..49161762 100644 --- a/copyparty/__main__.py +++ b/copyparty/__main__.py @@ -381,7 +381,9 @@ def run_argparse(argv, formatter): ap2.add_argument("-i", metavar="IP", type=u, default="0.0.0.0", help="ip to bind (comma-sep.)") ap2.add_argument("-p", metavar="PORT", type=u, default="3923", help="ports to bind (comma/range)") ap2.add_argument("--rproxy", metavar="DEPTH", type=int, default=1, help="which ip to keep; 0 = tcp, 1 = origin (first x-fwd), 2 = cloudflare, 3 = nginx, -1 = closest proxy") - + ap2.add_argument("--s-wr-sz", metavar="B", type=int, default=256*1024, help="socket write size in bytes") + ap2.add_argument("--s-wr-slp", metavar="SEC", type=float, default=0, help="socket write delay in seconds") + ap2 = ap.add_argument_group('SSL/TLS options') ap2.add_argument("--http-only", action="store_true", help="disable ssl/tls") ap2.add_argument("--https-only", action="store_true", help="disable plaintext") diff --git a/copyparty/httpcli.py b/copyparty/httpcli.py index 0f56aac9..8eeb7b95 100644 --- a/copyparty/httpcli.py +++ b/copyparty/httpcli.py @@ -1484,10 +1484,10 @@ class HttpCli(object): ret = True with open_func(*open_args) as f: - if use_sendfile: - remains = sendfile_kern(lower, upper, f, self.s) - else: - remains = sendfile_py(lower, upper, f, self.s) + sendfun = sendfile_kern if use_sendfile else sendfile_py + remains = sendfun( + lower, upper, f, self.s, self.args.s_wr_sz, self.args.s_wr_slp + ) if remains > 0: logmsg += " \033[31m" + unicode(upper - remains) + "\033[0m" diff --git a/copyparty/util.py b/copyparty/util.py index 990ac6db..66460e8e 100644 --- a/copyparty/util.py +++ b/copyparty/util.py @@ -1164,12 +1164,14 @@ def hashcopy(fin, fout): return tlen, hashobj.hexdigest(), digest_b64 -def sendfile_py(lower, upper, f, s): +def sendfile_py(lower, upper, f, s, bufsz, slp): remains = upper - lower f.seek(lower) while remains > 0: - # time.sleep(0.01) - buf = f.read(min(1024 * 32, remains)) + if slp: + time.sleep(slp) + + buf = f.read(min(bufsz, remains)) if not buf: return remains @@ -1182,7 +1184,7 @@ def sendfile_py(lower, upper, f, s): return 0 -def sendfile_kern(lower, upper, f, s): +def sendfile_kern(lower, upper, f, s, bufsz, slp): out_fd = s.fileno() in_fd = f.fileno() ofs = lower