2021-09-02 11:23:58 +02:00
const config = require ( '../config' ) ;
const overridingRoutes = require ( 'express' ) . Router ( ) ;
overridingRoutes . all ( '*' , ( req , res , next ) => {
let themeOverride = req . query . theme ;
if ( themeOverride ) {
// Convert Dark to dark since the stylesheet has it lower case
themeOverride = themeOverride . toLowerCase ( ) ;
// This override here will set it for the current request
req . cookies . theme = themeOverride ;
// this will set it for future requests
res . cookie ( 'theme' , themeOverride , { maxAge : 31536000 , httpOnly : true } ) ;
} else if ( ! req . cookies . theme && req . cookies . theme !== '' ) {
req . cookies . theme = config . theme ;
}
let flairsOverride = req . query . flairs ;
if ( flairsOverride ) {
req . cookies . flairs = flairsOverride ;
res . cookie ( 'flairs' , flairsOverride , { maxAge : 31536000 , httpOnly : true } ) ;
}
let nsfwEnabledOverride = req . query . nsfw _enabled ;
if ( nsfwEnabledOverride ) {
req . cookies . nsfw _enabled = nsfwEnabledOverride ;
res . cookie ( 'nsfw_enabled' , nsfwEnabledOverride , {
maxAge : 31536000 ,
httpOnly : true ,
} ) ;
}
let highlightControversialOverride = req . query . highlight _controversial ;
if ( highlightControversialOverride ) {
req . cookies . highlight _controversial = highlightControversialOverride ;
res . cookie ( 'highlight_controversial' , highlightControversialOverride , {
maxAge : 31536000 ,
httpOnly : true ,
} ) ;
}
let postMediaMaxHeight = req . query . post _media _max _height ;
if ( postMediaMaxHeight ) {
if (
config . post _media _max _heights . hasOwnProperty ( postMediaMaxHeight ) ||
! isNaN ( postMediaMaxHeight )
) {
req . cookies . post _media _max _height = postMediaMaxHeight ;
res . cookie ( 'post_media_max_height' , postMediaMaxHeight , {
maxAge : 31536000 ,
httpOnly : true ,
} ) ;
}
}
let collapseChildComments = req . query . collapse _child _comments ;
if ( collapseChildComments ) {
req . cookies . collapse _child _comments = collapseChildComments ;
res . cookie ( 'collapse_child_comments' , collapseChildComments , {
maxAge : 31536000 ,
httpOnly : true ,
} ) ;
}
let showUpvotedPercentage = req . query . show _upvoted _percentage ;
if ( showUpvotedPercentage ) {
req . cookies . show _upvoted _percentage = showUpvotedPercentage ;
res . cookie ( 'show_upvoted_percentage' , showUpvotedPercentage , {
maxAge : 31536000 ,
httpOnly : true ,
} ) ;
2021-12-21 22:19:38 +01:00
} else if ( ! req . cookies . show _upvoted _percentage ) {
if ( config . show _upvoted _percentage ) {
req . cookies . show _upvoted _percentage = 'true' ;
}
2021-09-02 11:23:58 +02:00
}
let domainTwitter = req . query . domain _twitter ;
if ( domainTwitter ) {
req . cookies . domain _twitter = domainTwitter ;
res . cookie ( 'domain_twitter' , domainTwitter , {
maxAge : 31536000 ,
httpOnly : true ,
} ) ;
}
let domainYoutube = req . query . domain _youtube ;
if ( domainYoutube ) {
req . cookies . domain _youtube = domainYoutube ;
res . cookie ( 'domain_youtube' , domainYoutube , {
maxAge : 31536000 ,
httpOnly : true ,
} ) ;
}
let domainInstagram = req . query . domain _instagram ;
if ( domainInstagram ) {
req . cookies . domain _instagram = domainInstagram ;
res . cookie ( 'domain_instagram' , domainInstagram , {
maxAge : 31536000 ,
httpOnly : true ,
} ) ;
}
2022-10-15 06:23:48 +02:00
let domainQuora = req . query . domain _quora ;
if ( domainQuora ) {
req . cookies . domain _quora = domainQuora ;
res . cookie ( 'domain_quora' , domainQuora , {
maxAge : 31536000 ,
httpOnly : true ,
} ) ;
}
2022-11-12 14:47:08 +01:00
let domainImgur = req . query . domain _imgur ;
if ( domainImgur ) {
req . cookies . domain _imgur = domainImgur ;
res . cookie ( 'domain_imgur' , domainImgur , {
maxAge : 31536000 ,
httpOnly : true ,
} ) ;
}
2021-09-02 11:23:58 +02:00
let videosMuted = req . query . videos _muted ;
if ( videosMuted ) {
req . cookies . videos _muted = videosMuted ;
res . cookie ( 'videos_muted' , videosMuted , {
maxAge : 31536000 ,
httpOnly : true ,
} ) ;
}
if ( ! config . rate _limiting ) {
return next ( ) ;
}
const valid _reddit _starts = [
'/https://old.reddit.com' ,
'/https://reddit.com' ,
'/https://www.reddit.com' ,
'/old.reddit.com' ,
'/reddit.com' ,
'/www.reddit.com' ,
] ;
for ( var i = 0 ; i < valid _reddit _starts . length ; i ++ ) {
if ( req . url . startsWith ( valid _reddit _starts [ i ] ) ) {
req . url = req . url . substring ( 1 ) ;
const redditRegex = /([A-z.]+\.)?(reddit(\.com))/gm ;
let teddified _url = req . url . replace ( redditRegex , '' ) ;
if ( teddified _url . includes ( '://' ) ) {
teddified _url = teddified _url . split ( '://' ) [ 1 ] ;
}
if ( teddified _url == '' ) {
teddified _url = '/' ;
}
return res . redirect ( teddified _url ) ;
}
}
if ( config . rate _limiting . enabled ) {
/ * *
* This route enforces request limits based on an IP address if
* config . rate _limiting . enabled is true . By default it ' s false .
* /
let ip = String (
req . headers [ 'x-forwarded-for' ] ||
req . connection . remoteAddress ||
'unknown'
) ;
if ( ip === 'unknown' ) {
return next ( ) ;
}
if ( ratelimit _counts [ ip ] == undefined ) {
ratelimit _counts [ ip ] = 0 ;
}
if ( ratelimit _timestamps [ ip ] == undefined ) {
ratelimit _timestamps [ ip ] = Date . now ( ) ;
}
let diff = Date . now ( ) - ratelimit _timestamps [ ip ] ;
let credit = ( diff / 60000 ) * config . rate _limiting . limit _after _limited ;
ratelimit _counts [ ip ] -= credit ;
if ( ratelimit _counts [ ip ] < 0 ) {
ratelimit _counts [ ip ] = 0 ;
}
ratelimit _counts [ ip ] ++ ;
ratelimit _timestamps [ ip ] = Date . now ( ) ;
if ( ratelimit _counts [ ip ] > config . rate _limiting . initial _limit ) {
console . log ( ` RATE LIMITED IP ADDRESS: ${ ip } ` ) ;
return res . send (
` Hold your horses! You have hit the request limit. You should be able to refresh this page in a couple of seconds. If you think you are wrongfully limited, create an issue at https://codeberg.org/teddit/teddit. Rate limiting is highly experimental feature. `
) ;
} else {
return next ( ) ;
}
} else {
return next ( ) ;
}
} ) ;
module . exports = overridingRoutes ;