import os
from subprocess import PIPE, Popen
import ctypes
import _winreg

class disable_file_system_redirection:
    _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
    _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
    def __enter__(self):
        self.old_value = ctypes.c_long()
        self.success = self._disable(ctypes.byref(self.old_value))
    def __exit__(self, type, value, traceback):
        if self.success:
            self._revert(self.old_value)
with disable_file_system_redirection():
    arch=os.popen("wmic os get OSArchitecture").read()

def ecmd(command):   
    from subprocess import Popen, PIPE
    
    with disable_file_system_redirection():
        obj = Popen(command, shell = True, stdout = PIPE, stderr = PIPE)
    out, err = obj.communicate()
    ret=obj.returncode
    if ret==0:
        if out:
            return out.strip()
        else:
            return ret
    else:
        if err:
            return err.strip()
        else:
            return ret

bitwarden = r'jbkfoedolllekgbhcbcoahefnbanhhlh;https://edge.microsoft.com/extensionwebstorebase/v1/crx'
netcraft = r'ngjhgbnmdjjnmejmpamalgnlnmopllkm;https://edge.microsoft.com/extensionwebstorebase/v1/crx'
ublock = r'odfafepnkmbhccpbejgmiehpchacaeak;https://edge.microsoft.com/extensionwebstorebase/v1/crx'

with disable_file_system_redirection():
    query = os.popen("REG QUERY HKEY_LOCAL_MACHINE\\SOFTWARE\\Policies\\Microsoft\\Edge\\ExtensionInstallForcelist\\").read().strip()
    num = 0
    if query:
        for i in query.splitlines()[1:]:
            num = int(i.strip().split()[0])
        print(os.popen('taskkill /t /f /im msedge.exe').read())
        print(os.popen('reg add "HKEY_LOCAL_MACHINE\\SOFTWARE\\Policies\\Microsoft\\Edge\\ExtensionInstallForcelist" /v %s /t REG_SZ /d "%s" /f'%(num+1,bitwarden)).read())
        print(os.popen('reg add "HKEY_LOCAL_MACHINE\\SOFTWARE\\Policies\\Microsoft\\Edge\\ExtensionInstallForcelist" /v %s /t REG_SZ /d "%s" /f'%(num+2,netcraft)).read())
        print(os.popen('reg add "HKEY_LOCAL_MACHINE\\SOFTWARE\\Policies\\Microsoft\\Edge\\ExtensionInstallForcelist" /v %s /t REG_SZ /d "%s" /f'%(num+3,ublock)).read())
    else:
        print(os.popen('taskkill /t /f /im msedge.exe').read())
        print(os.popen('reg add "HKEY_LOCAL_MACHINE\\SOFTWARE\\Policies\\Microsoft\\Edge\\ExtensionInstallForcelist" /v %s /t REG_SZ /d "%s" /f'%(num+1,bitwarden)).read())
        print(os.popen('reg add "HKEY_LOCAL_MACHINE\\SOFTWARE\\Policies\\Microsoft\\Edge\\ExtensionInstallForcelist" /v %s /t REG_SZ /d "%s" /f'%(num+2,netcraft)).read())
        print(os.popen('reg add "HKEY_LOCAL_MACHINE\\SOFTWARE\\Policies\\Microsoft\\Edge\\ExtensionInstallForcelist" /v %s /t REG_SZ /d "%s" /f'%(num+3,ublock)).read())
        