fix first critical issiue

This commit is contained in:
2026-03-27 16:06:23 +01:00
parent a6a19dad6e
commit a95bc43be6
4 changed files with 134 additions and 16 deletions
BIN
View File
Binary file not shown.
+2
View File
@@ -113,6 +113,8 @@ func HttpHandleLogin(response http.ResponseWriter, request *http.Request) {
client, err = CacheGetClientByName(username) client, err = CacheGetClientByName(username)
if err != nil { if err != nil {
client = &Client{Name: username}
err := DbSetClientByName(ctx, client) err := DbSetClientByName(ctx, client)
if err != nil { if err != nil {
http.Error(response, "bad login", http.StatusUnauthorized) http.Error(response, "bad login", http.StatusUnauthorized)
+115 -15
View File
@@ -43,23 +43,65 @@
#send-row input { flex: 1; margin: 0; } #send-row input { flex: 1; margin: 0; }
#send-row button { margin: 0; } #send-row button { margin: 0; }
.hint { margin-top: 20px; font-size: 12px; color: #666; border-top: 1px solid #2a2a2a; padding-top: 12px; } input[type="password"] {
.hint code { background: #2a2a2a; padding: 2px 5px; border-radius: 3px; color: #ccc; } width: 100%; padding: 8px; background: #2a2a2a; border: 1px solid #444;
color: #e0e0e0; font-family: monospace; font-size: 13px; border-radius: 4px;
}
.tabs { display: flex; gap: 8px; margin-bottom: 16px; }
.tab-btn {
padding: 6px 16px; background: #2a2a2a; border: 1px solid #444;
color: #aaa; border-radius: 4px; cursor: pointer; font-family: monospace; font-size: 13px;
}
.tab-btn.active { background: #4a90d9; color: #fff; border-color: #4a90d9; }
.tab { display: none; }
.tab.active { display: block; }
#form-status {
margin-top: 10px; font-size: 13px; padding: 6px 10px;
border-radius: 4px; background: #2a2a2a; border-left: 3px solid #555; display: none;
}
#form-status.ok { border-color: #4caf50; color: #81c784; display: block; }
#form-status.err { border-color: #f44336; color: #e57373; display: block; }
</style> </style>
</head> </head>
<body> <body>
<div id="auth-panel"> <div id="auth-panel">
<h2>WebSocket Client</h2> <h2>WebSocket Client</h2>
<label>JWT Token</label>
<input id="token-input" type="text" placeholder="Paste token from curl /login">
<button id="connect-btn" onclick="connect()">Connect</button>
<div id="status">Not connected</div>
<div class="hint"> <div class="tabs">
Register: <code>curl -X POST localhost:8080/register -d "username=alice&password=password123"</code><br><br> <button class="tab-btn active" onclick="switchTab('login')">Login</button>
Login: <code>curl -X POST localhost:8080/login -d "username=alice&password=password123"</code> <button class="tab-btn" onclick="switchTab('register')">Register</button>
<button class="tab-btn" onclick="switchTab('connect')">Connect</button>
</div> </div>
<div id="tab-login" class="tab active">
<label>Username</label>
<input id="login-username" type="text" placeholder="alice">
<label style="margin-top:10px;">Password</label>
<input id="login-password" type="password" placeholder="••••••••">
<button onclick="login()">Login</button>
</div>
<div id="tab-register" class="tab">
<label>Username</label>
<input id="reg-username" type="text" placeholder="alice">
<label style="margin-top:10px;">Password</label>
<input id="reg-password" type="password" placeholder="••••••••">
<label style="margin-top:10px;">Color (r,g,b)</label>
<input id="reg-color" type="text" placeholder="255,100,50">
<button onclick="register()">Register</button>
</div>
<div id="tab-connect" class="tab">
<label>Username (for display)</label>
<input id="username-input" type="text" placeholder="Your username">
<label style="margin-top:10px;">JWT Token</label>
<input id="token-input" type="text" placeholder="Paste token from login">
<button id="connect-btn" onclick="connect()">Connect</button>
</div>
<div id="form-status"></div>
<div id="status">Not connected</div>
</div> </div>
<div id="chat-panel"> <div id="chat-panel">
@@ -75,6 +117,64 @@
<script> <script>
let ws = null; let ws = null;
function switchTab(name) {
document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
document.querySelectorAll('.tab-btn').forEach(b => b.classList.remove('active'));
document.getElementById('tab-' + name).classList.add('active');
document.querySelectorAll('.tab-btn').forEach(b => {
if (b.textContent.toLowerCase() === name) b.classList.add('active');
});
setFormStatus('', '');
}
function setFormStatus(text, cls) {
const el = document.getElementById('form-status');
el.textContent = text;
el.className = cls;
}
async function register() {
const username = document.getElementById('reg-username').value.trim();
const password = document.getElementById('reg-password').value;
const color = document.getElementById('reg-color').value.trim();
if (!username || !password || !color) { setFormStatus('Fill all fields', 'err'); return; }
const body = new URLSearchParams({ username, password, color });
try {
const res = await fetch('http://localhost:8080/newuser', { method: 'POST', body });
const text = await res.text();
if (res.ok) {
setFormStatus('Registered! Now login.', 'ok');
} else {
setFormStatus('Error: ' + text, 'err');
}
} catch (e) {
setFormStatus('Request failed: ' + e.message, 'err');
}
}
async function login() {
const username = document.getElementById('login-username').value.trim();
const password = document.getElementById('login-password').value;
if (!username || !password) { setFormStatus('Fill all fields', 'err'); return; }
const body = new URLSearchParams({ username, password });
try {
const res = await fetch('http://localhost:8080/login', { method: 'POST', body });
const text = await res.text();
if (res.ok) {
document.getElementById('username-input').value = username;
document.getElementById('token-input').value = text;
setFormStatus('Logged in! Token copied to Connect tab.', 'ok');
switchTab('connect');
} else {
setFormStatus('Error: ' + text, 'err');
}
} catch (e) {
setFormStatus('Request failed: ' + e.message, 'err');
}
}
function setStatus(text, cls) { function setStatus(text, cls) {
const el = document.getElementById('status'); const el = document.getElementById('status');
el.textContent = text; el.textContent = text;
@@ -93,6 +193,7 @@
function connect() { function connect() {
const token = document.getElementById('token-input').value.trim(); const token = document.getElementById('token-input').value.trim();
if (!token) { setStatus('Paste a token first', 'err'); return; } if (!token) { setStatus('Paste a token first', 'err'); return; }
const username = document.getElementById('username-input').value.trim() || '?';
document.getElementById('connect-btn').disabled = true; document.getElementById('connect-btn').disabled = true;
setStatus('Connecting...'); setStatus('Connecting...');
@@ -101,18 +202,17 @@
ws.onopen = () => { ws.onopen = () => {
ws.send(JSON.stringify({ token })); ws.send(JSON.stringify({ token }));
document.getElementById('auth-name').textContent = username;
document.getElementById('auth-panel').style.display = 'none';
document.getElementById('chat-panel').style.display = 'block';
log('Authenticated as ' + username, 'msg-sys');
}; };
ws.onmessage = (e) => { ws.onmessage = (e) => {
let data; let data;
try { data = JSON.parse(e.data); } catch { log('← ' + e.data, 'msg-in'); return; } try { data = JSON.parse(e.data); } catch { log('← ' + e.data, 'msg-in'); return; }
if (data.authAs) { if (data.error) {
document.getElementById('auth-name').textContent = data.authAs;
document.getElementById('auth-panel').style.display = 'none';
document.getElementById('chat-panel').style.display = 'block';
log('Authenticated as ' + data.authAs, 'msg-sys');
} else if (data.error) {
log('Error: ' + data.error, 'msg-sys'); log('Error: ' + data.error, 'msg-sys');
} else { } else {
log('← ' + JSON.stringify(data), 'msg-in'); log('← ' + JSON.stringify(data), 'msg-in');
+17 -1
View File
@@ -1,5 +1,21 @@
package main package main
func main() { import (
"context"
"log"
"net/http"
)
func main() {
ctx := context.Background()
DbInit(ctx)
http.HandleFunc("/newuser", HttpHandleNewUser)
http.HandleFunc("/login", HttpHandleLogin)
http.HandleFunc("/group/create", HttpHandleGroupCreate)
http.HandleFunc("/group/addclient", HttpHandleGroupAddClient)
http.HandleFunc("/ws", ServeWsConnection)
log.Println("listening on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
} }