-
Notifications
You must be signed in to change notification settings - Fork 0
/
svc.py
171 lines (128 loc) · 6.27 KB
/
svc.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import keypirinha as kp
import keypirinha_util as kpu
import subprocess
from .lib.svcutil import *
class Svc(kp.Plugin):
# Categories
ITEMCAT_SVCACTION = kp.ItemCategory.USER_BASE + 1 # 1001
# Plugin actions
ITEMACT_START = "start"
ITEMACT_STOP = "stop"
ITEMACT_RESTART = "restart"
ITEMACT_PAUSE = "pause"
ITEMACT_RESUME = "resume"
ITEMACT_STATUS = "status"
_service_catalog = {}
_wsu = None
def __init__(self):
super().__init__()
self._wsu = WinServiceUtils()
def on_start(self):
pass
def on_activated(self):
pass
def on_deactivated(self):
pass
def on_events(self, flags):
if flags & kp.Events.PACKCONFIG:
self.on_catalog()
def on_catalog(self):
self._service_catalog = self._wsu.EnumServicesStatus(0x30, 0x3)
catalog = []
for (service_name, service) in self._service_catalog.items():
catalog.append(
self.create_item(
category = kp.ItemCategory.REFERENCE,
label = f"Service {service_name}",
short_desc = bytes.decode(service.display_name, "mbcs"),
target = service_name,
args_hint = kp.ItemArgsHint.REQUIRED,
hit_hint = kp.ItemHitHint.NOARGS) )
self.set_catalog(catalog)
def start_suggestion(self, service_name):
return self.create_item(
category = self.ITEMCAT_SVCACTION,
label = f"Start {service_name} service",
short_desc = "Start service",
target = f"{self.ITEMACT_START},{service_name}",
args_hint = kp.ItemArgsHint.FORBIDDEN,
hit_hint = kp.ItemHitHint.NOARGS)
def restart_suggestion(self, service_name):
return self.create_item(
category = self.ITEMCAT_SVCACTION,
label = f"Restart {service_name} service",
short_desc = "Restart service",
target = f"{self.ITEMACT_RESTART},{service_name}",
args_hint = kp.ItemArgsHint.FORBIDDEN,
hit_hint = kp.ItemHitHint.NOARGS)
def resume_suggestion(self, service_name):
return self.create_item(
category = self.ITEMCAT_SVCACTION,
label = f"Resume {service_name} service",
short_desc = "Resume service",
target = f"{self.ITEMACT_RESUME},{service_name}",
args_hint = kp.ItemArgsHint.FORBIDDEN,
hit_hint = kp.ItemHitHint.NOARGS)
def stop_suggestion(self, service_name):
return self.create_item(
category = self.ITEMCAT_SVCACTION,
label = f"Stop {service_name} service",
short_desc = "Stop service",
target = f"{self.ITEMACT_STOP},{service_name}",
args_hint = kp.ItemArgsHint.FORBIDDEN,
hit_hint = kp.ItemHitHint.NOARGS)
def pause_suggestion(self, service_name):
return self.create_item(
category = self.ITEMCAT_SVCACTION,
label = f"Pause {service_name} service",
short_desc = "Pause service",
target = f"{self.ITEMACT_PAUSE},{service_name}",
args_hint = kp.ItemArgsHint.FORBIDDEN,
hit_hint = kp.ItemHitHint.NOARGS)
def status_suggestion(self, service_name, wsu, service_status):
return self.create_item(
category = self.ITEMCAT_SVCACTION,
label = f"Service status: {wsu.ServiceStateText(service_status.current_state)}",
short_desc = f"Copy service status",
target = f"{self.ITEMACT_STATUS},{service_name}",
args_hint = kp.ItemArgsHint.FORBIDDEN,
hit_hint = kp.ItemHitHint.NOARGS)
def on_suggest(self, user_input, items_chain):
if not items_chain:
return
current_item = items_chain[-1]
service_name = current_item.target()
suggestions = []
wsu = self._wsu
service_status = wsu.QueryServiceStatusEx(service_name)
# Add actions based on status of the service
if service_status.controls_accepted & wsu.SERVICE_ACCEPT_STOP:
if service_status.current_state in [wsu.SERVICE_RUNNING, wsu.SERVICE_PAUSED, wsu.SERVICE_START_PENDING, wsu.SERVICE_PAUSE_PENDING]:
suggestions.append(self.stop_suggestion(service_name))
if service_status.current_state in [wsu.SERVICE_STOPPED]:
suggestions.append(self.start_suggestion(service_name))
if service_status.controls_accepted & wsu.SERVICE_ACCEPT_PAUSE_CONTINUE:
if service_status.current_state in [wsu.SERVICE_RUNNING]:
suggestions.append(self.pause_suggestion(service_name))
if service_status.current_state in [wsu.SERVICE_PAUSED]:
suggestions.append(self.resume_suggestion(service_name))
suggestions.append(self.status_suggestion(service_name, wsu, service_status))
self.set_suggestions(suggestions, kp.Match.ANY, kp.Sort.NONE)
def on_execute(self, item, kp_action):
if not item:
return
print(f"on execute, target={item.target()}")
(action, service_name) = item.target().split(",", 1)
if action in ["start", "stop", "pause", "resume"]:
self.service_control(action, service_name)
elif action == "restat":
self.service_control("stop", service_name)
else:
print(f"Unimplemented action <{action}> on service {service_name}")
#startup_info = subprocess.STARTUPINFO()
def service_control(self, command, service_name):
"""Sending control command to a service using a call to Windows' sc.exe with elevated rights
"""
args = ["sc.exe", command, service_name]
self.dbg("Calling:", args)
kpu.shell_execute(args[0], args[1:], verb="runas", show=subprocess.SW_HIDE)