<commit_before>import multiprocessing
import socket
import subprocess
import json
import time
import os.path
import base64
import hashlib
from Crypto.Cipher import AES
from Crypto import Random
from server import Server 
from vps import Vps 

vcakey = ''

def handle(connection, address):
    import logging
    global vcakey
    logging.basicConfig(level=logging.DEBUG)
    logger = logging.getLogger("process-%r" % (address,))
    try:
        logger.debug("Connected %r at %r", connection, address)
        while True:
            data = connection.recv(1024)
            if data == "" or data.decode('utf-8') == "":
                logger.debug("Socket closed remotely")
                break
            logger.debug("Received data %r", data)
            
            data = json.loads(data.decode('utf-8'))
            
            if data['iv']:
                AES.key_size=128
                crypt_object=AES.new(key=vcakey,mode=AES.MODE_CBC,IV=base64.b64decode(data['iv']))
                decrypted=crypt_object.decrypt(base64.b64decode(data['data']))
    
                logger.debug("Received data %r", decrypted.decode('utf-8').strip('\x00'))
                logger.debug("Sent data1")
                
                do = json.loads(decrypted.decode('utf-8').strip('\x00'))
                answer = (16) * ' '
                answer = answer + vcaAction(do['action'], do['server'], do['para'])
                answer = answer + (16 - len(answer) % 16) * ' '
                answer = base64.b64encode(crypt_object.encrypt(answer))
                connection.send(bytes(answer))
            connection.send(bytes('close', 'UTF-8'))
        logger.debug("Sent data2")
    except:
        logger.exception("Problem handling request")
    finally:
        logger.debug("Closing socket")
        connection.close()

class VcaServer(object):
    def __init__(self, hostname, port):
        import logging
        self.logger = logging.getLogger("server")
        self.hostname = hostname
        self.port = port

    def start(self):
        if os.path.isfile('vcakey.conf'):
            global vcakey
            file = open('vcakey.conf','r')
            vcakey = file.readline();
            vcakey = vcakey.replace('\n', '')
            vcakey = hashlib.md5(vcakey.encode()).hexdigest()
            file.close()
        
        self.logger.debug("listening")
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.bind((self.hostname, self.port))
        self.socket.listen(1)

        while True:
            conn, address = self.socket.accept()
            self.logger.debug("Got connection")
            process = multiprocessing.Process(target=handle, args=(conn, address))
            process.daemon = True
            process.start()
            self.logger.debug("Started process %r", process)

def vcaAction(action, serverDest, para):
    
    # Server And VPS
    if action == 'VpsList':
        return Server.VpsList()
    elif action == 'create':
        if serverDest != 0:
            server = Server()<commit_msg>Only root can read vcakey.conf<commit_after>import multiprocessing
import socket
import subprocess
import json
import time
import os.path
import base64
import hashlib
from Crypto.Cipher import AES
from Crypto import Random
from server import Server 
from vps import Vps 

vcakey = ''

def handle(connection, address):
    import logging
    global vcakey
    logging.basicConfig(level=logging.DEBUG)
    logger = logging.getLogger("process-%r" % (address,))
    try:
        logger.debug("Connected %r at %r", connection, address)
        while True:
            data = connection.recv(1024)
            if data == "" or data.decode('utf-8') == "":
                logger.debug("Socket closed remotely")
                break
            logger.debug("Received data %r", data)
            
            data = json.loads(data.decode('utf-8'))
            
            if data['iv']:
                AES.key_size=128
                crypt_object=AES.new(key=vcakey,mode=AES.MODE_CBC,IV=base64.b64decode(data['iv']))
                decrypted=crypt_object.decrypt(base64.b64decode(data['data']))
    
                logger.debug("Received data %r", decrypted.decode('utf-8').strip('\x00'))
                logger.debug("Sent data1")
                
                do = json.loads(decrypted.decode('utf-8').strip('\x00'))
                answer = (16) * ' '
                answer = answer + vcaAction(do['action'], do['server'], do['para'])
                answer = answer + (16 - len(answer) % 16) * ' '
                answer = base64.b64encode(crypt_object.encrypt(answer))
                connection.send(bytes(answer))
            connection.send(bytes('close', 'UTF-8'))
        logger.debug("Sent data2")
    except:
        logger.exception("Problem handling request")
    finally:
        logger.debug("Closing socket")
        connection.close()

class VcaServer(object):
    def __init__(self, hostname, port):
        import logging
        self.logger = logging.getLogger("server")
        self.hostname = hostname
        self.port = port

    def start(self):
        if os.path.isfile('vcakey.conf'):
            global vcakey
            os.chmod('vcakey.conf', 0o400)
            file = open('vcakey.conf','r')
            vcakey = file.readline();
            vcakey = vcakey.replace('\n', '')
            vcakey = hashlib.md5(vcakey.encode()).hexdigest()
            file.close()
        
        self.logger.debug("listening")
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.bind((self.hostname, self.port))
        self.socket.listen(1)

        while True:
            conn, address = self.socket.accept()
            self.logger.debug("Got connection")
            process = multiprocessing.Process(target=handle, args=(conn, address))
            process.daemon = True
            process.start()
            self.logger.debug("Started process %r", process)

def vcaAction(action, serverDest, para):
    
    # Server And VPS
    if action == 'VpsList':
        return Server.VpsList()
    elif action == 'create':
        if serverDest != 0:
            server = Server()
{'fraction_non_alphanumeric': 0.12632594021215043, 'fraction_numerical': 0.013821922211507553, 'mean_word_length': 2.074604743083004, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3581176', 'n_tokens_mistral': 1852, 'n_tokens_neox': 1757, 'n_words': 371}