-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcacher.go
128 lines (104 loc) · 2.77 KB
/
cacher.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
package godns
import (
"encoding/binary"
"fmt"
"io"
"log"
"os"
"path/filepath"
"github.com/panjf2000/ants/v2"
"github.com/tochusc/godns/dns"
)
type Cacher struct {
CacheLocation string
CacherLogger *log.Logger
CacherPool *ants.Pool
}
type CacherConfig struct {
CacheLocation string
LogWriter io.Writer
}
func NewCacher(conf CacherConfig, pool *ants.Pool) *Cacher {
cacherLogger := log.New(conf.LogWriter, "Cacher: ", log.LstdFlags)
return &Cacher{
CacheLocation: conf.CacheLocation,
CacherLogger: cacherLogger,
CacherPool: pool,
}
}
func (c *Cacher) CacheResponse(data []byte) error {
ident, err := IdentifyMessage(data)
if err != nil {
c.CacherLogger.Printf("Error identifying response: %v", err)
return err
}
path := filepath.Join(c.CacheLocation, ident)
// 将响应缓存到磁盘
// 如果缓存目录不存在,创建目录
if _, err := os.Stat(c.CacheLocation); os.IsNotExist(err) {
err := os.MkdirAll(c.CacheLocation, 0755)
if err != nil {
c.CacherLogger.Printf("Error creating cache directory %s: %v", c.CacheLocation, err)
return err
}
}
// 创建缓存文件
file, err := os.Create(path)
if err != nil {
c.CacherLogger.Printf("Error creating cache file %s: %v", ident, err)
return err
}
_, err = file.Write(data)
if err != nil {
c.CacherLogger.Printf("Error writing cache file %s: %v", ident, err)
return err
}
file.Close()
c.CacherLogger.Printf("Cache saved %s\n", ident)
return nil
}
func (c *Cacher) FetchCache(connInfo ConnectionInfo) ([]byte, error) {
ident, err := IdentifyMessage(connInfo.Packet)
if err != nil {
c.CacherLogger.Printf("Error identifying response: %v", err)
return []byte{}, err
}
path := filepath.Join(c.CacheLocation, ident)
file, err := os.Open(path)
if err != nil {
c.CacherLogger.Printf("Cache miss %s\n", ident)
return []byte{}, err
}
defer file.Close()
cache := make([]byte, 65535)
rd, err := file.Read(cache)
if err != nil {
c.CacherLogger.Printf("Error reading cache file %s: %v", ident, err)
return []byte{}, err
}
c.CacherLogger.Printf("Cache hit %s\n", ident)
// 修改Cache内容
cache[0] = connInfo.Packet[0]
cache[1] = connInfo.Packet[1]
for i := 0; ; i++ {
cache[12+i] = connInfo.Packet[12+i]
if cache[12+i] > dns.NamePointerFlag {
cache[13+i] = connInfo.Packet[13+i]
break
}
if cache[12+i] == 0x00 {
break
}
}
return cache[:rd], nil
}
func IdentifyMessage(data []byte) (string, error) {
// 解析 DNS 请求
qName, offset, err := dns.DecodeDomainNameFromBuffer(data, 12)
if err != nil {
return "", err
}
qType := dns.DNSType(binary.BigEndian.Uint16(data[offset : offset+2]))
qClass := dns.DNSClass(binary.BigEndian.Uint16(data[offset+2 : offset+4]))
return fmt.Sprintf("%s-%s-%s", qName, qType.String(), qClass.String()), nil
}