-
Notifications
You must be signed in to change notification settings - Fork 0
/
UEFIUpdateServer.py
318 lines (295 loc) · 10.7 KB
/
UEFIUpdateServer.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import http.server
import socketserver
import json
import os
import sys
from urllib.parse import parse_qs
import threading
import cgi
import socket
def get_local_ip():
"""获取本机IPv4地址"""
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
ip = s.getsockname()[0]
s.close()
return ip
except Exception:
return "127.0.0.1"
# 全局变量存储发布状态和IP地址
published_data = {
'is_published': False,
'version': None,
'file_path': None
}
LOCAL_IP = get_local_ip()
HTML = """
<!DOCTYPE html>
<html>
<head>
<title>UEFI BIOS Update Server</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
h1 { text-align: center; }
.container {
display: flex;
flex-direction: column;
gap: 20px;
}
.form-group {
display: flex;
flex-direction: column;
gap: 5px;
}
button {
padding: 10px;
cursor: pointer;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
}
button:disabled {
background-color: #cccccc;
cursor: not-allowed;
}
button#stopBtn {
background-color: #f44336;
}
.message {
padding: 10px;
margin-top: 10px;
border-radius: 4px;
}
.success { background-color: #dff0d8; }
.error { background-color: #f2dede; }
#filePath {
word-break: break-all;
margin-top: 5px;
font-size: 0.9em;
color: #666;
}
.status-section {
margin-top: 30px;
padding: 15px;
background-color: #f8f9fa;
border-radius: 4px;
}
.status-title {
font-size: 1.2em;
font-weight: bold;
margin-bottom: 10px;
}
.status-info {
font-family: monospace;
white-space: pre-wrap;
word-break: break-all;
background-color: white;
padding: 10px;
border-radius: 4px;
border: 1px solid #ddd;
}
.no-publish {
color: #666;
font-style: italic;
}
</style>
</head>
<body>
<h1>UEFI BIOS Update Server</h1>
<div class="container">
<div class="form-group">
<label for="version">Version:</label>
<input type="text" id="version" placeholder="Enter version (e.g., 1.0.0)" required>
</div>
<div class="form-group">
<label for="fileInput">Select BIOS File:</label>
<input type="file" id="fileInput" required>
<div id="filePath"></div>
</div>
<button id="publishBtn" disabled>Publish</button>
<button id="stopBtn">Stop Publishing</button>
<div id="message"></div>
<div class="status-section">
<div class="status-title">Current Published Status</div>
<div id="currentStatus" class="status-info no-publish">No BIOS currently published</div>
</div>
</div>
<script>
const version = document.getElementById('version');
const fileInput = document.getElementById('fileInput');
const publishBtn = document.getElementById('publishBtn');
const stopBtn = document.getElementById('stopBtn');
const message = document.getElementById('message');
const filePath = document.getElementById('filePath');
const currentStatus = document.getElementById('currentStatus');
async function updateStatus() {
try {
const response = await fetch('/status');
const status = await response.json();
if (status.is_published) {
currentStatus.classList.remove('no-publish');
currentStatus.innerHTML = `Version: ${status.version}\nFile Path: ${status.file_path}`;
} else {
currentStatus.classList.add('no-publish');
currentStatus.textContent = 'No BIOS currently published';
}
} catch (error) {
currentStatus.textContent = 'Error fetching status';
}
}
// 页面加载时获取状态
updateStatus();
function validateInputs() {
publishBtn.disabled = !version.value || !fileInput.files[0];
}
version.addEventListener('input', validateInputs);
fileInput.addEventListener('change', () => {
validateInputs();
if (fileInput.files[0]) {
filePath.textContent = `Selected file: ${fileInput.files[0].name}`;
} else {
filePath.textContent = '';
}
});
publishBtn.addEventListener('click', async () => {
const formData = new FormData();
formData.append('version', version.value);
formData.append('file', fileInput.files[0]);
try {
const response = await fetch('/publish', {
method: 'POST',
body: formData
});
const result = await response.text();
message.className = 'message success';
message.textContent = result;
// 更新状态显示
updateStatus();
} catch (error) {
message.className = 'message error';
message.textContent = 'Error publishing BIOS update';
}
});
stopBtn.addEventListener('click', async () => {
try {
const response = await fetch('/stop', {method: 'POST'});
const result = await response.text();
message.className = 'message success';
message.textContent = result;
// 更新状态显示
updateStatus();
} catch (error) {
message.className = 'message error';
message.textContent = 'Error stopping BIOS update service';
}
});
</script>
</body>
</html>
"""
class RequestHandler(http.server.SimpleHTTPRequestHandler):
def do_HEAD(self):
if self.path == '/update':
if published_data['is_published']:
response = {
"message": f"New BIOS version available: {published_data['version']}",
"image_url": f"http://{LOCAL_IP}:{PORT}/BIN/{os.path.basename(published_data['file_path'])}"
}
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.send_header('Content-Length', str(len(json.dumps(response).encode('utf-8'))))
self.end_headers()
else:
super().do_HEAD()
else:
super().do_HEAD()
def do_GET(self):
if self.path == '/':
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(HTML.encode())
elif self.path == '/update':
if published_data['is_published']:
response = {
"message": f"New BIOS version available: {published_data['version']}",
"image_url": f"http://{LOCAL_IP}:{PORT}/BIN/{os.path.basename(published_data['file_path'])}"
}
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.send_header('Content-Length', str(len(json.dumps(response).encode('utf-8'))))
self.end_headers()
self.wfile.write(json.dumps(response).encode())
else:
self.send_error(404, "No BIOS update currently published")
elif self.path == '/status':
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(published_data).encode())
else:
super().do_GET()
def do_POST(self):
if self.path == '/publish':
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD': 'POST',
'CONTENT_TYPE': self.headers['Content-Type']}
)
version = form['version'].value
file_item = form['file']
# 保存上传的文件
file_path = os.path.join(os.getcwd(), 'BIN', file_item.filename)
os.makedirs(os.path.join(os.getcwd(), 'BIN'), exist_ok=True)
with open(file_path, 'wb') as f:
f.write(file_item.file.read())
published_data.update({
'is_published': True,
'version': version,
'file_path': file_path
})
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()
self.wfile.write(b"BIOS update published successfully")
elif self.path == '/stop':
published_data.update({
'is_published': False,
'version': None,
'file_path': None
})
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()
self.wfile.write(b"BIOS update service stopped")
def run_server(port):
global PORT
PORT = port
handler = RequestHandler
with socketserver.TCPServer(("", port), handler) as httpd:
print(f"Server started at http://{LOCAL_IP}:{port}")
print("Press Ctrl+C to stop the server")
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nShutting down server...")
httpd.shutdown()
httpd.server_close()
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python3 server.py <port>")
sys.exit(1)
try:
port = int(sys.argv[1])
run_server(port)
except ValueError:
print("Error: Port must be a number")
sys.exit(1)