-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
77 lines (65 loc) · 1.93 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"fmt"
"log"
"net"
"net/http"
"os"
"strconv"
"time"
"github.com/miekg/dns"
)
func main() {
// Using cli app as a parser to process the flags and build a config struct
// which is passed to the supplied function.
NewParser(runner).Run(os.Args)
}
func runner(c *Config) {
log.Println("configuring app")
var (
dnsAddress = net.JoinHostPort(c.Bind, strconv.Itoa(c.DNSPort))
httpAddress = net.JoinHostPort(c.Bind, strconv.Itoa(c.HTTPPort))
proxyIP = net.ParseIP(c.Bind)
routes = NewRouter(c)
)
log.Printf("starting DNS to handle *.%s domains at %s\n", c.TLD, dnsAddress)
dns.HandleFunc(c.TLD, func(w dns.ResponseWriter, r *dns.Msg) {
m := new(dns.Msg)
m.SetReply(r)
m.Answer = append(m.Answer, &dns.A{
Hdr: dns.RR_Header{Name: r.Question[0].Name, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 10},
A: proxyIP,
})
w.WriteMsg(m)
})
go serveDNS(dnsAddress, "tcp")
go serveDNS(dnsAddress, "udp")
log.Printf("starting HTTP proxy at %s\n", httpAddress)
log.Printf("routing table: \n%s", routes)
s := &http.Server{
Addr: httpAddress,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
Handler: func() http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
// if request host is domain or subdomain match for any of the
// supplied routes proxy the request over
if proxy := routes.Match(req.Host); proxy != nil {
proxy.ServeHTTP(w, req)
return
}
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "no route setup to handle %s\ncurrent routes: \n%v", req.Host, routes)
}
}(),
}
if err := s.ListenAndServe(); err != nil {
fmt.Printf("failed to setup the proxy server: %v\n", err)
}
}
func serveDNS(address, net string) {
server := &dns.Server{Addr: address, Net: net, TsigSecret: nil}
if err := server.ListenAndServe(); err != nil {
log.Fatalf("failed to setup the %s DNS server: %v\n", net, err)
}
}