nekej
This commit is contained in:
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
1
.idea/.name
generated
Normal file
1
.idea/.name
generated
Normal file
@@ -0,0 +1 @@
|
|||||||
|
dns-thingymagik
|
||||||
9
.idea/dns-thingymagik.iml
generated
Normal file
9
.idea/dns-thingymagik.iml
generated
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="WEB_MODULE" version="4">
|
||||||
|
<component name="Go" enabled="true" />
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$" />
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/dns-thingymagik.iml" filepath="$PROJECT_DIR$/.idea/dns-thingymagik.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
42
cache/Records.go
vendored
Normal file
42
cache/Records.go
vendored
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/patrickmn/go-cache"
|
||||||
|
"golang.org/x/net/dns/dnsmessage"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RecordsCache struct {
|
||||||
|
records *cache.Cache
|
||||||
|
mu sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
defaultExpiration = 1 * time.Hour
|
||||||
|
purgeTime = 1 * time.Hour
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewRecordsCache() *RecordsCache {
|
||||||
|
Cache := cache.New(defaultExpiration, purgeTime)
|
||||||
|
return &RecordsCache{
|
||||||
|
records: Cache,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rc *RecordsCache) Get(name string) (dnsmessage.Resource, bool) {
|
||||||
|
rc.mu.Lock()
|
||||||
|
defer rc.mu.Unlock()
|
||||||
|
record, ok := rc.records.Get(name)
|
||||||
|
if ok {
|
||||||
|
res := record.(dnsmessage.Resource)
|
||||||
|
return res, ok
|
||||||
|
}
|
||||||
|
return dnsmessage.Resource{}, false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rc *RecordsCache) Set(resource dnsmessage.Resource) {
|
||||||
|
rc.mu.Lock()
|
||||||
|
defer rc.mu.Unlock()
|
||||||
|
rc.records.Set(resource.Header.Name.String(), resource, time.Duration(resource.Header.TTL)*time.Second)
|
||||||
|
}
|
||||||
10
go.mod
Normal file
10
go.mod
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
module dnsthingymagik
|
||||||
|
|
||||||
|
go 1.23.0
|
||||||
|
|
||||||
|
toolchain go1.23.4
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/patrickmn/go-cache v2.1.0+incompatible
|
||||||
|
golang.org/x/net v0.37.0
|
||||||
|
)
|
||||||
4
go.sum
Normal file
4
go.sum
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
|
||||||
|
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
||||||
|
golang.org/x/net v0.37.0 h1:1zLorHbz+LYj7MQlSf1+2tPIIgibq2eL5xkrGk6f+2c=
|
||||||
|
golang.org/x/net v0.37.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
|
||||||
109
main.go
Normal file
109
main.go
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"dnsthingymagik/cache"
|
||||||
|
"fmt"
|
||||||
|
"golang.org/x/net/dns/dnsmessage"
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
udpServer, err := net.ListenPacket("udp", ":53")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer udpServer.Close()
|
||||||
|
|
||||||
|
rc := cache.NewRecordsCache()
|
||||||
|
|
||||||
|
for {
|
||||||
|
buf := make([]byte, 514)
|
||||||
|
n, addr, err := udpServer.ReadFrom(buf)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
go process(addr, buf[:n], rc)
|
||||||
|
|
||||||
|
//go response(udpServer, addr, buf)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func process(addr net.Addr, buf []byte, rc *cache.RecordsCache) {
|
||||||
|
var parser dnsmessage.Parser
|
||||||
|
header, err := parser.Start(buf)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(header)
|
||||||
|
|
||||||
|
msg := dnsmessage.Message{
|
||||||
|
Header: header,
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
question, err := parser.Question()
|
||||||
|
if err == dnsmessage.ErrSectionDone {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(question)
|
||||||
|
msg.Questions = append(msg.Questions, question)
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
answer, err := parser.Answer()
|
||||||
|
if err == dnsmessage.ErrSectionDone {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(answer)
|
||||||
|
msg.Answers = append(msg.Answers, answer)
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
authority, err := parser.Authority()
|
||||||
|
if err == dnsmessage.ErrSectionDone {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(authority)
|
||||||
|
msg.Authorities = append(msg.Authorities, authority)
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
additional, err := parser.Additional()
|
||||||
|
if err == dnsmessage.ErrSectionDone {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(additional)
|
||||||
|
msg.Additionals = append(msg.Additionals, additional)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MESSAGE IS FULL
|
||||||
|
|
||||||
|
fmt.Println(msg)
|
||||||
|
|
||||||
|
}
|
||||||
43
query/Query.go
Normal file
43
query/Query.go
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package query
|
||||||
|
|
||||||
|
import (
|
||||||
|
"golang.org/x/net/dns/dnsmessage"
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func sendQuery(server string, query dnsmessage.Message) (dnsmessage.Message, error) {
|
||||||
|
conn, err := net.DialTimeout("udp", server+":53", 5*time.Second)
|
||||||
|
if err != nil {
|
||||||
|
return dnsmessage.Message{}, err
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
q, err := query.Pack()
|
||||||
|
if err != nil {
|
||||||
|
return dnsmessage.Message{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = conn.Write(q)
|
||||||
|
if err != nil {
|
||||||
|
return dnsmessage.Message{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := make([]byte, 512)
|
||||||
|
err = conn.SetReadDeadline(time.Now().Add(5 * time.Second))
|
||||||
|
if err != nil {
|
||||||
|
return dnsmessage.Message{}, err
|
||||||
|
}
|
||||||
|
n, err := conn.Read(buf)
|
||||||
|
if err != nil {
|
||||||
|
return dnsmessage.Message{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
msg := dnsmessage.Message{}
|
||||||
|
err = msg.Unpack(buf[:n])
|
||||||
|
if err != nil {
|
||||||
|
return dnsmessage.Message{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return msg, nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user