Skip to content

Commit 33d498b

Browse files
authored
Merge pull request #245 from RadND/master
添加*nix上对xdg spec的处理 ( close #244 )
2 parents 2896fc0 + 2cd8955 commit 33d498b

3 files changed

Lines changed: 63 additions & 17 deletions

File tree

accesser/utils/certmanager.py

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# You should have received a copy of the GNU General Public License
1717
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1818

19-
import os
19+
import os, platform
2020
import datetime
2121
from pathlib import Path
2222

@@ -33,14 +33,53 @@
3333
)
3434
from cryptography.x509.oid import ExtendedKeyUsageOID
3535

36+
from .log import logger
37+
logger = logger.getChild("certmanager")
3638
from . import setting
3739
from .setting import basepath
3840

39-
if setting.config["importca"]:
40-
certpath = os.path.join(basepath, "CERT")
41-
else:
42-
certpath = "CERT"
43-
if not os.path.exists(certpath):
41+
42+
def decide_state_path_legacy():
43+
if setting.config["importca"]:
44+
return Path(basepath)
45+
else:
46+
return Path()
47+
48+
49+
def decide_state_path_unix_like():
50+
if os.geteuid() == 0:
51+
logger.warn("Running Accesser as the root user carries certain risks; see pull #245")
52+
return Path("/var/lib") / "accesser"
53+
54+
state_path = os.getenv("XDG_STATE_HOME", None)
55+
if state_path is not None:
56+
state_path = Path(state_path) / "accesser"
57+
else:
58+
state_path = Path.home() / ".local/state" / "accesser"
59+
return state_path
60+
61+
62+
def decide_certpath():
63+
certpath = None
64+
# 人为指定最优先
65+
#if setting.config["state_dir"]:
66+
#return Path(setting.config["state_dir"]) / "cert"
67+
match platform.system():
68+
case 'Linux' | 'FreeBSD':
69+
deprecated_path = decide_state_path_legacy() / "CERT"
70+
# 暂仅在 *nix 上视为已废弃
71+
if deprecated_path.exists():
72+
logger.warn("deprecated path, see pull #245")
73+
return deprecated_path
74+
certpath = decide_state_path_unix_like() / "cert"
75+
case _:
76+
# windows,mac,android ...
77+
certpath = decide_state_path_legacy() / "CERT"
78+
return certpath
79+
80+
81+
certpath = decide_certpath()
82+
if not certpath.exists():
4483
os.makedirs(certpath, exist_ok=True)
4584

4685

@@ -92,28 +131,28 @@ def create_root_ca():
92131
.sign(key, hashes.SHA256())
93132
)
94133

95-
(Path(certpath) / "root.crt").write_bytes(
134+
(certpath / "root.crt").write_bytes(
96135
cert.public_bytes(serialization.Encoding.PEM)
97136
)
98137

99-
(Path(certpath) / "root.key").write_bytes(
138+
(certpath / "root.key").write_bytes(
100139
key.private_bytes(
101140
encoding=serialization.Encoding.PEM,
102141
format=serialization.PrivateFormat.PKCS8,
103142
encryption_algorithm=serialization.NoEncryption(),
104143
)
105144
)
106145

107-
(Path(certpath) / "root.pfx").write_bytes(
146+
(certpath / "root.pfx").write_bytes(
108147
serialization.pkcs12.serialize_key_and_certificates(
109148
b"Accesser", key, cert, None, serialization.NoEncryption()
110149
)
111150
)
112151

113152

114153
def create_certificate(server_name):
115-
rootpem = (Path(certpath) / "root.crt").read_bytes()
116-
rootkey = (Path(certpath) / "root.key").read_bytes()
154+
rootpem = (certpath / "root.crt").read_bytes()
155+
rootkey = (certpath / "root.key").read_bytes()
117156
ca_cert = x509.load_pem_x509_certificate(rootpem)
118157
pkey = serialization.load_pem_private_key(rootkey, password=None)
119158

@@ -180,7 +219,7 @@ def create_certificate(server_name):
180219
.sign(pkey, hashes.SHA256())
181220
)
182221

183-
(Path(certpath) / f"{server_name}.crt").write_bytes(
222+
(certpath / f"{server_name}.crt").write_bytes(
184223
cert.public_bytes(serialization.Encoding.PEM)
185224
+ pkey.private_bytes(
186225
encoding=serialization.Encoding.PEM,

accesser/utils/importca.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1818

1919
import os, sys
20+
from pathlib import Path
2021
import subprocess
2122
import locale
2223

@@ -25,14 +26,10 @@
2526

2627
from . import setting
2728
from . import certmanager as cm
28-
from .setting import basepath
2929
from .log import logger
3030
logger = logger.getChild('importca')
3131

32-
if setting.config['importca']:
33-
certpath = os.path.join(basepath, 'CERT')
34-
else:
35-
certpath = 'CERT'
32+
certpath = cm.certpath
3633

3734
def logandrun(cmd):
3835
if hasattr(subprocess, 'STARTUPINFO'):

accesser/utils/setting.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,18 @@ def parse_args():
7171
action="store_true",
7272
help="do not import certificate to system automatically",
7373
)
74+
parser.add_argument(
75+
"--state-dir",
76+
type=str,
77+
help="where state file store , override notimportca",
78+
default=None,
79+
)
7480
args = parser.parse_args()
7581
if args.notsetproxy:
7682
config["setproxy"] = False
83+
return
84+
# FIXME Wrong initialization sequence
85+
# see pull #245
7786
if args.notimportca:
7887
config["importca"] = False
88+
config["state_dir"] = args.state_dir

0 commit comments

Comments
 (0)