@@ -5,18 +5,21 @@ import (
55 "fmt"
66 "io/ioutil"
77 "math"
8+ "sync"
89
910 "github.com/miekg/dns"
1011 "github.com/pkg/errors"
1112)
1213
1314type DNSProxy struct {
14- Cache * Cache
15- CorrelationId string
16- Repo string
17- ApiClient * ApiClient
18- EgressPolicy string
19- AllowedEndpoints map [string ][]Endpoint
15+ Cache * Cache
16+ CorrelationId string
17+ Repo string
18+ ApiClient * ApiClient
19+ EgressPolicy string
20+ AllowedEndpoints map [string ][]Endpoint
21+ ReverseIPLookup map [string ]string
22+ ReverseIPLookupMutex sync.RWMutex
2023}
2124
2225type DNSResponse struct {
@@ -70,6 +73,25 @@ func (proxy *DNSProxy) getResponse(requestMsg *dns.Msg) (*dns.Msg, error) {
7073 return responseMsg , nil
7174}
7275
76+ func (proxy * DNSProxy ) SetReverseIPLookup (domain , ipAddress string ) {
77+ proxy .ReverseIPLookupMutex .Lock ()
78+
79+ proxy .ReverseIPLookup [ipAddress ] = domain
80+
81+ proxy .ReverseIPLookupMutex .Unlock ()
82+ }
83+
84+ func (proxy * DNSProxy ) GetReverseIPLookup (ipAddress string ) string {
85+ proxy .ReverseIPLookupMutex .RLock ()
86+ domain , found := proxy .ReverseIPLookup [ipAddress ]
87+ proxy .ReverseIPLookupMutex .RUnlock ()
88+ if found {
89+ return domain
90+ } else {
91+ return ""
92+ }
93+ }
94+
7395func (proxy * DNSProxy ) processOtherTypes (q * dns.Question , requestMsg * dns.Msg ) (* dns.RR , error ) {
7496 queryMsg := new (dns.Msg )
7597 requestMsg .CopyTo (queryMsg )
@@ -79,7 +101,7 @@ func (proxy *DNSProxy) processOtherTypes(q *dns.Question, requestMsg *dns.Msg) (
79101}
80102
81103func (proxy * DNSProxy ) isAllowedDomain (domain string ) bool {
82- for domainName , _ := range proxy .AllowedEndpoints {
104+ for domainName := range proxy .AllowedEndpoints {
83105 if dns .Fqdn (domainName ) == dns .Fqdn (domain ) {
84106 return true
85107 }
@@ -191,11 +213,12 @@ func (proxy *DNSProxy) processTypeA(q *dns.Question, requestMsg *dns.Msg) (*dns.
191213 return nil , err
192214 }
193215
194- return & rr , nil
216+ proxy . SetReverseIPLookup ( q . Name , ip )
195217
218+ return & rr , nil
196219}
197220
198- func startDNSServer (dnsProxy DNSProxy , server DNSServer , errc chan error ) {
221+ func startDNSServer (dnsProxy * DNSProxy , server DNSServer , errc chan error ) {
199222 dns .HandleFunc ("." , func (w dns.ResponseWriter , r * dns.Msg ) {
200223 switch r .Opcode {
201224 case dns .OpcodeQuery :
0 commit comments