282 lines
8.2 KiB
Go
282 lines
8.2 KiB
Go
package main
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
const (
|
|
passwordLength = 32
|
|
chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
|
//chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!?$%&=#+<>-:,.;_*@"
|
|
)
|
|
|
|
func generatePassword() string {
|
|
password := make([]byte, passwordLength)
|
|
_, err := rand.Read(password)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
for i := 0; i < passwordLength; i++ {
|
|
password[i] = chars[int(password[i])%len(chars)]
|
|
}
|
|
|
|
return string(password)
|
|
}
|
|
|
|
func passwordHandler(w http.ResponseWriter, r *http.Request) {
|
|
password := generatePassword()
|
|
fmt.Fprint(w, password)
|
|
}
|
|
|
|
func helpHandler(w http.ResponseWriter, r *http.Request) {
|
|
helpHTML := `
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>Hilfe</title>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<style>
|
|
body {
|
|
font-family: 'Helvetica Neue', Arial, sans-serif;
|
|
display: flex;
|
|
justify-content: center;
|
|
align-items: center;
|
|
height: 100vh;
|
|
margin: 0;
|
|
background-color: #f5f5f5;
|
|
color: #333;
|
|
}
|
|
.help-container {
|
|
text-align: left;
|
|
background: white;
|
|
padding: 2rem;
|
|
border-radius: 8px;
|
|
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
|
|
max-width: 800px;
|
|
width: 90%;
|
|
min-width: 600px;
|
|
}
|
|
h1 {
|
|
font-size: 1.5rem;
|
|
margin-bottom: 1.5rem;
|
|
color: #444;
|
|
}
|
|
pre {
|
|
font-family: 'Courier New', Courier, monospace;
|
|
background: #f0f0f0;
|
|
padding: 0.8rem;
|
|
border-radius: 4px;
|
|
overflow-x: auto;
|
|
}
|
|
a {
|
|
color: #007BFF;
|
|
text-decoration: none;
|
|
}
|
|
a:hover {
|
|
text-decoration: underline;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="help-container">
|
|
<h1>Hilfe: API-Endpunkt</h1>
|
|
<p>
|
|
Diese Anwendung bietet einen API-Endpunkt, um Passwörter direkt über die Kommandozeile abzurufen.
|
|
Der Endpunkt gibt das Passwort im Plain-Text-Format zurück.
|
|
</p>
|
|
<h2>Endpunkt:</h2>
|
|
<p><code>http://localhost:8080/api/password</code></p>
|
|
<h2>Beispiele:</h2>
|
|
<h3>Mac/Linux (Terminal):</h3>
|
|
<pre>echo $(curl -s http://localhost:8080/api/password)</pre>
|
|
<h3>Windows (PowerShell):</h3>
|
|
<pre>Invoke-RestMethod -Uri http://localhost:8080/api/password</pre>
|
|
<h3>Windows (cmd):</h3>
|
|
<pre>curl http://localhost:8080/api/password</pre>
|
|
<p>
|
|
<a href="/">Zurück zur Passwort-Generierung</a>
|
|
</p>
|
|
</div>
|
|
</body>
|
|
</html>
|
|
`
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
fmt.Fprint(w, helpHTML)
|
|
}
|
|
|
|
func webHandler(w http.ResponseWriter, r *http.Request) {
|
|
password := generatePassword()
|
|
html := fmt.Sprintf(
|
|
`<DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>Passwort-Generator</title>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<style>
|
|
body {
|
|
font-family: 'Helvetica Neue', Arial, sans-serif;
|
|
display: flex;
|
|
justify-content: center;
|
|
align-items: center;
|
|
height: 100vh;
|
|
margin: 0;
|
|
background-color: #f5f5f5;
|
|
color: #333;
|
|
}
|
|
.container {
|
|
text-align: center;
|
|
background: white;
|
|
padding: 2rem;
|
|
border-radius: 8px;
|
|
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
|
|
width: 90%%;
|
|
min-width: 600px;
|
|
position: relative;
|
|
}
|
|
h1 {
|
|
font-size: 1.5rem;
|
|
margin-bottom: 1.5rem;
|
|
color: #444;
|
|
}
|
|
#password {
|
|
font-family: 'Courier New', Courier, monospace;
|
|
font-size: 1.2rem;
|
|
letter-spacing: 1px;
|
|
margin: 1rem auto;
|
|
padding: 0.8rem;
|
|
background: #f0f0f0;
|
|
border-radius: 4px;
|
|
border: 1px solid #ddd;
|
|
width: 90%%;
|
|
word-break: break-all;
|
|
}
|
|
.copy-button {
|
|
background: #4CAF50;
|
|
color: white;
|
|
border: none;
|
|
padding: 0.6rem 1.2rem;
|
|
font-size: 1rem;
|
|
border-radius: 4px;
|
|
cursor: pointer;
|
|
transition: background 0.2s;
|
|
margin: 0.3rem;
|
|
}
|
|
.copy-button:hover {
|
|
background: #45a049;
|
|
}
|
|
.renew-button {
|
|
background: #007BFF;
|
|
color: white;
|
|
border: none;
|
|
padding: 0.6rem 1.2rem;
|
|
font-size: 1rem;
|
|
border-radius: 4px;
|
|
cursor: pointer;
|
|
transition: background 0.2s;
|
|
margin: 0.3rem;
|
|
}
|
|
.renew-button:hover {
|
|
background: #0056b3;
|
|
}
|
|
.about-link {
|
|
position: absolute;
|
|
top: 1rem;
|
|
left: 1rem;
|
|
font-size: 1.2rem;
|
|
color: #999;
|
|
text-decoration: none;
|
|
}
|
|
.about-link:hover {
|
|
color: #444;
|
|
}
|
|
.help-link {
|
|
position: absolute;
|
|
top: 1rem;
|
|
right: 1rem;
|
|
font-size: 1.2rem;
|
|
color: #999;
|
|
text-decoration: none;
|
|
}
|
|
.help-link:hover {
|
|
color: #444;
|
|
}
|
|
.buttons {
|
|
display: flex;
|
|
justify-content: center;
|
|
gap: 0.5rem;
|
|
}
|
|
#toast {
|
|
visibility: hidden;
|
|
min-width: 150px;
|
|
background-color: #4CAF50;
|
|
color: white;
|
|
text-align: center;
|
|
border-radius: 4px;
|
|
padding: 0.5rem;
|
|
position: fixed;
|
|
top: 20px;
|
|
right: 20px;
|
|
z-index: 1;
|
|
font-size: 0.9rem;
|
|
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<a href="/help" class="help-link">API</a>
|
|
<a class="about-link" href="https://gitea.scu.si/FlorianWalther/Web-Password">code</a>
|
|
<h1>Generiertes Passwort</h1>
|
|
<div id="password">%s</div>
|
|
<div class="buttons">
|
|
<button class="copy-button" onclick="copyToClipboard()">In Zwischenablage kopieren</button>
|
|
<button class="renew-button" onclick="generateNewPassword()">Neues Passwort generieren</button>
|
|
</div>
|
|
<div id="toast">✓ Kopiert!</div>
|
|
</div>
|
|
<script>
|
|
function copyToClipboard() {
|
|
const password = document.getElementById("password").innerText;
|
|
navigator.clipboard.writeText(password).then(() => {
|
|
const toast = document.getElementById("toast");
|
|
toast.style.visibility = "visible";
|
|
setTimeout(() => {
|
|
toast.style.visibility = "hidden";
|
|
}, 1500); // Toast verschwindet nach 1,5 Sekunden
|
|
}).catch(err => {
|
|
console.error("Fehler beim Kopieren: ", err);
|
|
alert("Kopieren fehlgeschlagen. Bitte manuell kopieren: " + password);
|
|
});
|
|
}
|
|
function generateNewPassword() {
|
|
fetch("/api/password")
|
|
.then(response => response.text())
|
|
.then(password => {
|
|
document.getElementById("password").innerText = password;
|
|
})
|
|
.catch(error => console.error("Fehler:", error));
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>`,
|
|
password,
|
|
)
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
fmt.Fprint(w, html)
|
|
}
|
|
|
|
func main() {
|
|
http.HandleFunc("/", webHandler)
|
|
http.HandleFunc("/api/password", passwordHandler)
|
|
http.HandleFunc("/help", helpHandler)
|
|
log.Println("Server läuft auf http://localhost:8080")
|
|
log.Println("Plain-Text-Passwort: curl http://localhost:8080/api/password")
|
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
|
}
|
|
|