State Repo is the one to flip on systemwide UI. Which forces state to match visuals
This commit is contained in:
parent
b6275269b5
commit
e73512770c
7 changed files with 77 additions and 43 deletions
|
|
@ -56,6 +56,19 @@ class SystemStateController:
|
|||
Notifications.emit(EventType.SYSTEM_DISABLE, profile_id)
|
||||
|
||||
|
||||
def sync_system_state(current_state: SystemState):
|
||||
"""Update the global state to reflect the inserted model values"""
|
||||
state_repo = ProfileStateRepository()
|
||||
state_repo.sync_system_state(current_state)
|
||||
|
||||
|
||||
def verify(profile_id: int) -> bool:
|
||||
"""Flip on Verification (which updates the UI), if it passes the check on state"""
|
||||
state_repo = ProfileStateRepository()
|
||||
return state_repo.verify_its_up(profile_id=profile_id)
|
||||
|
||||
|
||||
|
||||
# old version:
|
||||
# ProfileStateRepository().global_state.systemwide_profile_id = None
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
from core.observers.Notifications import Notifications, EventType
|
||||
from core.models.system.SystemState import SystemState
|
||||
from core.models.session.SessionState import SessionState
|
||||
from core.models.enum_models import ProtocolChoice
|
||||
|
|
@ -13,7 +14,7 @@ class GlobalState:
|
|||
dns_set: bool = False
|
||||
systemwide_profile_id: Optional[int] = None
|
||||
session_profile_ids: list[int] = field(default_factory=list)
|
||||
verified: bool = False
|
||||
verified_working: bool = False
|
||||
changed_port: bool = False
|
||||
|
||||
class ProfileStateRepository:
|
||||
|
|
@ -44,6 +45,7 @@ class ProfileStateRepository:
|
|||
self.global_state.firewalled = False
|
||||
self.global_state.dns_set = False
|
||||
self.global_state.systemwide_active = False
|
||||
self.global_state.verified_working = False
|
||||
|
||||
|
||||
def sync_system_state(self, state: SystemState):
|
||||
|
|
@ -56,6 +58,22 @@ class ProfileStateRepository:
|
|||
self.global_state.systemwide_active = True
|
||||
|
||||
|
||||
def verify_its_up(self, profile_id: int) -> bool:
|
||||
"""Flips on Verification, if the rest of the state matches"""
|
||||
|
||||
if not self.global_state.systemwide_active:
|
||||
return False
|
||||
|
||||
if self.global_state.systemwide_profile_id != profile_id:
|
||||
return False
|
||||
|
||||
self.global_state.verified_working = True
|
||||
notification = "Connected."
|
||||
Notifications.emit(EventType.STATUS_UPDATE, notification)
|
||||
Notifications.emit(EventType.SYSTEM_ENABLE, profile_id)
|
||||
return True
|
||||
|
||||
|
||||
def sync_session_added(self, profile_id: int):
|
||||
"""Called by SessionStateController to update memory cache."""
|
||||
if profile_id not in self.global_state.session_profile_ids:
|
||||
|
|
|
|||
|
|
@ -25,18 +25,23 @@ from functools import partial
|
|||
SINGBOX_PROTOCOLS = (ProtocolChoice.HYSTERIA2, ProtocolChoice.VLESS)
|
||||
|
||||
|
||||
def verify_systemwide_state(current_state: SystemState) -> bool:
|
||||
"""Confirm a valid connection, to update GlobalState (for UI)"""
|
||||
state_repo = ProfileStateRepository()
|
||||
state_repo.sync_system_state(current_state)
|
||||
state_repo.verified = True
|
||||
Notifications.emit(EventType.SYSTEM_ENABLE, current_state.profile_id)
|
||||
|
||||
def is_systemwide_on() -> bool:
|
||||
"""Returns a boolean if systemwide is connected. True is Yes, False is No"""
|
||||
state_repo = ProfileStateRepository()
|
||||
if not state_repo.global_state.verified_working:
|
||||
return False
|
||||
|
||||
return state_repo.global_state.systemwide_active
|
||||
|
||||
|
||||
def verified_system_profile_id() -> int|None:
|
||||
state_repo = ProfileStateRepository()
|
||||
if not state_repo.global_state.verified_working:
|
||||
return False
|
||||
else:
|
||||
return state_repo.global_state.systemwide_profile_id
|
||||
|
||||
|
||||
def connected_system_profile_id() -> int|None:
|
||||
"""Returns a single number of the connected system profile"""
|
||||
state_repo = ProfileStateRepository()
|
||||
|
|
@ -167,8 +172,11 @@ def coordinate_evaluation(connection_observer: ConnectionObserver) -> Result:
|
|||
|
||||
if firewall_dns_in_line_with_expectations(firewalled=firewall_on, dns_set=dns_on, protocol=protocol):
|
||||
logger.info("IP address is connected and firewall & DNS expectations match reality. Update/Verify Global State that it's on")
|
||||
verify_systemwide_state(current_state=json_state) # this notifies the UI
|
||||
return Result(valid=True)
|
||||
SystemStateController.sync_system_state(current_state=json_state)
|
||||
if SystemStateController.verify(profile_id=profile_id): # this notifies the UI:
|
||||
return Result(valid=True)
|
||||
else:
|
||||
return Result(valid=False, error_type=ResultError.STATE, message="Failed to verify the systemwide state.")
|
||||
|
||||
logger.error("Current connection did not meet the criteria to continue to use it as is.")
|
||||
return establish_connection(profile, ignore={}, connection_observer=connection_observer)
|
||||
|
|
|
|||
|
|
@ -27,7 +27,8 @@ class ResultError(Enum):
|
|||
EXPIRED_SUB = "expired_sub"
|
||||
ACTIVE_BILLING = "active_billing"
|
||||
BILLING_CONFLICT = "billing_conflict"
|
||||
STATE_CONFLICT = "state_conflict"
|
||||
STATE_CONFLICT = "state_conflict" # with actions to kill
|
||||
STATE = "state"
|
||||
ZOMBIE_CONNECTION = "zombie_connection"
|
||||
TICKET = "ticket"
|
||||
NO_TICKET = "no_ticket"
|
||||
|
|
|
|||
|
|
@ -340,10 +340,10 @@ def start_singbox(
|
|||
return validation
|
||||
|
||||
# 7. Success
|
||||
if connection_observer:
|
||||
connection_observer.notify("connected", "Connected.")
|
||||
# if connection_observer:
|
||||
# connection_observer.notify("connected", "Connected.")
|
||||
|
||||
Notifications.emit(EventType.SYSTEM_ENABLE, profile_id)
|
||||
Notifications.emit(EventType.STATUS_UPDATE, "Connected.")
|
||||
# Notifications.emit(EventType.SYSTEM_ENABLE, profile_id)
|
||||
# Notifications.emit(EventType.STATUS_UPDATE, "Connected.")
|
||||
|
||||
return Result(valid=True, data=process_id)
|
||||
|
|
|
|||
|
|
@ -350,6 +350,14 @@ def __establish_system_connection(
|
|||
did_dns_work = False
|
||||
logger.info("Skipping DNS setup, because it's off in settings")
|
||||
|
||||
# ============= SETUP STATE =============
|
||||
logger.info("Setting State JSON..")
|
||||
current_state = SystemStateController.create(
|
||||
profile_id=profile.id,
|
||||
firewalled=firewall_setting,
|
||||
dns_set=did_dns_work,
|
||||
protocol=ProtocolChoice.WIREGUARD
|
||||
)
|
||||
|
||||
# ============= TESTING =============
|
||||
logger.info("If we made it this far, we have nmcli on, ipv6 sinkhole on, firewall on.")
|
||||
|
|
@ -365,16 +373,16 @@ def __establish_system_connection(
|
|||
notification = "Test Failed."
|
||||
Notifications.emit(EventType.STATUS_UPDATE, notification)
|
||||
logger.error("Returning that the test failed..")
|
||||
return test_result
|
||||
return test_result.wrap(f"Firewall Status: {firewall_tracker}. DNS status: {did_dns_work}")
|
||||
|
||||
# ============= UPDATE STATE =============
|
||||
if dns_setting and not did_dns_work:
|
||||
current_state.firewalled = firewall_tracker
|
||||
current_state.dns_set = did_dns_work
|
||||
SystemStateController.update_or_create(current_state)
|
||||
notification = "DNS Set Did Not Work."
|
||||
Notifications.emit(EventType.STATUS_UPDATE, notification)
|
||||
|
||||
# ============= SETUP STATE =============
|
||||
logger.info("Setting State JSON..")
|
||||
current_state = SystemStateController.create(
|
||||
profile_id=profile.id,
|
||||
firewalled=firewall_setting,
|
||||
dns_set=did_dns_work,
|
||||
protocol=ProtocolChoice.WIREGUARD
|
||||
)
|
||||
return Result(valid=True, data=current_state)
|
||||
|
||||
|
||||
|
|
@ -537,13 +545,3 @@ def establish_system_connection(
|
|||
else:
|
||||
logger.info("entering final loop")
|
||||
return _conclude_loop_failure()
|
||||
|
||||
|
||||
|
||||
# ============= UPDATE STATE =============
|
||||
# if dns_setting and not did_dns_work:
|
||||
# current_state.firewalled = firewall_tracker
|
||||
# current_state.dns_set = did_dns_work
|
||||
# SystemStateController.update_or_create(current_state)
|
||||
# notification = "DNS Set Did Not Work."
|
||||
# Notifications.emit(EventType.STATUS_UPDATE, notification)
|
||||
|
|
|
|||
|
|
@ -161,16 +161,12 @@ def enable_profile(
|
|||
logger.info(f"Exiting establish connection with {connection_result}")
|
||||
|
||||
if connection_result.valid:
|
||||
# if profile_observer is not None:
|
||||
# profile_observer.notify('enabled', profile)
|
||||
|
||||
Notifications.emit(EventType.SYSTEM_ENABLE, profile.id)
|
||||
notification = "Connected."
|
||||
Notifications.emit(EventType.STATUS_UPDATE, notification)
|
||||
return Result(valid=True, data=profile.type)
|
||||
|
||||
if SystemStateController.verify(profile_id=profile.id):
|
||||
return Result(valid=True, data=profile.type)
|
||||
else:
|
||||
return connection_result.wrap("There's a state-mismatch bug. Although the connection_result claims it's on, the StateRepo rejected verification.")
|
||||
else:
|
||||
return connection_result
|
||||
return connection_result.wrap("Did NOT verify state or turn on notification")
|
||||
|
||||
except FirewallError:
|
||||
return Result(valid=False, error_type=ResultError.FIREWALL, message="Firewall Error.")
|
||||
|
|
|
|||
Loading…
Reference in a new issue