Skip to content

Commit

Permalink
begin work on modules, add binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
pforemski committed Sep 19, 2016
1 parent 6a50ebb commit 01fbcff
Show file tree
Hide file tree
Showing 15 changed files with 131 additions and 69 deletions.
31 changes: 31 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

[ -z "$1" ] && { echo "Usage: build.sh VERSION" >&1; exit 1; }
VERSION="$1"

###############################################

function build()
{
TARGET="$1"

echo "Building dingo v. $VERSION for $TARGET"
GOOS="${TARGET%-*}" GOARCH="${TARGET##*-}" go build \
-o release/dingo-$VERSION/dingo-$TARGET \
./dingo.go ./gdns.go
}

###############################################

rm -fr ./release/dingo-$VERSION
mkdir -p ./release/dingo-$VERSION

for target in \
darwin-386 darwin-amd64 \
freebsd-386 freebsd-amd64 \
linux-386 linux-amd64 \
netbsd-386 netbsd-amd64 \
openbsd-386 openbsd-amd64 \
windows-386 windows-amd64; do
build $target || exit 1
done
76 changes: 7 additions & 69 deletions dingo.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* dingo: a Google DNS over HTTPS caching proxy written in Go
* dingo: a DNS caching proxy written in Go
*
* Copyright (C) 2016 Pawel Foremski <[email protected]>
* Licensed under GNU GPL v3
Expand All @@ -15,29 +15,17 @@ import "net"
import "flag"
import "log"
import "github.com/miekg/dns"
import "net/http"
import "net/url"
import "time"
import "io/ioutil"
import "encoding/json"
import "crypto/tls"
import "math/rand"
import "strings"
import "github.com/patrickmn/go-cache"
//import "github.com/devsisters/goquic"
import "math/rand"

/**********************************************************************/

/* command-line arguments */
var (
bindip = flag.String("bind", "0.0.0.0", "bind to interface ip")
bindip = flag.String("bind", "0.0.0.0", "IP address to bind to")
port = flag.Int("port", 32000, "listen on port number")
dbglvl = flag.Int("dbg", 1, "debugging level")
workers = flag.Int("workers", 10, "number of independent workers")
server = flag.String("server", "216.58.209.174", "Google DNS web server address")
sni = flag.String("sni", "www.google.com", "SNI string to send (should match server certificate)")
edns = flag.String("edns", "0.0.0.0/0", "edns client subnet")
nopad = flag.Bool("nopad", false, "disable random padding")
dbglvl = flag.Int("dbg", 2, "debugging level")
)

/**********************************************************************/
Expand Down Expand Up @@ -149,57 +137,6 @@ func resolve(name string, qtype int) Reply {
return <-rchan
}

/* resolves queries */
func resolver(server string) {
/* setup the HTTP client */
var httpTr = http.DefaultTransport.(*http.Transport)
// var httpTr = goquic.NewRoundTripper(true)
var tlsCfg = &tls.Config{ ServerName: *sni }
httpTr.TLSClientConfig = tlsCfg;
// req,_ := http.NewRequest("GET", "https://www.google.com/", nil)
// httpTr.RoundTrip(req)
var httpClient = &http.Client{ Timeout: time.Second*10, Transport: httpTr }

for q := range qchan {
/* make the new response object */
r := Reply{ Status: -1 }

/* prepare the query */
v := url.Values{}
v.Set("name", q.Name)
v.Set("type", fmt.Sprintf("%d", q.Type))
if len(*edns) > 0 {
v.Set("edns_client_subnet", *edns)
}
if !*nopad {
v.Set("random_padding", strings.Repeat(string(65+rand.Intn(26)), rand.Intn(500)))
}

/* prepare request, send proper HTTP 'Host:' header */
addr := fmt.Sprintf("https://%s/resolve?%s", server, v.Encode())
hreq,_ := http.NewRequest("GET", addr, nil)
hreq.Host = "dns.google.com"

/* send the query */
resp,err := httpClient.Do(hreq)
if (err == nil) {
dbg(2, "[%s/%d] %s %s", q.Name, q.Type, resp.Status, resp.Proto)

/* read */
buf,_ := ioutil.ReadAll(resp.Body)
resp.Body.Close()
dbg(7, " reply: %s", buf)

/* parse JSON? */
if (resp.StatusCode == 200) { json.Unmarshal(buf, &r) }
r.Now = time.Now()
} else { dbg(1, "[%s/%d] error: %s", q.Name, q.Type, err.Error()) }

/* write the reply */
*q.rchan <- r
}
}

/* main */
func main() {
/* prepare */
Expand All @@ -215,10 +152,11 @@ func main() {
if err != nil { die(err) }

/* start workers */
for i := 0; i < *workers; i++ { go resolver(*server) }
gdns_start()
// odns_start()

/* accept new connections forever */
dbg(1, "dingo ver. 0.1 started on UDP port %d", laddr.Port)
dbg(1, "dingo ver. 0.11 started on UDP port %d", laddr.Port)
var buf []byte
for {
buf = make([]byte, 1500)
Expand Down
93 changes: 93 additions & 0 deletions gdns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* dingo: a DNS caching proxy written in Go
* This file implements a Google DNS-over-HTTPS client
*
* Copyright (C) 2016 Pawel Foremski <[email protected]>
* Licensed under GNU GPL v3
*/

package main

import "fmt"
import "net/http"
import "net/url"
import "time"
import "io/ioutil"
import "encoding/json"
import "crypto/tls"
import "math/rand"
import "strings"
import "flag"
//import "github.com/devsisters/goquic"

/* command-line arguments */
var (
gdns_workers = flag.Int("gdns:workers", 10,
"Google DNS: number of independent workers")
gdns_server = flag.String("gdns:server", "216.58.209.174",
"Google DNS: web server address")
gdns_sni = flag.String("gdns:sni", "www.google.com",
"Google DNS: SNI string to send (should match server certificate)")
gdns_edns = flag.String("gdns:edns", "",
"Google DNS: EDNS client subnet (set 0.0.0.0/0 to disable)")
gdns_nopad = flag.Bool("gdns:nopad", false,
"Google DNS: disable random padding")
)

/**********************************************************************/

func gdns_start() {
for i := 0; i < *gdns_workers; i++ { go gdns_resolver(*gdns_server) }
}

func gdns_resolver(server string) {
/* setup the HTTP client */
var httpTr = http.DefaultTransport.(*http.Transport)
// var httpTr = goquic.NewRoundTripper(true)

var tlsCfg = &tls.Config{ ServerName: *gdns_sni }
httpTr.TLSClientConfig = tlsCfg;
// req,_ := http.NewRequest("GET", "https://www.google.com/", nil)
// httpTr.RoundTrip(req)

var httpClient = &http.Client{ Timeout: time.Second*10, Transport: httpTr }

for q := range qchan {
/* make the new response object */
r := Reply{ Status: -1 }

/* prepare the query */
v := url.Values{}
v.Set("name", q.Name)
v.Set("type", fmt.Sprintf("%d", q.Type))
if len(*gdns_edns) > 0 {
v.Set("edns_client_subnet", *gdns_edns)
}
if !*gdns_nopad {
v.Set("random_padding", strings.Repeat(string(65+rand.Intn(26)), rand.Intn(500)))
}

/* prepare request, send proper HTTP 'Host:' header */
addr := fmt.Sprintf("https://%s/resolve?%s", server, v.Encode())
hreq,_ := http.NewRequest("GET", addr, nil)
hreq.Host = "dns.google.com"

/* send the query */
resp,err := httpClient.Do(hreq)
if (err == nil) {
dbg(2, "[%s/%d] %s %s", q.Name, q.Type, resp.Status, resp.Proto)

/* read */
buf,_ := ioutil.ReadAll(resp.Body)
resp.Body.Close()
dbg(7, " reply: %s", buf)

/* parse JSON? */
if (resp.StatusCode == 200) { json.Unmarshal(buf, &r) }
r.Now = time.Now()
} else { dbg(1, "[%s/%d] error: %s", q.Name, q.Type, err.Error()) }

/* write the reply */
*q.rchan <- r
}
}
Binary file added release/dingo-0.11/dingo-darwin-386
Binary file not shown.
Binary file added release/dingo-0.11/dingo-darwin-amd64
Binary file not shown.
Binary file added release/dingo-0.11/dingo-freebsd-386
Binary file not shown.
Binary file added release/dingo-0.11/dingo-freebsd-amd64
Binary file not shown.
Binary file added release/dingo-0.11/dingo-linux-386
Binary file not shown.
Binary file added release/dingo-0.11/dingo-linux-amd64
Binary file not shown.
Binary file added release/dingo-0.11/dingo-netbsd-386
Binary file not shown.
Binary file added release/dingo-0.11/dingo-netbsd-amd64
Binary file not shown.
Binary file added release/dingo-0.11/dingo-openbsd-386
Binary file not shown.
Binary file added release/dingo-0.11/dingo-openbsd-amd64
Binary file not shown.
Binary file added release/dingo-0.11/dingo-windows-386
Binary file not shown.
Binary file added release/dingo-0.11/dingo-windows-amd64
Binary file not shown.

0 comments on commit 01fbcff

Please sign in to comment.