Skip to content

Commit

Permalink
multiple changes
Browse files Browse the repository at this point in the history
- add cmd flags:
  - `-z` to activate authoritative zone
  - `-s` to activate dns sinkhole
- add example zone file
  • Loading branch information
bernoussama committed Nov 24, 2024
1 parent 90f472c commit 171675e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 35 deletions.
4 changes: 4 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/spf13/cobra"
)

var Verbose bool

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "mercury",
Expand Down Expand Up @@ -40,6 +42,8 @@ func Execute() {
}

func init() {
verbose := os.Getenv("VERBOSE") != ""
rootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", verbose, "verbose output")
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
Expand Down
61 changes: 54 additions & 7 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,36 @@ import (
"gopkg.in/yaml.v2"
)

// Logln is a wrapper around log.Println that only prints if Verbose is true
func Logln(a ...any) {
if Verbose {
log.Println(a...)
}
}

// Logf is a wrapper around log.Printf that only prints if Verbose is true
func Logf(format string, a ...any) {
if Verbose {
log.Printf(format, a...)
}
}

// Printf is a wrapper around fmt.Printf that only prints if Verbose is true
func Printf(format string, a ...any) (n int, err error) {
if Verbose {
return fmt.Printf(format, a...)
}
return 0, nil
}

// Println is a wrapper around fmt.Println that only prints if Verbose is true
func Println(a ...any) (n int, err error) {
if Verbose {
return fmt.Println(a...)
}
return 0, nil
}

// DNS header size
const BUFFER_SIZE = 2048

Expand Down Expand Up @@ -43,7 +73,7 @@ func loadZones() {
name := zone.Origin
zones[name] = zone
}
fmt.Printf("%+v\n", zones)
Printf("%+v\n", zones)
}

type Server struct {
Expand Down Expand Up @@ -73,8 +103,8 @@ func (s *Server) Run() {
if err != nil {
log.Fatal(err)
}
log.Println("Received", n, "bytes")
log.Println("from: ", remoteAddr)
Logln("Received", n, "bytes")
Logln("from: ", remoteAddr)
go s.handle(conn, remoteAddr, buffer[:n])
}
}
Expand All @@ -92,18 +122,30 @@ func (s *Server) handle(conn *net.UDPConn, remoteAddr *net.UDPAddr, data []byte)
conn.WriteToUDP(res, remoteAddr)
}

var (
Zone bool
Sinkhole bool
Source string
)

// serveCmd represents the serve command
var serveCmd = &cobra.Command{
Use: "serve",
Short: "serve a dns queries",
Short: "serve dns queries",
Long: `Mercury is a lightweight DNS server that provides DNS resolution for a given set of zones.
This server is designed to be used as as recursive resolver and a sinkhole, blocking unwanted DNS requests.`,

Run: func(cmd *cobra.Command, args []string) {
fmt.Println("serve called")
fmt.Println(Zone)
address := "0.0.0.0:53153"
loadZones()
// loadBlocklist()
blocklist["google.com."] = true
if Zone {
loadZones()
}
if Sinkhole {
// loadBlocklist()
blocklist["google.com."] = true
}
server := NewServer(
address,
)
Expand All @@ -112,6 +154,11 @@ This server is designed to be used as as recursive resolver and a sinkhole, bloc
}

func init() {
zone := os.Getenv("ZONE") != ""
sinkhole := os.Getenv("SINKHOLE") != ""
rootCmd.PersistentFlags().BoolVarP(&Zone, "zone", "z", zone, "authoritative zone")
rootCmd.PersistentFlags().BoolVarP(&Sinkhole, "sinkhole", "s", sinkhole, "dns sinkhole")

rootCmd.AddCommand(serveCmd)

// Here you will define your flags and configuration settings.
Expand Down
10 changes: 5 additions & 5 deletions zones/bernoussama.com.yml → zones/example.com.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
origin: bernoussama.com.
origin: example.com.
ttl: 3600
soa:
mname: ns1.bernoussama.com.
rname: admin.bernoussama.com.
mname: ns1.example.com.
rname: admin.example.com.
serial: 2024110400
refresh: 3600
retry: 600
expire: 604800
minimum: 86400
ns:
- host: ns1.bernoussama.com.
- host: ns2.bernoussama.com.
- host: ns1.example.com.
- host: ns2.example.com.
a:
- name: "@"
ttl: 5
Expand Down
23 changes: 0 additions & 23 deletions zones/otmane.com.yml

This file was deleted.

0 comments on commit 171675e

Please sign in to comment.