Skip to content
This repository has been archived by the owner on Oct 21, 2023. It is now read-only.

Commit

Permalink
chore: rename pkg to core (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy authored Aug 17, 2023
1 parent 8fe9384 commit b834a14
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ DNS written in Golang
# how to use :

```
go run ./cmd/main.go
go run main.go
```

### then:
Expand Down
15 changes: 10 additions & 5 deletions pkg/resolver.go → core/resolver.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dns
package core

import (
"bufio"
Expand All @@ -11,7 +11,8 @@ import (
"golang.org/x/net/dns/dnsmessage"
)

const ROOT_SERVERS = "198.41.0.4,199.9.14.201,192.33.4.12,199.7.91.13,192.203.230.10,192.5.5.241,192.112.36.4,198.97.190.53"
const ROOTSERVERS = `198.41.0.4,199.9.14.201,192.33.4.12,199.7.91.13,192.203.230.10,192.5.5.241,
192.112.36.4,198.97.190.53`

func HandlePacket(pc net.PacketConn, addr net.Addr, buf []byte) {
//* send incoming packets to handlePacket function
Expand Down Expand Up @@ -115,7 +116,11 @@ func dnsQuery(servers []net.IP, question dnsmessage.Question) (*dnsmessage.Messa
if !newResolverServersFound {
for _, nameserver := range nameservers {
if !newResolverServersFound {
response, err := dnsQuery(getRootServers(), dnsmessage.Question{Name: dnsmessage.MustNewName(nameserver), Type: dnsmessage.TypeA, Class: dnsmessage.ClassINET})
response, err := dnsQuery(getRootServers(),
dnsmessage.Question{
Name: dnsmessage.MustNewName(nameserver),
Type: dnsmessage.TypeA, Class: dnsmessage.ClassINET,
})
if err != nil {
fmt.Printf("warning: lookup of nameserver %s failed: %err\n", nameserver, err)
} else {
Expand Down Expand Up @@ -212,10 +217,10 @@ func outgoingDNSQuery(servers []net.IP, question dnsmessage.Question) (*dnsmessa
return &p, &header, nil
}

// * make a loop over ROOT SERVERS list and return a slice of root servers ip
// * make a loop over ROOT SERVERS list and return a slice of root servers ip.
func getRootServers() []net.IP {
rootServers := []net.IP{}
for _, rootServer := range strings.Split(ROOT_SERVERS, ",") {
for _, rootServer := range strings.Split(ROOTSERVERS, ",") {
rootServers = append(rootServers, net.ParseIP(rootServer))
}
return rootServers
Expand Down
8 changes: 6 additions & 2 deletions pkg/resolver_test.go → core/resolver_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dns
package core

import (
"crypto/rand"
Expand All @@ -24,15 +24,19 @@ func (m *MockPacketConn) Close() error {
func (m *MockPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
return 0, nil, nil
}

func (m *MockPacketConn) LocalAddr() net.Addr {
return nil
}

func (m *MockPacketConn) SetDeadline(t time.Time) error {
return nil
}

func (m *MockPacketConn) SetReadDeadline(t time.Time) error {
return nil
}

func (m *MockPacketConn) SetWriteDeadline(t time.Time) error {
return nil
}
Expand Down Expand Up @@ -80,7 +84,7 @@ func TestOutgoingDnsQuery(t *testing.T) {
Type: dnsmessage.TypeNS,
Class: dnsmessage.ClassINET,
}
rootServers := strings.Split(ROOT_SERVERS, ",")
rootServers := strings.Split(ROOTSERVERS, ",")
if len(rootServers) == 0 {
t.Fatalf("No root servers found")
}
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"net"

"github.com/kehiy/dns-server/pkg"
"github.com/kehiy/dns-server/core"
)

func main() {
Expand All @@ -22,6 +22,6 @@ func main() {
fmt.Printf("Connection error [%s]: %s\n", addr.String(), err)
continue
}
go dns.HandlePacket(p, addr, buf[:n])
go core.HandlePacket(p, addr, buf[:n])
}
}

0 comments on commit b834a14

Please sign in to comment.