Skip to content

Commit 00c7516

Browse files
committed
removed ngrok.exe required
now using pyngrok library, instead of opening ngrok using subprocess • updated build.py to remove including ngrok.exe • removed web_addrr:false fom createDefaultConfig as its needed for pyngrok to work. • set loggers for flask & ngrok to error to aviod spamming console
1 parent e74a8b6 commit 00c7516

File tree

6 files changed

+44
-23
lines changed

6 files changed

+44
-23
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/venv
22
*.yaml
33
*.pyc
4-
*.tp
4+
*.tp
5+
*.zip
6+
/dist

ngrok.exe

-28.2 MB
Binary file not shown.

requirements.txt

32 Bytes
Binary file not shown.

src/Kofi.py

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,22 @@
1212
from TPPEntry import TP_PLUGIN_SETTINGS, PLUGIN_ID, TP_PLUGIN_INFO, __version__
1313
from createDefaultConfig import create_default_yaml_file
1414
from updateConfig import update_config_file
15+
import os
16+
import signal
17+
18+
19+
## figure out how to set a static location for pyngrok to download the ngrok.exe file rather than it doing it every time?
20+
## need to find a way to kill the ngrok process when the plugin is closed rather than the os.kill.. it should be related to the pyinstaller process but
21+
## seems as if its not since its downloaded after the fact..
1522

1623
app = Flask(__name__)
1724
ngrok_running = False
25+
26+
g_log = Logger(name = PLUGIN_ID)
27+
## setting logging for flask to only log errors
28+
logging.getLogger('werkzeug').setLevel(logging.ERROR)
29+
logging.getLogger("pyngrok.process.ngrok").setLevel(logging.ERROR)
30+
1831
try:
1932
TPClient = TP.Client(
2033
pluginId = PLUGIN_ID, # required ID of this plugin
@@ -27,12 +40,6 @@
2740
except Exception as e:
2841
sys.exit(f"Could not create TP Client, exiting. Error was:\n{repr(e)}")
2942

30-
g_log = Logger(name = PLUGIN_ID)
31-
32-
33-
## setting logging for flask to only log errors
34-
log = logging.getLogger('werkzeug')
35-
log.setLevel(logging.ERROR)
3643

3744

3845
@app.route('/', methods=['POST'])
@@ -239,14 +246,23 @@ def onSettingUpdate(data):
239246
def onAction(data: dict):
240247
g_log.debug(f"Action: {data}")
241248

242-
249+
def start_flask():
250+
app.run(debug=False)
251+
252+
from pyngrok import ngrok, conf
253+
from pyngrok.conf import PyngrokConfig
254+
http_tunnel = None
243255
def start_ngrok():
244-
global ngrok_running
256+
global ngrok_running, http_tunnel
245257
## Starting up Ngrok server
246258
if TP_PLUGIN_SETTINGS['Ngrok Server Address']['value'] != "" and TP_PLUGIN_SETTINGS['Ngrok Port']['value'] != "":
247259
try:
248-
ngrok_command = "ngrok start --config=ngrok.yaml --all"
249-
subprocess.Popen(ngrok_command, shell=True)
260+
# ngrok_command = "ngrok start --config=ngrok.yaml --all"
261+
# subprocess.Popen(ngrok_command, shell=True)
262+
ngrok_config = PyngrokConfig()
263+
ngrok_config.config_path = "ngrok.yaml"
264+
http_tunnel = ngrok.connect(name='TP_NGROK', pyngrok_config=ngrok_config)
265+
250266
ngrok_running = True
251267
except Exception as e:
252268
g_log.error(f"Error starting ngrok server: {e}")
@@ -273,12 +289,15 @@ def shutdownNgrok():
273289
global ngrok_running
274290
try:
275291
g_log.info("Shutting down ngrok...")
292+
ngrok.disconnect(http_tunnel.public_url)
293+
294+
## ngrok is spawning outside of our pyinstaller sometimes and we need to assure its dead for next reload
276295
subprocess.run(["taskkill", "/F", "/IM", "ngrok.exe"], check=True)
277296
g_log.info("ngrok terminated.")
278297
ngrok_running = False
279-
280-
except subprocess.CalledProcessError as e:
281-
g_log.error(f"Failed to terminate ngrok: {e}")
298+
except Exception as e:
299+
g_log.error(f"Error shutting down ngrok: {e}")
300+
return False
282301

283302

284303

@@ -288,6 +307,9 @@ def onShutdown(data:dict):
288307
g_log.info('Received shutdown event from TP Client.')
289308
shutdownNgrok()
290309

310+
## Requesting a shutdown for the Flask server
311+
os.kill(os.getpid(), signal.SIGTERM)
312+
291313

292314

293315
## main
@@ -362,3 +384,6 @@ def main():
362384

363385
if __name__ == "__main__":
364386
sys.exit(main())
387+
388+
389+

src/build.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11

22
from TouchPortalAPI import tppbuild
33
from TPPEntry import PLUGIN_NAME, PLUGIN_FOLDER, PLUGIN_ICON, __version__
4-
import os
54

6-
script_dir = os.path.dirname(os.path.realpath(__file__))
75

86

97
PLUGIN_MAIN = f"{PLUGIN_NAME}.py"
@@ -34,10 +32,8 @@
3432

3533

3634
ADDITIONAL_FILES = [
37-
os.path.normpath(os.path.join(script_dir, "start.sh")),
38-
os.path.normpath(os.path.join(script_dir, "../ngrok.exe"))
39-
]
40-
35+
"start.sh"
36+
]
4137

4238
if PLUGIN_ICON != "":
4339
ADDITIONAL_FILES.append(PLUGIN_ICON)
@@ -50,8 +46,6 @@
5046
# "--noconsole"
5147
]
5248

53-
54-
5549
# validateBuild()
5650

5751
if __name__ == "__main__":

src/createDefaultConfig.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def create_default_yaml_file(file_path):
1313
}
1414
},
1515
'version': 2,
16-
'web_addr': False
16+
# 'web_addr': False
1717
}
1818
# Check if the file already exists
1919
if not os.path.exists(file_path):

0 commit comments

Comments
 (0)