Skip to content

Commit 8430b40

Browse files
author
Magnus
committed
Fix duplicate process detection - more robust tasklist parsing
1 parent b89bc63 commit 8430b40

2 files changed

Lines changed: 58 additions & 47 deletions

File tree

src/elmocut.py

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,62 +18,63 @@
1818
app = QApplication(argv)
1919
icon = ElmoCut.processIcon(app_icon)
2020

21-
# Check if Npcap is installed
21+
# Check if Npcap is installed (Windows only)
2222
if not npcap_exists():
2323
if msg_box('ArpCut', 'Npcap is not installed\n\nClick OK to download',
2424
MsgIcon.CRITICAL, icon, Buttons.OK | Buttons.CANCEL) == Buttons.OK:
2525
goto(NPCAP_URL)
26+
exit(1)
2627

2728
# Check if another ArpCut process is running
28-
elif duplicate_elmocut():
29+
if duplicate_elmocut():
2930
msg_box('ArpCut', 'ArpCut is already running!', MsgIcon.WARN, icon)
31+
exit(1)
3032

3133
# Run the GUI
32-
else:
33-
migrate_settings_file()
34-
repair_settings()
35-
GUI = ElmoCut()
36-
GUI.show()
37-
GUI.resizeEvent()
38-
39-
# Initialize scanner and ensure interface is valid
34+
migrate_settings_file()
35+
repair_settings()
36+
GUI = ElmoCut()
37+
GUI.show()
38+
GUI.resizeEvent()
39+
40+
# Initialize scanner and ensure interface is valid
41+
GUI.scanner.init()
42+
if GUI.scanner.iface.name == 'NULL':
43+
# Try to get a valid interface
44+
from tools.utils import get_default_iface
45+
GUI.scanner.iface = get_default_iface()
4046
GUI.scanner.init()
41-
if GUI.scanner.iface.name == 'NULL':
42-
# Try to get a valid interface
43-
from tools.utils import get_default_iface
44-
GUI.scanner.iface = get_default_iface()
45-
GUI.scanner.init()
46-
47-
# Ensure "Me" and "Router" are added immediately
48-
try:
49-
GUI.scanner.add_me()
50-
GUI.scanner.add_router()
51-
GUI.showDevices() # Show at least "Me" and "Router" on startup
52-
except Exception as e:
53-
GUI.log(f'Warning: Could not initialize local devices: {e}', 'orange')
54-
55-
GUI.scanner.flush_arp()
47+
48+
# Ensure "Me" and "Router" are added immediately
49+
try:
50+
GUI.scanner.add_me()
51+
GUI.scanner.add_router()
52+
GUI.showDevices() # Show at least "Me" and "Router" on startup
53+
except Exception as e:
54+
GUI.log(f'Warning: Could not initialize local devices: {e}', 'orange')
55+
56+
GUI.scanner.flush_arp()
5657

57-
# On macOS/Linux when not root, avoid ARP scan (requires /dev/bpf) and use Ping scan
58-
try:
59-
import os
60-
is_posix = (os.name == 'posix')
61-
is_root = (getattr(os, 'geteuid', lambda: 0)() == 0)
62-
except Exception:
63-
is_posix, is_root = False, True
58+
# On macOS/Linux when not root, avoid ARP scan (requires /dev/bpf) and use Ping scan
59+
try:
60+
import os
61+
is_posix = (os.name == 'posix')
62+
is_root = (getattr(os, 'geteuid', lambda: 0)() == 0)
63+
except Exception:
64+
is_posix, is_root = False, True
6465

65-
if is_posix and not is_root:
66-
GUI.log('Running without root: using Ping Scan', 'orange')
67-
GUI.ScanThread_Starter(scan_type=1)
66+
if is_posix and not is_root:
67+
GUI.log('Running without root: using Ping Scan', 'orange')
68+
GUI.ScanThread_Starter(scan_type=1)
69+
else:
70+
# Only check connection if interface is valid
71+
if GUI.scanner.iface.name != 'NULL':
72+
GUI.scanEasy()
6873
else:
69-
# Only check connection if interface is valid
70-
if GUI.scanner.iface.name != 'NULL':
71-
GUI.scanEasy()
72-
else:
73-
GUI.log('No network interface found. Please check your network connection.', 'red')
74+
GUI.log('No network interface found. Please check your network connection.', 'red')
7475

75-
GUI.UpdateThread_Starter()
76-
# Bring window to top on startup
77-
GUI.activateWindow()
78-
#GUI.scanner.print_report()
79-
exit(app.exec_())
76+
GUI.UpdateThread_Starter()
77+
# Bring window to top on startup
78+
GUI.activateWindow()
79+
#GUI.scanner.print_report()
80+
exit(app.exec_())

src/tools/utils_gui.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,18 @@ def duplicate_elmocut():
3535
Check if there is more than 1 instance of ArpCut running
3636
"""
3737
if sys.platform.startswith('win'):
38-
tasklist = terminal('tasklist')
39-
return tasklist.lower().count('arpcut.exe') > 1
38+
try:
39+
tasklist = terminal('tasklist')
40+
if not tasklist:
41+
return False
42+
# Count actual process entries (each on its own line)
43+
count = 0
44+
for line in tasklist.lower().split('\n'):
45+
if 'arpcut.exe' in line:
46+
count += 1
47+
return count > 1
48+
except Exception:
49+
return False
4050
# TODO: Implement PID/file lock if needed for macOS
4151
return False
4252

0 commit comments

Comments
 (0)