-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
187 lines (161 loc) · 6.3 KB
/
setup.py
File metadata and controls
187 lines (161 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python3
"""
NetGuard DNS Monitor - Setup Script
Automates the installation and configuration process
"""
import os
import sys
import subprocess
import platform
def print_banner():
"""Print setup banner"""
banner = """
╔══════════════════════════════════════════════════════════════╗
║ ║
║ 🛡️ NetGuard DNS Monitor - Setup Script 🛡️ ║
║ ║
╚══════════════════════════════════════════════════════════════╝
"""
print(banner)
def check_python_version():
"""Check if Python version is 3.8 or higher"""
print("🔍 Checking Python version...")
version = sys.version_info
if version.major < 3 or (version.major == 3 and version.minor < 8):
print(f"❌ Python 3.8+ required. You have Python {version.major}.{version.minor}")
sys.exit(1)
print(f"✅ Python {version.major}.{version.minor}.{version.micro} detected")
def check_venv_exists():
"""Check if virtual environment exists"""
venv_path = os.path.join(os.getcwd(), 'venv')
return os.path.exists(venv_path)
def create_virtual_environment():
"""Create virtual environment"""
print("\n📦 Creating virtual environment...")
try:
subprocess.run([sys.executable, '-m', 'venv', 'venv'], check=True)
print("✅ Virtual environment created successfully")
return True
except subprocess.CalledProcessError:
print("❌ Failed to create virtual environment")
return False
def get_pip_command():
"""Get the appropriate pip command based on OS and venv"""
if platform.system() == "Windows":
return os.path.join('venv', 'Scripts', 'pip.exe')
else:
return os.path.join('venv', 'bin', 'pip')
def get_python_command():
"""Get the appropriate python command based on OS and venv"""
if platform.system() == "Windows":
return os.path.join('venv', 'Scripts', 'python.exe')
else:
return os.path.join('venv', 'bin', 'python')
def install_dependencies():
"""Install required dependencies"""
print("\n📥 Installing dependencies...")
pip_cmd = get_pip_command()
if not os.path.exists('requirements.txt'):
print("❌ requirements.txt not found!")
return False
try:
# Upgrade pip first
print(" Upgrading pip...")
subprocess.run([pip_cmd, 'install', '--upgrade', 'pip'],
check=True, capture_output=True)
# Install requirements
print(" Installing packages from requirements.txt...")
subprocess.run([pip_cmd, 'install', '-r', 'requirements.txt'],
check=True)
print("✅ All dependencies installed successfully")
return True
except subprocess.CalledProcessError as e:
print(f"❌ Failed to install dependencies: {e}")
return False
def check_admin_privileges():
"""Check if script is run with admin privileges"""
print("\n🔐 Checking administrator privileges...")
if platform.system() == "Windows":
try:
import ctypes
is_admin = ctypes.windll.shell32.IsUserAnAdmin()
if is_admin:
print("✅ Running with administrator privileges")
else:
print("⚠️ Not running as administrator")
print(" Note: You'll need admin rights to run the DNS server")
return is_admin
except:
return False
else:
# Unix-like systems
is_root = os.geteuid() == 0
if is_root:
print("✅ Running with root privileges")
else:
print("⚠️ Not running as root")
print(" Note: You'll need to use 'sudo' to run the DNS server")
return is_root
def print_next_steps():
"""Print next steps after setup"""
print("\n" + "="*60)
print("🎉 Setup Complete!")
print("="*60)
print("\n📝 Next Steps:")
print("\n1. Activate virtual environment:")
if platform.system() == "Windows":
print(" venv\\Scripts\\activate")
else:
print(" source venv/bin/activate")
print("\n2. Run NetGuard DNS Monitor:")
if platform.system() == "Windows":
print(" python main.py")
print(" (Run Command Prompt as Administrator)")
else:
print(" sudo python3 main.py")
print("\n3. Configure device DNS:")
print(" - Find your computer's IP address")
print(" - Set device DNS to your computer's IP")
print(" - Set secondary DNS to 8.8.8.8")
print("\n📚 Documentation:")
print(" - Quick Start: QUICK_SETUP.md")
print(" - Full Manual: USAGE.md")
print(" - Installation: INSTALLATION.md")
print("\n🔗 GitHub: https://github.com/jhapendra-kandel/NetGuard-DNS-Monitor")
print("="*60 + "\n")
def main():
"""Main setup function"""
print_banner()
# Check Python version
check_python_version()
# Check/create virtual environment
if check_venv_exists():
print("\n📦 Virtual environment already exists")
response = input(" Do you want to recreate it? (y/N): ").strip().lower()
if response == 'y':
print(" Removing old virtual environment...")
import shutil
shutil.rmtree('venv')
if not create_virtual_environment():
sys.exit(1)
else:
if not create_virtual_environment():
sys.exit(1)
# Install dependencies
if not install_dependencies():
print("\n⚠️ Installation completed with errors")
print(" Please check the error messages above")
sys.exit(1)
# Check admin privileges
check_admin_privileges()
# Print next steps
print_next_steps()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\n\n❌ Setup cancelled by user")
sys.exit(1)
except Exception as e:
print(f"\n\n❌ Unexpected error: {e}")
sys.exit(1)