-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
478 lines (410 loc) · 11.4 KB
/
util.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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
package common
import (
"bytes"
"compress/gzip"
"compress/zlib"
"encoding/base64"
"errors"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/mail"
"net/smtp"
"os"
"reflect"
"regexp"
"runtime"
"strconv"
"strings"
"time"
"unicode/utf8"
)
func IsStructPtr(t reflect.Type) bool {
return t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct
}
//check the item whether in object
//object must be slice
func HasItem(obj interface{}, item interface{}) bool {
for i := 0; i < reflect.ValueOf(obj).Len(); i++ {
if reflect.DeepEqual(reflect.ValueOf(obj).Index(i).Interface(), item) {
return true
}
}
return false
}
//Be careful to use, from,to must be pointer
func DumpStruct(to interface{}, from interface{}) {
fromv := reflect.ValueOf(from)
tov := reflect.ValueOf(to)
if fromv.Kind() != reflect.Ptr || tov.Kind() != reflect.Ptr {
return
}
from_val := reflect.Indirect(fromv)
to_val := reflect.Indirect(tov)
for i := 0; i < from_val.Type().NumField(); i++ {
fdi_from_val := from_val.Field(i)
fd_name := from_val.Type().Field(i).Name
fdi_to_val := to_val.FieldByName(fd_name)
if fdi_to_val.IsValid() && fdi_from_val.Type() == fdi_to_val.Type() {
fdi_to_val.Set(fdi_from_val)
}
}
}
func DumpList(to interface{}, from interface{}) {
val_from := reflect.ValueOf(from)
val_to := reflect.ValueOf(to)
if val_from.Type().Kind() == reflect.Slice && val_to.Type().Kind() == reflect.Slice &&
val_from.Len() == val_to.Len() {
for i := 0; i < val_from.Len(); i++ {
DumpStruct(val_to.Index(i).Addr().Interface(), val_from.Index(i).Addr().Interface())
}
}
}
func ParseToLocalTime(str string) time.Time {
tm, _ := time.ParseInLocation("2006-01-02 15:04:05", str, time.Now().Location())
return tm
}
func TimeToLocalFormat(t time.Time) string {
return t.Format("2006-01-02 15:04:05")
}
func ValidateLessEqChar(str string, n int) bool {
return utf8.RuneCountInString(str) <= n
}
func ValidateNum(str string) bool {
_, err := strconv.ParseInt(str, 10, 64)
return err == nil
}
func ValidateEmail(str string) bool {
reg := "^(((([a-zA-Z]|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d|-|\\.|_|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\" +
"x{FDF0}-\\x{FFEF}])*([a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.?$"
Re := regexp.MustCompile(reg)
return Re.MatchString(str)
}
func ValidatePhone(str string) bool {
if !ValidateNum(str) {
return false
}
l := len(str)
return l >= 6 && l < 11
}
var (
dunno = []byte("???")
centerDot = []byte("·")
dot = []byte(".")
slash = []byte("/")
)
// source returns a space-trimmed slice of the n'th line.
func source(lines [][]byte, n int) []byte {
n-- // in stack trace, lines are 1-indexed but our array is 0-indexed
if n < 0 || n >= len(lines) {
return dunno
}
return bytes.TrimSpace(lines[n])
}
// function returns, if possible, the name of the function containing the PC.
func function(pc uintptr) []byte {
fn := runtime.FuncForPC(pc)
if fn == nil {
return dunno
}
name := []byte(fn.Name())
// The name includes the path name to the package, which is unnecessary
// since the file name is already included. Plus, it has center dots.
// That is, we see
// runtime/debug.*T·ptrmethod
// and want
// *T.ptrmethod
// Also the package path might contains dot (e.g. code.google.com/...),
// so first eliminate the path prefix
if lastslash := bytes.LastIndex(name, slash); lastslash >= 0 {
name = name[lastslash+1:]
}
if period := bytes.Index(name, dot); period >= 0 {
name = name[period+1:]
}
name = bytes.Replace(name, centerDot, dot, -1)
return name
}
// stack returns a nicely formated stack frame, skipping skip frames
func stack(skip int) []byte {
buf := new(bytes.Buffer) // the returned data
// As we loop, we open files and read them. These variables record the currently
// loaded file.
var lines [][]byte
var lastFile string
for i := skip; ; i++ { // Skip the expected number of frames
pc, file, line, ok := runtime.Caller(i)
if !ok {
break
}
// Print this much at least. If we can't find the source, it won't show.
fmt.Fprintf(buf, "%s:%d (0x%x)\n", file, line, pc)
if file != lastFile {
data, err := ioutil.ReadFile(file)
if err != nil {
continue
}
lines = bytes.Split(data, []byte{'\n'})
lastFile = file
}
fmt.Fprintf(buf, "\t%s: %s\n", function(pc), source(lines, line))
}
return buf.Bytes()
}
func WritePid() error {
pid_fp, err := os.OpenFile("./server.pid", os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "open pid file failed[%s]\n", err)
return err
}
defer pid_fp.Close()
pid := os.Getpid()
pid_fp.WriteString(strconv.Itoa(pid))
return nil
}
func GetBetweenStr(str, start, end string) string {
n := strings.Index(str, start) + len(start)
if n == -1 {
n = 0
}
str = string([]byte(str)[n:])
m := strings.Index(str, end)
if m == -1 {
m = len(str)
}
str = string([]byte(str)[:m])
return str
}
func SendSmsChina(pszMobis, pszMsg string) (int, string, error) {
var userId = "JKDX01"
var password = "2015abc"
var iMobiCount = "1"
var url = "http://114.67.48.66:5132/MWGate/wmgw.asmx/MongateCsSpSendSmsNew?"
var params = map[string]string{
"userId": userId,
"password": password,
"pszMobis": pszMobis,
"pszMsg": pszMsg,
"iMobiCount": iMobiCount,
"pszSubPort": "*",
}
code, result, err := SMSSendRequest("GET", url, params)
return code, result, err
}
func SendSmsInternational(pszMobis, pszMsg string) (int, string, error) {
var userId = "JKDX03"
var password = "codoon20150902"
var iMobiCount = "1"
var url = "http://114.67.48.66:5132/MWGate/wmgw.asmx/MongateCsSpSendSmsNew?"
var params = map[string]string{
"userId": userId,
"password": password,
"pszMobis": pszMobis,
"pszMsg": pszMsg,
"iMobiCount": iMobiCount,
"pszSubPort": "*",
}
code, result, err := SMSSendRequest("GET", url, params)
return code, result, err
}
func SendMail(email, password, host, toemail, sender, receiver, subject, body string) error {
b64 := base64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
from := mail.Address{sender, email}
to := mail.Address{receiver, toemail}
header := make(map[string]string)
header["From"] = from.String()
header["To"] = to.String()
header["Subject"] = fmt.Sprintf("=?UTF-8?B?%s?=", b64.EncodeToString([]byte(subject)))
header["MIME-Version"] = "1.0"
header["Content-Type"] = "text/html; charset=UTF-8"
header["Content-Transfer-Encoding"] = "base64"
message := ""
for k, v := range header {
message += fmt.Sprintf("%s: %s\r\n", k, v)
}
message += "\r\n" + b64.EncodeToString([]byte(body))
auth := smtp.PlainAuth(
"",
email,
password,
host,
)
err := smtp.SendMail(
host+":25",
auth,
email,
[]string{to.Address},
[]byte(message),
)
return err
}
func SendGroupMail(email, password, sender, host string, toemail []string, subject, body string) error {
b64 := base64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
var tos []string
from := mail.Address{sender, email}
for i := range toemail {
to := mail.Address{"", toemail[i]}
tos = append(tos, to.Address)
}
header := make(map[string]string)
header["From"] = from.String()
header["To"] = strings.Join(tos, ",")
header["Subject"] = fmt.Sprintf("=?UTF-8?B?%s?=", b64.EncodeToString([]byte(subject)))
header["MIME-Version"] = "1.0"
header["Content-Type"] = "text/html; charset=UTF-8"
header["Content-Transfer-Encoding"] = "base64"
message := ""
for k, v := range header {
message += fmt.Sprintf("%s: %s\r\n", k, v)
}
message += "\r\n" + b64.EncodeToString([]byte(body))
auth := smtp.PlainAuth(
"",
email,
password,
host,
)
err := smtp.SendMail(
host+":25",
auth,
email,
toemail,
[]byte(message),
)
return err
}
type DecimalismConfusion struct {
Move int64
StartId int64
}
func InitDecimalismConfusion(move, startId int64) (*DecimalismConfusion, error) {
if move <= 0 {
return nil, errors.New("move error")
}
d := DecimalismConfusion{
Move: 1,
StartId: startId,
}
var i int64 = 0
for ; i < move; i++ {
d.Move = d.Move * 10
}
return &d, nil
}
func (d *DecimalismConfusion) sign(id int64) int64 {
var signId int64
stringId := strconv.FormatInt(id, 10)
for i := 0; i < len(stringId); i++ {
k := id / 10
if k == 0 {
signId = signId + id
break
}
ii := id - k*10
signId = signId + ii
id = k
}
return signId % d.Move
}
func (d *DecimalismConfusion) EncodeId(id int64) int64 {
if id < d.StartId {
return id
}
encodeId := id * d.Move
encodeId = encodeId + d.sign(id)
return encodeId
}
func (d *DecimalismConfusion) DecodeId(id int64) (int64, error) {
if id < d.StartId {
return id, nil
}
var decodeId int64
decodeId = id / d.Move
signId := id - decodeId*d.Move
if signId != d.sign(decodeId) {
fmt.Println(decodeId, signId, d.sign(decodeId))
return id, errors.New("decode error")
}
return decodeId, nil
}
func RedirectCoreDump(dump_file *os.File) {
//syscall.Dup2(int(dump_file.Fd()), 2)
}
//进行zlib压缩
func DoZlibCompress(src []byte) []byte {
var in bytes.Buffer
w := zlib.NewWriter(&in)
w.Write(src)
w.Close()
return in.Bytes()
}
//进行zlib解压缩
func DoZlibUnCompress(compressSrc []byte) ([]byte, error) {
b := bytes.NewReader(compressSrc)
var out bytes.Buffer
r, err := zlib.NewReader(b)
if nil != err || r == nil {
Errorf("DoZlibUnCompress error :%v", err)
return compressSrc, err
}
io.Copy(&out, r)
return out.Bytes(), nil
}
//进行gzip压缩
func DoGzipCompress(src []byte) []byte {
var in bytes.Buffer
w := gzip.NewWriter(&in)
w.Write(src)
w.Close()
return in.Bytes()
}
//进行gzip解压缩
func DoGzipUnCompress(compressSrc []byte) ([]byte, error) {
b := bytes.NewReader(compressSrc)
var out bytes.Buffer
r, err := gzip.NewReader(b)
if nil != err || r == nil {
Errorf("DoGzipUnCompress error :%v", err)
return compressSrc, err
}
io.Copy(&out, r)
return out.Bytes(), nil
}
const USER_ID_LEN = 36
func ValidateUserId(user_id string) bool {
if USER_ID_LEN != len(user_id) {
return false
}
s := strings.Split(user_id, "-")
if len(s) != 5 {
return false
}
return true
}
// get rand num range [min, max]
// addd by wuql 2016-8-26
func RandIntRange(min, max int) int {
if min == max {
return min
}
// incompatible when min and max reversed
if min > max {
mid := min
min = max
max = mid
}
rand.Seed(time.Now().UTC().UnixNano())
return min + rand.Intn(max-min+1)
}