gistfile1.txt
· 1.8 KiB · Text
Bruto
def config_load(file):
"""
Load wgctl.json and output KEY=value pairs for all config fields.
Automatically handles nested sections — add fields to JSON, they appear here.
"""
import os
if not os.path.exists(file):
return
try:
with open(file) as f:
d = json.load(f)
def emit(key, val):
if val is not None and val != '':
print(f"{key}={val}")
wg = d.get('wireguard', {})
dns = d.get('dns', {})
hs = d.get('handshake', {})
act = d.get('activity', {})
dis = d.get('display', {})
emit('WG_INTERFACE', wg.get('interface'))
emit('WG_ENDPOINT', wg.get('endpoint'))
emit('WG_PORT', wg.get('port'))
emit('WG_SUBNET', wg.get('subnet'))
emit('WG_LAN', wg.get('lan'))
emit('WG_DNS', dns.get('primary'))
# fallback: join array to comma-separated string
fb = dns.get('fallback', [])
if fb: emit('WG_DNS_FALLBACK', ', '.join(str(x) for x in fb))
emit('WG_HANDSHAKE_CHECK_TIME_SEC', hs.get('check_interval_sec'))
emit('DATE_FORMAT', dis.get('date_format'))
atot = act.get('total', {})
acur = act.get('current', {})
emit('ACTIVITY_TOTAL_LOW_BYTES', atot.get('low'))
emit('ACTIVITY_TOTAL_MED_BYTES', atot.get('medium'))
emit('ACTIVITY_TOTAL_HIGH_BYTES', atot.get('high'))
emit('ACTIVITY_CURRENT_LOW_BYTES', acur.get('low'))
emit('ACTIVITY_CURRENT_MED_BYTES', acur.get('medium'))
emit('ACTIVITY_CURRENT_HIGH_BYTES', acur.get('high'))
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)
| 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) |