Skip to content

Instantly share code, notes, and snippets.

@rafaeljusto
Created February 26, 2016 17:57
Show Gist options
  • Save rafaeljusto/ec64b408ec240df52f77 to your computer and use it in GitHub Desktop.
Save rafaeljusto/ec64b408ec240df52f77 to your computer and use it in GitHub Desktop.
dnsdisco full example
dnsdisco.com. 86400 IN SOA ns1.dnsdisco.com. adm.rafael.net.br. (
2016022400 ; serial
86400 ; refresh
86400 ; retry
86400 ; expire
10 ) ; minimum
dnsdisco.com. 86400 IN NS ns1.dnsdisco.com.
_test._tcp.dnsdisco.com. 10 IN SRV 1 10 7777 s1.dnsdisco.com.
_test._tcp.dnsdisco.com. 10 IN SRV 10 90 7777 s2.dnsdisco.com.
_test._tcp.dnsdisco.com. 10 IN SRV 2 30 7777 s3.dnsdisco.com.
s1.dnsdisco.com. 86400 IN A 192.168.122.80
s2.dnsdisco.com. 86400 IN A 192.168.122.125
s3.dnsdisco.com. 86400 IN A 192.168.122.199
ns1.dnsdisco.com. 86400 IN A 127.0.0.1
package main
import (
"bufio"
"fmt"
"io"
"net"
"strings"
"time"
"github.com/rafaeljusto/dnsdisco"
)
func main() {
discovery := dnsdisco.NewDiscovery("test", "tcp", "dnsdisco.com")
finish := discovery.RefreshAsync(5 * time.Second)
defer func() {
close(finish)
}()
for {
if errs := discovery.Errors(); len(errs) > 0 {
for _, err := range errs {
fmt.Println(err)
}
}
target, port := discovery.Choose()
if target == "" || port == 0 {
fmt.Println("No server selected")
time.Sleep(time.Second)
continue
}
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", target, port))
if err != nil {
fmt.Println(err)
time.Sleep(time.Second)
continue
}
for {
fmt.Fprintf(conn, time.Now().UTC().Format(time.RFC3339))
message, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
if err != io.EOF {
fmt.Println(err)
}
break
}
fmt.Println(strings.TrimSpace(message))
}
conn.Close()
time.Sleep(time.Second)
}
}
package main
import (
"flag"
"fmt"
"io"
"net"
"os"
"sync/atomic"
)
var (
name = flag.String("name", "", "server name")
counter uint64
)
func main() {
flag.Parse()
if name == nil {
flag.PrintDefaults()
os.Exit(1)
}
l, err := net.Listen("tcp", "0.0.0.0:7777")
if err != nil {
fmt.Println("Error listening:", err.Error())
os.Exit(1)
}
defer l.Close()
for {
conn, err := l.Accept()
if err != nil {
fmt.Println("Error accepting: ", err.Error())
os.Exit(1)
}
go handleRequest(conn)
}
}
func handleRequest(conn net.Conn) {
defer conn.Close()
buf := make([]byte, 1024)
reqLen, err := conn.Read(buf)
if err != nil {
if err != io.EOF {
fmt.Println("Error reading:", err.Error())
}
return
}
atomic.AddUint64(&counter, 1)
fmt.Printf("Received message: %s (%d)\n", string(buf[:reqLen]), counter)
fmt.Fprintf(conn, "[%s] Message received: %s\n", *name, string(buf[:reqLen]))
}
@rafaeljusto
Copy link
Author

rafaeljusto commented Feb 26, 2016

The environment was created with 5 VMs (3 servers, 1 recursive/authoritative DNS and 1 client).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment