import sys import ctypes import os import subprocess from datetime import datetime from PyQt5.QtWidgets import ( QApplication, QWidget, QPushButton, QVBoxLayout, QLabel, QMessageBox, QTextEdit, QFrame, QGridLayout, QStatusBar, QInputDialog ) from PyQt5.QtGui import QFont from PyQt5.QtCore import Qt class HostsManager(QWidget): def __init__(self): super().__init__() self.hosts_file_path = r"C:\Windows\System32\drivers\etc\hosts" self.hosts_folder_path = r"C:\Windows\System32\drivers\etc" self.initUI() def initUI(self): layout = QVBoxLayout() self.titleLabel = QLabel("CGResetHosts") self.titleLabel.setFont(QFont("Arial", 20, QFont.Bold)) self.titleLabel.setAlignment(Qt.AlignCenter) self.titleLabel.setStyleSheet("color: #007BFF;") layout.addWidget(self.titleLabel) self.label = QLabel("Lütfen bir işlem seçin:") self.label.setFont(QFont("Arial", 12)) layout.addWidget(self.label) self.hostsContent = QTextEdit(self) self.hostsContent.setReadOnly(True) self.hostsContent.setPlaceholderText("Hosts dosyasının içeriği burada görünecek...") layout.addWidget(self.hostsContent) buttonFrame = QFrame(self) buttonLayout = QGridLayout() self.viewButton = self.createButton("Görüntüle", self.view_hosts) self.cleanButton = self.createButton("Temizle", self.confirm_clear) self.restoreButton = self.createButton("Geri Yükle", self.restore_hosts) self.openFolderButton = self.createButton("Klasörü Aç", self.open_hosts_folder) self.blockSiteButton = self.createButton("Site Engelle", self.prompt_block_site) self.unblockSiteButton = self.createButton("Site Engeli Kaldır", self.prompt_unblock_site) buttons = [ self.cleanButton, self.viewButton, self.restoreButton, self.openFolderButton, self.blockSiteButton, self.unblockSiteButton ] row, col = 0, 0 for button in buttons: buttonLayout.addWidget(button, row, col) col += 1 if col > 1: col = 0 row += 1 buttonFrame.setLayout(buttonLayout) layout.addWidget(buttonFrame) self.statusBar = QStatusBar(self) layout.addWidget(self.statusBar) self.setLayout(layout) self.setWindowTitle("CGResetHosts") self.setGeometry(300, 300, 500, 450) self.setStyleSheet("background-color: #f0f0f0;") def createButton(self, text, handler): button = QPushButton(text) button.setFont(QFont("Arial", 10)) button.setStyleSheet("background-color: #007BFF; color: white; padding: 10px; border-radius: 5px;") button.clicked.connect(handler) return button def view_hosts(self): try: with open(self.hosts_file_path, 'r') as file: content = file.read() self.hostsContent.setText(content) self.statusBar.showMessage("Hosts dosyası başarıyla görüntülendi.") except FileNotFoundError: self.show_error("Hosts dosyası bulunamadı.") except Exception as e: self.log_error(str(e)) self.show_error(f"Bir hata oluştu: {str(e)}") def confirm_clear(self): reply = QMessageBox.question(self, 'Yedekleme Onayı', 'Hosts dosyasını temizlemeden önce yedeklemek istiyor musunuz?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if reply == QMessageBox.Yes: self.clear_hosts(backup=True) else: self.clear_hosts(backup=False) def clear_hosts(self, backup=True): try: if backup: backup_file_path = self.create_backup() self.statusBar.showMessage(f"Yedek dosyası: {backup_file_path}") with open(self.hosts_file_path, 'w') as file: file.write("") subprocess.run(["ipconfig", "/flushdns"], check=True) QMessageBox.information(self, "Başarılı", "Hosts dosyası başarıyla temizlendi.") self.hostsContent.setText("") self.statusBar.showMessage("Hosts dosyası temizlendi.") except PermissionError: self.show_error("İşlem için yeterli izniniz yok.") except Exception as e: self.log_error(str(e)) self.show_error(f"Bir hata oluştu: {str(e)}") def restore_hosts(self): try: latest_backup = self.get_latest_backup() if latest_backup: with open(latest_backup, 'r') as backup_file: with open(self.hosts_file_path, 'w') as hosts_file: hosts_file.write(backup_file.read()) QMessageBox.information(self, "Başarılı", "Hosts dosyası başarıyla geri yüklendi.") self.view_hosts() self.statusBar.showMessage("Hosts dosyası geri yüklendi.") else: self.show_warning("Geri yüklemek için bir yedek bulunamadı.") except PermissionError: self.show_error("İşlem için yeterli izniniz yok.") except Exception as e: self.log_error(str(e)) self.show_error(f"Bir hata oluştu: {str(e)}") def open_hosts_folder(self): try: os.startfile(self.hosts_folder_path) self.statusBar.showMessage("Hosts klasörü açıldı.") except Exception as e: self.log_error(str(e)) self.show_error(f"Klasör açılamadı: {str(e)}") def prompt_block_site(self): site, ok = QInputDialog.getText(self, 'Site Engelle', 'Engellemek istediğiniz siteyi girin (ör: www.ornek.com):') if ok and site: self.block_site(site) def prompt_unblock_site(self): site, ok = QInputDialog.getText(self, 'Site Engeli Kaldır', 'Kaldırmak istediğiniz siteyi girin (ör: www.ornek.com):') if ok and site: self.unblock_site(site) def block_site(self, site): try: with open(self.hosts_file_path, 'a') as file: file.write(f"127.0.0.1 {site}\n") QMessageBox.information(self, "Başarılı", f"{site} başarıyla engellendi.") self.statusBar.showMessage(f"{site} başarıyla engellendi.") except PermissionError: self.show_error("İşlem için yeterli izniniz yok.") except Exception as e: self.log_error(str(e)) self.show_error(f"Bir hata oluştu: {str(e)}") def unblock_site(self, site): try: with open(self.hosts_file_path, 'r') as file: lines = file.readlines() with open(self.hosts_file_path, 'w') as file: for line in lines: if line.strip("\n") != f"127.0.0.1 {site}": file.write(line) QMessageBox.information(self, "Başarılı", f"{site} engeli kaldırıldı.") self.statusBar.showMessage(f"{site} engeli kaldırıldı.") except PermissionError: self.show_error("İşlem için yeterli izniniz yok.") except Exception as e: self.log_error(str(e)) self.show_error(f"Bir hata oluştu: {str(e)}") def create_backup(self): backup_name = f"hosts_backup_{datetime.now().strftime('%Y%m%d_%H%M%S')}.bak" backup_path = os.path.join(self.hosts_folder_path, backup_name) with open(self.hosts_file_path, 'r') as original_file: with open(backup_path, 'w') as backup_file: backup_file.write(original_file.read()) return backup_path def get_latest_backup(self): backups = [f for f in os.listdir(self.hosts_folder_path) if f.startswith('hosts_backup_')] backups.sort(reverse=True) return os.path.join(self.hosts_folder_path, backups[0]) if backups else None def log_error(self, error_message): pass def show_error(self, message): msg = QMessageBox(self) msg.setIcon(QMessageBox.Critical) msg.setText(message) msg.setWindowTitle("Hata") msg.exec_() def show_warning(self, message): msg = QMessageBox(self) msg.setIcon(QMessageBox.Warning) msg.setText(message) msg.setWindowTitle("Uyarı") msg.exec_() def run_as_admin(): if ctypes.windll.shell32.IsUserAnAdmin(): app = QApplication(sys.argv) ex = HostsManager() ex.show() sys.exit(app.exec_()) else: ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1) if __name__ == '__main__': run_as_admin()