Skip to content
This repository was archived by the owner on Nov 26, 2024. It is now read-only.

Commit 15f958f

Browse files
author
voidlesity
committed
added check for multiple instances so the program is only run once
1 parent 35bde33 commit 15f958f

File tree

3 files changed

+14
-23
lines changed

3 files changed

+14
-23
lines changed

NgrokDynamicDNS.iss

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!
33

44
#define MyAppName "Ngrok Dynamic DNS"
5-
#define MyAppVersion "1.1.0"
5+
#define MyAppVersion "1.1.1"
66
#define MyAppPublisher "voidlesity"
77
#define MyAppURL "https://github.com/voidlesity/ngrok-dynamic-dns"
88
#define MyAppExeName "NgrokDynamicDNS.exe"

NgrokDynamicDNS.py

+11-21
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
import requests
77
import pystray
88
import winreg
9+
import psutil
910
import time
1011
import json
1112
import sys
1213
import os
1314

14-
1515
def readConfig(configFilePath):
1616
config = configparser.ConfigParser()
1717

@@ -27,7 +27,6 @@ def readConfig(configFilePath):
2727
}
2828
return config
2929

30-
3130
def updateConfig():
3231
def displayRecords(records):
3332
newWindow = Toplevel(root)
@@ -118,17 +117,9 @@ def checkEntries(*args):
118117

119118
root.mainloop()
120119

121-
122120
def updateDns(target, port, apiToken, zoneId, recordId):
123121
with contextlib.suppress(Exception):
124122
response = requests.patch(f"https://api.cloudflare.com/client/v4/zones/{zoneId}/dns_records/{recordId}", json={"data": {"port": port, "target": target}}, headers={"Authorization": f"Bearer {apiToken}"})
125-
if response.status_code != 200:
126-
errorData = json.loads(response.content)
127-
errorMessages = [error['message'] for error in errorData.get('errors', [])]
128-
errorMessageStr = "\n".join(errorMessages)
129-
messagebox.showerror("Error", f"An error occurred while updating DNS:\n{errorMessageStr}\n\nMake sure your API token has the right privileges.")
130-
stop()
131-
132123

133124
def checkForUpdates():
134125
response = requests.get("https://api.github.com/repos/voidlesity/ngrok-dynamic-dns/releases/latest").json()
@@ -146,6 +137,13 @@ def checkForUpdates():
146137
subprocess.run(["NgrokDynamicDNS-installer.exe"])
147138
stop()
148139

140+
def checkForMultipleInstances():
141+
process_count = 0
142+
for process in psutil.process_iter(['name']):
143+
if process.info['name'] == "NgrokDynamicDNS.exe":
144+
process_count += 1
145+
if process_count > 2:
146+
stop()
149147

150148
def toggleAutostart():
151149
config.set('DEFAULT', 'autostart', f"{not config.getboolean('DEFAULT', 'autostart')}")
@@ -164,38 +162,29 @@ def autostart():
164162
else:
165163
winreg.DeleteValue(registry_key, "Ngrok Dynamic DNS")
166164

167-
168165
def stop():
169166
icon.stop()
170167
os._exit(0)
171168

172-
173169
def main():
174170
prevTarget = None
175171
prevPort = None
176-
177172
while True:
178-
try:
173+
with contextlib.suppress(Exception):
179174
response = requests.get(ngrokApiUrl)
180175
response.raise_for_status()
181-
182176
if response.status_code == 200:
183177
ngrokData = response.json()['tunnels'][0]['public_url'].strip("tcp://").split(":")
184178
target = ngrokData[0]
185179
port = ngrokData[1]
186-
187180
if target != prevTarget or port != prevPort:
188181
updateDns(target, port, apiToken, zoneId, recordId)
189182
prevTarget = target
190183
prevPort = port
191-
192184
time.sleep(30)
193-
except Exception as e:
194-
messagebox.showerror("Error", f"An error occurred while trying to fetch tunnel info:\n{e}\n\nMake sure the Ngrok URL is set correctly.")
195-
stop()
196185

197186
if __name__ == "__main__":
198-
currentVersion = "v1.1.0"
187+
currentVersion = "v1.1.1"
199188
allVars = ['api_token', 'zone_id', 'record_id', 'ngrok_api_url']
200189

201190
configFilePath = os.path.join(os.path.expanduser("~"), ".config", "voidlesity", "NgrokDynamicDNS.config")
@@ -220,6 +209,7 @@ def main():
220209
icon = pystray.Icon("NgrokDynamicDNS", iconImage, title="NgrokDynamicDNS", menu=menu)
221210
icon.run_detached()
222211

212+
checkForMultipleInstances()
223213
checkForUpdates()
224214
autostart()
225215
main()

requirements.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pyinstaller
22
requests
3-
pystray
3+
pystray
4+
psutil

0 commit comments

Comments
 (0)