Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 51 additions & 12 deletions accesser/utils/certmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os
import os, platform
import datetime
from pathlib import Path

Expand All @@ -33,14 +33,53 @@
)
from cryptography.x509.oid import ExtendedKeyUsageOID

from .log import logger
logger = logger.getChild("certmanager")
from . import setting
from .setting import basepath

if setting.config["importca"]:
certpath = os.path.join(basepath, "CERT")
else:
certpath = "CERT"
if not os.path.exists(certpath):

def decide_state_path_legacy():
if setting.config["importca"]:
return Path(basepath)
else:
return Path()


def decide_state_path_unix_like():
if os.geteuid() == 0:
logger.warn("Running Accesser as the root user carries certain risks; see pull #245")
return Path("/var/lib") / "accesser"

state_path = os.getenv("XDG_STATE_HOME", None)
if state_path is not None:
state_path = Path(state_path) / "accesser"
else:
state_path = Path.home() / ".local/state" / "accesser"
return state_path


def decide_certpath():
certpath = None
# 人为指定最优先
#if setting.config["state_dir"]:
#return Path(setting.config["state_dir"]) / "cert"
match platform.system():
case 'Linux' | 'FreeBSD':
deprecated_path = decide_state_path_legacy() / "CERT"
# 暂仅在 *nix 上视为已废弃
if deprecated_path.exists():
logger.warn("deprecated path, see pull #245")
return deprecated_path
certpath = decide_state_path_unix_like() / "cert"
case _:
# windows,mac,android ...
certpath = decide_state_path_legacy() / "CERT"
return certpath


certpath = decide_certpath()
if not certpath.exists():
os.makedirs(certpath, exist_ok=True)


Expand Down Expand Up @@ -92,28 +131,28 @@ def create_root_ca():
.sign(key, hashes.SHA256())
)

(Path(certpath) / "root.crt").write_bytes(
(certpath / "root.crt").write_bytes(
cert.public_bytes(serialization.Encoding.PEM)
)

(Path(certpath) / "root.key").write_bytes(
(certpath / "root.key").write_bytes(
key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption(),
)
)

(Path(certpath) / "root.pfx").write_bytes(
(certpath / "root.pfx").write_bytes(
serialization.pkcs12.serialize_key_and_certificates(
b"Accesser", key, cert, None, serialization.NoEncryption()
)
)


def create_certificate(server_name):
rootpem = (Path(certpath) / "root.crt").read_bytes()
rootkey = (Path(certpath) / "root.key").read_bytes()
rootpem = (certpath / "root.crt").read_bytes()
rootkey = (certpath / "root.key").read_bytes()
ca_cert = x509.load_pem_x509_certificate(rootpem)
pkey = serialization.load_pem_private_key(rootkey, password=None)

Expand Down Expand Up @@ -180,7 +219,7 @@ def create_certificate(server_name):
.sign(pkey, hashes.SHA256())
)

(Path(certpath) / f"{server_name}.crt").write_bytes(
(certpath / f"{server_name}.crt").write_bytes(
cert.public_bytes(serialization.Encoding.PEM)
+ pkey.private_bytes(
encoding=serialization.Encoding.PEM,
Expand Down
7 changes: 2 additions & 5 deletions accesser/utils/importca.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os, sys
from pathlib import Path
import subprocess
import locale

Expand All @@ -25,14 +26,10 @@

from . import setting
from . import certmanager as cm
from .setting import basepath
from .log import logger
logger = logger.getChild('importca')

if setting.config['importca']:
certpath = os.path.join(basepath, 'CERT')
else:
certpath = 'CERT'
certpath = cm.certpath

def logandrun(cmd):
if hasattr(subprocess, 'STARTUPINFO'):
Expand Down
10 changes: 10 additions & 0 deletions accesser/utils/setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,18 @@ def parse_args():
action="store_true",
help="do not import certificate to system automatically",
)
parser.add_argument(
"--state-dir",
type=str,
help="where state file store , override notimportca",
default=None,
)
args = parser.parse_args()
if args.notsetproxy:
config["setproxy"] = False
return
# FIXME Wrong initialization sequence
# see pull #245
if args.notimportca:
config["importca"] = False
config["state_dir"] = args.state_dir