forked from priscillachat/priscilla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommandQuery.go
87 lines (67 loc) · 2.05 KB
/
commandQuery.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
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"
"time"
)
type commandBlock struct {
Id string `json:"id"`
Action string `json:"action"`
Type string `json:"type"`
Time int64 `json:"time,omitempty"`
Data string `json:"data,omitempty"`
Error string `json"error,omitempty"`
Array []string `json:"array,omitempty"`
Options []string `json:"options,omitempty"`
Map map[string]string `json:"map,omitempty"`
}
func (c *commandBlock) handleCommand(source string,
dispatch chan<- *dispatcherRequest) {
logger.Debug.Println("Id: ", c.Id)
logger.Debug.Println("Action: ", c.Action)
logger.Debug.Println("Type: ", c.Type)
}
func (c *commandBlock) registerChk() error {
if c.Type != "prefix" && c.Type != "noprefix" && c.Type != "mention" &&
c.Type != "unhandled" {
return errors.New("Unsupported register type: " + c.Type)
}
if c.Data == "" {
return errors.New("Missing regex expression")
}
if len(c.Array) < 2 {
return errors.New("Missing help info (in \"array\" element)")
}
return nil
}
func (c *commandBlock) engageChk(source, secret string) error {
if c.Type != "adapter" && c.Type != "responder" {
return errors.New("Invalid client engagement type: " + c.Type)
}
if c.Data == "" {
return errors.New("No auth data received")
}
now := time.Now().UTC()
logger.Debug.Println("Current time:", now)
logger.Debug.Println("Current unix timestamp:", now.Unix())
logger.Debug.Println("Received auth unix timestamp:", c.Time)
diff := now.Unix() - c.Time
logger.Info.Println("Time differential:", diff)
if diff > 5 || diff < -5 {
return errors.New("Timestamp out of range")
}
decoded, err := base64.StdEncoding.DecodeString(c.Data)
if err != nil {
return err
}
authMsg := fmt.Sprintf("%d%s%s", c.Time, source, secret)
mac := hmac.New(sha256.New, []byte(secret))
mac.Write([]byte(authMsg))
if !hmac.Equal(decoded, mac.Sum(nil)) {
return errors.New("Incorrect auth code")
}
return nil
}