2121 "bluetooth"
2222 ] ;
2323
24- audioPciDevices =
25- if config . ghaf . common . hardware ? "audio" then config . ghaf . common . hardware . audio else [ ] ;
26- netPciDevices =
27- if config . ghaf . common . hardware ? "nics" then config . ghaf . common . hardware . nics else [ ] ;
28- camUsbDevices =
29- if config . ghaf . common . hardware ? "usb" then
30- lib . filter ( d : lib . hasPrefix "cam" d . name ) config . ghaf . common . hardware . usb
31- else
32- [ ] ;
33- btUsbDevices =
34- if config . ghaf . common . hardware ? "usb" then
35- lib . filter ( d : lib . hasPrefix "bt" d . name ) config . ghaf . common . hardware . usb
36- else
37- [ ] ;
38-
3924 # A function to generate shell commands for PCI devices
4025 mkPciCommands =
4126 {
4227 command ,
43- devices ,
28+ tag ,
4429 } :
45- lib . concatStringsSep "\n " (
46- map ( d : ''
47- vhotplugcli pci ${ command } \
48- ${ lib . optionalString ( d . vendorId != null ) "--vid ${ d . vendorId } " } \
49- ${ lib . optionalString ( d . productId != null ) "--did ${ d . productId } " }
50- '' ) devices
51- ) ;
30+ ''
31+ vhotplugcli pci ${ command } --tag ${ tag }
32+ '' ;
5233
5334 # A function to generate shell commands for USB devices
5435 mkUsbCommands =
5536 {
5637 command ,
57- devices ,
58- actionStr ,
38+ tag ,
5939 } :
60- lib . concatStringsSep "\n " (
61- map ( d : ''
62- echo "${ actionStr } device ${ d . name } ..."
63- vhotplugcli usb ${ command } \
64- ${ lib . optionalString ( d . vendorId != null ) "--vid ${ d . vendorId } " } \
65- ${ lib . optionalString ( d . productId != null ) "--pid ${ d . productId } " } \
66- ${ lib . optionalString ( d . hostbus != null ) "--bus ${ d . hostbus } " } \
67- ${ lib . optionalString ( d . hostport != null ) "--port ${ d . hostport } " }
68- '' ) devices
69- ) ;
40+ ''
41+ vhotplugcli usb ${ command } --tag ${ tag }
42+ '' ;
7043
7144 # A function to generate shell code for checking PCI device status
7245 mkPciStatusCheck =
7346 {
74- devices ,
47+ tag ,
7548 blockedVar ,
7649 } :
77- lib . concatStringsSep "\n " (
78- map ( d : ''
79- vid="${ lib . optionalString ( d . vendorId != null ) d . vendorId } "
80- did="${ lib . optionalString ( d . productId != null ) d . productId } "
81- if [ -n "$vid" ] && [ -n "$did" ] && echo "$pci_out" | grep -qi "'' ${vid}:'' ${did}"; then
82- ${ blockedVar } ="true"
83- fi
84- '' ) devices
85- ) ;
50+ ''
51+ if [ -z "$(vhotplugcli pci list --tag ${ tag } --connected | tr -d '[:space:]')" ]; then
52+ ${ blockedVar } ="true"
53+ fi
54+ '' ;
8655
8756 # A function to generate shell code for checking USB device status
8857 mkUsbStatusCheck =
8958 {
90- devices ,
59+ tag ,
9160 blockedVar ,
9261 } :
93- lib . concatStringsSep "\n " (
94- map ( d : ''
95- vid="${ lib . optionalString ( d . vendorId != null ) d . vendorId } "
96- did="${ lib . optionalString ( d . productId != null ) d . productId } "
97- hbus="${ lib . optionalString ( d . hostbus != null ) d . hostbus } "
98- hport="${ lib . optionalString ( d . hostport != null ) d . hostport } "
99-
100- # Normalize to lowercase for case-insensitive matching
101- vid_l=$(echo "$vid" | tr '[:upper:]' '[:lower:]')
102- did_l=$(echo "$did" | tr '[:upper:]' '[:lower:]')
103-
104- # Check if vid:pid match (case-insensitive)
105- if [ -n "$vid" ] && [ -n "$did" ]; then
106- if echo "$usb_out" | grep -qi "vid[[:space:]]*:[[:space:]]*$vid_l" \
107- && echo "$usb_out" | grep -qi "pid[[:space:]]*:[[:space:]]*$did_l"; then
108- ${ blockedVar } ="true"
109- fi
110- fi
111-
112- # Check if busnum + portnum match
113- if [ -n "$hbus" ] && [ -n "$hport" ]; then
114- if echo "$usb_out" | grep -q "busnum[[:space:]]*:[[:space:]]*$hbus" \
115- && echo "$usb_out" | grep -q "portnum[[:space:]]*:[[:space:]]*$hport"; then
116- ${ blockedVar } ="true"
117- fi
118- fi
119- '' ) devices
120- ) ;
62+ ''
63+ if [ -z "$(vhotplugcli usb list --tag ${ tag } --connected | tr -d '[:space:]')" ]; then
64+ ${ blockedVar } ="true"
65+ fi
66+ '' ;
12167
12268 ghaf-killswitch = pkgs . writeShellApplication {
12369 name = "ghaf-killswitch" ;
@@ -177,51 +123,31 @@ let
177123 case "$device" in
178124 net)
179125 echo "Blocking net device ..."
180- ${
181- if netPciDevices == [ ] then
182- ''echo "No net devices to block"''
183- else
184- mkPciCommands {
185- command = "detach" ;
186- devices = netPciDevices ;
187- }
188- }
126+ ${ mkPciCommands {
127+ command = "detach" ;
128+ tag = "net" ;
129+ } }
189130 ;;
190131 mic)
191132 echo "Blocking mic device ..."
192- ${
193- if audioPciDevices == [ ] then
194- ''echo "No mic devices to block"''
195- else
196- mkPciCommands {
197- command = "detach" ;
198- devices = audioPciDevices ;
199- }
200- }
133+ ${ mkPciCommands {
134+ command = "detach" ;
135+ tag = "audio" ;
136+ } }
201137 ;;
202138 cam)
203- ${
204- if camUsbDevices == [ ] then
205- ''echo "No cam devices to block"''
206- else
207- mkUsbCommands {
208- command = "detach" ;
209- devices = camUsbDevices ;
210- actionStr = "Blocking" ;
211- }
212- }
139+ echo "Blocking cam device ..."
140+ ${ mkUsbCommands {
141+ command = "detach" ;
142+ tag = "cam" ;
143+ } }
213144 ;;
214145 bluetooth)
215- ${
216- if btUsbDevices == [ ] then
217- ''echo "No bluetooth devices to block"''
218- else
219- mkUsbCommands {
220- command = "detach" ;
221- devices = btUsbDevices ;
222- actionStr = "Blocking" ;
223- }
224- }
146+ echo "Blocking bluetooth device ..."
147+ ${ mkUsbCommands {
148+ command = "detach" ;
149+ tag = "bt" ;
150+ } }
225151 ;;
226152 esac
227153 }
@@ -230,90 +156,65 @@ let
230156 case "$device" in
231157 net)
232158 echo "Unblocking net device ..."
233- ${
234- if netPciDevices == [ ] then
235- ''echo "No net devices to unblock"''
236- else
237- mkPciCommands {
238- command = "attach" ;
239- devices = netPciDevices ;
240- }
241- }
159+ ${ mkPciCommands {
160+ command = "attach" ;
161+ tag = "net" ;
162+ } }
242163 ;;
243164 mic)
244165 echo "Unblocking mic device ..."
245- ${
246- if audioPciDevices == [ ] then
247- ''echo "No mic devices to unblock"''
248- else
249- mkPciCommands {
250- command = "attach" ;
251- devices = audioPciDevices ;
252- }
253- }
166+ ${ mkPciCommands {
167+ command = "attach" ;
168+ tag = "audio" ;
169+ } }
254170 ;;
255171 cam)
256- ${
257- if camUsbDevices == [ ] then
258- ''echo "No cam devices to unblock"''
259- else
260- mkUsbCommands {
261- command = "attach" ;
262- devices = camUsbDevices ;
263- actionStr = "Unblocking" ;
264- }
265- }
172+ echo "Unblocking cam device ..."
173+ ${ mkUsbCommands {
174+ command = "attach" ;
175+ tag = "cam" ;
176+ } }
266177 ;;
267178 bluetooth)
268- ${
269- if btUsbDevices == [ ] then
270- ''echo "No bluetooth devices to unblock"''
271- else
272- mkUsbCommands {
273- command = "attach" ;
274- devices = btUsbDevices ;
275- actionStr = "Unblocking" ;
276- }
277- }
179+ echo "Unblocking bluetooth device ..."
180+ ${ mkUsbCommands {
181+ command = "attach" ;
182+ tag = "bt" ;
183+ } }
278184 ;;
279185 esac
280186 }
281187
282188 show_status() {
283- pci_out="$(vhotplugcli pci list --short --disconnected)"
284189
285190 # Check for Mic status
286191 mic_blocked="false"
287192 ${ mkPciStatusCheck {
288- devices = audioPciDevices ;
193+ tag = "audio" ;
289194 blockedVar = "mic_blocked" ;
290195 } }
291196 [ "$mic_blocked" = true ] && echo "mic: blocked" || echo "mic: unblocked"
292197
293198 # Check for Network status
294199 net_blocked="false"
295200 ${ mkPciStatusCheck {
296- devices = netPciDevices ;
201+ tag = "net" ;
297202 blockedVar = "net_blocked" ;
298203 } }
299204 [ "$net_blocked" = true ] && echo "net: blocked" || echo "net: unblocked"
300205
301- # Disable the warning that appears when no USB devices
302- # shellcheck disable=SC2034
303- usb_out="$(vhotplugcli usb list --disconnected)"
304-
305206 # Check for camera status
306207 cam_blocked="false"
307208 ${ mkUsbStatusCheck {
308- devices = camUsbDevices ;
209+ tag = "cam" ;
309210 blockedVar = "cam_blocked" ;
310211 } }
311212 [ "$cam_blocked" = true ] && echo "cam: blocked" || echo "cam: unblocked"
312213
313214 # Check for bluetooth status
314215 bt_blocked="false"
315216 ${ mkUsbStatusCheck {
316- devices = btUsbDevices ;
217+ tag = "bt" ;
317218 blockedVar = "bt_blocked" ;
318219 } }
319220 [ "$bt_blocked" = true ] && echo "bluetooth: blocked" || echo "bluetooth: unblocked"
0 commit comments