# Path: backdoors/connection.py
# class Connection:
# 	def __init__(self, intro, conn, numb):
# 		self.open = False
# 		self.type = intro
# 		self.connection = conn
# 		self.listening = 1
# 		self.number = int(numb) + 1
# 		self.thread = threading.Thread(target=self.wait)
# 		self.thread.start()
# 	def __str__(self):
# 		string = ""
# 		if(self.open):
# 			string += "Open\n"
# 		else:
# 			string += "Closed\n"
# 		string += self.type
# 		#string += self.connection + "\n"
# 		return string
# 
# 	def wait(self):
# 		try:
# 			self.connection.expect("Connection Received.")
# 			print("\nSession " + str(self.number) + " has recieved a connection. Type sessions -i " + str(self.number) + " to interact.")
# 			self.open = True
# 		except:
# 			self.open = False
# 
# 	def interact(self):
# 		if self.connection.isalive():
# 			if self.listening == 0:
# 				self.connection.sendline()
# 				print("Press Control + ] to exit the shell."),
# 
# 			else:
# 				self.listening = 0
# 				print("Press Control + ] to exit the shell."),
# 			self.connection.interact(escape_character='\x1d', input_filter=None, output_filter=None)
# 		else:
# 			print("The connection has been lost")


import os
import cmd
import math
import shlex
import importlib
import inspect
import sys
import multiprocessing
import socket
import time
import pexpect
import traceback
from colorama import *
from .option import *
from six.moves import input
from .connection import Connection
                except Exception as e:
                    #traceback.print_exc()
                    print(BAD + "No module \""+m+"\" available.")
        else:
            print(BAD + "Modules disabled by this backdoor.")

    def complete_set(self, text, line, begin_index, end_index):
        line = line.rsplit(" ")[1]
        segment = line.split(".")
        if len(segment) == 1:
            return [item for item in ([m.name.lower() for m in self.modules.keys()] + ["target"] + self.options.keys()) if item.startswith(text)]
        if len(segment) == 2:
            return [(segment[0] + "." + item) for item in self.get_by_name(segment[0]).options.keys() if item.startswith(text.replace(segment[0]+".",""))]

    def set_target(self, target):
        self.options['target'] = target

    def set_option(self, option, value):
        if option in self.options.keys():
            self.options[option] = value
            return True
        else:
            return False

    def do_exploit(self):
        return False

    def listen(self, passw="none", prompt="some"):
        self.child = pexpect.spawn("python listen.py " + str(self.get_value("port")) + " " + str(passw) + " " + str(prompt))
        time.sleep(.25)

# Path: backdoors/connection.py
# class Connection:
# 	def __init__(self, intro, conn, numb):
# 		self.open = False
# 		self.type = intro
# 		self.connection = conn
# 		self.listening = 1
# 		self.number = int(numb) + 1
# 		self.thread = threading.Thread(target=self.wait)
# 		self.thread.start()
# 	def __str__(self):
# 		string = ""
# 		if(self.open):
# 			string += "Open\n"
# 		else:
# 			string += "Closed\n"
# 		string += self.type
# 		#string += self.connection + "\n"
# 		return string
# 
# 	def wait(self):
# 		try:
# 			self.connection.expect("Connection Received.")
# 			print("\nSession " + str(self.number) + " has recieved a connection. Type sessions -i " + str(self.number) + " to interact.")
# 			self.open = True
# 		except:
# 			self.open = False
# 
# 	def interact(self):
# 		if self.connection.isalive():
# 			if self.listening == 0:
# 				self.connection.sendline()
# 				print("Press Control + ] to exit the shell."),
# 
# 			else:
# 				self.listening = 0
# 				print("Press Control + ] to exit the shell."),
# 			self.connection.interact(escape_character='\x1d', input_filter=None, output_filter=None)
# 		else:
# 			print("The connection has been lost")


# Path: backdoors/backdoor.py
import os
import cmd
import math
import shlex
import importlib
import inspect
import sys
import multiprocessing
import socket
import time
import pexpect
import traceback
from colorama import *
from .option import *
from six.moves import input
from .connection import Connection

                except Exception as e:
                    #traceback.print_exc()
                    print(BAD + "No module \""+m+"\" available.")
        else:
            print(BAD + "Modules disabled by this backdoor.")

    def complete_set(self, text, line, begin_index, end_index):
        line = line.rsplit(" ")[1]
        segment = line.split(".")
        if len(segment) == 1:
            return [item for item in ([m.name.lower() for m in self.modules.keys()] + ["target"] + self.options.keys()) if item.startswith(text)]
        if len(segment) == 2:
            return [(segment[0] + "." + item) for item in self.get_by_name(segment[0]).options.keys() if item.startswith(text.replace(segment[0]+".",""))]

    def set_target(self, target):
        self.options['target'] = target

    def set_option(self, option, value):
        if option in self.options.keys():
            self.options[option] = value
            return True
        else:
            return False

    def do_exploit(self):
        return False

    def listen(self, passw="none", prompt="some"):
        self.child = pexpect.spawn("python listen.py " + str(self.get_value("port")) + " " + str(passw) + " " + str(prompt))
        time.sleep(.25)

        self.core.curtarget.sessions.append(Connection(self.intro, self.child, len(self.core.curtarget.sessions)))