-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.go
229 lines (203 loc) · 6.24 KB
/
routes.go
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
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/altcha-org/altcha-lib-go"
"html/template"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
// handleRoot serves the root page with server info
func (s *Server) handleRoot(w http.ResponseWriter, r *http.Request) {
s.mutex.RLock()
defer s.mutex.RUnlock()
// Calculate total stats
var totalChallenges, solvedChallenges, failedChallenges int64
for _, stats := range s.config.Stats {
totalChallenges += stats.TotalChallenges
solvedChallenges += stats.SolvedChallenges
failedChallenges += stats.FailedChallenges
}
// Calculate success rate
var successRate float64
if totalChallenges > 0 {
successRate = float64(solvedChallenges) / float64(totalChallenges) * 100
}
// HTML template
htmlTemplate := `
<!DOCTYPE html>
<html>
<head>
<title>Verity Server Info</title>
<style>
body { font-family: sans-serif; padding: 20px; }
h1 { color: #333; }
.info { border: 1px solid #ddd; padding: 10px; margin-top: 20px; width: 300px;}
.info p { margin: 5px 0; }
</style>
</head>
<body>
<h1>Verity Server Info</h1>
<div class="info">
<p>This is <a href="https://github.com/Flameborn/verity">Verity</a>, a tiny server for <a href="https://altcha.org/">Altcha</a> Made by Erion (AKA Flameborn).</p>
</div>
<div class="info">
<p><strong>Total Challenges:</strong> {{.TotalChallenges}}</p>
<p><strong>Solved Challenges:</strong> {{.SolvedChallenges}}</p>
<p><strong>Failed Challenges:</strong> {{.FailedChallenges}}</p>
<p><strong>Success Rate:</strong> {{.SuccessRate}}</p>
</div>
</body>
</html>
`
// Data for the template
data := struct {
TotalChallenges int64
SolvedChallenges int64
FailedChallenges int64
SuccessRate string
}{
TotalChallenges: totalChallenges,
SolvedChallenges: solvedChallenges,
FailedChallenges: failedChallenges,
SuccessRate: fmt.Sprintf("%.2f%%", successRate),
}
// Parse and execute the template
tmpl, err := template.New("serverInfo").Parse(htmlTemplate)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
err = tmpl.Execute(w, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// handleGetChallenge generates a new challenge
func (s *Server) handleGetChallenge(w http.ResponseWriter, r *http.Request) {
// Get API key from context
apiKey, ok := r.Context().Value(APIKeyContextKey).(string)
if !ok {
writeErrorResponse(w, "Missing API key in context", http.StatusInternalServerError)
return
}
// Get client IP address
ip := GetRealIP(r)
// Parse expire time from config
duration, err := time.ParseDuration(s.config.ExpireTime)
if err != nil {
duration = 5 * time.Minute // Default to 5 minutes
}
expires := time.Now().Add(duration)
complexity := s.getAdjustedComplexity(ip)
// Create challenge
challengeOptions := altcha.ChallengeOptions{
Algorithm: s.config.Algorithm,
MaxNumber: complexity,
HMACKey: s.config.HMACKey,
Expires: &expires,
}
challenge, err := altcha.CreateChallenge(challengeOptions)
if err != nil {
writeErrorResponse(w, fmt.Sprintf("Failed to create challenge: %v", err), http.StatusInternalServerError)
return
}
// Update stats
s.mutex.Lock()
stats := s.config.Stats[apiKey]
stats.TotalChallenges++
// Track IP throttling
if stats.IPThrottleCount == nil {
stats.IPThrottleCount = make(map[string]int64)
}
stats.IPThrottleCount[ip]++
s.config.Stats[apiKey] = stats
s.mutex.Unlock()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(challenge)
}
// handleVerifyChallenge verifies a challenge solution
func (s *Server) handleVerifyChallenge(w http.ResponseWriter, r *http.Request) {
apiKey, ok := r.Context().Value(APIKeyContextKey).(string)
if !ok {
writeErrorResponse(w, "Missing API key in context", http.StatusInternalServerError)
return
}
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
writeErrorResponse(w, "Failed to read request body", http.StatusBadRequest)
return
}
defer r.Body.Close()
encodedPayload := strings.TrimSpace(string(bodyBytes))
// Decode base64 to check for replay attacks.
decodedBytes, err := base64.StdEncoding.DecodeString(encodedPayload)
if err != nil {
writeErrorResponse(w, "Invalid base64 encoding", http.StatusBadRequest)
return
}
var payload map[string]interface{}
if err := json.Unmarshal(decodedBytes, &payload); err != nil {
fmt.Printf("\nJSON unmarshal error: %v\n", err)
writeErrorResponse(w, "Invalid JSON payload", http.StatusBadRequest)
return
}
fmt.Printf("Salt: %s\n", payload["salt"])
// Extract challenge ID and expiration
challengeID, ok := payload["challenge"].(string)
if !ok {
writeErrorResponse(w, "Invalid challenge format", http.StatusBadRequest)
return
}
splitSalt := strings.Split(payload["salt"].(string), "?")
if len(splitSalt) <= 1 {
// Missing salt parameters, at least expiration is required.
writeErrorResponse(w, "Invalid challenge format", http.StatusBadRequest)
return
}
params, _ := url.ParseQuery(splitSalt[1])
challengeExpire, err := strconv.ParseInt(params.Get("expires"), 10, 64)
if err != nil || challengeExpire < 1 {
writeErrorResponse(w, "Invalid challenge format", http.StatusBadRequest)
return
}
// Check for duplicate challenge
if cm.Exists(challengeID) {
writeErrorResponse(w, "Challenge already solved", http.StatusConflict)
return
}
// Verify payload
verified, err := altcha.VerifySolution(encodedPayload, s.config.HMACKey, true)
if err != nil {
writeErrorResponse(w, fmt.Sprintf("Verification error: %v", err), http.StatusInternalServerError)
return
}
s.mutex.Lock()
defer s.mutex.Unlock()
w.Header().Set("Content-Type", "application/json")
if verified {
cm.AddChallenge(challengeID, challengeExpire)
stats := s.config.Stats[apiKey]
stats.SolvedChallenges++
s.config.Stats[apiKey] = stats
json.NewEncoder(w).Encode(Response{
Code: http.StatusOK,
Message: "OK",
})
} else {
stats := s.config.Stats[apiKey]
stats.FailedChallenges++
s.config.Stats[apiKey] = stats
json.NewEncoder(w).Encode(Response{
Code: http.StatusBadRequest,
Message: "Invalid payload",
})
}
return
}