Skip to content

Commit

Permalink
支持设置URL前缀/相对路径
Browse files Browse the repository at this point in the history
Close #39
  • Loading branch information
TransparentLC committed Jun 17, 2024
1 parent de55aac commit 6d6e618
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 20 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ php build-phar.php
],
"port": 9501, // 端口号,falsy 值表示不监听
"uds": "/var/run/cloud-clipboard.sock", // UNIX domain socket 路径,可以后接“:666”设定权限(默认666),falsy 值表示不监听
"prefix": "", // 部署时的URL前缀,例如想要在 http://localhost/prefix/ 访问,则将这一项设为 /prefix
"key": "localhost-key.pem", // HTTPS 私钥路径
"cert": "localhost.pem", // HTTPS 证书路径
"history": 10, // 消息历史记录的数量
Expand Down
8 changes: 4 additions & 4 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"build": "vue-cli-service build --modern && node after-build.js"
},
"dependencies": {
"axios": "^1.6.8",
"core-js": "^3.36.1",
"axios": "^1.7.2",
"core-js": "^3.37.1",
"vue": "^2.7.16",
"vue-axios": "^2.1.5",
"vue-linkify": "^1.0.1",
Expand All @@ -22,8 +22,8 @@
"@vue/cli-plugin-router": "^4.5.19",
"@vue/cli-service": "^4.5.19",
"fs-extra": "^11.2.0",
"glob": "^10.3.12",
"sass": "~1.72.0",
"glob": "^10.4.1",
"sass": "~1.77.5",
"sass-loader": "^10.5.2",
"typeface-roboto": "^1.1.13",
"vue-cli-plugin-vuetify": "^2.5.8",
Expand Down
6 changes: 3 additions & 3 deletions client/src/components/SendFile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,20 @@ export default {
this.uploadedSizes.splice(0);
this.uploadedSizes.push(...Array(this.$root.send.files.length).fill(0));
await Promise.all(this.$root.send.files.map(async (file, i) => {
let response = await this.$http.post('/upload', file.name, {headers: {'Content-Type': 'text/plain'}});
let response = await this.$http.post('upload', file.name, {headers: {'Content-Type': 'text/plain'}});
let uuid = response.data.result.uuid;
let uploadedSize = 0;
this.progress = true;
while (uploadedSize < file.size) {
let chunk = file.slice(uploadedSize, uploadedSize + chunkSize);
await this.$http.post(`/upload/chunk/${uuid}`, chunk, {
await this.$http.post(`upload/chunk/${uuid}`, chunk, {
headers: {'Content-Type': 'application/octet-stream'},
onUploadProgress: e => this.$set(this.uploadedSizes, i, uploadedSize + e.loaded),
});
uploadedSize += chunkSize;
}
await this.$http.post(`/upload/finish/${uuid}`, null, {
await this.$http.post(`upload/finish/${uuid}`, null, {
params: new URLSearchParams([['room', this.$root.room]]),
});
}));
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/SendText.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default {
methods: {
send() {
this.$http.post(
'/text',
'text',
this.$root.send.text,
{
params: new URLSearchParams([['room', this.$root.room]]),
Expand Down
8 changes: 4 additions & 4 deletions client/src/components/received-item/File.vue
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@ export default {
}
this.expand = true;
if (this.isPreviewableVideo || this.isPreviewableAudio) {
this.srcPreview = `/file/${this.meta.cache}`;
this.srcPreview = `file/${this.meta.cache}`;
} else {
this.loadingPreview = true;
this.loadedPreview = 0;
this.$http.get(`/file/${this.meta.cache}`, {
this.$http.get(`file/${this.meta.cache}`, {
responseType: 'arraybuffer',
onDownloadProgress: e => {this.loadedPreview = e.loaded},
}).then(response => {
Expand All @@ -176,11 +176,11 @@ export default {
}
},
deleteItem() {
this.$http.delete(`/revoke/${this.meta.id}`, {
this.$http.delete(`revoke/${this.meta.id}`, {
params: new URLSearchParams([['room', this.$root.room]]),
}).then(() => {
if (this.expired) return;
this.$http.delete(`/file/${this.meta.cache}`).then(() => {
this.$http.delete(`file/${this.meta.cache}`).then(() => {
this.$toast(`已删除文件 ${this.meta.name}`);
}).catch(error => {
if (error.response && error.response.data.msg) {
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/received-item/Text.vue
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export default {
this.$toast('复制成功');
},
deleteItem() {
this.$http.delete(`/revoke/${this.meta.id}`, {
this.$http.delete(`revoke/${this.meta.id}`, {
params: new URLSearchParams([['room', this.$root.room]]),
}).then(() => {
this.$toast('已删除文本消息');
Expand Down
2 changes: 1 addition & 1 deletion client/src/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default {
dismissable: false,
timeout: 0,
});
this.$http.get('/server').then(response => {
this.$http.get('server').then(response => {
if (this.authCode) localStorage.setItem('auth', this.authCode);
this.room = this.room.trim();
localStorage.setItem('room', this.room);
Expand Down
5 changes: 5 additions & 0 deletions server-node/app/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ if (!process.argv[2] && !fs.existsSync(defaultConfigPath)) {
host: [],
port: 9501,
uds: null,
prefix: '',
key: null,
cert: null,
history: 10,
Expand All @@ -34,6 +35,7 @@ if (!process.argv[2] && !fs.existsSync(defaultConfigPath)) {
* host: String | String[],
* port: [Number],
* uds: [String],
* prefix: [String],
* key: [String],
* cert: [String],
* forceWss: [Boolean],
Expand All @@ -52,6 +54,9 @@ if (!process.argv[2] && !fs.existsSync(defaultConfigPath)) {
*/
const config = JSON.parse(fs.readFileSync(process.argv[2] || defaultConfigPath));

if (!config.server.prefix) {
config.server.prefix = '';
}
if (config.server.auth === true) {
config.server.auth = '';
for (let i = 0; i < 6; i++) {
Expand Down
6 changes: 4 additions & 2 deletions server-node/app/http-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ const saveHistory = () => fs.promises.writeFile(historyPath, JSON.stringify({
receive: messageQueue.queue.filter(e => e.event === 'receive').filter(e => e.data.type !== 'file' || e.data.expire > Date.now() / 1e3).map(e => e.data),
}));

const router = new KoaRouter;
const router = new KoaRouter({
prefix: config.server.prefix,
});

router.get('/server', async ctx => {
ctx.body = {
'server': `ws://${ctx.request.host}/push`,
'server': `ws://${ctx.request.host}${config.server.prefix}/push`,
'auth': !!config.server.auth,
};
});
Expand Down
4 changes: 3 additions & 1 deletion server-node/app/ws-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import {
const deviceConnected = new Map;
const deviceHashSeed = Math.random() * 0xFFFFFFFF >>> 0;

const router = new KoaRouter;
const router = new KoaRouter({
prefix: config.server.prefix,
});

router.get('/push', async (/** @type {koaWebsocket.MiddlewareContext<Koa.DefaultState>} */ ctx) => {
if (config.server.auth) {
Expand Down
5 changes: 4 additions & 1 deletion server-node/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import path from 'node:path';
import url from 'node:url';
import Koa from 'koa';
import koaCompress from 'koa-compress';
import koaMount from 'koa-mount';
import koaStatic from 'koa-static';
import koaWebsocket from 'koa-websocket';

Expand Down Expand Up @@ -38,7 +39,9 @@ app.use(async (ctx, next) => {
console.log(new Date().toISOString(), '-', remoteAddress, ctx.request.method, ctx.request.path, statusString, `${(performance.now() - startTime).toFixed(2)}ms`);
})
app.use(koaCompress());
app.use(koaStatic(path.join(path.dirname(url.fileURLToPath(import.meta.url)), 'static')));
app.use(koaMount(config.server.prefix + '/', koaStatic(path.join(path.dirname(url.fileURLToPath(import.meta.url)), 'static'), {
maxage: 30 * 24 * 60 * 60 * 1000,
})));
app.use(httpRouter.routes());
app.use(httpRouter.allowedMethods());
app.ws.use(wsRouter.routes());
Expand Down
5 changes: 3 additions & 2 deletions server-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
"koa": "^2.15.3",
"koa-body": "^6.0.1",
"koa-compress": "^5.1.1",
"koa-mount": "^4.0.0",
"koa-static": "^5.0.0",
"koa-websocket": "^7.0.0",
"sharp": "^0.33.3",
"ua-parser-js": "2.0.0-alpha.3"
"sharp": "^0.33.4",
"ua-parser-js": "2.0.0-beta.3"
}
}

0 comments on commit 6d6e618

Please sign in to comment.