-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
endpoints.py
217 lines (191 loc) · 8.1 KB
/
endpoints.py
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
import logging
import psutil
from pydantic import BaseModel, ValidationError
from .main import app, database
from .auth import User, fake_users_db, fake_hash_password, authenticate_user, enforce_password_policy, generate_otp, verify_otp, track_failed_login_attempts, lock_account, unlock_account, register_user
from app.wifi import scan_wifi, connect_to_wifi
from .update_website import WebsiteUpdateRequest, current_website
router = APIRouter()
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class NetworkPreferences(BaseModel):
ssid: str
password: str
class Device(BaseModel):
name: str
mac_address: str
connected_devices = []
@router.post("/token")
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
user = await authenticate_user(form_data.username, form_data.password)
if not user:
track_failed_login_attempts(form_data.username)
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect username or password",
headers={"WWW-Authenticate": "Bearer"},
)
if user.is_locked:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Account is locked. Please contact support.",
)
return {"access_token": user.username, "token_type": "bearer"}
@router.get("/")
def read_root():
return {"message": "Welcome to PySkyWiFi!"}
@router.post("/users/")
async def create_user(username: str, password: str, otp: str):
if not register_user(username, password, otp):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="User registration failed",
)
return {"username": username}
@router.get("/wifi/scan")
def scan_wifi_networks():
try:
networks = scan_wifi()
return {"networks": networks}
except Exception as e:
logger.error(f"Error scanning Wi-Fi networks: {e}")
raise HTTPException(status_code=500, detail="Error scanning Wi-Fi networks")
@router.post("/wifi/connect")
def connect_to_wifi_network(ssid: str, password: str):
if not ssid or not password:
raise HTTPException(status_code=400, detail="SSID and password must not be empty")
try:
connect_to_wifi(ssid, password)
return {"message": "Connected to Wi-Fi network"}
except Exception as e:
logger.error(f"Error connecting to Wi-Fi network: {e}")
raise HTTPException(status_code=500, detail="Error connecting to Wi-Fi network")
@router.post("/network/preferences")
def configure_network_preferences(preferences: NetworkPreferences):
try:
preferences = NetworkPreferences(**preferences.dict())
logger.info(f"Network preferences updated: {preferences}")
return {"message": "Network preferences updated"}
except ValidationError as e:
logger.error(f"Validation error: {e}")
raise HTTPException(status_code=400, detail="Invalid network preferences")
except Exception as e:
logger.error(f"Error updating network preferences: {e}")
raise HTTPException(status_code=500, detail="Error updating network preferences")
@router.get("/network/status")
def get_network_status():
try:
network_status = {
"cpu_usage": psutil.cpu_percent(),
"memory_usage": psutil.virtual_memory().percent,
"disk_usage": psutil.disk_usage('/').percent
}
return {"network_status": network_status}
except Exception as e:
logger.error(f"Error getting network status: {e}")
raise HTTPException(status_code=500, detail="Error getting network status")
@router.get("/devices")
def list_connected_devices():
try:
return {"devices": connected_devices}
except Exception as e:
logger.error(f"Error listing connected devices: {e}")
raise HTTPException(status_code=500, detail="Error listing connected devices")
@router.post("/devices/restrict")
def restrict_device_access(device: Device):
try:
device = Device(**device.dict())
logger.info(f"Device access restricted: {device}")
return {"message": "Device access restricted"}
except ValidationError as e:
logger.error(f"Validation error: {e}")
raise HTTPException(status_code=400, detail="Invalid device data")
except Exception as e:
logger.error(f"Error restricting device access: {e}")
raise HTTPException(status_code=500, detail="Error restricting device access")
@router.get("/wifi/signal_strength")
def get_wifi_signal_strength():
try:
signal_strength = {
"ssid": "example_ssid",
"strength": -50
}
return {"signal_strength": signal_strength}
except Exception as e:
logger.error(f"Error getting Wi-Fi signal strength: {e}")
raise HTTPException(status_code=500, detail="Error getting Wi-Fi signal strength")
@router.post("/wifi/signal_strength/notify")
def notify_signal_strength_drop(threshold: int):
try:
current_signal_strength = -60
if current_signal_strength < threshold:
return {"message": "Signal strength has dropped below the threshold"}
return {"message": "Signal strength is above the threshold"}
except Exception as e:
logger.error(f"Error notifying signal strength drop: {e}")
raise HTTPException(status_code=500, detail="Error notifying signal strength drop")
@router.post("/wifi/auto_reconnect")
def auto_reconnect():
try:
return {"message": "Auto-reconnect feature implemented"}
except Exception as e:
logger.error(f"Error implementing auto-reconnect feature: {e}")
raise HTTPException(status_code=500, detail="Error implementing auto-reconnect feature")
@router.get("/wifi/speed_test")
def speed_test():
try:
speed_metrics = {
"download_speed": 50,
"upload_speed": 10,
"latency": 20,
"packet_loss": 0
}
return {"speed_metrics": speed_metrics}
except Exception as e:
logger.error(f"Error performing speed test: {e}")
raise HTTPException(status_code=500, detail="Error performing speed test")
@router.get("/network/usage_statistics")
def get_network_usage_statistics():
try:
usage_statistics = {
"data_usage": 1000,
"connected_time": 3600
}
return {"usage_statistics": usage_statistics}
except Exception as e:
logger.error(f"Error getting network usage statistics: {e}")
raise HTTPException(status_code=500, detail="Error getting network usage statistics")
@router.post("/guest_networks")
def create_guest_network(ssid: str, password: str):
if not ssid or not password:
raise HTTPException(status_code=400, detail="SSID and password must not be empty")
try:
return {"message": f"Guest network '{ssid}' created with limited privileges"}
except Exception as e:
logger.error(f"Error creating guest network: {e}")
raise HTTPException(status_code=500, detail="Error creating guest network")
@router.get("/guest_networks/usage")
def get_guest_network_usage():
try:
guest_network_usage = {
"data_usage": 500,
"connected_time": 1800
}
return {"guest_network_usage": guest_network_usage}
except Exception as e:
logger.error(f"Error getting guest network usage: {e}")
raise HTTPException(status_code=500, detail="Error getting guest network usage")
@router.put("/update_website/")
async def update_website(request: WebsiteUpdateRequest):
if not request.new_url:
raise HTTPException(status_code=400, detail="New URL must be provided")
current_website["url"] = request.new_url
return {"message": "Website URL updated successfully", "new_url": current_website["url"]}
@router.get("/current_website/")
async def get_current_website():
return {"current_url": current_website["url"]}
app.include_router(router)