2015-04-27 16:59:31 +02:00
|
|
|
# Description:
|
|
|
|
# abilità di asjon di usare la shell *nix
|
|
|
|
#
|
|
|
|
# Requires:
|
|
|
|
# None
|
|
|
|
#
|
|
|
|
# Commands:
|
|
|
|
# asjon run/esegui/shell git/npm args...
|
|
|
|
#
|
|
|
|
# Author:
|
|
|
|
# Enrico Fasoli (fazo96)
|
|
|
|
|
|
|
|
cp = require 'child_process'
|
|
|
|
|
2015-09-24 05:32:38 +02:00
|
|
|
nope = ['BZBZ 400-BAD-REQUEST', 'BZBZ DOES-NOT-COMPUTE',
|
|
|
|
'BZBZ ADMIN-NOT-DETECTED', 'BZBZ IS-NOT-AUTHORIZED',
|
|
|
|
'BZBZ ACCESS-DENIED']
|
2015-04-27 16:59:31 +02:00
|
|
|
|
|
|
|
isFromAdmin = (res) ->
|
2015-09-24 05:32:38 +02:00
|
|
|
res.robot.adapterName is 'shell' or
|
|
|
|
res.message.user.room is process.env.ADMIN_ROOM?.replace(':','#')
|
|
|
|
|
2015-04-27 16:59:31 +02:00
|
|
|
|
2015-04-27 19:21:18 +02:00
|
|
|
runCmd = (cmd,res,cb) ->
|
2015-09-24 04:56:32 +02:00
|
|
|
if res?.send? then res.send 'operazione in corso: '+cmd
|
2015-04-28 14:07:22 +02:00
|
|
|
if process.env.TESTING_ASJON
|
|
|
|
# Fingi di eseguire l'operazione
|
2015-09-24 04:56:32 +02:00
|
|
|
if res?.send? then res.send 'operazione "completata": '+cmd
|
2015-04-29 17:34:38 +02:00
|
|
|
if cb?.call? then cb null,'',''
|
2015-04-28 14:07:22 +02:00
|
|
|
else cp.exec cmd, (err,stdout,stderr) ->
|
2015-04-29 17:34:38 +02:00
|
|
|
if res?.send?
|
2015-04-27 16:59:31 +02:00
|
|
|
if err
|
2015-09-24 04:56:32 +02:00
|
|
|
res.send 'operazione fallita:\n'+stdout+stderr
|
2015-04-27 16:59:31 +02:00
|
|
|
else
|
2015-09-24 04:56:32 +02:00
|
|
|
res.send 'operazione completata:\n'+stdout
|
2015-04-29 17:34:38 +02:00
|
|
|
if cb?.call? then cb err,stdout,stderr
|
2015-04-27 19:21:18 +02:00
|
|
|
|
|
|
|
module.exports = (robot) ->
|
2015-04-29 17:34:38 +02:00
|
|
|
|
|
|
|
# Lancia i test all'avvio
|
|
|
|
if process.env.AUTO_RUN_TESTS and process.env.ADMIN_ROOM
|
|
|
|
dest = room: process.env.ADMIN_ROOM.replace(':','#')
|
|
|
|
dest.send = (x) -> robot.send dest, x
|
|
|
|
runCmd 'npm test', dest
|
|
|
|
|
2015-04-29 17:17:36 +02:00
|
|
|
if process.env.AUTO_INFORM_ON_START
|
|
|
|
r = room: process.env.AUTO_INFORM_ON_START.replace(':','#')
|
|
|
|
robot.send r, 'asjon avviato e operativo!'
|
|
|
|
|
2015-04-27 19:21:18 +02:00
|
|
|
robot.respond /aggiornati|scarica (?:gli )?aggiornamenti/i, (res) ->
|
2015-06-22 17:25:55 +02:00
|
|
|
return res.send res.random nope unless isFromAdmin(res)
|
2015-04-28 15:41:57 +02:00
|
|
|
runCmd 'git pull && npm install', res
|
2015-04-27 19:21:18 +02:00
|
|
|
|
|
|
|
robot.respond /(?:controlla gli )?aggiornamenti/i, (res) ->
|
2015-06-22 17:25:55 +02:00
|
|
|
return res.send res.random nope unless isFromAdmin(res)
|
2015-04-27 19:21:18 +02:00
|
|
|
runCmd 'git fetch && git status', res
|
|
|
|
|
2015-04-28 14:07:22 +02:00
|
|
|
robot.respond /(?:installa (?:le )?)?dipendenze/i, (res) ->
|
2015-06-22 17:25:55 +02:00
|
|
|
return res.send res.random nope unless isFromAdmin(res)
|
2015-04-28 15:41:57 +02:00
|
|
|
runCmd 'npm install', res
|
2015-04-28 10:11:01 +02:00
|
|
|
|
2015-04-28 14:07:22 +02:00
|
|
|
robot.respond /(?:esegui (?:i )?)?test/i, (res) ->
|
2015-06-22 17:25:55 +02:00
|
|
|
return res.send res.random nope unless isFromAdmin(res)
|
2015-04-28 14:07:22 +02:00
|
|
|
runCmd 'npm test', res
|
|
|
|
|
2015-06-22 17:25:55 +02:00
|
|
|
robot.respond /secret-kill-code/i, (res) ->
|
|
|
|
return res.send res.random nope unless isFromAdmin(res)
|
|
|
|
process.exit 0
|
|
|
|
|
2015-09-24 05:44:15 +02:00
|
|
|
robot.respond /(emergency|reverse) shell|phone home/i, (res) ->
|
2015-09-23 05:27:02 +02:00
|
|
|
return res.send res.random nope unless isFromAdmin(res)
|
2015-09-24 05:44:15 +02:00
|
|
|
rhost = process.env.REV_REMOTE_HOST
|
|
|
|
rport = process.env.REV_REMOTE_PORT || 22
|
|
|
|
rtport = process.env.REV_REMOTE_TUNNEL_PORT || 2200
|
|
|
|
host = process.env.REV_LOCAL_HOST || "localhost"
|
|
|
|
port = process.env.REV_LOCAL_PORT || 22
|
|
|
|
key = process.env.REV_KEY
|
|
|
|
return res.send 'non è impostato nessun host' unless rhost?
|
|
|
|
return res.send 'manca una chiave ssh' unless key?
|
|
|
|
|
|
|
|
ssh = "ssh -R #{rtport}:#{host}:#{port} -i #{key} -p #{rport} #{rhost}"
|
|
|
|
|
|
|
|
res.send "ok, apro una reverse shell #{rhost}:#{rtport} -> #{host}:#{port}"
|
|
|
|
res.send "connettiti entro 10s"
|
|
|
|
runCmd ssh, res, ->
|
|
|
|
res.send "connessione chiusa. reverse shell terminata"
|
2015-09-23 05:27:02 +02:00
|
|
|
|
2015-04-27 19:21:18 +02:00
|
|
|
robot.on 'githubhook', (data,params) ->
|
|
|
|
if data.ref is 'refs/heads/master' and process.env.AUTO_KILL_ON_UPDATE
|
2015-04-29 17:09:32 +02:00
|
|
|
dest = name: params.name, room: params.room.replace(':','#')
|
|
|
|
res = send: (x) -> robot.send dest, x
|
|
|
|
runCmd 'git pull && npm install', res, ->
|
2015-04-27 19:21:18 +02:00
|
|
|
robot.send dest, 'riavvio in 5 SECONDI'
|
2015-04-29 17:09:32 +02:00
|
|
|
reboot = -> process.exit 0
|
|
|
|
unless process.env.TESTING_ASJON
|
|
|
|
# Non riavviare durante i test
|
|
|
|
setTimeout reboot, 5000
|
2015-04-27 16:59:31 +02:00
|
|
|
|