2019-11-17 17:00:19 -05:00
const crypto = require ( 'crypto' )
function sha1 ( data ) { return crypto . createHash ( "sha1" ) . update ( data , "binary" ) . digest ( "hex" ) ; }
let rateLimit = { } ;
2020-04-08 13:08:58 -04:00
let cooldown = 15000 // GD has a secret rate limit and doesn't return -1 when a comment is rejected, so this keeps track
2019-11-17 17:00:19 -05:00
function getTime ( time ) {
let seconds = Math . ceil ( time / 1000 ) ;
seconds = seconds % 60 ;
return seconds }
module . exports = async ( app , req , res ) => {
2021-12-07 11:06:33 -08:00
if ( req . method !== 'POST' ) return res . status ( 405 ) . send ( "Method not allowed." )
2019-11-17 17:00:19 -05:00
if ( ! req . body . comment ) return res . status ( 400 ) . send ( "No comment provided!" )
if ( ! req . body . username ) return res . status ( 400 ) . send ( "No username provided!" )
if ( ! req . body . levelID ) return res . status ( 400 ) . send ( "No level ID provided!" )
if ( ! req . body . accountID ) return res . status ( 400 ) . send ( "No account ID provided!" )
if ( ! req . body . password ) return res . status ( 400 ) . send ( "No password provided!" )
2019-11-18 18:39:17 -05:00
if ( req . body . comment . includes ( '\n' ) ) return res . status ( 400 ) . send ( "Comments cannot contain line breaks!" )
2019-11-17 17:00:19 -05:00
if ( rateLimit [ req . body . username ] ) return res . status ( 400 ) . send ( ` Please wait ${ getTime ( rateLimit [ req . body . username ] + cooldown - Date . now ( ) ) } seconds before posting another comment! ` )
2020-11-01 15:29:32 -05:00
let params = { percent : 0 }
2019-11-17 17:00:19 -05:00
2020-09-22 18:37:21 -04:00
params . comment = Buffer . from ( req . body . comment + ( req . body . color ? "☆" : "" ) ) . toString ( 'base64' ) . replace ( /\//g , '_' ) . replace ( /\+/g , "-" )
2021-01-18 21:54:18 -05:00
params . gjp = app . xor . encrypt ( req . body . password , 37526 )
2019-11-18 18:39:17 -05:00
params . levelID = req . body . levelID . toString ( )
params . accountID = req . body . accountID . toString ( )
2019-11-17 17:00:19 -05:00
params . userName = req . body . username
let percent = parseInt ( req . body . percent )
2019-11-18 18:39:17 -05:00
if ( percent && percent > 0 && percent <= 100 ) params . percent = percent . toString ( )
2019-11-17 17:00:19 -05:00
let chk = params . userName + params . comment + params . levelID + params . percent + "0xPT6iUrtws0J"
chk = sha1 ( chk )
2021-01-18 21:54:18 -05:00
chk = app . xor . encrypt ( chk , 29481 )
2019-11-17 17:00:19 -05:00
params . chk = chk
2021-01-18 21:54:18 -05:00
req . gdRequest ( 'uploadGJComment21' , params , function ( err , resp , body ) {
2021-08-18 20:10:35 -04:00
if ( err ) return res . status ( 400 ) . send ( ` The Geometry Dash servers rejected your comment! Try again later, or make sure your username and password are entered correctly. Last worked: ${ app . timeSince ( req . id ) } ago. ` )
2019-12-29 18:59:29 -05:00
if ( body . startsWith ( "temp" ) ) {
let banStuff = body . split ( "_" )
2019-12-31 18:51:08 -05:00
return res . status ( 400 ) . send ( ` You have been banned from commenting for ${ ( parseInt ( banStuff [ 1 ] ) / 86400 ) . toFixed ( 0 ) } days. Reason: ${ banStuff [ 2 ] || "None" } ` )
2019-12-29 18:59:29 -05:00
}
2021-01-11 16:00:21 -05:00
2019-11-17 17:00:19 -05:00
res . status ( 200 ) . send ( ` Comment posted to level ${ params . levelID } with ID ${ body } ` )
2021-01-18 21:54:18 -05:00
app . trackSuccess ( req . id )
2020-02-27 23:35:42 -05:00
rateLimit [ req . body . username ] = Date . now ( ) ;
setTimeout ( ( ) => { delete rateLimit [ req . body . username ] ; } , cooldown ) ;
2019-11-17 17:00:19 -05:00
} )
2019-11-19 23:53:24 -05:00
}