| 
						
						
						
					 | 
				
				 | 
				
					@ -1,13 +1,21 @@ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					// Internal
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					const { spawn } = require('child_process'); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					const { EOL } = require('os'); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					const os = require('os'); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					// Modules
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					const Telegraf = require('telegraf'); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					const { path } = require('./util/index.js'); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					//Config
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					const config = require('./config.js'); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					const validator = require('./lib/validator'); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					// Utils
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					const { path } = require('./util/index.js'); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					// Lib
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					const validator = require('./lib/validator.js'); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					const responder = require('./lib/responseHandler.js'); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					const listeners = require('./lib/listeners.js'); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					const dateOptions = { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						weekday: 'long', | 
				
			
			
		
	
	
		
			
				
					| 
						
							
								
							
						
						
							
								
							
						
						
					 | 
				
				 | 
				
					@ -40,11 +48,9 @@ bot.command('start', | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							const newProc = spawn(defaultShell, { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								cwd: home | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							}); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							newProc.stdout.setEncoding('utf8'); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							sessions.push(newProc); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							sessions.currentSession = newProc; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							sessions.currentSession.stdout.on('data', d => responder.success(d)(ctx)); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							sessions.currentSession.stderr.on('data', e => responder.success(e)(ctx)); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							listeners.add(sessions.currentSession, responder, ctx); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							return ctx.replyWithHTML(`Welcome to tsh -- <code>Telegram Shell!</code>\n\n` | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								+ `You are now connected to <code>${hostname}</code>` | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								+ ` as <strong>${username}</strong>`); | 
				
			
			
		
	
	
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
				
				 | 
				
					@ -64,8 +70,7 @@ bot.command('attach', | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							if(Number.isNaN(sessionIndex) || !sessions[sessionIndex]) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								return responder.fail('Session not found. /ls for list of sessions')(ctx); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							sessions.currentSession = sessions[sessionIndex]; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							sessions.currentSession.stdout.on('data', d => responder.success(d)(ctx)); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							sessions.currentSession.stderr.on('data', e => responder.success(e)(ctx)); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							listeners.add(sessions.currentSession, responder, ctx); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							return responder.success(`Reattached to shell ${sessionIndex}`)(ctx); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						}); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
	
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
				
				 | 
				
					@ -78,8 +83,7 @@ bot.command('detach', | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
									? sessions.currentSession : sessions[sessionIndex]; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							if(!currentSession) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								return responder.fail('Session not found. /ls for list of sessions.')(ctx); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							sessions.currentSession.stdout.removeAllListeners('data'); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							sessions.currentSession.stderr.removeAllListeners('data'); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							listeners.remove(sessions.currentSession); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							sessions.currentSession = undefined; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							return responder.success(`Detached from shell ${sessionIndex}`)(ctx); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						}); | 
				
			
			
		
	
	
		
			
				
					| 
						
							
								
							
						
						
						
					 | 
				
				 | 
				
					
  |