230 lines
5.3 KiB
Go
230 lines
5.3 KiB
Go
package main
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
const (
|
|
passwordLength = 32
|
|
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>curl 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;
|
|
}
|
|
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;
|
|
}
|
|
button:hover {
|
|
background: #0056b3;
|
|
}
|
|
.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;
|
|
}
|
|
</style>
|
|
<script>
|
|
function copyToClipboard() {
|
|
navigator.clipboard.writeText(document.getElementById("password").innerText);
|
|
alert("Passwort kopiert!");
|
|
}
|
|
function generateNewPassword() {
|
|
fetch("/api/password")
|
|
.then(response => response.text())
|
|
.then(password => {
|
|
document.getElementById("password").innerText = password;
|
|
})
|
|
.catch(error => console.error("Fehler:", error));
|
|
}
|
|
</script>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<a href="/help" class="help-link">?</a>
|
|
<h1>Generiertes Passwort</h1>
|
|
<div id="password">%s</div>
|
|
<div class="buttons">
|
|
<button onclick="copyToClipboard()">In Zwischenablage kopieren</button>
|
|
<button onclick="generateNewPassword()">Neues Passwort generieren</button>
|
|
</div>
|
|
</div>
|
|
</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))
|
|
}
|
|
|