Browse Source

配置文件加密

gushoubang 1 week ago
parent
commit
4288dc99fa

+ 0 - 26
.env

@@ -1,26 +0,0 @@
-# database
-db_host=192.168.60.52
-db_port=5432
-db_schema=vector
-db_user=postgres
-db_password=postgres
-db_name=real3d
-
-# redis
-redis_host=192.168.60.220
-redis_port=6379
-redis_db=0
-
-# geoserver
-gs_url=http://192.168.60.2:28085/geoserver
-gs_user=admin
-gs_password=geoserver
-gs_default_workspace=demo
-
-# ftp
-ftp_host = '192.168.60.52'
-ftp_port = 2021
-ftp_username = 'user1'
-ftp_password = 'password'
-model_viewer_uri = 'http://192.168.60.52:8091/website/#model='
-model_baseUri = 'http://192.168.60.52:8090'

+ 0 - 11
config.py

@@ -1,11 +0,0 @@
-# config.py
-import os
-import json
-
-CONFIG_PATH = os.path.join(os.path.dirname(__file__), 'config.json')
-
-with open(CONFIG_PATH, 'r') as f:
-    CONFIG = json.load(f)
-
-# 可导出为全局访问变量
-globals()['CONFIG'] = CONFIG

+ 34 - 0
encrypt/data_decrypted.json

@@ -0,0 +1,34 @@
+{
+    "db": {
+        "host": "192.168.60.52",
+        "port": "5432",
+        "schema": "vector",
+        "schema_base": "base",
+        "user": "postgres",
+        "password": "postgres",
+        "name": "real3d"
+    },
+    "redis": {
+        "host": "192.168.60.220",
+        "port": 6379,
+        "db": 0
+    },
+    "geoserver": {
+        "url": "http://192.168.60.2:28085/geoserver",
+        "username": "admin",
+        "password": "geoserver",
+        "default_workspace": "demo",
+        "cachestart": "0",
+        "cacheend": "15"
+    },
+    "ftp": {
+        "host": "192.168.60.52",
+        "port": 2021,
+        "username": "user1",
+        "password": "password"
+    },
+    "model": {
+        "viewer_uri": "http://192.168.60.52:8091/website/#model=",
+        "base_uri": "http://192.168.60.52:8090"
+    }
+}

+ 52 - 0
encrypt/encry.py

@@ -0,0 +1,52 @@
+import json
+from cryptography.fernet import Fernet
+
+# 生成密钥,只需生成一次并保存
+def generate_key():
+    key = Fernet.generate_key()
+    with open("secret.key", "wb") as key_file:
+        key_file.write(key)
+
+# 读取密钥
+def load_key():
+    with open("secret.key", "rb") as key_file:
+        return key_file.read()
+
+# 加密 JSON 文件
+def encrypt_json(input_file, encrypted_file):
+    key = load_key()
+    fernet = Fernet(key)
+
+    with open(input_file, 'r', encoding='utf-8') as f:
+        data = json.load(f)
+
+    json_str = json.dumps(data)
+    encrypted_data = fernet.encrypt(json_str.encode('utf-8'))
+
+    with open(encrypted_file, 'wb') as f:
+        f.write(encrypted_data)
+
+# 解密 JSON 文件
+def decrypt_json(encrypted_file, output_file):
+    key = load_key()
+    fernet = Fernet(key)
+
+    with open(encrypted_file, 'rb') as f:
+        encrypted_data = f.read()
+
+    decrypted_data = fernet.decrypt(encrypted_data)
+    data = json.loads(decrypted_data.decode('utf-8'))
+
+    with open(output_file, 'w', encoding='utf-8') as f:
+        json.dump(data, f, indent=4, ensure_ascii=False)
+
+# 示例用法
+if __name__ == "__main__":
+    # 只需生成一次密钥
+    # generate_key()
+
+    # 加密
+    encrypt_json("siwei_config.json", "siwei_config.json.enc")
+
+    # 解密
+    # decrypt_json("siwei_config_encry.json", "data_decrypted.json")

+ 5 - 0
encrypt/readme.md

@@ -0,0 +1,5 @@
+1.修改siwei_config.json 中的配置信息
+
+2.运行encry.py,生成加密文件siwei_config.json.enc
+
+3.将siwei_config.json.enc文件放入'C:\siwei_model'目录下

+ 1 - 0
encrypt/secret.key

@@ -0,0 +1 @@
+II-b_Wce5jHbJYYA5f1WqqYEA8R2_i1smaJErzGlpvQ=

+ 1 - 0
config.json → encrypt/siwei_config.json

@@ -3,6 +3,7 @@
     "host": "192.168.60.52",
     "port": "5432",
     "schema": "vector",
+    "schema_base": "base",
     "user": "postgres",
     "password": "postgres",
     "name": "real3d"

+ 1 - 0
encrypt/siwei_config.json.enc

@@ -0,0 +1 @@
+gAAAAABoB1it0uNeIQdbvJzOVcUeeu1xBsaDbCojMB6IOByxt5m-ZiQNl_eaXrvrCjPgZO4DHHURYVx0E4pLOYD-kI7uZqW3JjV1FidTi7Di5D-86k7zIxUkcbeA981Ju_j0XiRmghhcNfBYl7ZnOst-Oi_31VP1dh_bhaBjqCtt4K1tpU7RcmTXCmSlBcqBY56_u5KYSRglnhbE1_Bhud_bBXVkxpBH5cWNfPaw8e1Gih9xOrJ6cw0481oDV1m9mmH4wJJYGMhsWDi4KuMwDGkbizDkwU2GKgqtOwhvKM8vOUJj3aZ5AmhkN2TZI8_VFi410pXX3aYqQ8zMRRpgTd4jlLybuvNabWNrlVho4Q20UzrkdSZuJmr6OyZINmWWJU5zDPvEeMuXzjsA57m284nCx5Q2DC2keK4QtSy70__jMmHIkqP427jguUKQd-gw63SsVyDJs-q0JBPFIEp9jTW98HDnP0nw_-lX09eP1U9CIfJnCBHo2LrqEL8Fc_QiWCI252LKHmyHPmZu91mnxXlim4fJy-T4PP1ruWBaxQYes8UspZ3owNoazj_WYOYNPw4vy_Jy2gx83gFoANlk-G0bLNeTQoRrJETguRnIu6p_HttvvpctZIUfYX11tx0-FPIMZoRBMnNCB2ZR958XrwkgMTXX3NIewrykGCYDAmsOFC0ZwNXF5v3x6RkbvSRAhx041GKx18gr0TJdRkqBhSwrS_loxSIossXSesadtIIpTHEAozuDJHDjIrBHeKRECX5e5AOpQoLsH6nh_AD-0aarOKokXn_HLIygtw4XHxL0nntZ4BUf0067nntXjNZKWSQDLxwoxNXmHSzjuqsoX5p4yv_-15Z8ag==

+ 4 - 3
processing/algs/gdal/GdalAlgorithm.py

@@ -52,7 +52,7 @@ from processing.tools.SubprocessUtils import RunSubprocess
 import processing.tools.QGS.load
 from qgis.PyQt.QtCore import NULL
 
-import config
+import siwei_config
 
 pluginPath = os.path.normpath(os.path.join(
     os.path.split(os.path.dirname(__file__))[0], os.pardir))
@@ -61,10 +61,11 @@ pluginPath = os.path.normpath(os.path.join(
 class GdalAlgorithm(QgsProcessingAlgorithm):
     def __init__(self):
         super().__init__()
-        self.db_config = config.CONFIG['db']
-        self.geoserver_config = config.CONFIG['geoserver']
 
         # 全局参数修改
+        self.db_config = siwei_config.CONFIG['db']
+        self.geoserver_config = siwei_config.CONFIG['geoserver']
+
         self.output_values = {}
         self.spatialite_db_path = "D:\\temp\\output.sqlite"
         self.pgcoon = {

+ 2 - 2
processing/algs/gdal/postgistogeoserver.py

@@ -53,7 +53,7 @@ from processing.tools.StringUtils import getConnectionStr
 from processing.tools.GeoServer.Geoserver import Geoserver
 
 from processing.tools.system import isWindows
-import config
+import siwei_config
 
 pluginPath = os.path.normpath(os.path.join(
     os.path.split(os.path.dirname(__file__))[0], os.pardir))
@@ -88,7 +88,7 @@ class Postgistogeoserver(GdalAlgorithm):
     selectedValue = "selectedValue"
 
     # 全局参数修改
-    geoserver_config = config.CONFIG['geoserver']
+    geoserver_config = siwei_config.CONFIG['geoserver']
     GeoServerConfig = {
         "url": geoserver_config["url"],
         "username": geoserver_config["username"],

+ 3 - 3
processing/tools/FTP/FtpConfig.py

@@ -1,8 +1,8 @@
-import config
+import siwei_config
 
 # 全局参数修改
-ftp_config = config.CONFIG['ftp']
-model_config = config.CONFIG['model']
+ftp_config = siwei_config.CONFIG['ftp']
+model_config = siwei_config.CONFIG['model']
 
 ftpHost = ftp_config['host']
 ftpPort = ftp_config['port']

+ 2 - 2
processing/tools/FileGDB/ImportGDB.py

@@ -1,7 +1,7 @@
 import os
 import subprocess
 import psycopg2
-import config
+import siwei_config
 from osgeo import ogr
 
 from qgis.core import *
@@ -48,7 +48,7 @@ def getLayerGeometryType(geom_type):
 layer_count = gdb.GetLayerCount()
 if layer_count > 0:
     # 全局参数修改
-    db_config = config.CONFIG['db']
+    db_config = siwei_config.CONFIG['db']
 
     # 数据库连接参数
     db_params = {

+ 2 - 2
processing/tools/GeoServer/GeoServerRest.py

@@ -1,8 +1,8 @@
 # -*- coding: utf-8 -*-
 from Geoserver import Geoserver
-import config
+import siwei_config
 
-geoserver_config = config.CONFIG['geoserver']
+geoserver_config = siwei_config.CONFIG['geoserver']
 
 # 连接GeoServer
 geo = Geoserver(geoserver_config['url'], username=geoserver_config['username'], password=geoserver_config['password'])

+ 2 - 2
processing/tools/Login/PostgreSQL.py

@@ -10,14 +10,14 @@ from typing import Optional
 import os
 import psycopg2
 import uuid
-import config
+import siwei_config
 
 
 class PostgreSQL:
     # 用户表
     Sys_user = "sys_user"
     # 全局参数修改
-    db_config = config.CONFIG['db']
+    db_config = siwei_config.CONFIG['db']
 
     def __init__(
             self,

+ 2 - 2
processing/tools/Login/Redis.py

@@ -11,7 +11,7 @@ import os
 import uuid
 import redis
 import socket
-import config
+import siwei_config
 
 
 class Redis:
@@ -21,7 +21,7 @@ class Redis:
     ipaddress = socket.gethostbyname(hostname)
 
     # 全局参数修改
-    redis_config = config.CONFIG['redis']
+    redis_config = siwei_config.CONFIG['redis']
 
     def __init__(
             self,

+ 2 - 2
processing/tools/PostgreSQL/PostgreSQL.py

@@ -10,7 +10,7 @@ from typing import Optional
 import os
 import psycopg2
 import uuid
-import config
+import siwei_config
 from ..FTP.FtpUitl import FtpOper
 from ..FTP.FtpConfig import *
 from ..FileUtils import getInputFileName
@@ -39,7 +39,7 @@ class PostgreSQL:
     Vector_Version_Details = "t_vector_version_details"
 
     # 全局参数修改
-    db_config = config.CONFIG['db']
+    db_config = siwei_config.CONFIG['db']
 
     def __init__(
             self,

+ 1 - 0
secret.key

@@ -0,0 +1 @@
+II-b_Wce5jHbJYYA5f1WqqYEA8R2_i1smaJErzGlpvQ=

+ 31 - 0
siwei_config.py

@@ -0,0 +1,31 @@
+# siwei_config.py
+import os
+import json
+
+from cryptography.fernet import Fernet
+
+# 加载密钥
+def load_key(key_path='secret.key'):
+    current_dir = os.path.dirname(os.path.abspath(__file__))
+    key_path = os.path.join(current_dir, key_path)
+    with open(key_path, 'rb') as key_file:
+        return key_file.read()
+
+# 解密并返回 JSON 数据(不写入磁盘)
+def decrypt_json_to_dict(encrypted_file_path, key_path='secret.key'):
+    key = load_key(key_path)
+    fernet = Fernet(key)
+
+    with open(encrypted_file_path, 'rb') as f:
+        encrypted_data = f.read()
+
+    decrypted_data = fernet.decrypt(encrypted_data)
+    return json.loads(decrypted_data.decode('utf-8'))
+
+CONFIG_PATH = r'C:\siwei_model\siwei_config.json.enc'
+
+# 解密并赋值给 CONFIG
+CONFIG = decrypt_json_to_dict(CONFIG_PATH)
+
+# 可导出为全局访问变量
+globals()['CONFIG'] = CONFIG