1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
package main
import (
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"strings"
"wg-portal/internal"
)
// APIResponse represents a standard API response structure
type APIResponse struct {
Success bool `json:"success"`
Data any `json:"data,omitempty"`
Error string `json:"error,omitempty"`
}
// Server encapsulates our HTTP server
type Server struct {
mux *http.ServeMux
templates *template.Template
config *internal.Config
sessionManager *internal.SessionManager
}
// NewServer creates a new server instance
func NewServer(config *internal.Config) (*Server, error) {
// Parse templates
templates, err := template.ParseFiles("templates/index.html", "templates/login.html")
if err != nil {
return nil, fmt.Errorf("failed to parse templates: %w", err)
}
s := &Server{
mux: http.NewServeMux(),
templates: templates,
config: config,
sessionManager: internal.NewSessionManager(),
}
s.setupRoutes()
return s, nil
}
// setupRoutes configures all HTTP routes
func (s *Server) setupRoutes() {
// Serve static files (no auth required)
s.mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))
// Auth routes (no auth required)
s.mux.HandleFunc("/login", s.handleLogin)
s.mux.HandleFunc("/logout", s.handleLogout)
// Protected routes
s.mux.HandleFunc("/", s.requireAuth(s.handleHome))
s.mux.HandleFunc("/api/connections", s.requireAuth(s.handleConnectionsAPI))
s.mux.HandleFunc("/api/connections/toggle", s.requireAuth(s.handleToggleAPI))
s.mux.HandleFunc("/api/status", s.requireAuth(s.handleStatusAPI))
}
// handleHome serves the main HTML page
func (s *Server) handleHome(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if err := s.templates.ExecuteTemplate(w, "index.html", nil); err != nil {
log.Printf("Error rendering template: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
}
// handleConnectionsAPI returns connection data as JSON
func (s *Server) handleConnectionsAPI(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
s.sendErrorResponse(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
connections, err := internal.GetConnections()
if err != nil {
log.Printf("Error getting connections: %v", err)
s.sendErrorResponse(w, fmt.Sprintf("%v", err), http.StatusInternalServerError)
return
}
s.sendSuccessResponse(w, connections)
}
// handleToggleAPI handles connection toggle requests
func (s *Server) handleToggleAPI(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
s.sendErrorResponse(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
var req struct {
Name string `json:"name"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
s.sendErrorResponse(w, "Invalid JSON", http.StatusBadRequest)
return
}
if strings.TrimSpace(req.Name) == "" {
s.sendErrorResponse(w, "Connection name is required", http.StatusBadRequest)
return
}
output, err := internal.ToggleConnection(req.Name)
if err != nil {
log.Printf("Error toggling connection %s: %v (output: %s)", req.Name, err, string(output))
s.sendErrorResponse(w, err.Error(), http.StatusInternalServerError)
return
}
response := map[string]any{
"message": fmt.Sprintf("Connection %s toggled successfully", req.Name),
"output": string(output),
}
s.sendSuccessResponse(w, response)
}
// handleStatusAPI returns WireGuard status information
func (s *Server) handleStatusAPI(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
s.sendErrorResponse(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
status, err := internal.GetStatus()
if err != nil {
log.Printf("Error getting status: %v", err)
s.sendErrorResponse(w, fmt.Sprintf("%v", err), http.StatusInternalServerError)
return
}
response := map[string]any{
"status": status,
}
s.sendSuccessResponse(w, response)
}
// sendSuccessResponse sends a JSON success response
func (*Server) sendSuccessResponse(w http.ResponseWriter, data any) {
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(APIResponse{
Success: true,
Data: data,
})
}
// sendErrorResponse sends a JSON error response
func (*Server) sendErrorResponse(w http.ResponseWriter, message string, statusCode int) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
_ = json.NewEncoder(w).Encode(APIResponse{
Success: false,
Error: message,
})
}
// requireAuth middleware checks for valid authentication
func (s *Server) requireAuth(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("session_id")
if err != nil {
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
_, valid := s.sessionManager.ValidateSession(cookie.Value)
if !valid {
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
next(w, r)
}
}
// handleLogin handles login form display and processing
func (s *Server) handleLogin(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
// Show login form
w.Header().Set("Content-Type", "text/html; charset=utf-8")
templateData := map[string]any{
"Error": "",
}
if err := s.templates.ExecuteTemplate(w, "login.html", templateData); err != nil {
log.Printf("Error rendering login template: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
case http.MethodPost:
password := r.FormValue("password")
// Validate credentials
if internal.ValidatePassword(password, s.config.PasswordHash) {
// Create session
sessionID, expires, err := s.sessionManager.CreateSession()
if err != nil {
log.Printf("Error creating session: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
// Set session cookie
cookie := &http.Cookie{
Name: "session_id",
Value: sessionID,
Expires: expires,
HttpOnly: true,
SameSite: http.SameSiteStrictMode,
}
http.SetCookie(w, cookie)
http.Redirect(w, r, "/", http.StatusSeeOther)
} else {
// Invalid credentials
w.Header().Set("Content-Type", "text/html; charset=utf-8")
templateData := map[string]any{
"Error": "Wrong password",
}
if err := s.templates.ExecuteTemplate(w, "login.html", templateData); err != nil {
log.Printf("Error rendering login template: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
}
default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
}
// handleLogout handles user logout
func (s *Server) handleLogout(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Get session cookie and delete session
if cookie, err := r.Cookie("session_id"); err == nil {
s.sessionManager.DeleteSession(cookie.Value)
}
// Clear session cookie
cookie := &http.Cookie{
Name: "session_id",
Value: "",
MaxAge: -1,
HttpOnly: true,
SameSite: http.SameSiteStrictMode,
}
http.SetCookie(w, cookie)
http.Redirect(w, r, "/login", http.StatusSeeOther)
}
// Start starts the HTTP server
func (s *Server) Start() error {
addr := s.config.GetAddress()
log.Printf("Starting on http://%s\n", addr)
return http.ListenAndServe(addr, s.mux)
}
func main() {
// Load configuration
config, err := internal.LoadConfig("config.yml")
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
server, err := NewServer(config)
if err != nil {
log.Fatalf("Failed to create server: %v", err)
}
if err := server.Start(); err != nil {
log.Fatalf("Server failed to start: %v", err)
}
}
|