|
6 | 6 | import pathlib |
7 | 7 |
|
8 | 8 | CONFIG_DIR = pathlib.Path("/etc/d2ray") |
9 | | -PRIVKEY = CONFIG_DIR.joinpath("certs/private_key") |
10 | | -PUBKEY = CONFIG_DIR.joinpath("certs/public_key") |
| 9 | +KEY_FILE = CONFIG_DIR.joinpath("certs/keys") |
11 | 10 | LOG_DIR = CONFIG_DIR.joinpath("logs") |
12 | 11 | XRAY_BIN = pathlib.Path("/opt/xray/xray") |
13 | 12 |
|
@@ -118,21 +117,23 @@ def main(): |
118 | 117 | args.from_env() |
119 | 118 |
|
120 | 119 | print("====== init.py ======", flush=True) |
121 | | - print(f"Checking server private key...", flush=True) |
122 | | - if not PRIVKEY.exists(): |
123 | | - print(f"Server private key not found at {PRIVKEY}. Generating...") |
124 | | - skey, _ = parse_xray_x25519_output(subprocess.check_output(f"{XRAY_BIN} x25519", shell = True).decode()) |
125 | | - with open(PRIVKEY, "w") as f: |
126 | | - f.write(skey) |
| 120 | + print(f"Checking key file...", flush=True) |
| 121 | + if not KEY_FILE.exists(): |
| 122 | + print(f"Key file not found at {KEY_FILE}. Generating...") |
| 123 | + out = subprocess.check_output(f"{XRAY_BIN} x25519", shell = True).decode() |
| 124 | + with open(KEY_FILE, "w") as f: |
| 125 | + f.write(out) |
127 | 126 |
|
128 | | - with open(PRIVKEY, "r") as f: |
129 | | - skey = f.read().strip() |
| 127 | + with open(KEY_FILE, "r") as f: |
| 128 | + out = f.read() |
130 | 129 |
|
131 | | - print(f"Deriving public key...", flush=True) |
132 | | - _, pkey = parse_xray_x25519_output(subprocess.check_output(f"{XRAY_BIN} x25519 -i {skey}", shell = True).decode()) |
| 130 | + print(f"Reading keys...", flush=True) |
| 131 | + skey, pkey = parse_xray_x25519_output(out) |
133 | 132 |
|
134 | | - with open(PUBKEY, "w") as f: |
135 | | - f.write(pkey) |
| 133 | + print(f"Verifying public key...", flush=True) |
| 134 | + _, _pkey = parse_xray_x25519_output(subprocess.check_output(f"{XRAY_BIN} x25519 -i {skey}", shell = True).decode()) |
| 135 | + if (_pkey != pkey): |
| 136 | + print(f"Unmatching public key: expected \"{_pkey}\" but key file provided \"{pkey}\". Please verify the key file.", flush=True) |
136 | 137 |
|
137 | 138 | print(f"\nConfigurations:\n{str(args)}\nPublic key: {pkey}\n", flush=True) |
138 | 139 |
|
|
0 commit comments