|
| 1 | +import os, sys, subprocess, time |
| 2 | +from ui_pycode.main import Ui_Main |
| 3 | +from info import * |
| 4 | +from db_connect import * |
| 5 | +from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox |
| 6 | + |
| 7 | + |
| 8 | +class Main(QMainWindow, Ui_Main): |
| 9 | + def __init__(self): |
| 10 | + super().__init__() |
| 11 | + self.setupUi(self) |
| 12 | + self.setWindowTitle(AppName) |
| 13 | + self.switch_general() |
| 14 | + self.set_username() |
| 15 | + |
| 16 | + self.btn_general.clicked.connect(self.switch_general) |
| 17 | + self.btn_username.clicked.connect(self.switch_username) |
| 18 | + self.btn_connect.clicked.connect(self.connect) |
| 19 | + |
| 20 | + def connect_db(self): |
| 21 | + with Session() as session: |
| 22 | + user = session.query(Connection).first() |
| 23 | + try: |
| 24 | + return user |
| 25 | + except Exception as e: |
| 26 | + self.get_error(f"\n{e}") |
| 27 | + |
| 28 | + def switch_general(self): |
| 29 | + self.stackedWidget.setCurrentIndex(0) |
| 30 | + user = self.connect_db() |
| 31 | + self.input_port.setText(user.port_number) |
| 32 | + |
| 33 | + def switch_username(self): |
| 34 | + self.stackedWidget.setCurrentIndex(1) |
| 35 | + self.input_username.setFocus() |
| 36 | + |
| 37 | + def set_username(self): |
| 38 | + self.input_username.clear() |
| 39 | + |
| 40 | + user = self.connect_db() |
| 41 | + if user.log_username is None: |
| 42 | + username = user.default_username |
| 43 | + else: |
| 44 | + username = user.log_username |
| 45 | + |
| 46 | + self.input_username.setText(username) |
| 47 | + |
| 48 | + # Update username |
| 49 | + def UpdateUserAndPort(self): |
| 50 | + username = self.input_username.text().strip().upper() |
| 51 | + port_number = self.input_port.text() |
| 52 | + |
| 53 | + if username: |
| 54 | + with Session() as session: |
| 55 | + user = session.query(Connection).first() |
| 56 | + if self.checkbox_save_me.isChecked(): |
| 57 | + user.default_username = username |
| 58 | + |
| 59 | + user.log_username = username |
| 60 | + user.port_number = port_number |
| 61 | + session.commit() |
| 62 | + else: |
| 63 | + self.get_error(f"\nEnter Username!") |
| 64 | + |
| 65 | + |
| 66 | + # Control ip and password and connect remote desktop |
| 67 | + def connect(self): |
| 68 | + self.UpdateUserAndPort() |
| 69 | + |
| 70 | + user = self.connect_db() |
| 71 | + if user.log_username is None: |
| 72 | + username = user.default_username |
| 73 | + else: |
| 74 | + username = user.log_username |
| 75 | + |
| 76 | + ip_address = self.input_ip_address.text() |
| 77 | + port_number = self.input_port.text() |
| 78 | + password = self.input_password.text() |
| 79 | + |
| 80 | + if not ip_address: |
| 81 | + self.get_error(f"\nEnter IP address!") |
| 82 | + elif not password: |
| 83 | + self.get_error(f"\nEnter password!") |
| 84 | + else: |
| 85 | + self.input_ip_address.clear() |
| 86 | + self.input_password.clear() |
| 87 | + if port_number: |
| 88 | + complete_address = ip_address+':'+port_number |
| 89 | + else: |
| 90 | + complete_address = ip_address |
| 91 | + |
| 92 | + self.remote_desktop_connection(ip_address, username, password, complete_address) |
| 93 | + |
| 94 | + |
| 95 | + def remote_desktop_connection(self, ip_address, username, password, complete_address): |
| 96 | + # Create the RDP file path |
| 97 | + rdp_file_path = os.path.join(os.environ['HOMEPATH'], 'Documents', 'local.rdp') |
| 98 | + |
| 99 | + # Create the RDP file with local resource settings |
| 100 | + with open(rdp_file_path, 'w') as rdp_file: |
| 101 | + rdp_file.write(f"full address:s:{complete_address}\n") |
| 102 | + rdp_file.write("redirectprinters:i:1\n") # Enable printer redirection |
| 103 | + rdp_file.write("redirectclipboard:i:1\n") # Enable clipboard redirection |
| 104 | + rdp_file.write("redirectcomports:i:1\n") # Enable COM port redirection |
| 105 | + |
| 106 | + credentials = f'cmdkey /generic:{ip_address} /user:{username} /pass:{password}' |
| 107 | + connect = f'mstsc "{rdp_file_path}"' |
| 108 | + |
| 109 | + # Commands to clean up after connection |
| 110 | + delete_connection = f'cmdkey /delete:{ip_address}' |
| 111 | + delete_connection_term = f'cmdkey /delete:TERMSRV/{ip_address}' |
| 112 | + delete_TSC = r'reg delete "HKEY_CURRENT_USER\Software\Microsoft\Terminal Server Client" /f' |
| 113 | + delete_hidden_default_rdp = f'del /ah "{os.path.join(os.environ["HOMEPATH"], "Documents", "Default.rdp")}"' |
| 114 | + delete_default_rdp = f'del "{os.path.join(os.environ["HOMEPATH"], "Documents", "Default.rdp")}"' |
| 115 | + delete_default_rdp = f'del "{os.path.join(os.environ["HOMEPATH"], "Documents", "local.rdp")}"' |
| 116 | + |
| 117 | + # List of commands to execute |
| 118 | + commands = [credentials, connect, delete_connection, delete_connection_term, delete_TSC, |
| 119 | + delete_hidden_default_rdp, delete_default_rdp] |
| 120 | + |
| 121 | + self.close() |
| 122 | + |
| 123 | + for command in commands: |
| 124 | + try: |
| 125 | + proc = subprocess.Popen(command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
| 126 | + proc.communicate() |
| 127 | + time.sleep(0.08) |
| 128 | + except Exception as e: |
| 129 | + self.get_error(f"\n{e}") |
| 130 | + |
| 131 | + |
| 132 | + # Show Error |
| 133 | + def get_error(self, error): |
| 134 | + QMessageBox.critical(self, AppName, f"{error}") |
| 135 | + |
| 136 | + |
| 137 | +if __name__ == "__main__": |
| 138 | + app = QApplication(sys.argv) |
| 139 | + main_window = Main() |
| 140 | + main_window.show() |
| 141 | + sys.exit(app.exec_()) |
0 commit comments