mirror of
https://github.com/dragonpilot/dragonpilot.git
synced 2026-04-07 15:53:55 +08:00
fileserv - 2025-06-17
This commit is contained in:
@@ -122,4 +122,5 @@ inline static std::unordered_map<std::string, uint32_t> keys = {
|
||||
{"Version", PERSISTENT},
|
||||
{"dp_device_last_log", CLEAR_ON_ONROAD_TRANSITION},
|
||||
{"dp_device_reset_conf", CLEAR_ON_MANAGER_START},
|
||||
{"dp_device_ip", CLEAR_ON_MANAGER_START},
|
||||
};
|
||||
|
||||
260
dragonpilot/selfdrive/fileserv/fileserv.py
Normal file
260
dragonpilot/selfdrive/fileserv/fileserv.py
Normal file
@@ -0,0 +1,260 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
'''
|
||||
MIT Non-Commercial License
|
||||
|
||||
Copyright (c) 2019, dragonpilot
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, for non-commercial purposes only, subject to the following conditions:
|
||||
|
||||
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
- Commercial use (e.g., use in a product, service, or activity intended to generate revenue) is prohibited without explicit written permission from dragonpilot. Contact ricklan@gmail.com for inquiries.
|
||||
- Any project that uses the Software must visibly mention the following acknowledgment: "This project uses software from dragonpilot and is licensed under a custom license requiring permission for use."
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
'''
|
||||
|
||||
import os
|
||||
import signal
|
||||
import threading
|
||||
import socket
|
||||
import math
|
||||
from datetime import datetime
|
||||
from urllib.parse import urlparse, parse_qs, quote
|
||||
from openpilot.system.hardware import PC
|
||||
|
||||
HOST = '0.0.0.0'
|
||||
PORT = 5000
|
||||
DEFAULT_DIR = os.path.realpath('.' if PC else '/data/media/0/realdata')
|
||||
|
||||
def format_file_size(size_bytes):
|
||||
"""Formats a file size in bytes to a human-readable string (B, KB, MB, GB)."""
|
||||
if size_bytes == 0:
|
||||
return "0 B"
|
||||
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
|
||||
i = int(math.floor(math.log(size_bytes, 1024)))
|
||||
p = math.pow(1024, i)
|
||||
s = round(size_bytes / p, 1)
|
||||
return f"{s} {size_name[i]}"
|
||||
|
||||
def get_safe_path(requested_path):
|
||||
"""Joins the requested path with the base directory and resolves it, ensuring it's safe."""
|
||||
combined_path = os.path.join(DEFAULT_DIR, requested_path.lstrip('/'))
|
||||
safe_path = os.path.realpath(combined_path)
|
||||
if os.path.commonprefix((safe_path, DEFAULT_DIR)) == DEFAULT_DIR:
|
||||
return safe_path
|
||||
return None
|
||||
|
||||
def list_directory(client_socket, path):
|
||||
try:
|
||||
if not os.path.isdir(path):
|
||||
response = "HTTP/1.1 404 Not Found\r\n\r\n"
|
||||
client_socket.sendall(response.encode())
|
||||
return
|
||||
|
||||
items = []
|
||||
for entry in os.listdir(path):
|
||||
full_path = os.path.join(path, entry)
|
||||
try:
|
||||
stat = os.stat(full_path)
|
||||
is_dir = os.path.isdir(full_path)
|
||||
items.append({
|
||||
'name': entry,
|
||||
'is_dir': is_dir,
|
||||
'mtime': datetime.fromtimestamp(stat.st_mtime).strftime('%Y-%m-%d %H:%M:%S'),
|
||||
'size': stat.st_size if not is_dir else 0
|
||||
})
|
||||
except FileNotFoundError:
|
||||
continue
|
||||
|
||||
items.sort(key=lambda x: (not x['is_dir'], x['name'].lower()))
|
||||
|
||||
# --- HTML and CSS for the page ---
|
||||
content = """
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta name='viewport' content='width=device-width, initial-scale=1'>
|
||||
<meta charset="UTF-8">
|
||||
<title>FileServ by dragonpilot</title>
|
||||
<style>
|
||||
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif; margin: 0; background-color: #f8f9fa; color: #212529; }
|
||||
.container { max-width: 960px; margin: 20px auto; padding: 0 10px; }
|
||||
h3 { border-bottom: 2px solid #dee2e6; padding-bottom: 10px; margin-top: 0; font-weight: 500; }
|
||||
table { width: 100%; border-collapse: collapse; }
|
||||
th, td { padding: 12px 15px; text-align: left; border-bottom: 1px solid #dee2e6; white-space: nowrap; }
|
||||
th { background-color: #f1f3f5; font-weight: 600; }
|
||||
tr:hover { background-color: #f1f3f5; }
|
||||
a { text-decoration: none; color: #007bff; font-weight: 500; }
|
||||
a:hover { text-decoration: underline; }
|
||||
.play-link { font-weight: bold; color: #28a745; font-size: 1.5em; text-align: center; display: block; }
|
||||
th.actions-col, td.actions-col { width: 1%; padding-right: 5px; padding-left: 5px; }
|
||||
.name-col { word-break: break-all; white-space: normal; }
|
||||
td.size { text-align: right; font-family: "Menlo", "Consolas", "Courier New", monospace; color: #495057; }
|
||||
.path-links { margin-bottom: 20px; font-size: 1.1em; word-wrap: break-word; }
|
||||
.path-links a { color: #495057; }
|
||||
|
||||
/* Responsive styles for mobile */
|
||||
@media screen and (max-width: 600px) {
|
||||
.container { margin: 0; padding: 0; }
|
||||
h3, .path-links { padding-left: 10px; padding-right: 10px; }
|
||||
th, td { padding: 10px 8px; }
|
||||
.hide-on-mobile { display: none; }
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
"""
|
||||
# Breadcrumb navigation
|
||||
content += "<h3>FileServ by dragonpilot</h3>"
|
||||
content += "<div class='path-links'><strong>Path:</strong> "
|
||||
relative_path_from_root = os.path.relpath(path, DEFAULT_DIR)
|
||||
content += "<a href='/'>/</a>"
|
||||
if relative_path_from_root != ".":
|
||||
current_href = ""
|
||||
for part in relative_path_from_root.split(os.sep):
|
||||
current_href = os.path.join(current_href, part)
|
||||
content += f" <a href='/{current_href}/'>{part}</a> /"
|
||||
content += "</div>"
|
||||
|
||||
# Table header - Swapped 'hide-on-mobile' from Last Modified to Size
|
||||
content += "<table><thead><tr><th class='actions-col'></th><th>Name</th><th>Last Modified</th><th class='size hide-on-mobile'>Size</th></tr></thead><tbody>"
|
||||
|
||||
# Parent directory link
|
||||
if path != DEFAULT_DIR:
|
||||
content += "<tr><td class='actions-col'></td><td class='name-col'><a href='..'>.. (Parent Directory)</a></td><td></td><td class='size hide-on-mobile'>-</td></tr>"
|
||||
|
||||
# Table rows for directories and files
|
||||
for item in items:
|
||||
relative_item_path = os.path.relpath(os.path.join(path, item['name']), DEFAULT_DIR)
|
||||
encoded_path = quote(relative_item_path)
|
||||
|
||||
size_str = format_file_size(item['size']) if not item['is_dir'] else '-'
|
||||
|
||||
actions_cell = ""
|
||||
if not item['is_dir'] and item['name'].endswith('.ts'):
|
||||
actions_cell = f"<a href='/play?file={encoded_path}' class='play-link' target='_blank'>▶</a>"
|
||||
|
||||
# Assemble the row with the 'hide-on-mobile' class on the correct cell
|
||||
content += f"<tr>"
|
||||
content += f"<td class='actions-col'>{actions_cell}</td>"
|
||||
content += f"<td class='name-col'><a href='/{encoded_path}{'/' if item['is_dir'] else ''}'>{item['name']}</a></td>"
|
||||
content += f"<td>{item['mtime']}</td>"
|
||||
content += f"<td class='size hide-on-mobile'>{size_str}</td>"
|
||||
content += f"</tr>"
|
||||
|
||||
|
||||
content += "</tbody></table></div></body></html>"
|
||||
|
||||
body = content.encode("utf-8")
|
||||
response_headers = f"HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: {len(body)}\r\n\r\n"
|
||||
client_socket.sendall(response_headers.encode())
|
||||
client_socket.sendall(body)
|
||||
|
||||
except Exception as e:
|
||||
response = f"HTTP/1.1 500 Internal Server Error\r\n\r\n<h3>500 Internal Server Error</h3><p>{e}</p>"
|
||||
client_socket.sendall(response.encode())
|
||||
|
||||
def serve_player(client_socket, file_path):
|
||||
encoded_path = quote(file_path)
|
||||
html = f"""
|
||||
<!DOCTYPE html><html><head><title>HLS Player</title><style>body,html{{margin:0;padding:0;height:100%;background-color:#000}}#video{{width:100%;height:100%}}</style><script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script></head><body><video id="video" controls autoplay></video><script>var v=document.getElementById('video'),s='/manifest.m3u8?file={encoded_path}';if(Hls.isSupported()){{var h=new Hls();h.loadSource(s);h.attachMedia(v)}}else if(v.canPlayType('application/vnd.apple.mpegurl')){{v.src=s}}</script></body></html>"""
|
||||
body = html.encode('utf-8')
|
||||
headers = f"HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: {len(body)}\r\n\r\n"
|
||||
client_socket.sendall(headers.encode())
|
||||
client_socket.sendall(body)
|
||||
|
||||
def serve_manifest(client_socket, file_path):
|
||||
encoded_path = quote(file_path)
|
||||
manifest = f"""#EXTM3U\n#EXT-X-VERSION:3\n#EXT-X-TARGETDURATION:60\n#EXT-X-PLAYLIST-TYPE:VOD\n#EXTINF:60.0,\n/{encoded_path}\n#EXT-X-ENDLIST\n"""
|
||||
body = manifest.encode('utf-8')
|
||||
headers = f"HTTP/1.1 200 OK\r\nContent-Type: application/vnd.apple.mpegurl\r\nContent-Length: {len(body)}\r\n\r\n"
|
||||
client_socket.sendall(headers.encode())
|
||||
client_socket.sendall(body)
|
||||
|
||||
def handle_request(client_socket):
|
||||
try:
|
||||
request = client_socket.recv(1024).decode('utf-8')
|
||||
if not request: return
|
||||
|
||||
first_line = request.split('\r\n')[0]
|
||||
parts = first_line.split()
|
||||
if len(parts) < 2: return
|
||||
|
||||
method, full_path = parts[0], parts[1]
|
||||
url = urlparse(full_path)
|
||||
path_qs = parse_qs(url.query)
|
||||
|
||||
file_param = path_qs.get('file', [None])[0]
|
||||
|
||||
if url.path == '/play' and file_param:
|
||||
serve_player(client_socket, file_param)
|
||||
return
|
||||
if url.path == '/manifest.m3u8' and file_param:
|
||||
serve_manifest(client_socket, file_param)
|
||||
return
|
||||
|
||||
safe_path = get_safe_path(url.path)
|
||||
if not safe_path:
|
||||
client_socket.sendall(b"HTTP/1.1 403 Forbidden\r\n\r\nAccess denied.")
|
||||
return
|
||||
|
||||
if os.path.isfile(safe_path):
|
||||
file_extension = os.path.splitext(safe_path)[1].lower()
|
||||
content_type = "application/octet-stream"
|
||||
if file_extension == '.ts':
|
||||
content_type = "video/mp2t"
|
||||
|
||||
file_size = os.path.getsize(safe_path)
|
||||
response = f"HTTP/1.1 200 OK\r\nContent-Type: {content_type}\r\nContent-Length: {file_size}\r\n\r\n"
|
||||
client_socket.sendall(response.encode())
|
||||
|
||||
with open(safe_path, 'rb') as f:
|
||||
while chunk := f.read(8192):
|
||||
try:
|
||||
client_socket.sendall(chunk)
|
||||
except BrokenPipeError:
|
||||
break
|
||||
|
||||
elif os.path.isdir(safe_path):
|
||||
list_directory(client_socket, safe_path)
|
||||
|
||||
else:
|
||||
client_socket.sendall(b"HTTP/1.1 404 Not Found\r\n\r\nNot Found.")
|
||||
|
||||
except Exception:
|
||||
pass
|
||||
finally:
|
||||
client_socket.close()
|
||||
|
||||
|
||||
def start_server():
|
||||
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
server_socket.bind((HOST, PORT))
|
||||
server_socket.listen(5)
|
||||
|
||||
while True:
|
||||
try:
|
||||
client_socket, _ = server_socket.accept()
|
||||
threading.Thread(target=handle_request, args=(client_socket,)).start()
|
||||
except KeyboardInterrupt:
|
||||
break
|
||||
server_socket.close()
|
||||
|
||||
def main():
|
||||
print(f"Server starting on http://{HOST}:{PORT}")
|
||||
|
||||
server_thread = threading.Thread(target=start_server)
|
||||
server_thread.daemon = True
|
||||
server_thread.start()
|
||||
|
||||
signal.signal(signal.SIGINT, lambda s, f: os._exit(0))
|
||||
signal.signal(signal.SIGTERM, lambda s, f: os._exit(0))
|
||||
|
||||
server_thread.join()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
2
dragonpilot/selfdrive/fileserv/static/hls.min.js
vendored
Normal file
2
dragonpilot/selfdrive/fileserv/static/hls.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
@@ -4,6 +4,9 @@
|
||||
|
||||
#include "selfdrive/ui/qt/util.h"
|
||||
|
||||
#include <QrCode.hpp>
|
||||
using qrcodegen::QrCode;
|
||||
|
||||
void Sidebar::drawMetric(QPainter &p, const QPair<QString, QString> &label, QColor c, int y) {
|
||||
const QRect rect = {30, y, 240, 126};
|
||||
|
||||
@@ -106,6 +109,13 @@ void Sidebar::updateState(const UIState &s) {
|
||||
pandaStatus = {{tr("NO"), tr("PANDA")}, danger_color};
|
||||
}
|
||||
setProperty("pandaStatus", QVariant::fromValue(pandaStatus));
|
||||
// rick - update every 5 secs
|
||||
if (sm.frame % UI_FREQ*5 == 0) {
|
||||
ip_addr = QString::fromStdString(Params().get("dp_device_ip"));
|
||||
ip_changed = ip_addr != ip_addr_prev;
|
||||
setProperty("ipAddr", ip_addr);
|
||||
ip_addr_prev = ip_addr;
|
||||
}
|
||||
}
|
||||
|
||||
void Sidebar::paintEvent(QPaintEvent *event) {
|
||||
@@ -119,7 +129,45 @@ void Sidebar::paintEvent(QPaintEvent *event) {
|
||||
p.setOpacity(settings_pressed ? 0.65 : 1.0);
|
||||
p.drawPixmap(settings_btn.x(), settings_btn.y(), settings_img);
|
||||
p.setOpacity(onroad && flag_pressed ? 0.65 : 1.0);
|
||||
p.drawPixmap(home_btn.x(), home_btn.y(), onroad ? flag_img : home_img);
|
||||
{
|
||||
if (ip_qrcode_cache.isNull() || ip_changed) {
|
||||
QSize ip_qrcode_img_size = QSize(250, 250);
|
||||
// Recreate the compass image with the current size
|
||||
ip_qrcode_cache = QImage(ip_qrcode_img_size, QImage::Format_ARGB32);
|
||||
ip_qrcode_cache.fill(Qt::transparent); // Fill with a transparent background
|
||||
// Create a QPainter for the image
|
||||
QPainter cache_painter(&ip_qrcode_cache);
|
||||
|
||||
QString url = "http://" + ipAddr + ":5000/";
|
||||
QrCode qr = QrCode::encodeText(url.toUtf8().data(), QrCode::Ecc::LOW);
|
||||
|
||||
const int s = qr.getSize();
|
||||
const float aspect = 250/250;
|
||||
const float size = (aspect > 1.0)? 250 : 250;
|
||||
const float scale = size/(s+2);
|
||||
|
||||
// NOTE: For performance reasons my implementation only draws the foreground parts in supplied color.
|
||||
// It expects background to be prepared already (in white or whatever is preferred).
|
||||
cache_painter.setPen(Qt::NoPen);
|
||||
cache_painter.setBrush(QColor(0xff, 0xff, 0xff, 0xff));
|
||||
for(int y=0; y < s; y++) {
|
||||
for(int x=0; x < s; x++) {
|
||||
const int color=qr.getModule(x, y); // 0 for white, 1 for black
|
||||
if(0x0 != color) {
|
||||
const double rx1 = (x+1)*scale, ry1=(y+1)*scale;
|
||||
QRectF r(rx1, ry1, scale, scale);
|
||||
cache_painter.drawRects(&r,1);
|
||||
}
|
||||
}
|
||||
}
|
||||
cache_painter.end();
|
||||
}
|
||||
}
|
||||
if (ipAddr.isEmpty()) {
|
||||
p.drawPixmap(home_btn.x(), home_btn.y(), onroad ? flag_img : home_img);
|
||||
} else {
|
||||
p.drawImage(home_btn.x()-30, home_btn.y()-30, ip_qrcode_cache);
|
||||
}
|
||||
p.setOpacity(1.0);
|
||||
|
||||
// network
|
||||
@@ -136,6 +184,12 @@ void Sidebar::paintEvent(QPaintEvent *event) {
|
||||
const QRect r = QRect(58, 247, width() - 100, 50);
|
||||
p.drawText(r, Qt::AlignLeft | Qt::AlignVCenter, net_type);
|
||||
|
||||
// IP
|
||||
p.setFont(InterFont(30));
|
||||
p.setPen(QColor(0xff, 0xff, 0xff));
|
||||
const QRect ip = QRect(40, 260, 230, 100);
|
||||
p.drawText(ip, Qt::AlignCenter, ipAddr);
|
||||
|
||||
// metrics
|
||||
drawMetric(p, temp_status.first, temp_status.second, 338);
|
||||
drawMetric(p, panda_status.first, panda_status.second, 496);
|
||||
|
||||
@@ -18,6 +18,7 @@ class Sidebar : public QFrame {
|
||||
Q_PROPERTY(ItemStatus tempStatus MEMBER temp_status NOTIFY valueChanged);
|
||||
Q_PROPERTY(QString netType MEMBER net_type NOTIFY valueChanged);
|
||||
Q_PROPERTY(int netStrength MEMBER net_strength NOTIFY valueChanged);
|
||||
Q_PROPERTY(QString ipAddr MEMBER ipAddr NOTIFY valueChanged);
|
||||
|
||||
public:
|
||||
explicit Sidebar(QWidget* parent = 0);
|
||||
@@ -57,6 +58,11 @@ protected:
|
||||
ItemStatus connect_status, panda_status, temp_status;
|
||||
QString net_type;
|
||||
int net_strength = 0;
|
||||
QString ipAddr = "";
|
||||
QString ip_addr = "";
|
||||
QString ip_addr_prev = "";
|
||||
bool ip_changed = false;
|
||||
QImage ip_qrcode_cache;
|
||||
|
||||
private:
|
||||
std::unique_ptr<PubMaster> pm;
|
||||
|
||||
@@ -26,6 +26,7 @@ from openpilot.common.swaglog import cloudlog
|
||||
from openpilot.system.hardware.power_monitoring import PowerMonitoring
|
||||
from openpilot.system.hardware.fan_controller import TiciFanController
|
||||
from openpilot.system.version import terms_version, training_version
|
||||
import socket
|
||||
|
||||
ThermalStatus = log.DeviceState.ThermalStatus
|
||||
NetworkType = log.DeviceState.NetworkType
|
||||
@@ -456,6 +457,25 @@ def hardware_thread(end_event, hw_queue) -> None:
|
||||
should_start_prev = should_start
|
||||
|
||||
|
||||
def ip_thread(end_event, hw_queue):
|
||||
count = 0
|
||||
params = Params()
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
while not end_event.is_set():
|
||||
# update IP every 10s
|
||||
if count % int(10. / DT_HW) == 0:
|
||||
try:
|
||||
# doesn't even have to be reachable
|
||||
s.connect(('10.255.255.255', 1))
|
||||
ip = s.getsockname()[0]
|
||||
except:
|
||||
ip = ''
|
||||
|
||||
params.put_nonblocking('dp_device_ip', ip.strip())
|
||||
|
||||
count += 1
|
||||
time.sleep(DT_HW)
|
||||
|
||||
def main():
|
||||
hw_queue = queue.Queue(maxsize=1)
|
||||
end_event = threading.Event()
|
||||
@@ -463,6 +483,7 @@ def main():
|
||||
threads = [
|
||||
threading.Thread(target=hw_state_thread, args=(end_event, hw_queue)),
|
||||
threading.Thread(target=hardware_thread, args=(end_event, hw_queue)),
|
||||
threading.Thread(target=ip_thread, args=(end_event, hw_queue)),
|
||||
]
|
||||
|
||||
if TICI:
|
||||
|
||||
@@ -106,6 +106,7 @@ procs = [
|
||||
PythonProcess("updated", "system.updated.updated", only_offroad, enabled=not PC),
|
||||
PythonProcess("uploader", "system.loggerd.uploader", always_run),
|
||||
PythonProcess("statsd", "system.statsd", always_run),
|
||||
PythonProcess("fileserv", "dragonpilot.selfdrive.fileserv.fileserv", always_run),
|
||||
|
||||
# debug procs
|
||||
NativeProcess("bridge", "cereal/messaging", ["./bridge"], notcar),
|
||||
|
||||
Reference in New Issue
Block a user