@@ -69,14 +69,43 @@ router.post('/account/change_password/:username', async function(req, res) {
6969} ) ;
7070
7171
72+ // Settings API Routes
73+ router . post ( '/settings/change_ui' , function ( req , res ) {
74+ var data = req . body ;
75+ var newConfig = config ;
76+ newConfig . title = data . title ;
77+ newConfig . locale = data . locale ;
78+ newConfig . style = data . style ;
79+ newConfig . logging = data . logging === 'on' ? 1 : 0 ;
80+ fs . writeFileSync ( path . resolve ( __dirname , '../config.json' ) , JSON . stringify ( newConfig , null , 2 ) ) ;
81+ res . redirect ( '/settings' ) ;
82+ } ) ;
83+
84+ router . post ( '/settings/change_db' , function ( req , res ) {
85+ var data = req . body ;
86+ var newConfig = config ;
87+ newConfig . db . host = data . host ;
88+ newConfig . db . port = data . port ;
89+ newConfig . db . username = data . username ;
90+ newConfig . db . password = data . password ;
91+ newConfig . db . database = data . database ;
92+ newConfig . db . charset = data . charset ;
93+ fs . writeFileSync ( path . resolve ( __dirname , '../config.json' ) , JSON . stringify ( newConfig , null , 2 ) ) ;
94+ res . redirect ( '/settings' ) ;
95+ } ) ;
96+
97+
7298// Device API Routes
7399router . get ( '/devices' , async function ( req , res ) {
74100 try {
75101 var devices = await Device . getAll ( ) ;
76102 devices . forEach ( function ( device ) {
77103 var exists = fs . existsSync ( path . join ( screenshotsDir , device . uuid + '.png' ) ) ;
78- var image = exists ? `/screenshots/${ device . uuid } .png` : '/img/offline.png' ;
79- device . image = `<a href='${ image } ' target='_blank'><img src='${ image } ' width='64' height='96'/></a>` ;
104+ // Device received a config last 15 minutes
105+ var delta = 15 * 60 ;
106+ var isOffline = device . last_seen > ( Math . round ( ( new Date ( ) ) . getTime ( ) / 1000 ) - delta ) ? 0 : 1 ;
107+ var image = exists ? `/screenshots/${ device . uuid } .png` : ( isOffline ? '/img/offline.png' : '/img/online.png' ) ;
108+ device . image = `<a href='${ image } ' target='_blank'><img src='${ image } ' width='72' height='96'/></a>` ;
80109 device . last_seen = utils . getDateTime ( device . last_seen ) ;
81110 device . buttons = `
82111 <div class='btn-group' role='group'>
@@ -167,12 +196,15 @@ router.get('/configs', async function(req, res) {
167196 }
168197} ) ;
169198
170- router . get ( '/config/:uuid' , async function ( req , res ) {
171- var uuid = req . params . uuid ;
199+ router . post ( '/config' , async function ( req , res ) {
200+ var data = req . body ;
201+ var uuid = data . uuid ;
202+ var iosVersion = data . ios_version ;
203+ var ipaVersion = data . ipa_version ;
172204 var device = await Device . getByName ( uuid ) ;
173205 var noConfig = false ;
174206 var assignDefault = false ;
175- // Check for a proxied IP before the normal IP and set the first one at exists
207+ // Check for a proxied IP before the normal IP and set the first one that exists
176208 var clientip = ( ( req . headers [ 'x-forwarded-for' ] || '' ) . split ( ', ' ) [ 0 ] ) || ( req . connection . remoteAddress ) . match ( '[0-9]+.[0-9].+[0-9]+.[0-9]+$' ) [ 0 ] ;
177209 console . log ( '[' + new Date ( ) . toLocaleString ( ) + ']' , 'Client' , uuid , 'at' , clientip , 'is requesting a config.' ) ;
178210
@@ -181,6 +213,8 @@ router.get('/config/:uuid', async function(req, res) {
181213 // Device exists
182214 device . lastSeen = new Date ( ) / 1000 ;
183215 device . clientip = clientip ;
216+ device . iosVersion = iosVersion ;
217+ device . ipaVersion = ipaVersion ;
184218 device . save ( ) ;
185219 if ( device . config ) {
186220 // Nothing to do besides respond with config
@@ -192,7 +226,8 @@ router.get('/config/:uuid', async function(req, res) {
192226 } else {
193227 console . log ( 'Device does not exist, creating...' ) ;
194228 // Device doesn't exist, create db entry
195- device = await Device . create ( uuid , null , new Date ( ) / 1000 , clientip ) ; // REVIEW: Maybe return Device object upon creation to prevent another sql call to get Device object?
229+ var ts = new Date ( ) / 1000 ;
230+ device = await Device . create ( uuid , null , ts , clientip , iosVersion , ipaVersion ) ;
196231 if ( device ) {
197232 // Success, assign default config if there is one.
198233 assignDefault = true ;
@@ -227,11 +262,11 @@ router.get('/config/:uuid', async function(req, res) {
227262 var c = await Config . getByName ( device . config ) ;
228263 if ( c === null ) {
229264 console . error ( 'Failed to grab config' , device . config ) ;
230- var data = {
265+ var noConfigData2 = {
231266 status : 'error' ,
232267 error : 'Device not assigned to config!'
233268 } ;
234- res . send ( JSON . stringify ( data ) ) ;
269+ res . send ( JSON . stringify ( noConfigData2 ) ) ;
235270 return ;
236271 }
237272 // Build json config
@@ -397,9 +432,9 @@ router.get('/schedule/delete_all', function(req, res) {
397432
398433
399434// Logging API requests
400- router . get ( '/logs/:uuid' , async function ( req , res ) {
435+ router . get ( '/logs/:uuid' , function ( req , res ) {
401436 var uuid = req . params . uuid ;
402- var logs = await Log . getByDevice ( uuid ) ;
437+ var logs = Log . getByDevice ( uuid ) ;
403438 res . send ( {
404439 uuid : uuid ,
405440 data : {
@@ -408,33 +443,37 @@ router.get('/logs/:uuid', async function(req, res) {
408443 } ) ;
409444} ) ;
410445
411- router . post ( '/log/new/:uuid' , async function ( req , res ) {
446+ router . post ( '/log/new' , function ( req , res ) {
412447 if ( config . logging === false ) {
413448 // Logs are disabled
414449 res . send ( 'OK' ) ;
415450 return ;
416451 }
417- var uuid = req . params . uuid ;
418- var msg = Object . keys ( req . body ) [ 0 ] ; // Dumb hack
419- var result = await Log . create ( uuid , msg ) ;
420- if ( result ) {
421- // Success
452+ var uuid = req . body . uuid ;
453+ var messages = req . body . messages ;
454+ if ( messages ) {
455+ messages . forEach ( function ( message ) {
456+ var result = Log . create ( uuid , message ) ;
457+ if ( result ) {
458+ // Success
459+ }
460+ console . log ( '[SYSLOG]' , uuid , ':' , message ) ;
461+ } ) ;
422462 }
423- console . log ( '[SYSLOG]' , uuid , ':' , msg ) ;
424463 res . send ( 'OK' ) ;
425464} ) ;
426465
427- router . get ( '/log/delete/:uuid' , async function ( req , res ) {
466+ router . get ( '/log/delete/:uuid' , function ( req , res ) {
428467 var uuid = req . params . uuid ;
429- var result = await Log . delete ( uuid ) ;
468+ var result = Log . delete ( uuid ) ;
430469 if ( result ) {
431470 // Success
432471 }
433472 res . redirect ( '/device/logs/' + uuid ) ;
434473} ) ;
435474
436- router . get ( '/logs/delete_all' , async function ( req , res ) {
437- var result = await Log . deleteAll ( ) ;
475+ router . get ( '/logs/delete_all' , function ( req , res ) {
476+ var result = Log . deleteAll ( ) ;
438477 if ( result ) {
439478 // Success
440479 }
0 commit comments