diff --git a/dist/tls-0.1.0-py3-none-any.whl b/dist/tls-0.1.0-py3-none-any.whl new file mode 100644 index 0000000..34b3bb5 Binary files /dev/null and b/dist/tls-0.1.0-py3-none-any.whl differ diff --git a/dist/tls-0.1.0.tar.gz b/dist/tls-0.1.0.tar.gz new file mode 100644 index 0000000..af5abcf Binary files /dev/null and b/dist/tls-0.1.0.tar.gz differ diff --git a/setup.py b/setup.py index e651d1b..5272ef6 100644 --- a/setup.py +++ b/setup.py @@ -12,11 +12,21 @@ long_description=long_description, long_description_content_type="text/markdown", url="https://github.com/Noriskky/tls", + keywords="cli", packages=setuptools.find_packages(), classifiers=[ "Programming Language :: Python :: 3", "License :: OSI Approved :: GNU General Public License v3 (GPLv3)", "Operating System :: POSIX :: Linux", ], + install_requires=[ + "requests", + "tqdm" + ], python_requires='>=3.6', -) \ No newline at end of file + entry_points={ + "console_scripts": [ + "tls=tls.main:main", + ], + }, +) diff --git a/src/main.py b/src/main.py deleted file mode 100755 index 75054ce..0000000 --- a/src/main.py +++ /dev/null @@ -1,121 +0,0 @@ -import subprocess -import requests -import os -import argparse -from tqdm import tqdm -import shutil -import uuid -import socket - -def get_latest_alpine_version(): - try: - # Construct the URL for the latest version of Alpine Linux - latest_version_url = "https://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/x86_64/" - response = requests.get(latest_version_url) - response.raise_for_status() - # Extract the latest version number from the response content - version = response.text.split('alpine-minirootfs-')[1].split('-x86_64.tar.gz')[0] - return version - except Exception as e: - print(f"An error occurred while fetching the latest version: {e}") - return None - -def start_temp_alpine_shell(hostname, command=None, directory=None): - try: - if directory: - working_directory = os.path.abspath(os.path.join(os.getcwd(), directory)) - else: - working_directory = f"/var/tmp/alpine_temp_{uuid.uuid4().hex}" - - if not os.path.exists(working_directory): - os.makedirs(working_directory) - latest_version = get_latest_alpine_version() - stored_version_path = "/tmp/alpine_version.txt" - if latest_version: - # Check if there is a previously stored version - if os.path.exists(stored_version_path): - with open(stored_version_path, "r") as file: - stored_version = file.read().strip() - # Download the latest release of Alpine Linux - print(f"\033[94mDownloading Alpine Linux version {latest_version}...\033[0m") - download_url = f"https://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/x86_64/alpine-minirootfs-{latest_version}-x86_64.tar.gz" - with requests.get(download_url, stream=True) as response: - response.raise_for_status() - total_size = int(response.headers.get('content-length', 0)) - with open(f"{working_directory}/alpine.tar.gz", "wb") as file, tqdm( - desc="Progress", - total=total_size, - unit='B', - unit_scale=True, - unit_divisor=1024, - ) as bar: - for data in response.iter_content(chunk_size=1024): - file.write(data) - bar.update(len(data)) - - # Extract the Alpine Linux filesystem - print("\033[94mExtracting Alpine Linux filesystem...\033[0m") - subprocess.run(["sudo", "tar", "-xzf", f"{working_directory}/alpine.tar.gz", "-C", working_directory]) - - # Save the downloaded version to a file - with open(stored_version_path, "w") as file: - file.write(latest_version) - - # Add package repositories - print("\033[94mAdding package repositories...\033[0m") - subprocess.run(["sudo", "sh", "-c", f"echo 'http://dl-cdn.alpinelinux.org/alpine/latest-stable/main' >> {working_directory}/etc/apk/repositories"]) - subprocess.run(["sudo", "sh", "-c", f"echo 'http://dl-cdn.alpinelinux.org/alpine/latest-stable/community' >> {working_directory}/etc/apk/repositories"]) - - # Configure network - print("\033[94mConfiguring network...\033[0m") - subprocess.run(["sudo", "cp", "/etc/resolv.conf", f"{working_directory}/etc/resolv.conf"]) # Copy host's resolv.conf to chroot - subprocess.run(["sudo", "mount", "-t", "proc", "/proc", f"{working_directory}/proc"]) - subprocess.run(["sudo", "mount", "--rbind", "/sys", f"{working_directory}/sys"]) - subprocess.run(["sudo", "mount", "--rbind", "/dev", f"{working_directory}/dev"]) - - # Change the hostname - print("\033[94mChanging hostname...\033[0m") - subprocess.run(["sudo", "sh", "-c", f"echo '{hostname}' > {working_directory}/etc/hostname"]) - - # Execute the specified command if provided - if command: - print(f"\033[94mExecuting command: {command}...\033[0m") - subprocess.run(["sudo", "chroot", working_directory, "/bin/sh", "-c", command]) - else: - # Print welcome message - #ip_address = socket.gethostbyname(hostname) - os.system("clear") - print(f"\033[94mWelcome to the Temp Alpine Linux Shell!\033[0m") - print(f"\033[94mYou are now in the Alpine Linux environment.\033[0m") - print(f"\033[94m * Hostname: {hostname}\033[0m") - #print(f"\033[94m * IP Address: {ip_address}\033[0m") - print(f"\033[94m * Working Directory: {working_directory}\033[0m") - print("") - # Execute chroot command to change root directory to the extracted Alpine Linux environment - subprocess.run(["sudo", "chroot", working_directory, "/bin/sh", "-l"]) - - except FileNotFoundError: - print("Required commands are not available. Make sure 'wget', 'tar', 'chroot', and 'bash' commands are installed.") - except Exception as e: - print(f"An error occurred: {e}") - #finally: - # if not persistent: - # # Clear the temporary directory after exiting the shell - # if os.path.exists(working_directory): - # if - # shutil.rmtree(working_directory) - # print(f"\033[94mTemp directory cleared.\033[0m") - -if __name__ == "__main__": - parser = argparse.ArgumentParser(description="Start a temporary Alpine Linux shell") - parser.add_argument("--hostname", "-hn", default="alpine", help="Specify the hostname for the Alpine Linux environment") - parser.add_argument("--command", "-c", help="Command to execute in the Alpine Linux environment") - parser.add_argument("--directory", "-d", help="Where the folder of the Shell should be") - #parser.add_argument("--persistent", "-p", help="A Flag to make the Shell persistent. Make sure to specify a directory.", action="store_true") - args = parser.parse_args() - - #if args.persistent and args.directory == None: - # print("Make sure to specify a directory.") - # quit() - - start_temp_alpine_shell(args.hostname, args.command, args.directory) \ No newline at end of file diff --git a/tls.egg-info/PKG-INFO b/tls.egg-info/PKG-INFO new file mode 100644 index 0000000..66576de --- /dev/null +++ b/tls.egg-info/PKG-INFO @@ -0,0 +1,19 @@ +Metadata-Version: 2.1 +Name: tls +Version: 0.1.0 +Summary: Wrapper around Chroot to make it easy to use temporary Linux Shells. +Home-page: https://github.com/Noriskky/tls +Author: Noriskky +Author-email: Noriskky44@proton.me +Keywords: cli +Classifier: Programming Language :: Python :: 3 +Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3) +Classifier: Operating System :: POSIX :: Linux +Requires-Python: >=3.6 +Description-Content-Type: text/markdown +License-File: LICENSE +Requires-Dist: requests +Requires-Dist: tqdm + +# TLS +TLS - Temporary Linux(Alpine) Shell/System diff --git a/tls.egg-info/SOURCES.txt b/tls.egg-info/SOURCES.txt new file mode 100644 index 0000000..ba3dddb --- /dev/null +++ b/tls.egg-info/SOURCES.txt @@ -0,0 +1,11 @@ +LICENSE +README.md +setup.py +tls/__init__.py +tls/main.py +tls.egg-info/PKG-INFO +tls.egg-info/SOURCES.txt +tls.egg-info/dependency_links.txt +tls.egg-info/entry_points.txt +tls.egg-info/requires.txt +tls.egg-info/top_level.txt \ No newline at end of file diff --git a/tls.egg-info/dependency_links.txt b/tls.egg-info/dependency_links.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/tls.egg-info/dependency_links.txt @@ -0,0 +1 @@ + diff --git a/tls.egg-info/entry_points.txt b/tls.egg-info/entry_points.txt new file mode 100644 index 0000000..7a00df8 --- /dev/null +++ b/tls.egg-info/entry_points.txt @@ -0,0 +1,2 @@ +[console_scripts] +tls = tls.main:main diff --git a/tls.egg-info/requires.txt b/tls.egg-info/requires.txt new file mode 100644 index 0000000..5bb8c66 --- /dev/null +++ b/tls.egg-info/requires.txt @@ -0,0 +1,2 @@ +requests +tqdm diff --git a/tls.egg-info/top_level.txt b/tls.egg-info/top_level.txt new file mode 100644 index 0000000..c7c2277 --- /dev/null +++ b/tls.egg-info/top_level.txt @@ -0,0 +1 @@ +tls diff --git a/tls/__init__.py b/tls/__init__.py new file mode 100644 index 0000000..c80af62 --- /dev/null +++ b/tls/__init__.py @@ -0,0 +1 @@ +from tls import main \ No newline at end of file diff --git a/tls/main.py b/tls/main.py new file mode 100755 index 0000000..30c182f --- /dev/null +++ b/tls/main.py @@ -0,0 +1,117 @@ +import subprocess +import requests +import os +import argparse +from tqdm import tqdm +import shutil +import uuid +import socket +import sys + +def main(): + tls(sys.argv[1:]) + +def get_latest_alpine_version(): + try: + # Construct the URL for the latest version of Alpine Linux + latest_version_url = "https://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/x86_64/" + response = requests.get(latest_version_url) + response.raise_for_status() + # Extract the latest version number from the response content + version = response.text.split('alpine-minirootfs-')[1].split('-x86_64.tar.gz')[0] + return version + except Exception as e: + print(f"An error occurred while fetching the latest version: {e}") + return None +def start_temp_alpine_shell(hostname, command=None, directory=None): + try: + if directory: + working_directory = os.path.abspath(os.path.join(os.getcwd(), directory)) + else: + working_directory = f"/var/tmp/alpine_temp_{uuid.uuid4().hex}" + + if not os.path.exists(working_directory): + os.makedirs(working_directory) + + latest_version = get_latest_alpine_version() + + # Download the latest release of Alpine Linux + print(f"\033[94mDownloading Alpine Linux version {latest_version}...\033[0m") + download_url = f"https://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/x86_64/alpine-minirootfs-{latest_version}-x86_64.tar.gz" + with requests.get(download_url, stream=True) as response: + response.raise_for_status() + total_size = int(response.headers.get('content-length', 0)) + with open(f"{working_directory}/alpine.tar.gz", "wb") as file, tqdm( + desc="Progress", + total=total_size, + unit='B', + unit_scale=True, + unit_divisor=1024, + ) as bar: + for data in response.iter_content(chunk_size=1024): + file.write(data) + bar.update(len(data)) + + # Extract the Alpine Linux filesystem + print("\033[94mExtracting Alpine Linux filesystem...\033[0m") + subprocess.run(["sudo", "tar", "-xzf", f"{working_directory}/alpine.tar.gz", "-C", working_directory]) + + # Add package repositories + print("\033[94mAdding package repositories...\033[0m") + subprocess.run(["sudo", "sh", "-c", f"echo 'http://dl-cdn.alpinelinux.org/alpine/latest-stable/main' >> {working_directory}/etc/apk/repositories"]) + subprocess.run(["sudo", "sh", "-c", f"echo 'http://dl-cdn.alpinelinux.org/alpine/latest-stable/community' >> {working_directory}/etc/apk/repositories"]) + + # Configure network + print("\033[94mConfiguring network...\033[0m") + subprocess.run(["sudo", "cp", "/etc/resolv.conf", f"{working_directory}/etc/resolv.conf"]) # Copy host's resolv.conf to chroot + subprocess.run(["sudo", "mount", "-t", "proc", "/proc", f"{working_directory}/proc"]) + subprocess.run(["sudo", "mount", "--rbind", "/sys", f"{working_directory}/sys"]) + subprocess.run(["sudo", "mount", "--rbind", "/dev", f"{working_directory}/dev"]) + # Change the hostname + print("\033[94mChanging hostname...\033[0m") + subprocess.run(["sudo", "sh", "-c", f"echo '{hostname}' > {working_directory}/etc/hostname"]) + + # Execute the specified command if provided + if command: + print(f"\033[94mExecuting command: {command}...\033[0m") + subprocess.run(["sudo", "chroot", working_directory, "/bin/sh", "-c", command]) + else: + # Print welcome message + #ip_address = socket.gethostbyname(hostname) + os.system("clear") + print(f"\033[94mWelcome to the Temp Alpine Linux Shell!\033[0m") + print(f"\033[94mYou are now in the Alpine Linux environment.\033[0m") + print(f"\033[94m * Hostname: {hostname}\033[0m") + #print(f"\033[94m * IP Address: {ip_address}\033[0m") + print(f"\033[94m * Working Directory: {working_directory}\033[0m") + print("") + # Execute chroot command to change root directory to the extracted Alpine Linux environment + subprocess.run(["sudo", "chroot", working_directory, "/bin/sh", "-l"]) + except FileNotFoundError: + print("Required commands are not available. Make sure 'wget', 'tar', 'chroot', and 'bash' commands are installed.") + except Exception as e: + print(f"An error occurred: {e}") + #finally: + # if not persistent: + # # Clear the temporary directory after exiting the shell + # if os.path.exists(working_directory): + # if + # shutil.rmtree(working_directory) + # print(f"\033[94mTemp directory cleared.\033[0m") + + +class tls: + + def __init__(self, name): + parser = argparse.ArgumentParser(description="Start a temporary Alpine Linux shell") + parser.add_argument("--hostname", "-hn", default="alpine", help="Specify the hostname for the Alpine Linux environment") + parser.add_argument("--command", "-c", help="Command to execute in the Alpine Linux environment") + parser.add_argument("--directory", "-d", help="Where the folder of the Shell should be") + #parser.add_argument("--persistent", "-p", help="A Flag to make the Shell persistent. Make sure to specify a directory.", action="store_true") + args = parser.parse_args() + + #if args.persistent and args.directory == None: + # print("Make sure to specify a directory.") + # quit() + + start_temp_alpine_shell(args.hostname, args.command, args.directory) \ No newline at end of file