From 702b4a3358dd34d941801505883719444ef834aa Mon Sep 17 00:00:00 2001 From: amass <168062547@qq.com> Date: Sun, 16 Jun 2024 06:50:10 +0000 Subject: [PATCH] =?UTF-8?q?1.=E5=AE=9E=E7=8E=B0cookie=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Server/conf/nginx.conf | 37 ++++--------- Server/lua/authentication.lua | 13 +++++ Server/lua/helloworld.lua | 14 ----- Server/lua/{settings.lua => initialize.lua} | 9 ++++ Server/lua/login.lua | 60 ++++++++++++++++----- resource/build.sh | 17 +++--- 6 files changed, 88 insertions(+), 62 deletions(-) create mode 100644 Server/lua/authentication.lua delete mode 100755 Server/lua/helloworld.lua rename Server/lua/{settings.lua => initialize.lua} (51%) mode change 100755 => 100644 mode change 100755 => 100644 Server/lua/login.lua diff --git a/Server/conf/nginx.conf b/Server/conf/nginx.conf index bfb31f1..160edbf 100644 --- a/Server/conf/nginx.conf +++ b/Server/conf/nginx.conf @@ -2,9 +2,7 @@ user root; worker_processes 1; -#error_log logs/error.log; -#error_log logs/error.log notice; -#error_log logs/error.log info; +error_log logs/error.log info; #pid logs/nginx.pid; events { @@ -29,7 +27,7 @@ http { gzip on; gzip_types application/octet-stream text/markdown text/plain application/json application/x-javascript text/css application/xml text/javascript application/javascript application/x-httpd-php image/jpeg image/gif image/png; - + init_by_lua_file lua/initialize.lua; upstream local { server 127.0.0.1:8081; } @@ -61,9 +59,7 @@ http { upstream typesense { server 127.0.0.1:8108; } - - init_by_lua_file lua/settings.lua; - + server { listen 443 ssl; server_name unraid.amass.fun; @@ -263,7 +259,9 @@ http { ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #加密算法 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #安全链接可选的加密协议 ssl_prefer_server_ciphers on; #使用服务器端的首选算法 - + + # access_by_lua_file lua/authentication.lua; + location / { proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; @@ -337,28 +335,15 @@ http { index index.html index.htm; } - location /lua { - default_type text/html; - content_by_lua_file lua/helloworld.lua; - } - - location = /api/login { - content_by_lua_file lua/login.lua; - } - location = /blog/profile { content_by_lua_file lua/profile.lua; } - location /video { - access_by_lua_file lua/access.lua; - proxy_pass http://local; + location /api/v1/login { + default_type 'application/json; charset=utf-8'; + content_by_lua_file lua/login.lua; } - location /phtot_gallery { - proxy_pass http://local; - } - location ~ /api/v1/.*$ { proxy_pass http://local; } @@ -416,10 +401,6 @@ http { root amass_blog; } - location ^~ /index/ { - try_files $uri /index.html; - } - location /wechat { proxy_pass http://local; } diff --git a/Server/lua/authentication.lua b/Server/lua/authentication.lua new file mode 100644 index 0000000..1a3d869 --- /dev/null +++ b/Server/lua/authentication.lua @@ -0,0 +1,13 @@ +local session, err, exists = require "resty.session".open() +if exists and session:get("authenticated") then + ngx.log(ngx.INFO, session:get("account"), " 访问") +else + local server = "" + if ngx.var.server_port == "80" then + server = ngx.var.host + else + server = ngx.var.host .. ":" .. ngx.var.server_port + end + local target_url = ngx.var.scheme .. "://" .. server .. ngx.var.request_uri + ngx.redirect('https://amass.fun/LoginPage?next=' .. ngx.escape_uri(target_url)) +end diff --git a/Server/lua/helloworld.lua b/Server/lua/helloworld.lua deleted file mode 100755 index dcf46b7..0000000 --- a/Server/lua/helloworld.lua +++ /dev/null @@ -1,14 +0,0 @@ -ngx.log(ngx.ERR, "cookie a:", ngx.var.cookie_a) - -ngx.header['Set-Cookie'] = {'a=32; path=/ ', 'b=4; path=/'} -ngx.header['Content-Type'] = "text/html; charset=utf-8"; - -local session = require"resty.session".start() - -ngx.log(ngx.ERR, "cookie a:", ngx.var.cookie_a) - -session.data.name = "OpenResty Fan" -session.cookie.path = "/123" -session:save() - -ngx.say("
123Hello,world 中国
") diff --git a/Server/lua/settings.lua b/Server/lua/initialize.lua old mode 100755 new mode 100644 similarity index 51% rename from Server/lua/settings.lua rename to Server/lua/initialize.lua index c262e44..3a91882 --- a/Server/lua/settings.lua +++ b/Server/lua/initialize.lua @@ -1,3 +1,12 @@ +require "resty.session".init({ + cookie_http_only=true, + remember = true, + storage = "cookie", + remember_rolling_timeout = 3600, +}) + +app_version="0.0.1" + private_folder = {"个人笔记"}; user_infomation = { diff --git a/Server/lua/login.lua b/Server/lua/login.lua old mode 100755 new mode 100644 index 922465a..24330df --- a/Server/lua/login.lua +++ b/Server/lua/login.lua @@ -1,21 +1,57 @@ local cjson = require "cjson" +local password_path = "password.txt" + ngx.req.read_body() local body = ngx.req.get_body_data() -local json = cjson.decode(body) -local password = json["password"]; -local result = {} -if (password == user_password) then - result.code = 0 - result.message = "succuess" - local session = require"resty.session".start() - session.data.identify = "myself" - session.cookie.idletime = 120 +if not body then + ngx.status = ngx.HTTP_BAD_REQUEST + ngx.say("No body found") + return +end + +local ok, json_data = pcall(cjson.decode, body) +if not ok then + ngx.status = ngx.HTTP_BAD_REQUEST + ngx.say("Invalid JSON") + return +end + +local user_account = json_data.account +local user_password = json_data.password + +local reply = {} + +local file = io.open(password_path, "r") +if not file then + ngx.log(ngx.INFO, "无法打开文件: ", password_path) + reply.status = -1000 + reply.message = "服务器错误,找不到 " .. password_path; + ngx.say(cjson.encode(reply)) + return +end + +local credentials = {} +for line in file:lines() do + local account, password = line:match("([^=]+)=([^=]+)") + if account and password then + credentials[account] = password + end +end +file:close() + +local session = require "resty.session".start() + +if credentials[user_account] == user_password then + reply.status = 0 + reply.message = "登录成功" + session:set("account", user_account) + session:set("authenticated", true) session:save() else - result.code = 1 - result.message = "faliure" + reply.status = -100 + reply.message = "登录失败" end -ngx.say(cjson.encode(result)); +ngx.say(cjson.encode(reply)) diff --git a/resource/build.sh b/resource/build.sh index 6c0c35c..55afb4a 100755 --- a/resource/build.sh +++ b/resource/build.sh @@ -5,14 +5,17 @@ build_path=${base_path}/build libraries_root="/opt/Libraries" server_location=/root/HttpServer +if command -v cmake >/dev/null 2>&1; then + cmake_exe=cmake +else + cmake_exe=/opt/Qt/Tools/CMake/bin/cmake +fi + function cmake_scan() { if [ ! -d ${build_path} ]; then mkdir ${build_path} fi - /opt/Qt/Tools/CMake/bin/cmake \ - -G Ninja \ - -S ${base_path} \ - -B ${build_path} \ + ${cmake_exe} -G Ninja -S ${base_path} -B ${build_path} \ -DCMAKE_BUILD_TYPE=Debug \ -DBOOST_ROOT=${libraries_root}/boost_1_85_0 } @@ -24,9 +27,7 @@ function build() { if [ $? -ne 0 ]; then exit 1 fi - /opt/Qt/Tools/CMake/bin/cmake \ - --build ${build_path} \ - --target all + ${cmake_exe} --build ${build_path} --target all if [ $? -ne 0 ]; then exit 1 fi @@ -46,7 +47,7 @@ function deploy() { nohup ./HttpServer >logs/HttpServer.log 2>&1 &" } -function init(){ +function init() { scp -r /opt/Libraries/boost_1_85_0 root@amass.fun:/opt/Libraries/ }