Skip to content

Commit

Permalink
ref: remove nubia
Browse files Browse the repository at this point in the history
  • Loading branch information
sepgh committed Feb 25, 2023
1 parent c62deaf commit af830b8
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 82 deletions.
1 change: 0 additions & 1 deletion clients/windows/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def __init__(self):
self.configuration.get("username", "unknown"),
self.configuration.get("password", "unknown"),
self.configuration.get("host_key", "unknown"),
lambda: self.stop()
)

def get_server(self):
Expand Down
16 changes: 10 additions & 6 deletions clients/windows/client/assets_loader.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

import requests
from termcolor import cprint


def download(url: str):
Expand All @@ -13,15 +14,15 @@ def download(url: str):

r = requests.get(url, stream=True)
if r.ok:
print("saving to", os.path.abspath(file_path))
cprint(f"saving to {os.path.abspath(file_path)}", 'green')
with open(file_path, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024 * 8):
if chunk:
f.write(chunk)
f.flush()
os.fsync(f.fileno())
else: # HTTP status code 4XX/5XX
print("Download failed: status code {}\n{}".format(r.status_code, r.text))
cprint("Download failed: status code {}\n{}".format(r.status_code, r.text), 'red')


assets = [
Expand Down Expand Up @@ -49,16 +50,19 @@ def download(url: str):


def validate_assets():

called_once = False
for asset in assets:
if not os.path.exists(
os.path.join(os.path.relpath("assets"), asset["name"])
):
print(f"Could not find {asset['name']} in assets directory.")
called_once = True
cprint(f"Could not find {asset['name']} in assets directory.", 'yellow')
if 'handler' not in asset or asset.get('handler') is None:
print("I can't download it for you. This may break the application. Skipping.")
cprint("I can't download it for you. This may break the application. Skipping.")
continue
else:
print(f"Downloading {asset['name']}")
cprint(f"Downloading {asset['name']}")
asset['handler'](asset)

if not called_once:
cprint("All assets are already loaded", "green")
26 changes: 0 additions & 26 deletions clients/windows/client/commands.py

This file was deleted.

47 changes: 14 additions & 33 deletions clients/windows/client/plink.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@


class PLink:
max_retries = 5

def __init__(
self,
Expand All @@ -18,7 +17,6 @@ def __init__(
username: str,
password: str,
host_key: str,
on_interrupt=None
):
self.socks_port = socks_port
self.server = server
Expand All @@ -28,57 +26,40 @@ def __init__(
self.host_key = host_key
self.process_thread = None
self.subprocess = None
self.current_retries = 0
self.stopped = False
self.on_interrupt = on_interrupt

def get_process_path(self):
return os.path.join(
os.path.relpath("assets"),
"plink.exe"
)

def set_process(self, process):
self.subprocess = process

def _on_exit(self):
if self.current_retries != self.max_retries and not self.stopped:
cprint("Disconnected from server. Retrying ...", "red")
self.start()
elif not self.stopped:
self.stop()
self.on_interrupt()

def start(self):
self.stopped = False
if self.process_thread is not None:
return

si = subprocess.STARTUPINFO()
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW

self.process_thread = utils.popen_and_call(
self._on_exit,
self.set_process,
[
f" -hostkey {self.host_key} -ssh {self.server} -D {self.socks_port}"
f" -l {self.username} -P {self.server_port} -no-antispoof -pw {self.password}"
f" -N"
],
{
"executable": f"{self.get_process_path()}",
"stdout": subprocess.PIPE,
"stderr": subprocess.DEVNULL,
"startupinfo": si,
}
self.subprocess = subprocess.Popen(
f"{self.get_process_path()} -hostkey {self.host_key} -ssh {self.server} -D {self.socks_port}"
f" -l {self.username} -P {self.server_port} -no-antispoof -pw {self.password}"
f" -N",
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
# startupinfo=si
)
cprint(f"Socks proxy available on {self.socks_port}", 'yellow')
self.stopped = False
setup_windows_proxy(self.socks_port)

def stop(self):
self.stopped = True
current_stop_status = self.stopped
if self.subprocess is not None:
self.stopped = True
self.subprocess.terminate()
self.subprocess.wait()
self.subprocess = None
drop_windows_proxy()
self.process_thread = None
if not current_stop_status:
drop_windows_proxy()
self.process_thread = None
7 changes: 3 additions & 4 deletions clients/windows/client/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@
import threading


def popen_and_call(on_exit, on_proc, popen_args, popen_kwargs):
def popen_and_call(on_proc, popen_args, popen_kwargs):
"""
Runs the given args in a subprocess.Popen, and then calls the function
on_exit when the subprocess completes.
on_exit is a callable object, and popen_args is a list/tuple of args that
would give to subprocess.Popen.
"""
def run_in_thread(on_exit, on_proc, popen_args, popen_kwargs):
def run_in_thread(on_proc, popen_args, popen_kwargs):
proc = subprocess.Popen(*popen_args, **popen_kwargs)
on_proc(proc)
proc.wait()
on_exit()
return
thread = threading.Thread(target=run_in_thread, args=(on_exit, on_proc, popen_args, popen_kwargs))
thread = threading.Thread(target=run_in_thread, args=(on_exit, on_proc, popen_args, popen_kwargs), daemon=True)
thread.start()
# returns immediately after the thread starts
return thread
55 changes: 44 additions & 11 deletions clients/windows/mooshak.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import sys
import os
from nubia import Nubia, Options
import signal
import sys
import threading
import time

from termcolor import cprint
from client import rights
import client.commands

from client import rights, MooshakClient
from client.assets_loader import validate_assets

os.system('color')

Expand All @@ -21,12 +25,41 @@
cprint("Run this program as administrator", "red")
exit(0)

cprint(BANNER, "light_cyan")
if len(sys.argv) < 2:
print("Argument missing. Choose between: load_assets, connect, disconnect")
exit(0)

command = sys.argv[1]

# Connect
if command == "connect":
cprint(BANNER, "light_cyan")

shell = Nubia(
name="Mooshak Proxy CLI",
command_pkgs=client,
options=Options(persistent_history=False, auto_execute_single_suggestions=False),
)
sys.exit(shell.run())
client = MooshakClient()

def signal_handler(signal, frame):
cprint('Disconnecting gracefully ...', 'green')
client.stop()
cprint('Disconnected', 'red')
sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)
client.start()
cprint('Connected', 'green')

cprint('Press Ctrl+C to disconnect')
while True:
try:
time.sleep(3)
except InterruptedError:
signal.raise_signal(signal.SIGINT)

elif command == "disconnect":
MooshakClient().stop()

elif command == "load_assets":
validate_assets()

else:
cprint("Unknown command.")
exit(0)
2 changes: 1 addition & 1 deletion clients/windows/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
requests==2.28.2
python-nubia
termcolor

0 comments on commit af830b8

Please sign in to comment.