diff --git a/.eslintignore b/.eslintignore index 6e71b808e..d7f97d597 100644 --- a/.eslintignore +++ b/.eslintignore @@ -5,3 +5,6 @@ LICENSE src/client/lib/marked/* src/server/self-hosted/data/* yarn.lock +src/server/pkg/dist/* +src/server/pkg/patches/* +src/server/pkg/web.config \ No newline at end of file diff --git a/src/server/pkg/.env b/src/server/pkg/.env new file mode 100644 index 000000000..d2f35e844 --- /dev/null +++ b/src/server/pkg/.env @@ -0,0 +1,25 @@ +# twikoo 配置 + +# MongoDB 数据库连接字符串,不传则使用 lokijs +MONGODB_URI= + +# MongoDB 数据库连接字符串,不传则使用 lokijs +MONGO_URL= + +# lokijs 数据库存储路径 +TWIKOO_DATA= './data' + +# 端口号 +TWIKOO_PORT=8080 + +# IP 请求限流,当同一 IP 短时间内请求次数超过阈值将对该 IP 返回错误 +TWIKOO_THROTTLE=250 + +# 为true时只监听本地请求,使得 nginx 等服务器反代之后不暴露原始端口 +TWIKOO_LOCALHOST_ONLY= + +# 日志级别,支持 verbose / info / warn / error +TWIKOO_LOG_LEVEL=info + +# 在一些特殊情况下使用,如使用了CloudFlare CDN 它会将请求 IP 写到请求头的 cf-connecting-ip 字段上,为了能够正确的获取请求 IP 你可以写成 ['headers.cf-connecting-ip'] +TWIKOO_IP_HEADERS=[] \ No newline at end of file diff --git a/src/server/pkg/.eslintignore b/src/server/pkg/.eslintignore new file mode 100644 index 000000000..b2e095d0b --- /dev/null +++ b/src/server/pkg/.eslintignore @@ -0,0 +1,9 @@ +*.css +*.json +*.md +LICENSE +src/client/lib/marked/* +src/server/self-hosted/data/* +yarn.lock +patches/ +web.config \ No newline at end of file diff --git a/src/server/pkg/.gitignore b/src/server/pkg/.gitignore new file mode 100644 index 000000000..a89f00a13 --- /dev/null +++ b/src/server/pkg/.gitignore @@ -0,0 +1,25 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +pnpm-debug.log* +lerna-debug.log* + +node_modules +.DS_Store +dist +data +!.env + +/cypress/videos/ +/cypress/screenshots/ + +# Editor directories and files +.idea +*.suo +*.ntvs* +*.njsproj +*.sln +*.sw? diff --git a/src/server/pkg/.npmrc b/src/server/pkg/.npmrc new file mode 100644 index 000000000..6bca57226 --- /dev/null +++ b/src/server/pkg/.npmrc @@ -0,0 +1,2 @@ +# pnpm 依赖安装方式 +node-linker=hoisted diff --git a/src/server/pkg/.vscode/settings.json b/src/server/pkg/.vscode/settings.json new file mode 100644 index 000000000..12209e721 --- /dev/null +++ b/src/server/pkg/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "*.config": "xml", + } +} \ No newline at end of file diff --git a/src/server/pkg/README.md b/src/server/pkg/README.md new file mode 100644 index 000000000..5b576f948 --- /dev/null +++ b/src/server/pkg/README.md @@ -0,0 +1,15 @@ +# twikoo 可执行文件 + +## 打包命令 + +```sh +npm run build +``` + +## 可执行文件下载地址 + +[latest](https://github.com/kongxiangyiren/twikoo-pkg/releases/latest) + +## window iis 启动 + +1、[下载安装 aspNetCore](https://dotnet.microsoft.com/zh-cn/download/dotnet/thank-you/runtime-aspnetcore-8.0.0-windows-hosting-bundle-installer) diff --git a/src/server/pkg/index.js b/src/server/pkg/index.js new file mode 100644 index 000000000..53037caa1 --- /dev/null +++ b/src/server/pkg/index.js @@ -0,0 +1,57 @@ +const { program } = require('commander') +const { join } = require('path') +const { existsSync, writeFileSync, readFileSync } = require('fs') + +program + .name(require('./package.json').name) + .version(require('./package.json').dependencies.tkserver, '-v, --version') + .description( + `DESCRIPTION: + Official website: https://twikoo.js.org/` + ) + .addHelpCommand(false) + +program.parse(process.argv) + +// 创建.env文件 +if ( + process.pkg && + !existsSync(join(process.execPath, '..', '.env')) && + existsSync(join(__dirname, '.env')) +) { + writeFileSync( + join(process.execPath, '..', '.env'), + readFileSync(join(__dirname, '.env'), 'utf-8') + ) +} + +// 适配iis +if ( + process.pkg && + process.platform === 'win32' && + !existsSync(join(process.execPath, '..', './web.config')) && + existsSync(join(__dirname, './web.config')) +) { + writeFileSync( + join(process.execPath, '..', './web.config'), + readFileSync(join(__dirname, './web.config'), 'utf-8') + ) +} + +// 获取env +require('dotenv').config({ + path: + process.pkg && existsSync(join(process.execPath, '..', '.env')) + ? join(process.execPath, '..', '.env') + : existsSync(join(__dirname, '.env')) + ? join(__dirname, '.env') + : undefined +}) + +// 匹配iis +if (process.env.ASPNETCORE_PORT) { + process.env.TWIKOO_PORT = process.env.ASPNETCORE_PORT + process.env.TWIKOO_LOCALHOST_ONLY = null +} + +require('tkserver') diff --git a/src/server/pkg/package.json b/src/server/pkg/package.json new file mode 100644 index 000000000..6f8eac5b2 --- /dev/null +++ b/src/server/pkg/package.json @@ -0,0 +1,37 @@ +{ + "name": "twikoo", + "version": "1.0.0", + "main": "index.js", + "license": "MIT", + "bin": "index.js", + "scripts": { + "start": "node index.js", + "postinstall": "patch-package", + "diff":"patch-package tkserver", + "build": "rimraf ./dist && pkg . -C GZip --no-bytecode --public-packages \"*\" --public" + }, + "pkg": { + "scripts": [], + "assets": [ + "./node_modules/axios/dist/node/axios.cjs", + "./node_modules/@imaegoo/node-ip2region/data/ip2region.db", + "./web.config", + "./.env" + ], + "targets": [ + "node18-win-x64", + "node18-linux-x64", + "node18-macos-x64" + ], + "outputPath": "dist" + }, + "dependencies": { + "commander": "^11.1.0", + "dotenv": "^16.3.1", + "tkserver": "1.6.30" + }, + "devDependencies": { + "patch-package": "^8.0.0", + "pkg": "5.8.1" + } +} diff --git a/src/server/pkg/patches/pkg-fetch+3.4.2.patch b/src/server/pkg/patches/pkg-fetch+3.4.2.patch new file mode 100644 index 000000000..d7c0211b6 --- /dev/null +++ b/src/server/pkg/patches/pkg-fetch+3.4.2.patch @@ -0,0 +1,13 @@ +diff --git a/node_modules/pkg-fetch/lib-es5/index.js b/node_modules/pkg-fetch/lib-es5/index.js +index 4b1339e..08d09ee 100644 +--- a/node_modules/pkg-fetch/lib-es5/index.js ++++ b/node_modules/pkg-fetch/lib-es5/index.js +@@ -79,7 +79,7 @@ function download(_a, local) { + return __generator(this, function (_c) { + switch (_c.label) { + case 0: +- url = "https://github.com/vercel/pkg-fetch/releases/download/" + tag + "/" + name; ++ url = "https://mirror.ghproxy.com/https://github.com/vercel/pkg-fetch/releases/download/" + tag + "/" + name; + _c.label = 1; + case 1: + _c.trys.push([1, 4, , 5]); diff --git a/src/server/pkg/patches/tkserver+1.6.30.patch b/src/server/pkg/patches/tkserver+1.6.30.patch new file mode 100644 index 000000000..48cdb5fa2 --- /dev/null +++ b/src/server/pkg/patches/tkserver+1.6.30.patch @@ -0,0 +1,13 @@ +diff --git a/node_modules/tkserver/index.js b/node_modules/tkserver/index.js +index d1fb7c9..a0972a2 100644 +--- a/node_modules/tkserver/index.js ++++ b/node_modules/tkserver/index.js +@@ -210,7 +210,7 @@ function anonymousSignIn (request) { + + async function connectToDatabase () { + if (db) return db +- const dataDir = path.resolve(process.cwd(), process.env.TWIKOO_DATA || './data') ++ const dataDir = path.resolve(process.pkg ? path.join(process.execPath, '..') : process.cwd(), process.env.TWIKOO_DATA || './data') + if (!fs.existsSync(dataDir)) { + fs.mkdirSync(dataDir) + } diff --git a/src/server/pkg/web.config b/src/server/pkg/web.config new file mode 100644 index 000000000..69fd47daa --- /dev/null +++ b/src/server/pkg/web.config @@ -0,0 +1,11 @@ + + + + + + + + + + +