312 lines
8.8 KiB
Go
312 lines
8.8 KiB
Go
|
|
package app
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"html/template"
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"strings"
|
||
|
|
)
|
||
|
|
|
||
|
|
// WebTemplateManager handles HTML template management for web honeypots
|
||
|
|
type WebTemplateManager struct {
|
||
|
|
templateDir string
|
||
|
|
templates map[string]*template.Template
|
||
|
|
}
|
||
|
|
|
||
|
|
// NewWebTemplateManager creates a new template manager
|
||
|
|
func NewWebTemplateManager(configPath string) *WebTemplateManager {
|
||
|
|
templateDir := filepath.Join(filepath.Dir(configPath), "webtemplates")
|
||
|
|
|
||
|
|
// Ensure template directory exists
|
||
|
|
os.MkdirAll(templateDir, 0755)
|
||
|
|
|
||
|
|
return &WebTemplateManager{
|
||
|
|
templateDir: templateDir,
|
||
|
|
templates: make(map[string]*template.Template),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetTemplateDir returns the template directory path
|
||
|
|
func (wtm *WebTemplateManager) GetTemplateDir() string {
|
||
|
|
return wtm.templateDir
|
||
|
|
}
|
||
|
|
|
||
|
|
// CreateDefaultTemplate creates a default login template if it doesn't exist
|
||
|
|
func (wtm *WebTemplateManager) CreateDefaultTemplate(templateName string) error {
|
||
|
|
templatePath := filepath.Join(wtm.templateDir, templateName)
|
||
|
|
|
||
|
|
// Check if template already exists
|
||
|
|
if _, err := os.Stat(templatePath); err == nil {
|
||
|
|
return nil // Template already exists
|
||
|
|
}
|
||
|
|
|
||
|
|
defaultHTML := `<!DOCTYPE html>
|
||
|
|
<html lang="en">
|
||
|
|
<head>
|
||
|
|
<meta charset="UTF-8">
|
||
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
|
|
<title>Login - {{ .ServiceName }}</title>
|
||
|
|
<style>
|
||
|
|
* {
|
||
|
|
margin: 0;
|
||
|
|
padding: 0;
|
||
|
|
box-sizing: border-box;
|
||
|
|
}
|
||
|
|
body {
|
||
|
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
||
|
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||
|
|
min-height: 100vh;
|
||
|
|
display: flex;
|
||
|
|
align-items: center;
|
||
|
|
justify-content: center;
|
||
|
|
}
|
||
|
|
.login-container {
|
||
|
|
background: white;
|
||
|
|
padding: 2rem;
|
||
|
|
border-radius: 10px;
|
||
|
|
box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
|
||
|
|
width: 100%;
|
||
|
|
max-width: 400px;
|
||
|
|
}
|
||
|
|
.login-header {
|
||
|
|
text-align: center;
|
||
|
|
margin-bottom: 2rem;
|
||
|
|
}
|
||
|
|
.login-header h1 {
|
||
|
|
color: #333;
|
||
|
|
font-size: 1.8rem;
|
||
|
|
margin-bottom: 0.5rem;
|
||
|
|
}
|
||
|
|
.login-header p {
|
||
|
|
color: #666;
|
||
|
|
font-size: 0.9rem;
|
||
|
|
}
|
||
|
|
.form-group {
|
||
|
|
margin-bottom: 1.5rem;
|
||
|
|
}
|
||
|
|
.form-group label {
|
||
|
|
display: block;
|
||
|
|
margin-bottom: 0.5rem;
|
||
|
|
color: #333;
|
||
|
|
font-weight: 500;
|
||
|
|
}
|
||
|
|
.form-group input {
|
||
|
|
width: 100%;
|
||
|
|
padding: 0.75rem;
|
||
|
|
border: 2px solid #e1e5e9;
|
||
|
|
border-radius: 5px;
|
||
|
|
font-size: 1rem;
|
||
|
|
transition: border-color 0.3s ease;
|
||
|
|
}
|
||
|
|
.form-group input:focus {
|
||
|
|
outline: none;
|
||
|
|
border-color: #667eea;
|
||
|
|
}
|
||
|
|
.login-button {
|
||
|
|
width: 100%;
|
||
|
|
padding: 0.75rem;
|
||
|
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||
|
|
color: white;
|
||
|
|
border: none;
|
||
|
|
border-radius: 5px;
|
||
|
|
font-size: 1rem;
|
||
|
|
font-weight: 600;
|
||
|
|
cursor: pointer;
|
||
|
|
transition: transform 0.2s ease;
|
||
|
|
}
|
||
|
|
.login-button:hover {
|
||
|
|
transform: translateY(-2px);
|
||
|
|
}
|
||
|
|
.error-message {
|
||
|
|
background: #fee;
|
||
|
|
color: #c33;
|
||
|
|
padding: 0.75rem;
|
||
|
|
border-radius: 5px;
|
||
|
|
margin-bottom: 1rem;
|
||
|
|
text-align: center;
|
||
|
|
display: none;
|
||
|
|
}
|
||
|
|
.footer {
|
||
|
|
text-align: center;
|
||
|
|
margin-top: 2rem;
|
||
|
|
color: #666;
|
||
|
|
font-size: 0.8rem;
|
||
|
|
}
|
||
|
|
</style>
|
||
|
|
</head>
|
||
|
|
<body>
|
||
|
|
<div class="login-container">
|
||
|
|
<div class="login-header">
|
||
|
|
<h1>{{ .ServiceName }}</h1>
|
||
|
|
<p>Please sign in to continue</p>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<div class="error-message" id="error-message">
|
||
|
|
Invalid username or password
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<form method="POST" action="{{ .LoginPath }}">
|
||
|
|
<div class="form-group">
|
||
|
|
<label for="username">Username</label>
|
||
|
|
<input type="text" id="username" name="username" required autocomplete="username">
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<div class="form-group">
|
||
|
|
<label for="password">Password</label>
|
||
|
|
<input type="password" id="password" name="password" required autocomplete="current-password">
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<button type="submit" class="login-button">Sign In</button>
|
||
|
|
</form>
|
||
|
|
|
||
|
|
<div class="footer">
|
||
|
|
<p>© 2024 {{ .ServiceName }}. All rights reserved.</p>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<script>
|
||
|
|
// Show error message if login failed
|
||
|
|
const urlParams = new URLSearchParams(window.location.search);
|
||
|
|
if (urlParams.get('error') === 'invalid') {
|
||
|
|
document.getElementById('error-message').style.display = 'block';
|
||
|
|
}
|
||
|
|
|
||
|
|
// Focus on username field
|
||
|
|
document.getElementById('username').focus();
|
||
|
|
</script>
|
||
|
|
</body>
|
||
|
|
</html>`
|
||
|
|
|
||
|
|
return os.WriteFile(templatePath, []byte(defaultHTML), 0644)
|
||
|
|
}
|
||
|
|
|
||
|
|
// LoadTemplate loads and parses a template file
|
||
|
|
func (wtm *WebTemplateManager) LoadTemplate(templateName string) (*template.Template, error) {
|
||
|
|
// Check if template is already loaded
|
||
|
|
if tmpl, exists := wtm.templates[templateName]; exists {
|
||
|
|
return tmpl, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
templatePath := filepath.Join(wtm.templateDir, templateName)
|
||
|
|
|
||
|
|
// Create default template if it doesn't exist
|
||
|
|
if _, err := os.Stat(templatePath); os.IsNotExist(err) {
|
||
|
|
if err := wtm.CreateDefaultTemplate(templateName); err != nil {
|
||
|
|
return nil, fmt.Errorf("failed to create default template: %w", err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Parse template
|
||
|
|
tmpl, err := template.ParseFiles(templatePath)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("failed to parse template %s: %w", templateName, err)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Cache template
|
||
|
|
wtm.templates[templateName] = tmpl
|
||
|
|
|
||
|
|
return tmpl, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// SaveTemplate saves template content to file
|
||
|
|
func (wtm *WebTemplateManager) SaveTemplate(templateName, content string) error {
|
||
|
|
templatePath := filepath.Join(wtm.templateDir, templateName)
|
||
|
|
|
||
|
|
// Validate template by parsing it
|
||
|
|
_, err := template.New("test").Parse(content)
|
||
|
|
if err != nil {
|
||
|
|
return fmt.Errorf("invalid template syntax: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Save to file
|
||
|
|
if err := os.WriteFile(templatePath, []byte(content), 0644); err != nil {
|
||
|
|
return fmt.Errorf("failed to save template: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Remove from cache to force reload
|
||
|
|
delete(wtm.templates, templateName)
|
||
|
|
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetTemplate returns template content as string
|
||
|
|
func (wtm *WebTemplateManager) GetTemplate(templateName string) (string, error) {
|
||
|
|
templatePath := filepath.Join(wtm.templateDir, templateName)
|
||
|
|
|
||
|
|
content, err := os.ReadFile(templatePath)
|
||
|
|
if err != nil {
|
||
|
|
// If template doesn't exist, create default and return it
|
||
|
|
if os.IsNotExist(err) {
|
||
|
|
if err := wtm.CreateDefaultTemplate(templateName); err != nil {
|
||
|
|
return "", fmt.Errorf("failed to create default template: %w", err)
|
||
|
|
}
|
||
|
|
content, err = os.ReadFile(templatePath)
|
||
|
|
if err != nil {
|
||
|
|
return "", fmt.Errorf("failed to read created template: %w", err)
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
return "", fmt.Errorf("failed to read template: %w", err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return string(content), nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// ListTemplates returns a list of available templates
|
||
|
|
func (wtm *WebTemplateManager) ListTemplates() ([]string, error) {
|
||
|
|
files, err := os.ReadDir(wtm.templateDir)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("failed to read template directory: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
var templates []string
|
||
|
|
for _, file := range files {
|
||
|
|
if !file.IsDir() && strings.HasSuffix(file.Name(), ".html") {
|
||
|
|
templates = append(templates, file.Name())
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return templates, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// DeleteTemplate removes a template file
|
||
|
|
func (wtm *WebTemplateManager) DeleteTemplate(templateName string) error {
|
||
|
|
templatePath := filepath.Join(wtm.templateDir, templateName)
|
||
|
|
|
||
|
|
if err := os.Remove(templatePath); err != nil {
|
||
|
|
return fmt.Errorf("failed to delete template: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Remove from cache
|
||
|
|
delete(wtm.templates, templateName)
|
||
|
|
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// ValidateTemplate checks if template has required fields
|
||
|
|
func (wtm *WebTemplateManager) ValidateTemplate(content string) error {
|
||
|
|
// Check for required form fields
|
||
|
|
requiredFields := []string{
|
||
|
|
`name="username"`,
|
||
|
|
`name="password"`,
|
||
|
|
`method="POST"` + ` ` + `action=`, // Split to avoid matching this comment
|
||
|
|
}
|
||
|
|
|
||
|
|
contentLower := strings.ToLower(content)
|
||
|
|
|
||
|
|
for _, field := range requiredFields {
|
||
|
|
if !strings.Contains(contentLower, strings.ToLower(field)) {
|
||
|
|
return fmt.Errorf("template missing required field: %s", field)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Try to parse as template
|
||
|
|
_, err := template.New("validation").Parse(content)
|
||
|
|
if err != nil {
|
||
|
|
return fmt.Errorf("invalid template syntax: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
return nil
|
||
|
|
}
|