Skip to content

Commit 3084981

Browse files
committed
merge key file
1 parent d32be90 commit 3084981

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ d2ray is a single Docker container that provides easy 5-minute setups and braind
1818
All d2ray logs and private/public key pairs are stored in `/etc/d2ray` in the container. You can mount an external folder to that location to persist settings. See the example `docker-compose.yml`.
1919

2020
## Key Generation
21-
d2ray checks whether a private key file exists at path `/etc/xray/certs/private_key` and generates a new private key if not found.
21+
d2ray checks whether a key file exists at path `/etc/xray/certs/keys` and generates a new key pair if not found.
2222

23-
You can either supply a pre-generated private key using `xray x25519` or let d2ray generate one. The corresponding public key is both printed to the container log (`docker logs`) and written to `/etc/xray/certs/public_key`, which clients use to connect.
23+
You can either supply a pre-generated private key using `xray x25519` or let d2ray generate one. The corresponding public key is printed to the container log (`docker logs`), which clients use to connect.
24+
25+
If you are generating the private key yourself, the key file must contain exactly the output of `xray x25519`.
2426

2527
## How To Update?
2628
- `docker compose down`

opt/init.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
import pathlib
77

88
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")
1110
LOG_DIR = CONFIG_DIR.joinpath("logs")
1211
XRAY_BIN = pathlib.Path("/opt/xray/xray")
1312

@@ -118,21 +117,23 @@ def main():
118117
args.from_env()
119118

120119
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)
127126

128-
with open(PRIVKEY, "r") as f:
129-
skey = f.read().strip()
127+
with open(KEY_FILE, "r") as f:
128+
out = f.read()
130129

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)
133132

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)
136137

137138
print(f"\nConfigurations:\n{str(args)}\nPublic key: {pkey}\n", flush=True)
138139

0 commit comments

Comments
 (0)