2018-10-20 10:23:46 +02:00
const fs = require ( 'fs' ) ;
2018-06-28 22:16:48 +02:00
// middleware
const multipartMiddleware = require ( '../../middleware/multipartMiddleware' ) ;
const torCheckMiddleware = require ( '../../middleware/torCheckMiddleware' ) ;
// route handlers
2018-04-27 18:54:36 +02:00
const channelAvailability = require ( '../../controllers/api/channel/availability' ) ;
const channelClaims = require ( '../../controllers/api/channel/claims' ) ;
const channelData = require ( '../../controllers/api/channel/data' ) ;
const channelShortId = require ( '../../controllers/api/channel/shortId' ) ;
const claimAvailability = require ( '../../controllers/api/claim/availability' ) ;
const claimData = require ( '../../controllers/api/claim/data/' ) ;
const claimGet = require ( '../../controllers/api/claim/get' ) ;
2018-04-30 00:30:11 +02:00
const claimList = require ( '../../controllers/api/claim/list' ) ;
2018-04-27 18:54:36 +02:00
const claimLongId = require ( '../../controllers/api/claim/longId' ) ;
const claimPublish = require ( '../../controllers/api/claim/publish' ) ;
const claimResolve = require ( '../../controllers/api/claim/resolve' ) ;
const claimShortId = require ( '../../controllers/api/claim/shortId' ) ;
2018-10-29 08:04:43 +01:00
const claimViews = require ( '../../controllers/api/claim/views' ) ;
2018-04-27 18:54:36 +02:00
const fileAvailability = require ( '../../controllers/api/file/availability' ) ;
2018-10-24 10:43:30 +02:00
const specialClaims = require ( '../../controllers/api/special/claims' ) ;
2018-05-31 03:52:13 +02:00
const userPassword = require ( '../../controllers/api/user/password' ) ;
2018-06-23 23:12:50 +02:00
const publishingConfig = require ( '../../controllers/api/config/site/publishing' ) ;
2018-06-28 02:30:26 +02:00
const getTorList = require ( '../../controllers/api/tor' ) ;
2018-06-28 22:16:48 +02:00
const getBlockedList = require ( '../../controllers/api/blocked' ) ;
2018-07-28 00:56:56 +02:00
const getOEmbedData = require ( '../../controllers/api/oEmbed' ) ;
2018-03-29 02:35:41 +02:00
2018-10-19 21:36:18 +02:00
const logger = require ( 'winston' ) ;
2018-10-20 10:44:25 +02:00
const ipBanFile = './config/ipBan.txt' ;
2018-10-18 19:05:01 +02:00
const forbiddenMessage = '<h1>Forbidden</h1>If you are seeing this by mistake, please contact us using <a href="https://chat.lbry.io/">https://chat.lbry.io/</a>' ;
let ipCounts = { } ;
let blockedAddresses = [ ] ;
2018-10-20 10:23:46 +02:00
if ( fs . existsSync ( ipBanFile ) ) {
const lineReader = require ( 'readline' ) . createInterface ( {
input : require ( 'fs' ) . createReadStream ( ipBanFile ) ,
} ) ;
lineReader . on ( 'line' , ( line ) => {
if ( line && line !== '' ) {
blockedAddresses . push ( line ) ;
}
} ) ;
}
2018-10-18 19:05:01 +02:00
const autoblockPublishMiddleware = ( req , res , next ) => {
2018-10-19 21:36:18 +02:00
let ip = ( req . headers [ 'x-forwarded-for' ] || req . connection . remoteAddress ) . split ( /,\s?/ ) [ 0 ] ;
2018-10-18 19:05:01 +02:00
if ( blockedAddresses . indexOf ( ip ) !== - 1 ) {
res . status ( 403 ) . send ( forbiddenMessage ) ;
res . end ( ) ;
return ;
}
let count = ipCounts [ ip ] = ( ipCounts [ ip ] || 0 ) + 1 ;
setTimeout ( ( ) => {
ipCounts [ ip ] -- ;
if ( ipCounts [ ip ] === 0 ) {
delete ipCounts [ ip ] ;
}
} , 600000 /* 10 minute retainer */ )
if ( count === 10 ) {
2018-10-19 21:36:18 +02:00
logger . error ( ` Banning IP: ${ ip } ` ) ;
2018-10-18 19:05:01 +02:00
blockedAddresses . push ( ip ) ;
res . status ( 403 ) . send ( forbiddenMessage ) ;
res . end ( ) ;
2018-10-20 10:23:46 +02:00
fs . appendFile ( ipBanFile , ip + '\n' , ( ) => { } ) ;
2018-10-18 19:05:01 +02:00
} else {
next ( ) ;
}
}
2018-09-28 05:56:10 +02:00
module . exports = {
2018-10-09 02:02:05 +02:00
// homepage routes
'/api/homepage/data/channels' : { controller : [ torCheckMiddleware , channelData ] } ,
2018-03-29 20:24:52 +02:00
// channel routes
2018-09-28 05:56:10 +02:00
'/api/channel/availability/:name' : { controller : [ torCheckMiddleware , channelAvailability ] } ,
'/api/channel/short-id/:longId/:name' : { controller : [ torCheckMiddleware , channelShortId ] } ,
'/api/channel/data/:channelName/:channelClaimId' : { controller : [ torCheckMiddleware , channelData ] } ,
2018-10-09 02:02:05 +02:00
'/api/channel/data/:channelName/:channelClaimId' : { controller : [ torCheckMiddleware , channelData ] } ,
2018-09-28 05:56:10 +02:00
'/api/channel/claims/:channelName/:channelClaimId/:page' : { controller : [ torCheckMiddleware , channelClaims ] } ,
2018-10-24 10:43:30 +02:00
// sepcial routes
'/api/special/:name/:page' : { controller : [ torCheckMiddleware , specialClaims ] } ,
2018-03-29 20:24:52 +02:00
// claim routes
2018-09-28 05:56:10 +02:00
'/api/claim/availability/:name' : { controller : [ torCheckMiddleware , claimAvailability ] } ,
'/api/claim/data/:claimName/:claimId' : { controller : [ torCheckMiddleware , claimData ] } ,
'/api/claim/get/:name/:claimId' : { controller : [ torCheckMiddleware , claimGet ] } ,
'/api/claim/list/:name' : { controller : [ torCheckMiddleware , claimList ] } ,
'/api/claim/long-id' : { method : 'post' , controller : [ torCheckMiddleware , claimLongId ] } , // note: should be a 'get'
2018-10-18 19:05:01 +02:00
'/api/claim/publish' : { method : 'post' , controller : [ torCheckMiddleware , autoblockPublishMiddleware , multipartMiddleware , claimPublish ] } ,
2018-09-28 05:56:10 +02:00
'/api/claim/resolve/:name/:claimId' : { controller : [ torCheckMiddleware , claimResolve ] } ,
'/api/claim/short-id/:longId/:name' : { controller : [ torCheckMiddleware , claimShortId ] } ,
2018-10-29 08:04:43 +01:00
'/api/claim/views/:claimId' : { controller : [ torCheckMiddleware , claimViews ] } ,
2018-03-29 20:24:52 +02:00
// file routes
2018-09-28 05:56:10 +02:00
'/api/file/availability/:name/:claimId' : { controller : [ torCheckMiddleware , fileAvailability ] } ,
2018-05-31 03:52:13 +02:00
// user routes
2018-09-28 05:56:10 +02:00
'/api/user/password/' : { method : 'put' , controller : [ torCheckMiddleware , userPassword ] } ,
2018-06-23 23:12:50 +02:00
// configs
2018-09-28 05:56:10 +02:00
'/api/config/site/publishing' : { controller : [ torCheckMiddleware , publishingConfig ] } ,
2018-06-28 02:30:26 +02:00
// tor
2018-09-28 05:56:10 +02:00
'/api/tor' : { controller : [ torCheckMiddleware , getTorList ] } ,
2018-06-28 22:16:48 +02:00
// blocked
2018-09-28 05:56:10 +02:00
'/api/blocked' : { controller : [ torCheckMiddleware , getBlockedList ] } ,
2018-07-28 00:56:56 +02:00
// open embed
2018-09-28 05:56:10 +02:00
'/api/oembed' : { controller : [ torCheckMiddleware , getOEmbedData ] } ,
2018-03-29 02:35:41 +02:00
} ;