-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns.go
69 lines (58 loc) · 1.18 KB
/
dns.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
package handler
import (
"bytes"
"encoding/binary"
"encoding/json"
"io/ioutil"
"net/http"
"unsafe"
"github.com/golang/glog"
)
// DNS防护策略
type DNS struct {
Filter uint16 `json:"filter"`
Enable uint8 `json:"enable"`
LimitLevel uint8 `json:"limit_level"`
Algo uint32 `json:"algo"`
TotalLimit uint32 `json:"total_limit"`
}
var DefaultDNS = new(DNS)
func GetDefaultDNS() *DNS {
return DefaultDNS
}
func (d *DNS) Handler(w http.ResponseWriter, r *http.Request) {
s, _ := ioutil.ReadAll(r.Body)
nd, err := d.Decode(string(s))
if err != nil {
return
}
nd.Valid()
nd.SerializeAndSend()
w.Write(s)
}
func (d *DNS) Decode(buf string) (*DNS, error) {
nd := new(DNS)
err := json.Unmarshal([]byte(buf), nd)
if err != nil {
glog.Error("json unmarshal error:", err)
}
return nd, err
}
func (d *DNS) Valid() {
return
}
func (d *DNS) SerializeAndSend() {
var hdr = Header{
Type: 1,
Len: uint32(unsafe.Sizeof(*d)),
Cmd: 1,
Status: 1,
Id: 1,
}
var buf bytes.Buffer
err := binary.Write(&buf, binary.LittleEndian, hdr)
err = binary.Write(&buf, binary.LittleEndian, *d)
glog.Info(buf, err)
// TODO: send policy to the engine
return
}