From aba5b938bc15f99c0221dfc80cd162a26e3196d3 Mon Sep 17 00:00:00 2001
From: Simone Margaritelli <evilsocket@gmail.com>
Date: Tue, 5 Nov 2019 14:48:26 +0100
Subject: [PATCH] misc: small fix or general refactoring i did not bother
 commenting

---
 pwnagotchi/agent.py          | 7 +++----
 pwnagotchi/ui/display.py     | 7 +++----
 pwnagotchi/ui/view.py        | 3 +++
 pwnagotchi/ui/web/handler.py | 6 +++---
 pwnagotchi/ui/web/server.py  | 6 +++---
 5 files changed, 15 insertions(+), 14 deletions(-)

diff --git a/pwnagotchi/agent.py b/pwnagotchi/agent.py
index f4673b1..e6c34a3 100644
--- a/pwnagotchi/agent.py
+++ b/pwnagotchi/agent.py
@@ -8,6 +8,7 @@ import _thread
 import pwnagotchi
 import pwnagotchi.utils as utils
 import pwnagotchi.plugins as plugins
+from pwnagotchi.ui.web.server import Server
 from pwnagotchi.automata import Automata
 from pwnagotchi.log import LastSession
 from pwnagotchi.bettercap import Client
@@ -18,8 +19,6 @@ RECOVERY_DATA_FILE = '/root/.pwnagotchi-recovery'
 
 
 class Agent(Client, Automata, AsyncAdvertiser, AsyncTrainer):
-    INSTANCE = None
-
     def __init__(self, view, config, keypair):
         Client.__init__(self, config['bettercap']['hostname'],
                         config['bettercap']['scheme'],
@@ -36,6 +35,8 @@ class Agent(Client, Automata, AsyncAdvertiser, AsyncTrainer):
         self._supported_channels = utils.iface_channels(config['main']['iface'])
         self._view = view
         self._view.set_agent(self)
+        self._web_ui = Server(self, self.config['ui']['display'])
+
         self._access_points = []
         self._last_pwnd = None
         self._history = {}
@@ -46,8 +47,6 @@ class Agent(Client, Automata, AsyncAdvertiser, AsyncTrainer):
         if not os.path.exists(config['bettercap']['handshakes']):
             os.makedirs(config['bettercap']['handshakes'])
 
-        Agent.INSTANCE = self
-
     def config(self):
         return self._config
 
diff --git a/pwnagotchi/ui/display.py b/pwnagotchi/ui/display.py
index 8abb1d7..751f19e 100644
--- a/pwnagotchi/ui/display.py
+++ b/pwnagotchi/ui/display.py
@@ -4,8 +4,6 @@ import threading
 
 import pwnagotchi.plugins as plugins
 import pwnagotchi.ui.hw as hw
-import pwnagotchi.ui.web as web
-from pwnagotchi.ui.web.server import Server
 from pwnagotchi.ui.view import View
 
 
@@ -16,7 +14,6 @@ class Display(View):
 
         self._enabled = config['enabled']
         self._rotation = config['rotation']
-        self._webui = Server(config)
 
         self.init_display()
 
@@ -28,6 +25,9 @@ class Display(View):
         )
         self._render_thread_instance.start()
 
+    def set_ready(self):
+        self._webui.start()
+
     def is_inky(self):
         return self._implementation.name == 'inky'
 
@@ -90,7 +90,6 @@ class Display(View):
             self._implementation.render(self._canvas_next)
 
     def _on_view_rendered(self, img):
-        web.update_frame(img)
         try:
             if self._config['ui']['display']['video']['on_frame'] != '':
                 os.system(self._config['ui']['display']['video']['on_frame'])
diff --git a/pwnagotchi/ui/view.py b/pwnagotchi/ui/view.py
index ffd3ae2..36d280c 100644
--- a/pwnagotchi/ui/view.py
+++ b/pwnagotchi/ui/view.py
@@ -10,6 +10,7 @@ import pwnagotchi.utils as utils
 import pwnagotchi.plugins as plugins
 from pwnagotchi.voice import Voice
 
+import pwnagotchi.ui.web as web
 import pwnagotchi.ui.fonts as fonts
 import pwnagotchi.ui.faces as faces
 from pwnagotchi.ui.components import *
@@ -369,6 +370,8 @@ class View(object):
                 for key, lv in self._state.items():
                     lv.draw(self._canvas, drawer)
 
+                web.update_frame(self._canvas)
+
                 for cb in self._render_cbs:
                     cb(self._canvas)
 
diff --git a/pwnagotchi/ui/web/handler.py b/pwnagotchi/ui/web/handler.py
index d0db0e2..c1b47a8 100644
--- a/pwnagotchi/ui/web/handler.py
+++ b/pwnagotchi/ui/web/handler.py
@@ -8,7 +8,6 @@ os.environ['WERKZEUG_RUN_MAIN'] = 'true'
 
 import pwnagotchi
 import pwnagotchi.ui.web as web
-from pwnagotchi.agent import Agent
 from pwnagotchi import plugins
 
 from flask import send_file
@@ -87,7 +86,8 @@ STATUS_PAGE = """<html>
 
 
 class Handler:
-    def __init__(self, app):
+    def __init__(self, agent, app):
+        self._agent = agent
         self._app = app
         self._app.add_url_rule('/', 'index', self.index)
         self._app.add_url_rule('/ui', 'ui', self.ui)
@@ -102,7 +102,7 @@ class Handler:
 
     def index(self):
         return render_template_string(INDEX, title=pwnagotchi.name(),
-                                      other_mode='AUTO' if Agent.INSTANCE.mode == 'manual' else 'MANU')
+                                      other_mode='AUTO' if self._agent.mode == 'manual' else 'MANU')
 
     def plugins(self, name, subpath):
         if name is None:
diff --git a/pwnagotchi/ui/web/server.py b/pwnagotchi/ui/web/server.py
index 5bc1fef..7a54f2e 100644
--- a/pwnagotchi/ui/web/server.py
+++ b/pwnagotchi/ui/web/server.py
@@ -15,12 +15,12 @@ from pwnagotchi.ui.web.handler import Handler
 
 
 class Server:
-    def __init__(self, config):
+    def __init__(self, agent, config):
         self._enabled = config['video']['enabled']
         self._port = config['video']['port']
         self._address = config['video']['address']
         self._origin = None
-
+        self._agent = agent
         if 'origin' in config['video']:
             self._origin = config['video']['origin']
 
@@ -36,7 +36,7 @@ class Server:
                 CORS(app, resources={r"*": {"origins": self._origin}})
 
             CSRFProtect(app)
-            Handler(app)
+            Handler(agent, app)
 
             app.run(host=self._address, port=self._port, debug=False)
         else: