From c344937eec4777e48ea7cfd238a44c9ace7c82cb Mon Sep 17 00:00:00 2001 From: Kiritow <1362050620@qq.com> Date: Thu, 27 Jan 2022 03:12:58 +0000 Subject: [PATCH] Add MTU patch --- generate.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/generate.py b/generate.py index 790dcce..56d2b5c 100644 --- a/generate.py +++ b/generate.py @@ -1,8 +1,12 @@ import os import sys import time +import getopt -filepath = sys.argv[1] + +opts, args = getopt.getopt(sys.argv[1:], 'k') + +filepath = args[0] path_udp2raw = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), 'bin/udp2raw_amd64') path_w2u = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), 'bin/w2u') @@ -30,12 +34,16 @@ def add_tmux_session_once(): for line in content: if line.startswith('ListenPort'): gen_ctx['wg_port'] = int(line.split('=')[1]) + if line.startswith('MTU'): + gen_ctx['mtu_detected'] = True if line.startswith('[Peer]'): if 'peer_started' not in gen_ctx: gen_ctx['peer_started'] = True + if 'mtu_detected' not in gen_ctx: + sys.stderr.write('[WARN] MTU not detected, using suggested mtu value (1280).\n') + results.append('MTU=1280') if gen_ctx['post_down']: results.extend(gen_ctx['post_down']) - if 'has_setup_tmux' in gen_ctx: results.append('PostDown=sleep 1; /usr/bin/tmux kill-session -t tunnel-{}'.format(wg_name)) @@ -177,9 +185,12 @@ for line in content: else: results.insert(last_postup_idx, 'PostUp=ip -4 route add {} dev wg0'.format(route_target)) else: - print('[WARN] comment or unknown hint: {}'.format(line)) + sys.stderr.write('[WARN] comment or unknown hint: {}\n'.format(line)) -with open('{}.gen'.format(filename), 'w') as f: - f.write('# Generated by wg-ops at {}. DO NOT EDIT\n'.format(time.strftime("%Y-%m-%d %H:%M:%S"))) - f.write('\n'.join(results)) +final_content = '# Generated by wg-ops at {}. DO NOT EDIT\n{}'.format(time.strftime("%Y-%m-%d %H:%M:%S"), '\n'.join(results)) +if '-k' not in final_content: + with open('{}.gen'.format(filename), 'w') as f: + f.write(final_content) +else: + print(final_content)