(address string, dnsPort int, doRedirect bool, nameserver string, netProtocol string, proxyPort int, scriptPath string, certFile string, keyFile string)
| 62 | } |
| 63 | |
| 64 | func (p *DNSProxy) Configure(address string, dnsPort int, doRedirect bool, nameserver string, netProtocol string, proxyPort int, scriptPath string, certFile string, keyFile string) error { |
| 65 | var err error |
| 66 | |
| 67 | p.Address = address |
| 68 | p.doRedirect = doRedirect |
| 69 | p.CertFile = certFile |
| 70 | p.KeyFile = keyFile |
| 71 | |
| 72 | if scriptPath != "" { |
| 73 | if err, p.Script = LoadDnsProxyScript(scriptPath, p.Sess); err != nil { |
| 74 | return err |
| 75 | } else { |
| 76 | p.Debug("proxy script %s loaded.", scriptPath) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | dnsClient := dns.Client{ |
| 81 | DialTimeout: dialTimeout, |
| 82 | Net: netProtocol, |
| 83 | ReadTimeout: readTimeout, |
| 84 | WriteTimeout: writeTimeout, |
| 85 | } |
| 86 | |
| 87 | resolverAddr := fmt.Sprintf("%s:%d", nameserver, dnsPort) |
| 88 | |
| 89 | handler := dns.HandlerFunc(func(w dns.ResponseWriter, req *dns.Msg) { |
| 90 | m := new(dns.Msg) |
| 91 | m.SetReply(req) |
| 92 | |
| 93 | clientIP := strings.Split(w.RemoteAddr().String(), ":")[0] |
| 94 | |
| 95 | req, res := p.onRequestFilter(req, clientIP) |
| 96 | if res == nil { |
| 97 | // unused var is time til res |
| 98 | res, _, err := dnsClient.Exchange(req, resolverAddr) |
| 99 | if err != nil { |
| 100 | p.Debug("error while resolving DNS query: %s", err.Error()) |
| 101 | m.SetRcode(req, dns.RcodeServerFailure) |
| 102 | w.WriteMsg(m) |
| 103 | return |
| 104 | } |
| 105 | res = p.onResponseFilter(req, res, clientIP) |
| 106 | if res == nil { |
| 107 | p.Debug("response is nil") |
| 108 | m.SetRcode(req, dns.RcodeServerFailure) |
| 109 | w.WriteMsg(m) |
| 110 | return |
| 111 | } else { |
| 112 | if err := w.WriteMsg(res); err != nil { |
| 113 | p.Error("Error writing response: %s", err) |
| 114 | } |
| 115 | } |
| 116 | } else { |
| 117 | if err := w.WriteMsg(res); err != nil { |
| 118 | p.Error("Error writing response: %s", err) |
| 119 | } |
| 120 | } |
| 121 | }) |
nothing calls this directly
no test coverage detected