dnslog 简单实现

This commit is contained in:
Shikong 2023-03-24 16:12:11 +08:00
parent 51febb313f
commit 1245ea1a08
5 changed files with 25 additions and 22 deletions

View File

@ -46,8 +46,6 @@ func Main() {
dns.HandleFunc(conf.DomainPattern, func(w dns.ResponseWriter, r *dns.Msg) { dns.HandleFunc(conf.DomainPattern, func(w dns.ResponseWriter, r *dns.Msg) {
qname := r.Question[0].Name qname := r.Question[0].Name
logger.Log().Infof("接收值: %s\n", qname)
// 去除后缀 只保留前缀 // 去除后缀 只保留前缀
name := strings.TrimSuffix(qname, conf.DomainPattern) name := strings.TrimSuffix(qname, conf.DomainPattern)
// 处理数据 // 处理数据

View File

@ -3,6 +3,7 @@ module matrix-dnslog-service
go 1.19 go 1.19
require ( require (
github.com/matoous/go-nanoid v1.5.0
github.com/miekg/dns v1.1.52 github.com/miekg/dns v1.1.52
github.com/pelletier/go-toml/v2 v2.0.6 github.com/pelletier/go-toml/v2 v2.0.6
github.com/spf13/viper v1.15.0 github.com/spf13/viper v1.15.0

View File

@ -135,6 +135,8 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/matoous/go-nanoid v1.5.0 h1:VRorl6uCngneC4oUQqOYtO3S0H5QKFtKuKycFG3euek=
github.com/matoous/go-nanoid v1.5.0/go.mod h1:zyD2a71IubI24efhpvkJz+ZwfwagzgSO6UNiFsZKN7U=
github.com/miekg/dns v1.1.52 h1:Bmlc/qsNNULOe6bpXcUTsuOajd0DzRHwup6D9k1An0c= github.com/miekg/dns v1.1.52 h1:Bmlc/qsNNULOe6bpXcUTsuOajd0DzRHwup6D9k1An0c=
github.com/miekg/dns v1.1.52/go.mod h1:uInx36IzPl7FYnDcMeVWxj9byh7DutNykX4G9Sj60FY= github.com/miekg/dns v1.1.52/go.mod h1:uInx36IzPl7FYnDcMeVWxj9byh7DutNykX4G9Sj60FY=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=

View File

@ -1,18 +1,24 @@
package internal package internal
import ( import (
"encoding/hex"
gonanoid "github.com/matoous/go-nanoid"
"matrix-common/pkg/logger"
"fmt" "fmt"
"math" "math"
"matrix-common/pkg/logger"
"net" "net"
"strings" "strings"
"testing" "testing"
) )
func TestLookup(t *testing.T) { func TestLookup(t *testing.T) {
text := strings.Repeat("1234567890", 66) id, _ := gonanoid.Generate("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", 6)
originalText := strings.Repeat("1234567890", 66)
chunkSize := 10 text := hex.EncodeToString([]byte(originalText))
chunkSize := 18
data := make([]string, 0) data := make([]string, 0)
fLen := float64(len(text)) / float64(chunkSize) fLen := float64(len(text)) / float64(chunkSize)
@ -32,7 +38,7 @@ func TestLookup(t *testing.T) {
logger.Log().Infof("%#v", data) logger.Log().Infof("%#v", data)
for i, datum := range data { for i, datum := range data {
_, _ = net.LookupIP(fmt.Sprintf("%s.%s.%d.%s.log.skcks.cn", "a", "1", i, datum)) _, _ = net.LookupIP(fmt.Sprintf("%s.%s.%d.%s.log.skcks.cn", "a", id, i, datum))
} }
_, _ = net.LookupIP(fmt.Sprintf("%s.%s.%s.%s.log.skcks.cn", "e", "1", "x", "x")) _, _ = net.LookupIP(fmt.Sprintf("%s.%s.%s.%s.log.skcks.cn", "e", id, "x", "x"))
} }

View File

@ -1,6 +1,7 @@
package handler package handler
import ( import (
"encoding/hex"
"matrix-common/pkg/logger" "matrix-common/pkg/logger"
"matrix-dnslog-service/pkg/cache" "matrix-dnslog-service/pkg/cache"
"matrix-dnslog-service/pkg/handler/type" "matrix-dnslog-service/pkg/handler/type"
@ -9,7 +10,6 @@ import (
func ProcessHandler(data string) { func ProcessHandler(data string) {
handlers := []_type.Handler{ handlers := []_type.Handler{
createData,
appendData, appendData,
endData, endData,
} }
@ -17,12 +17,11 @@ func ProcessHandler(data string) {
if len(datum) != 5 { if len(datum) != 5 {
return return
} }
command := datum[0] command := strings.ToLower(datum[0])
id := datum[1] id := strings.ToLower(datum[1])
seq := datum[2] seq := datum[2]
d := datum[3] d := datum[3]
logger.Log().Infof("command: %s, id: %s, seq: %s, data: %s", command, id, seq, d)
for _, handler := range handlers { for _, handler := range handlers {
if handler(command, id, seq, d) { if handler(command, id, seq, d) {
return return
@ -30,16 +29,6 @@ func ProcessHandler(data string) {
} }
} }
var createData = func(command, id, seq, data string) bool {
if command != "s" {
return false
}
logger.Log().Infof("[dnslog] (开始记录) id: %s 数据: %s", id, data)
cache.Append(id, seq, data)
return true
}
var appendData = func(command, id, seq, data string) bool { var appendData = func(command, id, seq, data string) bool {
if command != "a" { if command != "a" {
return false return false
@ -53,6 +42,13 @@ var endData = func(command, id, seq, data string) bool {
return false return false
} }
logger.Log().Infof("[dnslog] (完成) id: %s 数据: %s", id, cache.Get(id)) datum := cache.Get(id)
if len(datum) == 0 {
return false
}
originalData, _ := hex.DecodeString(datum)
datum = string(originalData)
logger.Log().Infof("[dnslog] (完成) id: %s \n数据: %s", id, datum)
return true return true
} }