Refactor code structure for improved readability and maintainability
This commit is contained in:
+22
-4
@@ -12,6 +12,7 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -80,11 +81,17 @@ Example:
|
||||
}
|
||||
fmt.Printf("✓ Logged in, device ID: %s\n", deviceID)
|
||||
|
||||
// Step 3: Print results
|
||||
// Step 3: Generate pickle key for E2EE crypto store
|
||||
pickleKey := generatePickleKey()
|
||||
|
||||
// Derive env var prefix from envVar (e.g. MATRIX_TOKEN_FOO → FOO)
|
||||
norm := strings.TrimPrefix(envVar, "MATRIX_TOKEN_")
|
||||
|
||||
// Step 4: Print results — parseable lines for register.sh
|
||||
fmt.Println("\n─── Add to your .env ───────────────────────────────")
|
||||
fmt.Printf("%s=%s\n", envVar, token)
|
||||
fmt.Printf("MATRIX_HOMESERVER=%s\n", homeserver)
|
||||
fmt.Printf("MATRIX_SERVER_NAME=%s\n", serverName)
|
||||
fmt.Printf("MATRIX_PASSWORD_%s=%s\n", norm, password)
|
||||
fmt.Printf("PICKLE_KEY_%s=%s\n", norm, pickleKey)
|
||||
fmt.Println("────────────────────────────────────────────────────")
|
||||
fmt.Printf("\nUser ID: %s\n", userID)
|
||||
fmt.Printf("Device ID: %s\n", deviceID)
|
||||
@@ -176,7 +183,6 @@ func loginAs(homeserver, username, password string) (token, deviceID string, err
|
||||
|
||||
// generatePassword creates a random-enough password for the bot account.
|
||||
func generatePassword() string {
|
||||
// Simple: use os.ReadFile on /dev/urandom, encode hex
|
||||
f, err := os.Open("/dev/urandom")
|
||||
if err != nil {
|
||||
return "agent-bot-default-please-change"
|
||||
@@ -186,3 +192,15 @@ func generatePassword() string {
|
||||
_, _ = io.ReadFull(f, buf)
|
||||
return fmt.Sprintf("%x", buf)
|
||||
}
|
||||
|
||||
// generatePickleKey creates a 32-byte hex-encoded key for E2EE crypto store encryption.
|
||||
func generatePickleKey() string {
|
||||
f, err := os.Open("/dev/urandom")
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
defer f.Close()
|
||||
buf := make([]byte, 32)
|
||||
_, _ = io.ReadFull(f, buf)
|
||||
return hex.EncodeToString(buf)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user