Merge pull request #4 from sirius422/add-auth
add a simple authentication
This commit is contained in:
@@ -5,12 +5,13 @@ const { v4: uuidv4 } = require("uuid");
|
||||
const app = express();
|
||||
const axios = require("axios");
|
||||
const port = 8080;
|
||||
const validApiKey = process.env.PASSWORD;
|
||||
|
||||
axios.defaults.headers.common["User-Agent"] = process.env.USER_AGENT;
|
||||
axios.defaults.headers.common["Cookie"] = process.env.YOUCOM_COOKIE;
|
||||
|
||||
|
||||
app.post("/v1/messages", (req, res) => {
|
||||
app.post("/v1/messages", apiKeyAuth, (req, res) => {
|
||||
req.rawBody = "";
|
||||
req.setEncoding("utf8");
|
||||
|
||||
@@ -99,7 +100,7 @@ app.post("/v1/messages", (req, res) => {
|
||||
|
||||
|
||||
// 试算用户消息长度
|
||||
if(encodeURIComponent(JSON.stringify(userMessage)).length + encodeURIComponent(userQuery).length > 32000) {
|
||||
if(encodeURIComponent(JSON.stringify(userMessage)).length + encodeURIComponent(userQuery).length > 32000) {
|
||||
//太长了,需要上传
|
||||
|
||||
// user message to plaintext
|
||||
@@ -281,8 +282,24 @@ app.use((req, res, next) => {
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`YouChat proxy listening on port ${port}`);
|
||||
if (!validApiKey) {
|
||||
console.log(`Proxy is currently running with no authentication`)
|
||||
}
|
||||
});
|
||||
|
||||
function apiKeyAuth(req, res, next) {
|
||||
const reqApiKey = req.header('x-api-key');
|
||||
|
||||
if (validApiKey && (reqApiKey !== validApiKey)) {
|
||||
// If Environment variable PASSWORD is set AND x-api-key header is not equal to it, return 401
|
||||
const clientIpAddress = req.headers['x-forwarded-for'] || req.ip;
|
||||
console.log(`Receviced Request from IP ${clientIpAddress} but got invalid password.`);
|
||||
return res.status(401).json({error: 'Invalid Password'});
|
||||
}
|
||||
|
||||
next();
|
||||
}
|
||||
|
||||
// eventStream util
|
||||
function createEvent(event, data) {
|
||||
// if data is object, stringify it
|
||||
@@ -305,11 +322,11 @@ function createDocx(content) {
|
||||
sections: [
|
||||
{
|
||||
properties: {},
|
||||
children:
|
||||
children:
|
||||
paragraphs
|
||||
,
|
||||
},
|
||||
],
|
||||
});
|
||||
return docx.Packer.toBuffer(doc).then((buffer) => buffer);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,11 +12,13 @@
|
||||
|
||||
6. 编辑 `start.bat` 文件,把上面的 Cookie 和 User Agent 粘贴进去
|
||||
|
||||
7. 启动 start.bat
|
||||
7. (可选)如果需要,您可以仿照第6步在`start.bat`中设定一个名为 "PASSWORD" 的环境变量,并将其用作密码。如果没有定义该环境变量,程序将接受所有传入的请求,而不进行任何身份验证
|
||||
|
||||
8. 酒馆中选择 Claude,反向代理地址填 http://127.0.0.1:8080/v1 反代密码必须填,随便什么都可以。
|
||||
8. 启动 start.bat
|
||||
|
||||
9. 开始使用。如果失败了/没有结果/403/Warning 就多重试几次。
|
||||
9. 酒馆中选择 Claude,反向代理地址填 http://127.0.0.1:8080/v1 **反代密码必须填, 同时打开流式传输**,随便什么都可以(除非你在第7步设置了PASSWORD)。
|
||||
|
||||
10. 开始使用。如果失败了/没有结果/403/Warning 就多重试几次。
|
||||
|
||||
## 注意事项
|
||||
|
||||
@@ -36,12 +38,14 @@
|
||||
|
||||
6. Edit the `start.bat` file and paste the cookie and User Agent into it.
|
||||
|
||||
7. Start start.bat
|
||||
7. (Optional) you can set an environment variable named `PASSWORD` in `start.bat`, similar to Step 6, and use it as the password. If this environment variable is not defined, the program will accept all incoming requests without performing any authentication.
|
||||
|
||||
8. Select Claude in the Tavern and put http://127.0.0.1:8080/v1 as the address of the reverse proxy. Use any random string for password.
|
||||
8. Start start.bat
|
||||
|
||||
9. Enjoy it. If it fails/no result/403/Warning, try again.
|
||||
9. Select Claude in the Tavern and put http://127.0.0.1:8080/v1 as the address of the reverse proxy. **Use any random string for password, also turn on Streaming** (unless you set PASSWORD in step 7).
|
||||
|
||||
10. Enjoy it. If it fails/no result/403/Warning, try again.
|
||||
|
||||
## Caution
|
||||
|
||||
If you get 403 errors, consider getting the cookie again or changing your IP.
|
||||
If you get 403 errors, consider getting the cookie again or changing your IP.
|
||||
|
||||
Reference in New Issue
Block a user