var request = require ( 'request-promise' ) ;
var fs = require ( 'fs' ) ;
var common = require ( '../../routes/common' ) ;
var logger = require ( '../shared/logger' ) ;
var options = { } ;
function getFilesList ( callback ) {
let files _list = [ ] ;
let all _restore _exists = false ;
let response = { all _restore _exists : false , files : [ ] } ;
fs . readdir ( common . selectedNode . channel _backup _path + common . path _separator + 'restore' , function ( err , files ) {
if ( err && err . code !== 'ENOENT' && err . errno !== - 4058 ) { response = { message : 'Channels Restore List Failed!' , error : err } }
if ( files && files . length > 0 ) {
files . forEach ( file => {
if ( ! file . includes ( '.restored' ) ) {
if ( file . toLowerCase ( ) === 'channel-all.bak' || file . toLowerCase ( ) === 'backup-channel-all.bak' ) {
all _restore _exists = true ;
} else {
files _list . push ( { channel _point : file . substring ( 8 , file . length - 4 ) . replace ( '-' , ':' ) } ) ;
}
}
} ) ;
}
response = { all _restore _exists : all _restore _exists , files : files _list } ;
callback ( response ) ;
} ) ;
}
exports . getBackup = ( req , res , next ) => {
logger . log ( { level : 'INFO' , fileName : 'ChannelBackup' , msg : 'Getting Channel Backup..' } ) ;
options = common . getOptions ( ) ;
let channel _backup _file = '' ;
let message = '' ;
if ( req . params . channelPoint === 'ALL' ) {
channel _backup _file = common . selectedNode . channel _backup _path + common . path _separator + 'channel-all.bak' ;
message = 'All Channels Backup Successful.' ;
options . url = common . getSelLNServerUrl ( ) + '/v1/channels/backup' ;
} else {
channel _backup _file = common . selectedNode . channel _backup _path + common . path _separator + 'channel-' + req . params . channelPoint . replace ( ':' , '-' ) + '.bak' ;
message = 'Channel Backup Successful.' ;
let channelpoint = req . params . channelPoint . replace ( ':' , '/' ) ;
options . url = common . getSelLNServerUrl ( ) + '/v1/channels/backup/' + channelpoint ;
let exists = fs . existsSync ( channel _backup _file ) ;
if ( exists ) {
fs . writeFile ( channel _backup _file , '' , ( ) => { } ) ;
} else {
try {
var createStream = fs . createWriteStream ( channel _backup _file ) ;
createStream . end ( ) ;
}
catch ( errRes ) {
let err = JSON . parse ( JSON . stringify ( errRes ) ) ;
if ( err . options && err . options . headers && err . options . headers [ 'Grpc-Metadata-macaroon' ] ) {
delete err . options . headers [ 'Grpc-Metadata-macaroon' ] ;
}
if ( err . response && err . response . request && err . response . request . headers && err . response . request . headers [ 'Grpc-Metadata-macaroon' ] ) {
delete err . response . request . headers [ 'Grpc-Metadata-macaroon' ] ;
}
logger . log ( { level : 'ERROR' , fileName : 'ChannelsBackup' , msg : 'Channels Backup Error' , error : err } ) ;
return res . status ( 500 ) . json ( { message : 'Channels Backup Failed!' , error : err } ) ;
}
}
}
request ( options ) . then ( function ( body ) {
logger . log ( { level : 'DEBUG' , fileName : 'ChannelsBackup' , msg : 'Channel Backup' , data : body } ) ;
fs . writeFile ( channel _backup _file , JSON . stringify ( body ) , function ( errRes ) {
if ( errRes ) {
let err = JSON . parse ( JSON . stringify ( errRes ) ) ;
if ( err . options && err . options . headers && err . options . headers [ 'Grpc-Metadata-macaroon' ] ) {
delete err . options . headers [ 'Grpc-Metadata-macaroon' ] ;
}
if ( err . response && err . response . request && err . response . request . headers && err . response . request . headers [ 'Grpc-Metadata-macaroon' ] ) {
delete err . response . request . headers [ 'Grpc-Metadata-macaroon' ] ;
}
logger . log ( { level : 'ERROR' , fileName : 'ChannelsBackup' , msg : 'Channels Backup Error' , error : err } ) ;
return res . status ( 500 ) . json ( { message : 'Channels Backup Failed!' , error : err . error } ) ;
} else {
logger . log ( { level : 'INFO' , fileName : 'ChannelBackup' , msg : 'Channel Backup Finished' } ) ;
res . status ( 200 ) . json ( { message : message } ) ;
}
} ) ;
} )
. catch ( errRes => {
let err = JSON . parse ( JSON . stringify ( errRes ) ) ;
if ( err . options && err . options . headers && err . options . headers [ 'Grpc-Metadata-macaroon' ] ) {
delete err . options . headers [ 'Grpc-Metadata-macaroon' ] ;
}
if ( err . response && err . response . request && err . response . request . headers && err . response . request . headers [ 'Grpc-Metadata-macaroon' ] ) {
delete err . response . request . headers [ 'Grpc-Metadata-macaroon' ] ;
}
logger . log ( { level : 'ERROR' , fileName : 'ChannelsBackup' , msg : 'Channel Backup Error' , error : err } ) ;
return res . status ( 500 ) . json ( {
message : 'Channels Backup Failed!' ,
error : err . error
} ) ;
} ) ;
} ;
exports . postBackupVerify = ( req , res , next ) => {
logger . log ( { level : 'INFO' , fileName : 'ChannelBackup' , msg : 'Verifying Channel Backup..' } ) ;
options = common . getOptions ( ) ;
options . url = common . getSelLNServerUrl ( ) + '/v1/channels/backup/verify' ;
let channel _verify _file = '' ;
let message = '' ;
let verify _backup = '' ;
if ( req . params . channelPoint === 'ALL' ) {
message = 'All Channels Verify Successful.' ;
channel _verify _file = common . selectedNode . channel _backup _path + common . path _separator + 'channel-all.bak' ;
let exists = fs . existsSync ( channel _verify _file ) ;
if ( exists ) {
verify _backup = fs . readFileSync ( channel _verify _file , 'utf-8' ) ;
if ( verify _backup !== '' ) {
verify _backup = JSON . parse ( verify _backup ) ;
delete verify _backup . single _chan _backups ;
options . form = JSON . stringify ( verify _backup ) ;
} else {
logger . log ( { level : 'ERROR' , fileName : 'ChannelBackup' , msg : 'Channel Backup Verify Error. Channel backup to verify does not Exist!' , error : { error : 'Channel backup to verify does not Exist.' } } ) ;
res . status ( 404 ) . json ( { message : 'Channels backup to verify does not Exist!' } ) ;
}
} else {
verify _backup = '' ;
logger . log ( { level : 'ERROR' , fileName : 'ChannelBackup' , msg : 'Channel Backup Verify Error. Channel backup to verify does not Exist!' , error : { error : 'Channel backup to verify does not Exist.' } } ) ;
res . status ( 404 ) . json ( { message : 'Channels backup to verify does not Exist!' } ) ;
}
} else {
message = 'Channel Verify Successful.' ;
channel _verify _file = common . selectedNode . channel _backup _path + common . path _separator + 'channel-' + req . params . channelPoint . replace ( ':' , '-' ) + '.bak' ;
let exists = fs . existsSync ( channel _verify _file ) ;
if ( exists ) {
verify _backup = fs . readFileSync ( channel _verify _file , 'utf-8' ) ;
options . form = JSON . stringify ( { single _chan _backups : { chan _backups : [ JSON . parse ( verify _backup ) ] } } ) ;
} else {
verify _backup = '' ;
logger . log ( { level : 'ERROR' , fileName : 'ChannelBackup' , msg : 'Channel Backup Verify Error. Channel backup to verify does not Exist!' , error : { error : 'Channel backup to verify does not Exist.' } } ) ;
res . status ( 404 ) . json ( { message : 'Channel backup to verify does not Exist!' } ) ;
}
}
if ( verify _backup !== '' ) {
request . post ( options ) . then ( function ( body ) {
logger . log ( { level : 'DEBUG' , fileName : 'ChannelBackup' , msg : 'Channel Backup Verify' , data : body } ) ;
logger . log ( { level : 'INFO' , fileName : 'ChannelBackup' , msg : 'Channel Backup Verified' } ) ;
res . status ( 201 ) . json ( { message : message } ) ;
} )
. catch ( errRes => {
let err = JSON . parse ( JSON . stringify ( errRes ) ) ;
if ( err . options && err . options . headers && err . options . headers [ 'Grpc-Metadata-macaroon' ] ) {
delete err . options . headers [ 'Grpc-Metadata-macaroon' ] ;
}
if ( err . response && err . response . request && err . response . request . headers && err . response . request . headers [ 'Grpc-Metadata-macaroon' ] ) {
delete err . response . request . headers [ 'Grpc-Metadata-macaroon' ] ;
}
logger . log ( { level : 'ERROR' , fileName : 'ChannelBackup' , msg : 'Channel Backup Verify Error' , error : err } ) ;
return res . status ( 404 ) . json ( {
message : 'Channel backup to Verify failed!' ,
error : err . error
} ) ;
} ) ;
}
} ;
exports . postRestore = ( req , res , next ) => {
logger . log ( { level : 'INFO' , fileName : 'ChannelBackup' , msg : 'Restoring Channel Backup..' } ) ;
options = common . getOptions ( ) ;
options . url = common . getSelLNServerUrl ( ) + '/v1/channels/backup/restore' ;
let channel _restore _file = '' ;
let message = '' ;
let restore _backup = '' ;
if ( req . params . channelPoint === 'ALL' ) {
message = 'All Channels Restore Successful.' ;
channel _restore _file = common . selectedNode . channel _backup _path + common . path _separator + 'restore' + common . path _separator ;
let exists = fs . existsSync ( channel _restore _file + 'channel-all.bak' ) ;
let downloaded _exists = fs . existsSync ( channel _restore _file + 'backup-channel-all.bak' ) ;
if ( exists ) {
restore _backup = fs . readFileSync ( channel _restore _file + 'channel-all.bak' , 'utf-8' ) ;
if ( restore _backup !== '' ) {
restore _backup = JSON . parse ( restore _backup ) ;
options . form = JSON . stringify ( { multi _chan _backup : restore _backup . multi _chan _backup . multi _chan _backup } ) ;
} else {
logger . log ( { level : 'ERROR' , fileName : 'ChannelBackup' , msg : 'Channel Restore Error.Channels backup to restore does not Exist!' , error : { error : 'Channel backup to restore does not Exist.' } } ) ;
res . status ( 404 ) . json ( { message : 'Channels backup to restore does not Exist!' } ) ;
}
} else if ( downloaded _exists ) {
restore _backup = fs . readFileSync ( channel _restore _file + 'backup-channel-all.bak' , 'utf-8' ) ;
if ( restore _backup !== '' ) {
restore _backup = JSON . parse ( restore _backup ) ;
options . form = JSON . stringify ( { multi _chan _backup : restore _backup . multi _chan _backup . multi _chan _backup } ) ;
} else {
logger . log ( { level : 'ERROR' , fileName : 'ChannelBackup' , msg : 'Channel Restore Error. Channels backup to restore does not Exist!' , error : { error : 'Channel backup to restore does not Exist.' } } ) ;
res . status ( 404 ) . json ( { message : 'Channels backup to restore does not Exist!' } ) ;
}
} else {
restore _backup = '' ;
logger . log ( { level : 'ERROR' , fileName : 'ChannelBackup' , msg : 'Channel Restore Error. Channels backup to restore does not Exist!' , error : { error : 'Channel backup to restore does not Exist.' } } ) ;
res . status ( 404 ) . json ( { message : 'Channels backup to restore does not Exist!' } ) ;
}
} else {
message = 'Channel Restore Successful.' ;
channel _restore _file = common . selectedNode . channel _backup _path + common . path _separator + 'restore' + common . path _separator + 'channel-' + req . params . channelPoint . replace ( ':' , '-' ) + '.bak' ;
let exists = fs . existsSync ( channel _restore _file ) ;
if ( exists ) {
restore _backup = fs . readFileSync ( channel _restore _file , 'utf-8' ) ;
options . form = JSON . stringify ( { chan _backups : { chan _backups : [ JSON . parse ( restore _backup ) ] } } ) ;
} else {
restore _backup = '' ;
logger . log ( { level : 'ERROR' , fileName : 'ChannelBackup' , msg : 'Channel Restore Error. Channels backup to restore does not Exist!' , error : { error : 'Channel backup to restore does not Exist.' } } ) ;
res . status ( 404 ) . json ( { message : 'Channel backup to restore does not Exist!' } ) ;
}
}
if ( restore _backup !== '' ) {
request . post ( options ) . then ( function ( body ) {
logger . log ( { level : 'DEBUG' , fileName : 'ChannelBackup' , msg : 'Channel Backup Restore' , data : body } ) ;
fs . rename ( channel _restore _file , channel _restore _file + '.restored' , ( ) => {
getFilesList ( getFilesListRes => {
if ( getFilesListRes . error ) {
logger . log ( { level : 'ERROR' , fileName : 'ChannelBackup' , msg : 'Channel Restore Error' , error : getFilesListRes . error } ) ;
return res . status ( 500 ) . json ( { message : 'Channel restore failed!' , list : getFilesListRes } ) ;
} else {
logger . log ( { level : 'INFO' , fileName : 'ChannelBackup' , msg : 'Channel Restored' } ) ;
return res . status ( 201 ) . json ( { message : message , list : getFilesListRes } ) ;
}
} ) ;
} ) ;
} )
. catch ( errRes => {
let err = JSON . parse ( JSON . stringify ( errRes ) ) ;
if ( err . options && err . options . headers && err . options . headers [ 'Grpc-Metadata-macaroon' ] ) {
delete err . options . headers [ 'Grpc-Metadata-macaroon' ] ;
}
if ( err . response && err . response . request && err . response . request . headers && err . response . request . headers [ 'Grpc-Metadata-macaroon' ] ) {
delete err . response . request . headers [ 'Grpc-Metadata-macaroon' ] ;
}
logger . log ( { level : 'ERROR' , fileName : 'ChannelBackup' , msg : 'Channel Restore Error' , error : err } ) ;
return res . status ( 404 ) . json ( {
message : 'Channel restore failed!' ,
error : err . error . error
} ) ;
} ) ;
}
} ;
exports . getRestoreList = ( req , res , next ) => {
getFilesList ( getFilesListRes => {
if ( getFilesListRes . error ) {
return res . status ( 500 ) . json ( getFilesListRes ) ;
} else {
return res . status ( 200 ) . json ( getFilesListRes ) ;
}
} ) ;
} ;