Skip to content

Commit

Permalink
Merge pull request #8 from super-coffee/dev
Browse files Browse the repository at this point in the history
支持自动重连
  • Loading branch information
CharlieYu4994 authored Apr 28, 2020
2 parents 69fccb7 + 1ad2926 commit 3329aaf
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 39 deletions.
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/web/ @jinzhijie
/server/ @CharlieYu4994
4 changes: 2 additions & 2 deletions server/coffee-keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def addNew():
u_password = database.encrypt_password(
request.form['password'].encode()) # BASE64 交给 database.py
u_pubkey = request.form['pubkey']
u_uuid = str(database.get_u_uuid(u_mail))
u_uuid = database.get_u_uuid(u_mail)
u_date = database.get_u_date()
status, msg = database.add_new(
u_uuid, u_name, u_mail, u_password, u_pubkey, u_date)
Expand Down Expand Up @@ -120,7 +120,7 @@ def update():
return redirect(f'/updateInfo.html?msg=原密码查询失败', 302)
# 执行 update
u_pubkey = request.form['pubkey']
u_uuid = str(database.get_u_uuid(u_mail))
u_uuid = database.get_u_uuid(u_mail)
u_date = database.get_u_date()
id_status, u_id = database.find_ID(origin_mail)
if id_status:
Expand Down
55 changes: 35 additions & 20 deletions server/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@


def get_u_uuid(mail_addr):
"""传入邮箱,返回 UUID"""
return uuid.uuid5(uuid.NAMESPACE_DNS, mail_addr)
"""传入邮箱,返回 UUID (str)"""
return str(uuid.uuid5(uuid.NAMESPACE_DNS, mail_addr))


def get_u_date():
Expand All @@ -48,8 +48,18 @@ def encrypt_password(original_password):
return hashed_password


def reconnect():
"""重新连接数据库"""
try:
db.ping(reconnect=True)
return True, 'reconnected'
except Exception as e:
return False, e


def add_new(u_uuid, u_name, u_mail, u_password, u_pubkey, u_date):
"""新增数据"""
db.ping(reconnect=True)
sql = f"""INSERT INTO `{settings.Database.table}` (`uuid`, `name`, `mail`, `password`, `pubkey`, `date`)
VALUES (%s, %s, %s, %s, %s, %s)"""
try:
Expand All @@ -58,7 +68,8 @@ def add_new(u_uuid, u_name, u_mail, u_password, u_pubkey, u_date):
print(m)
return False, m
u_password = base64.b64encode(u_password).decode()
cursor.execute(sql, (u_uuid, u_name, u_mail, u_password, u_pubkey, u_date))
cursor.execute(sql, (u_uuid, u_name, u_mail,
u_password, u_pubkey, u_date))
# 提交到数据库执行
db.commit()
m = 'Added'
Expand All @@ -74,8 +85,9 @@ def add_new(u_uuid, u_name, u_mail, u_password, u_pubkey, u_date):
def is_exist(u_mail):
"""
根据邮箱查询,检查是否存在;
False 是不重复
False 是不存在
"""
db.ping(reconnect=True)
sql = f"""SELECT * FROM `{settings.Database.table}` WHERE mail = %s"""
try:
cursor.execute(sql, u_mail)
Expand All @@ -89,13 +101,12 @@ def is_exist(u_mail):

def find(u_mail):
"""根据邮箱查询,不返回 password 字段"""
db.ping(reconnect=True)
sql = f"""SELECT name, mail, pubkey, date FROM `{settings.Database.table}` WHERE mail = %s"""
try:
cursor.execute(sql, u_mail)
# 获取所有记录列表
results = cursor.fetchall()
if len(results) == 0: # 过滤找不到
return False, errors.info_not_found
row = results[0]
data = {
'name': row[0],
Expand All @@ -111,6 +122,7 @@ def find(u_mail):

def query_password(u_mail):
"""根据邮箱查询 password 字段"""
db.ping(reconnect=True)
sql = f"""SELECT password FROM `{settings.Database.table}` WHERE mail = %s"""
try:
cursor.execute(sql, u_mail)
Expand All @@ -124,6 +136,7 @@ def query_password(u_mail):

def find_ID(u_mail):
"""根据邮箱查询 id 字段"""
db.ping(reconnect=True)
sql = f"""SELECT id FROM `{settings.Database.table}` WHERE mail = %s"""
try:
cursor.execute(sql, u_mail)
Expand All @@ -135,12 +148,14 @@ def find_ID(u_mail):


def update(u_uuid, u_name, u_mail, u_password, u_pubkey, u_date, u_id):
"""根据id字段更新数据库"""
"""根据 id 字段更新数据库"""
db.ping(reconnect=True)
sql = f"""UPDATE `{settings.Database.table}` SET uuid=%s, name=%s, mail=%s,
password=%s, pubkey=%s, date=%s WHERE id={u_id}"""
try:
u_password = base64.b64encode(u_password).decode()
cursor.execute(sql, (u_uuid, u_name, u_mail, u_password, u_pubkey, u_date))
cursor.execute(sql, (u_uuid, u_name, u_mail,
u_password, u_pubkey, u_date))
db.commit()
return True, 'ok'
except Exception as e:
Expand All @@ -150,36 +165,36 @@ def update(u_uuid, u_name, u_mail, u_password, u_pubkey, u_date, u_id):


def delete(ID):
"""根据id字段删除记录"""
"""根据 id 字段删除记录"""
db.ping(reconnect=True)
sql = """DELETE FROM `{table}` WHERE id = {id}""".format(
table=settings.Database.table, id=ID)
try:
print(sql)
cursor.execute(sql)
db.commit()
return True
except:
m = 'Error: unable to delete data'
except Exception as e:
db.rollback()
print(m)
return False, m
return False, repr(e)


def confirm_authority(u_mail, u_password):
"""根据mail字段确认操作权限"""
"""根据 mail 字段确认操作权限"""
db.ping(reconnect=True)
try:
status, d_password = query_password(u_mail)
if status and check_password(u_password, d_password):
return True
return True, None
else:
return False
except:
m = 'Error: unable to confirm authority'
return False, m
return False, 'no permission'
except Exception as e:
return False, repr(e)


def reformat_id():
"""重新排列 id 列"""
db.ping(reconnect=True)
try:
cursor.execute("ALTER TABLE `{table}` DROP `id`;".format(
table=settings.Database.table))
Expand All @@ -195,4 +210,4 @@ def reformat_id():


if __name__ == "__main__":
reformat_id()
_, msg = reformat_id()
2 changes: 2 additions & 0 deletions server/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
recaptcha_verify_failed = {'status': False, 'data': 'reCAPTCHA 验证失败,请刷新重试'}

hack_warning = 'We have detected that you are hacking. STOP it!'

info_not_found = '邮箱不存在'
9 changes: 0 additions & 9 deletions web/js/newKey.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
// urlParams
const urlParams = new URLSearchParams(window.location.search);
let msg = urlParams.get('msg');
if (msg != null) {
layui.use('layer', function () {
var layermsg = layui.layer;
layermsg.msg(msg);
});
};
// vue.js
var pem2text = new Vue({
el: '#pem2text',
Expand Down
11 changes: 4 additions & 7 deletions web/js/searchKey.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
// urlParams
const urlParams = new URLSearchParams(window.location.search);
let userMail = urlParams.get('mail');
let msg = urlParams.get('msg');
if (userMail != null) {
searchKey(userMail);
};
if (msg != null) {
layui.use('layer', function () {
var layermsg = layui.layer;
layermsg.msg(msg);
});
};

// vue
var datas = new Vue({
Expand Down Expand Up @@ -52,4 +45,8 @@ clipboard.on('error', function (e) {
layer.tips('复制失败,请手动复制', '#pubkey', {
tips: 3
});
});

layui.use('form', function () {
var form = layui.form;
});
1 change: 1 addition & 0 deletions web/newKey.html
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
<script src="./js/renderUi.js"></script>
<script src="https://www.recaptcha.net/recaptcha/api.js?onload=grecaptcha&render=explicit" async defer></script>
<script src="./js/newKey.js"></script>
<script src="./js/showMsg.js"></script>
</body>

</html>
3 changes: 2 additions & 1 deletion web/searchKey.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
</div>
<div class="layui-input-inline">
<input type="text" name="mail" required placeholder="持有者的邮箱"
lay-verify="required|email" class="layui-input">
lay-verify="email" class="layui-input">
</div>
</div>
<div class="layui-form-item">
Expand Down Expand Up @@ -87,6 +87,7 @@
<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script src="./js/renderUi.js"></script>
<script src="./js/searchKey.js"></script>
<script src="./js/showMsg.js"></script>
<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
</body>

Expand Down

0 comments on commit 3329aaf

Please sign in to comment.