From e38a31567bc08fa03ee722d617f0f4deb34fe272 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 11 May 2023 17:36:17 +0800 Subject: [PATCH] test --- .gitignore | 4 +++ README.md | 40 ++++++++++++++++++++++ chat.txt | 4 --- config.json_example | 6 ++++ index.php | 58 ++++++++++++++++++-------------- js/chat.js | 2 +- log/此文件夹用于存放日志文件.txt | 2 ++ stream.php | 22 ++++++++---- 8 files changed, 101 insertions(+), 37 deletions(-) create mode 100644 .gitignore delete mode 100644 chat.txt create mode 100644 config.json_example create mode 100755 log/此文件夹用于存放日志文件.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a9595c1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/log/* +!/log/此文件夹用于存放日志文件.txt +config.json +favicon.ico diff --git a/README.md b/README.md index 4a2ed45..11719f5 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,46 @@ ChatGPT的横空出世真的改变了世界,用过的人都知道ChatGPT完全 ![t2](https://user-images.githubusercontent.com/5563148/232330566-c6ea7fb3-474f-45e4-adda-37f3db27b92a.jpg) +------ +**调整部分内容为配置文件形式优化拉取代码配置信息冲突** +拉取代码后请复制config.json_example 为 config.json + +linux下 进入项目目录执行 cp config.json_example config.json + +配置定义如下 + +// 控制是否显示私人密钥输入框 + +"personalKey": false, + +//设置全局密钥 开启私人密钥输入框后此key将不启用 + +"key": "sk-replace_with_your_api_key_dude", + +//代理服务器未设置则不启用代理示例 http://127.0.0.1 + +"proxy": "http://127.0.0.1" + +//白名单ip数组内的ip加入白名单访问时直接进入内部专用版 + +"homeAddress": [ + + "127.0.0.1" + +], + +//设置可访问用户名和与密码 + +"userList": { + + "admin": "123" + +} + +------ +**log调整每日为单独文件** +**注意请赋予站点修改log文件夹内内容权限** + ------ **本项目常见问题:** diff --git a/chat.txt b/chat.txt deleted file mode 100644 index 7b0db87..0000000 --- a/chat.txt +++ /dev/null @@ -1,4 +0,0 @@ -127.0.0.1 | 2023-03-08 10:37:47 -Q:你好 -A:你好!有什么我可以帮助你的吗? ----------------- diff --git a/config.json_example b/config.json_example new file mode 100644 index 0000000..ca8eee9 --- /dev/null +++ b/config.json_example @@ -0,0 +1,6 @@ +{ + "homeAddress":[]:, + "personalKey": false, + "key": "sk-replace_with_your_api_key_dude", + "proxy": "" +} \ No newline at end of file diff --git a/index.php b/index.php index e387627..123c64e 100644 --- a/index.php +++ b/index.php @@ -1,26 +1,35 @@ 

请点击右上角,选择”在浏览器打开“

"; -// exit; -// } -// if (!isset($_SERVER['PHP_AUTH_USER'])) { -// header('WWW-Authenticate: Basic realm="Please input username and password."'); -// header('HTTP/1.0 401 Unauthorized'); -// echo 'Bye, honey.'; -// exit; -// } else { -// if (($_SERVER['PHP_AUTH_USER']=="admin")&&($_SERVER['PHP_AUTH_PW']=="admin")){ -// $type = "外网"; -// } else { -// echo 'Wrong password, bye...'; -// exit; -// } -// } -// } else { -// $type = "内网"; -// } +// 读取JSON文件 +$json = file_get_contents('config.json'); +// 解码JSON数据 +$configs = json_decode($json, true); +$isHome = false; + +if(in_array($_SERVER["REMOTE_ADDR"],$configs['homeAddress'])){ + $isHome = true; +} +if ($isHome) { + $type = "内部"; +} else { + if (strpos($_SERVER["HTTP_USER_AGENT"], "MicroMessenger")) { + echo "

请点击右上角,选择”在浏览器打开“

"; + exit; + } + if (!isset($_SERVER['PHP_AUTH_USER'])) { + header('WWW-Authenticate: Basic realm="Please input username and password."'); + header('HTTP/1.0 401 Unauthorized'); + echo 'Bye, honey.'; + exit; + } else { + if ($configs["userList"][$_SERVER['PHP_AUTH_USER']] == $_SERVER['PHP_AUTH_PW']) { + $type = "外网"; + } else { + echo 'Wrong password, bye...'; + exit; + } + } +} ?> @@ -35,6 +44,7 @@ $type = "个人"; +
@@ -51,12 +61,10 @@ $type = "个人";
-
  连续对话: @@ -229,4 +237,4 @@ $type = "个人"; - + \ No newline at end of file diff --git a/js/chat.js b/js/chat.js index 454c582..7585fc3 100644 --- a/js/chat.js +++ b/js/chat.js @@ -163,7 +163,7 @@ $(document).ready(function () { }); function send_post() { - if (($('#key').length) && ($('#key').val().length != 51)) { + if ($("#keydiv").attr("style").indexOf("display:none") < 0 && ($('#key').length) && ($('#key').val().length != 51)) { layer.msg("请输入正确的API-KEY", { icon: 5 }); return; } diff --git a/log/此文件夹用于存放日志文件.txt b/log/此文件夹用于存放日志文件.txt new file mode 100755 index 0000000..df82dda --- /dev/null +++ b/log/此文件夹用于存放日志文件.txt @@ -0,0 +1,2 @@ +此文件夹用于存放日志文件.txt +若无法自动生成YYYY-mm-dd_chat.txt的日志文件请给php允许着log文件夹修改权限 或 设置log文件夹为所有用户皆可修改 \ No newline at end of file diff --git a/stream.php b/stream.php index d683107..983e9c8 100644 --- a/stream.php +++ b/stream.php @@ -6,11 +6,15 @@ session_start(); $postData = $_SESSION['data']; $_SESSION['response'] = ""; $ch = curl_init(); -$OPENAI_API_KEY = "sk-replace_with_your_api_key_dude"; +// 读取JSON配置文件 +$json = file_get_contents('config.json'); +// 解码JSON配置文件数据 +$configs = json_decode($json, true); +$OPENAI_API_KEY = $configs["key"]; if (isset($_SESSION['key'])) { $OPENAI_API_KEY = $_SESSION['key']; } -$headers = [ +$headers = [ 'Accept: application/json', 'Content-Type: application/json', 'Authorization: Bearer ' . $OPENAI_API_KEY @@ -54,7 +58,9 @@ curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); curl_setopt($ch, CURLOPT_WRITEFUNCTION, $callback); -//curl_setopt($ch, CURLOPT_PROXY, "http://127.0.0.1:1081"); +if($configs["proxy"]){ + curl_setopt($ch, CURLOPT_PROXY, $configs["proxy"]); +} curl_exec($ch); @@ -74,7 +80,9 @@ foreach ($responsearr as $msg) { $questionarr = json_decode($_SESSION['data'], true); $filecontent = $_SERVER["REMOTE_ADDR"] . " | " . date("Y-m-d H:i:s") . "\n"; $filecontent .= "Q:" . end($questionarr['messages'])['content'] . "\nA:" . trim($answer) . "\n----------------\n"; -$myfile = fopen(__DIR__ . "/chat.txt", "a") or die("Writing file failed."); -fwrite($myfile, $filecontent); -fclose($myfile); -curl_close($ch); + +file_put_contents(__DIR__ . "/log/" . date('Y-m-d') . "_chat.txt",$filecontent,FILE_APPEND); +// $myfile = fopen(__DIR__ . "/chat.txt", "a") or die("Writing file failed."); +// fwrite($myfile, $filecontent); +// fclose($myfile); +curl_close($ch); \ No newline at end of file