Skip to content

Add endpoint to allow pulling log files from the web_interface #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"prompt": "^1.0.0",
"dotenv": "^8.2.0",
"wickrio-bot-api": "5.76.x",
"multer": "^1.4.2"
"multer": "^1.4.2",
"tar": "^6.0.5"
},
"scripts": {
"start": "pm2 start processes.json --update-env",
Expand Down
106 changes: 106 additions & 0 deletions web_interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const fs = require("fs")
const app = express()
app.use(helmet()) //security http headers
const multer = require("multer")
const tar = require("tar")

const bot = new WickrIOBotAPI.WickrIOBot()

Expand Down Expand Up @@ -688,6 +689,101 @@ async function main() {
}
})

app.get(endpoint + "/Logs", function (req, res) {
var authHeader = req.get("Authorization")
var authToken
if (authHeader) {
if (authHeader.indexOf(" ") == -1) {
authToken = authHeader
} else {
authHeader = authHeader.split(" ")
authToken = authHeader[1]
}
} else {
res.set("Authorization", "Basic base64_auth_token")
return res
.type("txt")
.status(401)
.send(
'Access denied: invalid Authorization Header format. Correct format: "Authorization: Basic base64_auth_token"'
)
}
Comment on lines +693 to +710
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I absolutely sure that this part can be carried out from each route, avoiding terrible code repetition!!!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that is true. We will add that to the list of things to address.

if (!checkCreds(authToken)) {
res.set("Authorization", "Basic base64_auth_token")
return res
.type("txt")
.status(401)
.send("Access denied: invalid basic-auth token.")
}

try {
var logAndOutputFiles = []
const tarFile = 'LogAndOutputFiles.tar'
const userAttachments = process.cwd() + "/attachments"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not work at Windows OS. You should use path object to join paths.

const tempDir = userAttachments + '/logs'

fs.mkdirSync(tempDir, { recursive: true })


const clientLogsDir = process.cwd() + '/../../logs'
const serverLogsDir = process.cwd() + '/../../../../logs'
if (!fs.existsSync(clientLogsDir)) {
console.log('get logs: cannot find:' + clientLogsDir)
res.status(400).send('Cannot find:' + clientLogsDir)
return
}

// Get the WickrIO client output file
const wickrClientOutputFile = 'WickrIO' + bot_username + '.output'
const wickrClientOutputPath = clientLogsDir + '/' + wickrClientOutputFile
checkForLog( wickrClientOutputFile, wickrClientOutputPath, tempDir, logAndOutputFiles)

// Get the WickrIO server output file
const wickrServerOutputFile = 'WickrIOSvr.output'
const wickrServerOutputPath = serverLogsDir + '/' + wickrServerOutputFile
checkForLog( wickrServerOutputFile, wickrServerOutputPath, tempDir, logAndOutputFiles)

// Get the WickrIODebug server output file
const wickrServerDebugOutputFile = 'WickrIOSvrDebug.output'
const wickrServerDebugOutputPath = serverLogsDir + '/' + wickrServerDebugOutputFile
checkForLog( wickrServerDebugOutputFile, wickrServerDebugOutputPath, tempDir, logAndOutputFiles)

// Get the log.output
const integrationLogOutputFile = 'log.output'
const integrationLogOutputPath = process.cwd() + '/' + integrationLogOutputFile
checkForLog( integrationLogOutputFile, integrationLogOutputPath, tempDir, logAndOutputFiles)

// Get the err.output
const integrationErrOutputFile = 'err.output'
const integrationErrOutputPath = process.cwd() + '/' + integrationErrOutputFile
checkForLog( integrationErrOutputFile, integrationErrOutputPath, tempDir, logAndOutputFiles)

// If there are not logs/output files found then return error
if (logAndOutputFiles.length === 0) {
console.log('get logs: there are not log or output files!')
res.status(400).send('Cannot find any log or output files')
return
}

/*
* Create a tar with all the log/output files in it
*/
tar.c( { file: tarFile, cwd: tempDir, sync: true }, logAndOutputFiles)

const stat = fs.statSync(tarFile)
res.writeHead(200, {
'Content-Type': 'application/x-tar',
'Content-Length': stat.size
});
var readStream = fs.createReadStream(tarFile);
readStream.pipe(res);
} catch (err) {
console.log(err)
res.statusCode = 400
res.type("txt").send(err.toString())
}
})

// What to do for ALL requests for ALL Paths
// that are not handled above
app.all("*", function (req, res) {
Expand Down Expand Up @@ -724,5 +820,15 @@ function isJson(str) {
return str
}

function checkForLog(fileName, pathName, destDir, list) {
if (!fs.existsSync(pathName)) {
console.log('get logs: cannot find:' + pathName)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is much better to use return after this line and avoid using else.

} else {
const newOutput = destDir + '/' + fileName
list.push(fileName)
fs.copyFileSync(pathName, newOutput)
}
}

main()