-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
267 lines (226 loc) · 6.07 KB
/
Copy pathmain.go
File metadata and controls
267 lines (226 loc) · 6.07 KB
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
package main
import (
"embed"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"html/template"
"io/fs"
"log"
"math/rand"
"net/http"
"strings"
"sync"
)
//go:embed static/*
var staticFiles embed.FS
//go:embed static/index.html
var indexHTML string
type LotterySystem struct {
mu sync.Mutex
Players []string
}
type DrawRequest struct {
Count int `json:"count"`
Players []string `json:"players"`
ElapsedTime int64 `json:"elapsedTime"`
}
type DrawResponse struct {
Winners []string `json:"winners"`
Seed string `json:"seed"`
}
// 从drand获取随机数作为随机源
func getDrandRandomness() (int64, error) {
resp, err := http.Get("https://drand.cloudflare.com/public/latest")
if err != nil {
return 0, err
}
defer resp.Body.Close()
var result struct {
Randomness string `json:"randomness"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return 0, err
}
// 解码十六进制字符串
randomBytes, err := hex.DecodeString(result.Randomness)
if err != nil {
return 0, err
}
// 使用所有字节计算一个种子值
var seed int64
for i := 0; i < len(randomBytes); i++ {
seed = (seed << 8) | int64(randomBytes[i])
if (i+1)%8 == 0 {
seed ^= seed >> 32
}
}
return seed, nil
}
// 从CSPRNG获取随机数作为随机源
func getCSPRNGRandomness() (int64, error) {
resp, err := http.Get("https://csprng.xyz/v1/api")
if err != nil {
return 0, err
}
defer resp.Body.Close()
var result struct {
Data string `json:"Data"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return 0, err
}
// 解码Base64字符串
randomBytes, err := base64.StdEncoding.DecodeString(result.Data)
if err != nil {
return 0, err
}
// 使用所有字节计算一个种子值
var seed int64
for i := 0; i < len(randomBytes); i++ {
// 循环左移并异或
seed = (seed << 8) | int64(randomBytes[i])
// 每8字节做一次异或混合
if (i+1)%8 == 0 {
seed ^= seed >> 32
}
}
return seed, nil
}
func (ls *LotterySystem) Draw(count int, elapsedTime int64) ([]string, string, error) {
ls.mu.Lock()
defer ls.mu.Unlock()
if count > len(ls.Players) {
return nil, "", fmt.Errorf("抽奖人数超过参与人数")
}
// 获取���个随机源
drandSeed, err := getDrandRandomness()
if err != nil {
return nil, "", err
}
csprngSeed, err := getCSPRNGRandomness()
if err != nil {
return nil, "", err
}
// 创建两个独立的随机数生成器
log.Println("drandseed", drandSeed)
r1 := rand.New(rand.NewSource(drandSeed))
log.Println("csprngseed", csprngSeed)
r2 := rand.New(rand.NewSource(csprngSeed))
// 创建参与者索引切片
indices := make([]int, len(ls.Players))
for i := range indices {
indices[i] = i
}
// 第一轮洗牌
for round := 0; round < 3; round++ {
// 使用第一个随机源洗牌
for i := len(indices) - 1; i > 0; i-- {
j := r1.Intn(i + 1)
indices[i], indices[j] = indices[j], indices[i]
}
// 使用第二个随机源再次洗牌
for i := len(indices) - 1; i > 0; i-- {
j := r2.Intn(i + 1)
indices[i], indices[j] = indices[j], indices[i]
}
// 随机决定是否反转切片
if r1.Intn(2) == 1 {
for i, j := 0, len(indices)-1; i < j; i, j = i+1, j-1 {
indices[i], indices[j] = indices[j], indices[i]
}
}
// 随机交换一些位置
swapCount := r2.Intn(len(indices)/2) + 1
for i := 0; i < swapCount; i++ {
a := r1.Intn(len(indices))
b := r2.Intn(len(indices))
indices[a], indices[b] = indices[b], indices[a]
}
}
log.Println("elapsedTime", elapsedTime)
// 使用时间戳作为新的随机源进行二次打散
timeRand := rand.New(rand.NewSource(elapsedTime))
// 第二轮洗牌(使用时间随机源)
for i := len(indices) - 1; i > 0; i-- {
j := timeRand.Intn(i + 1)
indices[i], indices[j] = indices[j], indices[i]
}
// 再次随机交换一些位置
swapCount := timeRand.Intn(len(indices)/2) + 1
for i := 0; i < swapCount; i++ {
a := timeRand.Intn(len(indices))
b := timeRand.Intn(len(indices))
indices[a], indices[b] = indices[b], indices[a]
}
// 选择获奖者
winners := make([]string, count)
for i := 0; i < count; i++ {
winners[i] = ls.Players[indices[i]]
}
// 生成种子字符串,包含所有随机源
seed := fmt.Sprintf("%x-%x-%x", drandSeed, csprngSeed, elapsedTime)
return winners, seed, nil
}
func main() {
ls := &LotterySystem{}
staticContent, err := fs.Sub(staticFiles, "static")
if err != nil {
log.Fatal(err)
}
mux := http.NewServeMux()
// 根路由处理
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
tmpl, err := template.New("index").Parse(indexHTML)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tmpl.Execute(w, nil)
return
}
// 其他路径返回 404
http.NotFound(w, r)
})
// 静态文件处理
fileServer := http.FileServer(http.FS(staticContent))
mux.HandleFunc("/static/", func(w http.ResponseWriter, r *http.Request) {
// 设置正确的 Content-Type
switch {
case strings.HasSuffix(r.URL.Path, ".css"):
w.Header().Set("Content-Type", "text/css")
case strings.HasSuffix(r.URL.Path, ".js"):
w.Header().Set("Content-Type", "application/javascript")
}
// 移除 /static/ 前缀
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/static")
fileServer.ServeHTTP(w, r)
})
// API endpoints
mux.HandleFunc("/api/draw", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
var req DrawRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
ls.Players = req.Players
winners, seed, err := ls.Draw(req.Count, req.ElapsedTime)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(DrawResponse{
Winners: winners,
Seed: seed,
})
})
// 启动服务器
log.Printf("Server starting on http://localhost:8080")
log.Fatal(http.ListenAndServe(":8080", mux))
}