-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.go
76 lines (59 loc) · 1.17 KB
/
random.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
package main
import (
"flag"
"log"
"net/http"
"regexp"
"strconv"
"strings"
"github.com/zhanglindeng/phpgo"
)
var port = flag.String("port", "8090", "--port=8090 default 8090")
var (
s = `0123456789`
x = `abcdefghijklmnopqrstuvwxyz`
d = `ABCDEFGHIJKLMNOPQRSTUVWXYZ`
t = `!@#$%^&*()_-=+|{}[];.,'"<>?~\/`
)
func main() {
flag.Parse()
http.HandleFunc("/", index)
err := http.ListenAndServe(":"+*port, nil)
if err != nil {
log.Fatal(err)
}
}
func index(w http.ResponseWriter, r *http.Request) {
_type := r.URL.Query().Get("type")
_length := r.URL.Query().Get("length")
// 默认 32 个字符
length, err := strconv.Atoi(_length)
if err != nil {
length = 32
}
if length <= 0 {
length = 32
}
// 最长 1024 个字符
if length > 1024 {
length = 1024
}
// sxdt
if ok, err := regexp.MatchString("^[sxdt]+$", _type); !ok || err != nil {
_type = "sxd"
}
seed := ""
if strings.Contains(_type, "s") {
seed = seed + s
}
if strings.Contains(_type, "x") {
seed = seed + x
}
if strings.Contains(_type, "d") {
seed = seed + d
}
if strings.Contains(_type, "t") {
seed = seed + t
}
w.Write([]byte(phpgo.StringRandomStr(length, []byte(seed)...)))
}