fix(save): log synthient scans and update request handling

This commit is contained in:
localhost 2026-10-03 17:55:36 +02:00
parent 9c25660dbc
commit 38a6094129
3 changed files with 10 additions and 7 deletions

View File

@ -27,7 +27,7 @@ app.get('/save', async ({ query: { url }, set, headers, error, request, redirect
if (!url) return error(400, 'No url provided.')
const ranges = await checkIpRanges(headers['cf-connecting-ip'] || headers['x-forwarded-for'] || '')
if (ranges.blocked || headers['cf-ipcountry'] === 'T1') {
if (ranges.blocked || headers['cf-ipcountry'] === 'T1' || headers['cf-ipcountry'] === 'CN') {
set.headers['Content-Type'] = 'text/html; charset=utf-8'
return error(412, await m(eta.render('./blocked', {
title: 'Blocked | PreserveTube'
@ -64,7 +64,7 @@ app.get('/savechannel', async ({ query: { url }, set, headers, error, request, r
if (!url) return error(400, 'No url provided.')
const ranges = await checkIpRanges(headers['cf-connecting-ip'] || headers['x-forwarded-for'] || '')
if (ranges.blocked || headers['cf-ipcountry'] === 'T1') {
if (ranges.blocked || headers['cf-ipcountry'] === 'T1' || headers['cf-ipcountry'] === 'CN') {
set.headers['Content-Type'] = 'text/html; charset=utf-8'
return error(412, await m(eta.render('./blocked', {
title: 'Blocked | PreserveTube'

View File

@ -69,7 +69,7 @@ const cleanup = async (ws: any, videoId: string) => {
await redis.del(ws.id);
};
const handleUpload = async (ws: any, videoId: string, metadata: { data: any, channelData: any }, isChannel: boolean = false) => {
const handleUpload = async (ws: any, videoId: string, metadata: { data: any, channelData: any }) => {
// the pattern of files that have finished downloading is [videoid].mp4, but some extensions are also possible due to
// current youtube changes, so we need to make sure the other extensions are also covered
let filePath = fs.readdirSync('./videos/').find(f => f.includes(`${videoId}.`))
@ -91,7 +91,6 @@ const handleUpload = async (ws: any, videoId: string, metadata: { data: any, cha
return false;
}
if (!isChannel) ws.send(`DONE - ${process.env.FRONTEND}/watch?v=${videoId}`);
return true;
} catch (error: any) {
ws.send(`ERROR - Upload failed for ${videoId}: ${error.message}`);
@ -126,7 +125,7 @@ app.ws('/save', {
if (range.list != 'cloudflare') return sendError(ws, 'There\'s something wrong with your connection.')
const blacklistCheck = await checkIpRanges(ws.data.headers['cf-connecting-ip']!)
if (blacklistCheck.blocked || ws.data.headers['cf-ipcountry'] == 'T1') return sendError(ws, `Your network is flagged as malicious.`)
if (blacklistCheck.blocked || ws.data.headers['cf-ipcountry'] == 'T1' || ws.data.headers['cf-ipcountry'] == 'CN') return sendError(ws, `Your network is flagged as malicious.`)
console.log(`${ws.id} - ${ws.data.path} - ${JSON.stringify(ws.data.query)}`)
@ -226,6 +225,7 @@ app.ws('/save', {
}
const uploadSuccess = await handleUpload(ws, videoId, { data, channelData });
if (uploadSuccess) ws.send(`DONE - ${process.env.FRONTEND}/watch?v=${videoId}`)
if (!uploadSuccess) await redis.del(saveKey(videoId));
await cleanup(ws, videoId);
@ -251,7 +251,7 @@ app.ws('/savechannel', {
if (range.list != 'cloudflare') return sendError(ws, 'There\'s something wrong with your connection.')
const blacklistCheck = await checkIpRanges(ws.data.headers['cf-connecting-ip']!)
if (blacklistCheck.blocked || ws.data.headers['cf-ipcountry'] == 'T1') return sendError(ws, `Your network is flagged as malicious.`)
if (blacklistCheck.blocked || ws.data.headers['cf-ipcountry'] == 'T1' || ws.data.headers['cf-ipcountry'] == 'CN') return sendError(ws, `Your network is flagged as malicious.`)
console.log(`${ws.id} - ${ws.data.path} - ${JSON.stringify(ws.data.query)}`)
@ -343,7 +343,7 @@ app.ws('/savechannel', {
sendError(ws, limitStatus.isNewVisitorLimited ? NEW_VISITOR_STORAGE_LIMIT_MESSAGE : DEFAULT_STORAGE_LIMIT_MESSAGE, false);
break;
}
const uploadSuccess = await handleUpload(ws, video.videoId, { data, channelData }, true);
const uploadSuccess = await handleUpload(ws, video.videoId, { data, channelData });
if (uploadSuccess) ws.send(`DATA - Created video page for ${video.title}`)
}

View File

@ -51,6 +51,7 @@ export async function isSynthientBlocked(ip: string): Promise<boolean> {
const decision = getBlockDecision(body)
if (!decision.blocked) {
await redis.set(responseKey(ip), body, 'EX', 24 * 60 * 60)
console.log('[synthient] scan allowed')
return false
}
@ -60,8 +61,10 @@ export async function isSynthientBlocked(ip: string): Promise<boolean> {
transaction.set(`synthient:blocked-ip:${ip}`, '1', 'EX', banTtlSeconds)
if (range) transaction.set(`synthient:blocked-range:${range}`, '1', 'EX', banTtlSeconds)
await transaction.exec()
console.log('[synthient] scan blocked')
return true
} catch {
console.log('[synthient] scan failed')
return false
}
}