diff --git a/src/router/html.ts b/src/router/html.ts index 97cc911..7e2d99d 100644 --- a/src/router/html.ts +++ b/src/router/html.ts @@ -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' diff --git a/src/router/websocket.ts b/src/router/websocket.ts index 01771ad..9dd6ad6 100644 --- a/src/router/websocket.ts +++ b/src/router/websocket.ts @@ -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}`) } diff --git a/src/utils/synthient.ts b/src/utils/synthient.ts index 605cf4c..b0dd6aa 100644 --- a/src/utils/synthient.ts +++ b/src/utils/synthient.ts @@ -51,6 +51,7 @@ export async function isSynthientBlocked(ip: string): Promise { 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 { 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 } }