Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Password #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
84 changes: 79 additions & 5 deletions polarishub_flask/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import json
from polarishub_flask.server import help

from password import add_password, delete_password, has_password, verify_password

os_name = os.name
platform = sys.platform
# printv("os_name:", os_name)
Expand Down Expand Up @@ -67,14 +69,80 @@ def file(filename):
printv (local_path)
is_admin = network.checkIP(request.remote_addr)

if os.path.isfile(local_path):
return send_file(local_path)
elif os.path.isdir(local_path):
if is_admin:
if os.path.isfile(local_path):
return send_file(local_path)
elif os.path.isdir(local_path):
return render_template('index.html', cwd = local_path.replace('\\', "\\\\") if platform=="win32" else local_path,
dirs = file_handler.get_dir(local_path), is_admin = is_admin,
user_settings = file_handler.get_settings(), ip = network.get_host_ip())
else:
if os.path.isfile(local_path):
filename = local_path.split('\\')[-1] # 这一行好像是依赖操作系统的
cwd = local_path[:-len(filename)]
elif os.path.isdir(local_path):
filename = None
cwd = local_path
if has_password(cwd, filename):
# verify会比index更低一个层级,需要反馈的是本事而不是子文件
filename = local_path.split('\\')[-2] # 这是一行系统依赖代码
local_path = local_path[:-len(filename)-1]
local_path = local_path.replace('\\', "\\\\") if platform=="win32" else local_path
return render_template('verify.html', user_settings = file_handler.get_settings(),
cwd = local_path,
dir = filename)
else:
if os.path.isfile(local_path):
return send_file(local_path)
elif os.path.isdir(local_path):
return render_template('index.html', cwd = local_path.replace('\\', "\\\\") if platform=="win32" else local_path,
dir = file_handler.get_dir(local_path), is_admin = is_admin,
user_settings = file_handler.get_settings(), ip = network.get_host_ip())
else:
abort(404)

@app.route('/verify', methods=['POST'])
def verify():
filename = request.form["filename"] # 这个filename可能是一个文件夹名
password = request.form["password"]
cwd = request.form["cwd"]
local_path = os.path.join(os.getcwd(), 'files', filename)
if os.path.isfile(os.path.join(cwd, filename)): # filename正确化,若为文件夹则为none
pass
elif os.path.isdir(os.path.join(cwd, filename)):
cwd = os.path.join(cwd, filename)
filename = None
else:
abort(404)
if verify_password(cwd, filename, password): # 这一行也是依赖操作系统的
return render_template('index.html', cwd = local_path.replace('\\', "\\\\") if platform=="win32" else local_path,
dirs = file_handler.get_dir(local_path), is_admin = is_admin,
user_settings = file_handler.get_settings(), ip = network.get_host_ip())
dirs = file_handler.get_dir(local_path), is_admin = False,
user_settings = file_handler.get_settings(), ip = network.get_host_ip())
else:
return "wrong password!"

@app.route('/password', methods=['POST'])
def password():
filename = request.form["filename"] # 这个filename可能是一个文件夹名
password = request.form["password"]
cwd = request.form["cwd"]
if os.path.isfile(os.path.join(cwd, filename)): # filename正确化,若为文件夹则为none
pass
elif os.path.isdir(os.path.join(cwd, filename)):
cwd = os.path.join(cwd, filename)
filename = None
else:
abort(404)
is_admin = network.checkIP(request.remote_addr)
if is_admin: # 判断是否为主机
if password == '':
delete_password(cwd, filename) # 如果返回密码值为空即视为删除
else:
add_password(cwd, filename, password)
else:
pass

return redirect('/files')

@app.route('/opendir')
def opendir():
Expand Down Expand Up @@ -146,4 +214,10 @@ def help_page():
return redirect('/static/help.html')
# return render_template('help.html', help_content = help.help_content)


@app.errorhandler(404)
def miss(e):
return request.url, 404


return app
65 changes: 65 additions & 0 deletions polarishub_flask/server/password.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import os
from flask import abort
from polarishub_flask.server.parser import printv
import sqlite3

def has_password(cwd, filename):
directory = get_directory(cwd)
conn = sqlite3.connect(os.path.join(os.getcwd(), 'server', 'passwords.db'))
cursor = conn.cursor()
cursor.execute(f'SELECT * FROM passwords WHERE directory = "{directory}" AND filename = "{filename}";')
if len(list(cursor)) == 0:
return False
else:
return True

def add_password(cwd, filename, password):
cwd = cwd.replace("\\\\", "\\") # 这一行是依赖操作系统的
directory = get_directory(cwd)
conn = sqlite3.connect(os.path.join(os.getcwd(), 'server', 'passwords.db'))
cursor = conn.cursor()
cursor.execute(f'INSERT INTO passwords (directory, filename, password) VALUES ("{directory}", "{filename}", "{password}");')
conn.commit()
conn.close()

def delete_password(cwd, filename):
cwd = cwd.replace("\\\\", "\\") # 这一行是依赖操作系统的
conn = sqlite3.connect(os.path.join(os.getcwd(), 'server', 'passwords.db'))
directory = get_directory(cwd)
cursor = conn.cursor()
cursor.execute(f'DELETE FROM passwords WHERE directory = "{directory}" AND filename = "{filename}";')
conn.commit()
conn.close()

def check_password():
'''验证是否为双层密码,如果是,否决
'''
pass

def verify_password(cwd, filename, password):
'''验证密码是否正确
'''
cwd = cwd.replace("\\\\", "\\") # 这一行是依赖操作系统的
directory = get_directory(cwd)
conn = sqlite3.connect(os.path.join(os.getcwd(), 'server', 'passwords.db'))
cursor = conn.cursor()
cursor.execute(f'SELECT * FROM passwords WHERE directory = "{directory}" AND filename = "{filename}" AND password = "{password}";')
if len(list(cursor)) == 0:
return False
else:
return True

def get_directory(cwd):
'''获取相对路径
'''
main = os.path.join(os.getcwd(), 'files')
directory = cwd[len(main):]
if directory == '':
return '/'
else:
return directory

def refresh_passwords():
'''更新数据库,剔除已经删除的文件的密码
'''
pass
Binary file added polarishub_flask/server/passwords.db
Binary file not shown.
27 changes: 25 additions & 2 deletions polarishub_flask/server/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,39 @@ <h1 class="mr-3">{{user_settings.username}}'s PolarisHub</h1> <p> (Flask v0.1)</

{{dir[0]}}
</a>
<div class="float-right d-flex flex-row">
<div class="float-right d-flex flex-row" id="row">
<div class="mr-3">{{"http://{}:5000{}".format(ip, dir[2].replace("\\", "/"))}}</div>
<!-- <a href="/qr?filepath={{dir[2].replace("\\", "/")}}"> <i class="fas fa-share-alt"></i> </a> -->
<form style="display:none" id="password-form{{ loop.index0 }}" action="/password" method="POST">
<div class="form-group">
<input type="text" name="cwd" class="form-control" style="display:none" value="{{ cwd }}">
<input type="text" name="filename" class="form-control" style="display:none" value="{{ dir[0] }}">
<input type="text" name="password" class="form-control">
<button class="btn btn-outline-primary" type="submit" id="submit-button{{ loop.index0 }}">Submit / 提交</button>
</div>
</form>
<button class="btn btn-outline-primary" id="callout-button{{ loop.index0 }}">Password / 密码</button>
<form action = "/qr" method="POST">
<input type="text" style="display: none" id="filepath" name="filepath" value="{{dir[2].replace("\\", "/")}}">
<button class="btn btn-outline-primary" type="submit"><i class="fas fa-share-alt"></i> </button>
</form>
<script>
document.querySelector("#callout-button{{ loop.index0 }}").onclick = callout;
document.querySelector("#submit-button{{ loop.index0 }}").onclick = submit;

function callout() {
document.querySelector("#password-form{{ loop.index0 }}").style.display = "";
document.querySelector("#callout-button{{ loop.index0 }}").style.display = "none";
};

function submit() {
document.querySelector("#callout-button{{ loop.index0 }}").style.display = "";
document.querySelector("#password-form{{ loop.index0 }}").style.display = "none";
};
</script>
</div>
</li>
{% endfor %}
</ul>
</div>
</div>
</div>
2 changes: 2 additions & 0 deletions polarishub_flask/server/templates/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ <h1>Settings / 设置</h1>
</div>
<button type="submit" class="btn btn-primary mb-2">Update / 更新设置</button>
</form>

<!-- 更新一个清空所有密码的按钮 -->
</div>
</div>
61 changes: 61 additions & 0 deletions polarishub_flask/server/templates/verify.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<!doctype html>
{% if user_settings.username == "" %}
<title>PolarisHub (Flask v0.1)</title>
{% else %}
<title>{{user_settings.username}}'s PolarisHub (Flask v0.1)</title>
{% endif %}
<link rel="stylesheet" href="/static/css/bootstrap.css">
<link rel="stylesheet" href="/static/css/all.css">
<link rel="icon" href="/static/favicon.ico"type=”image/x-icon”>
<meta name="viewpoint" content="user-scalable=no, width=device-width, initial-scale=1.0">

<div class="container">
<div class="my-5">
<div class="d-flex justify-content-between">
<div class="d-flex align-items-end">
{% if user_settings.username == "" %}
<h1 class="mr-3">PolarisHub</h1> <p>(Flask v0.1)</p>
{% else %}
<h1 class="mr-3">{{user_settings.username}}'s PolarisHub</h1> <p> (Flask v0.1)</p>
{% endif %}
</div>

<div>
<a href="https://github.com/XieGuochao/polarishub_flask" target="_blank">
<button type="button" class="btn btn-outline-primary my-1">
<i class="fab fa-github"></i>
</button>
</a>
<a href="/about">
<button type="button" class="btn btn-outline-primary my-1 ml-3 mr-3">
About / 关于
</button>
</a>
<a href="/help">
<button type="button" class="btn btn-outline-primary my-1 float-right">
Help / 帮助
</button>
</a>
</div>
</div>
<div>
<form action="/qr" method = "POST">
<!-- <a href="/qr?filepath=/files" class="float-right"> -->
<input type="text" style="display: none" id="filepath" name="filepath" value="/files">
<button type="submit" class="btn btn-outline-primary my-1 float-right">
<i class="fas fa-share-alt"></i>
</button>
<!-- </a> -->
</form>
</div>
</div>
<div>
<form class="form-group" action="/verify" method="POST">
<input type="text" name="cwd" class="form-control" style="display:none" value="{{ cwd }}">
<input type="text" name="filename" class="form-control" style="display:none" value="{{ dir }}">
<input type="text" name="password" class="form-control">
<button class="btn btn-outline-primary" type="submit">Submit / 提交</button>
</form>
</div>

</div>
8 changes: 8 additions & 0 deletions reports
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Week1 10.22-10.28
Haowen Yang : 1. 安装AndroidStudio 及阅读JAVA文档
others:none

# Week3 11.10
Ke LI: 1.阅读flask官方quickstart文档(进度40/100)和Youtube上关于flask的tutorial 2.Python快速学习中
haowen Yang:1.安装版服务器搭建成功
Yukun Lin:1.开始学Go
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import setuptools

with open("README.md", "r") as fh:
with open("README.md", "r", encoding='utf-8') as fh:
long_description = fh.read()

REQUIRED = [
Expand Down