asjon/scripts/shell.coffee

99 lines
3.3 KiB
CoffeeScript
Raw Permalink Normal View History

# Description:
# abilità di asjon di usare la shell *nix
#
# Requires:
# None
#
# Commands:
2018-06-20 20:12:48 +02:00
# asjon run/esegui/shell git/yarn args...
#
# Author:
# Enrico Fasoli (fazo96)
cp = require 'child_process'
2017-02-22 22:11:20 +01:00
nope = ['BZBZ 400-BAD-REQUEST', 'BZBZ DOES-NOT-COMPUTE',
2015-09-24 05:32:38 +02:00
'BZBZ ADMIN-NOT-DETECTED', 'BZBZ IS-NOT-AUTHORIZED',
'BZBZ ACCESS-DENIED']
isFromAdmin = (res) ->
2015-09-24 05:32:38 +02:00
res.robot.adapterName is 'shell' or
2017-02-22 22:11:20 +01:00
res.message.room.id is process.env.ADMIN_ROOM
2015-09-24 05:32:38 +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
if cb?.call? then cb null,'',''
2015-04-28 14:07:22 +02:00
else cp.exec cmd, (err,stdout,stderr) ->
if res?.send?
if err
2015-09-24 04:56:32 +02:00
res.send 'operazione fallita:\n'+stdout+stderr
else
2015-09-24 04:56:32 +02:00
res.send 'operazione completata:\n'+stdout
if cb?.call? then cb err,stdout,stderr
module.exports = (robot) ->
# Lancia i test all'avvio
if process.env.AUTO_RUN_TESTS and process.env.ADMIN_ROOM
dest = room: process.env.ADMIN_ROOM
dest.send = (x) -> robot.send dest, x
2018-06-20 20:12:48 +02:00
runCmd 'yarn test', dest
if process.env.AUTO_INFORM_ON_START
2017-02-22 22:11:20 +01:00
room = id: process.env.AUTO_INFORM_ON_START
robot.messageRoom room, 'asjon avviato e operativo!'
robot.respond /aggiornati|scarica (?:gli )?aggiornamenti/i, (res) ->
2017-02-22 22:11:20 +01:00
return res.send res.random nope unless isFromAdmin res
2018-06-20 20:12:48 +02:00
runCmd 'git pull && yarn install', res
robot.respond /(?:controlla gli )?aggiornamenti/i, (res) ->
2017-02-22 22:11:20 +01:00
return res.send res.random nope unless isFromAdmin res
runCmd 'git fetch && git status', res
2015-04-28 14:07:22 +02:00
robot.respond /(?:installa (?:le )?)?dipendenze/i, (res) ->
2017-02-22 22:11:20 +01:00
return res.send res.random nope unless isFromAdmin res
2018-06-20 20:12:48 +02:00
runCmd 'yarn 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) ->
2017-02-22 22:11:20 +01:00
return res.send res.random nope unless isFromAdmin res
2018-06-20 20:12:48 +02:00
runCmd 'yarn test', res
2015-04-28 14:07:22 +02:00
robot.respond /secret-kill-code/i, (res) ->
2017-02-22 22:11:20 +01:00
return res.send res.random nope unless isFromAdmin res
process.exit 0
robot.respond /(emergency|reverse) shell|phone home/i, (res) ->
2017-02-22 22:11:20 +01:00
return res.send res.random nope unless isFromAdmin res
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?
2017-02-22 22:11:20 +01:00
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
2018-06-08 08:57:39 +02:00
robot.on 'githook', (data,params) ->
if data.ref is 'refs/heads/master' and process.env.AUTO_KILL_ON_UPDATE
dest = name: params.name, room: params.room
res = send: (x) -> robot.send dest, x
2018-06-20 20:12:48 +02:00
runCmd 'git pull && yarn install', res, ->
robot.send dest, 'riavvio in 5 SECONDI'
reboot = -> process.exit 0
unless process.env.TESTING_ASJON
# Non riavviare durante i test
setTimeout reboot, 5000