nuno 修订了这个 Gist 1 month ago. 转到此修订
1 file changed, 45 insertions
gistfile1.txt(文件已创建)
| @@ -0,0 +1,45 @@ | |||
| 1 | + | def config_load(file): | |
| 2 | + | """ | |
| 3 | + | Load wgctl.json and output KEY=value pairs for all config fields. | |
| 4 | + | Automatically handles nested sections — add fields to JSON, they appear here. | |
| 5 | + | """ | |
| 6 | + | import os | |
| 7 | + | if not os.path.exists(file): | |
| 8 | + | return | |
| 9 | + | try: | |
| 10 | + | with open(file) as f: | |
| 11 | + | d = json.load(f) | |
| 12 | + | ||
| 13 | + | def emit(key, val): | |
| 14 | + | if val is not None and val != '': | |
| 15 | + | print(f"{key}={val}") | |
| 16 | + | ||
| 17 | + | wg = d.get('wireguard', {}) | |
| 18 | + | dns = d.get('dns', {}) | |
| 19 | + | hs = d.get('handshake', {}) | |
| 20 | + | act = d.get('activity', {}) | |
| 21 | + | dis = d.get('display', {}) | |
| 22 | + | ||
| 23 | + | emit('WG_INTERFACE', wg.get('interface')) | |
| 24 | + | emit('WG_ENDPOINT', wg.get('endpoint')) | |
| 25 | + | emit('WG_PORT', wg.get('port')) | |
| 26 | + | emit('WG_SUBNET', wg.get('subnet')) | |
| 27 | + | emit('WG_LAN', wg.get('lan')) | |
| 28 | + | emit('WG_DNS', dns.get('primary')) | |
| 29 | + | # fallback: join array to comma-separated string | |
| 30 | + | fb = dns.get('fallback', []) | |
| 31 | + | if fb: emit('WG_DNS_FALLBACK', ', '.join(str(x) for x in fb)) | |
| 32 | + | emit('WG_HANDSHAKE_CHECK_TIME_SEC', hs.get('check_interval_sec')) | |
| 33 | + | emit('DATE_FORMAT', dis.get('date_format')) | |
| 34 | + | ||
| 35 | + | atot = act.get('total', {}) | |
| 36 | + | acur = act.get('current', {}) | |
| 37 | + | emit('ACTIVITY_TOTAL_LOW_BYTES', atot.get('low')) | |
| 38 | + | emit('ACTIVITY_TOTAL_MED_BYTES', atot.get('medium')) | |
| 39 | + | emit('ACTIVITY_TOTAL_HIGH_BYTES', atot.get('high')) | |
| 40 | + | emit('ACTIVITY_CURRENT_LOW_BYTES', acur.get('low')) | |
| 41 | + | emit('ACTIVITY_CURRENT_MED_BYTES', acur.get('medium')) | |
| 42 | + | emit('ACTIVITY_CURRENT_HIGH_BYTES', acur.get('high')) | |
| 43 | + | except Exception as e: | |
| 44 | + | print(f"Error: {e}", file=sys.stderr) | |
| 45 | + | sys.exit(1) | |
上一页
下一页