-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
149 lines (126 loc) · 2.66 KB
/
utils.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
package main
import (
"context"
"fmt"
"golang.org/x/crypto/scrypt"
"io/ioutil"
"net/http"
"net/url"
"os"
"time"
)
func GetSquare(w, h uint32) (x, y, wo, ho uint32) {
if w == h {
return 0, 0, w, h
} else if w > h {
return (w - h) / 2, 0, h, h
} else {
return 0, (h - w) / 2, w, w
}
}
// X return a context cancel after one second.
// This is an convenient method for mongodb operations
func X() context.Context {
var ctx context.Context
if !config.Site.Debug {
ctx, _ = context.WithTimeout(context.Background(), time.Second)
} else {
// allow more time when debugging
ctx, _ = context.WithTimeout(context.Background(), time.Minute)
}
return ctx
}
// Xd return a context cancel after given duration.
// This is an convenient method for mongodb operations
func Xd(duration time.Duration) context.Context {
ctx, _ := context.WithTimeout(context.Background(), duration)
return ctx
}
func PasswordHash(password string) []byte {
h, _ := scrypt.Key([]byte(password), []byte(config.Security.Salt), 32768, 8, 1, 32)
return h
}
func PasswordVerify(password string) bool {
if len(password) < 8 {
return false
}
var n, a, A, o bool
for _, c := range []byte(password) {
if c >= '0' && c <= '9' {
n = true
} else if c >= 'a' && c <= 'z' {
a = true
} else if c >= 'A' && c <= 'Z' {
A = true
} else {
o = true
}
}
count := 0
if n {
count++
}
if a {
count++
}
if A {
count++
}
if o {
count++
}
return count >= 3
}
func RecaptchaVerify(response string) SErr {
resp, err := http.PostForm("https://recaptcha.net/recaptcha/api/siteverify",
url.Values{
"secret": {config.Security.RecaptchaKey},
"response": {response}})
if err != nil {
return EUnknown
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return EUnknown
}
var result struct{ Success bool `json:"success"` }
if json.Unmarshal(body, &result) != nil {
return EUnknown
}
if !result.Success {
return EBadRecaptcha
} else {
return EOk
}
}
type fileLogger struct {
template string
year int
month time.Month
curFile *os.File
}
func (l *fileLogger) refresh() (err error) {
year, month, _ := time.Now().Date()
if l.year == year && l.month == month {
return
}
l.year = year
l.month = month
if l.curFile != nil {
if err = l.curFile.Close(); err != nil {
return
}
}
l.curFile, err = os.OpenFile(fmt.Sprintf(l.template, l.year, l.month), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
return
}
func (l *fileLogger) Write(p []byte) (n int, err error) {
if err := l.refresh(); err != nil {
return 0, err
}
return l.curFile.Write(p)
}
func (l *fileLogger) Close() error {
return l.curFile.Close()
}