@@ -19,12 +19,14 @@ type CloakedName struct {
1919 lastUpdate * time.Time
2020 lineNo int
2121 isIP bool
22+ PTR []string
2223}
2324
2425type PluginCloak struct {
2526 sync.RWMutex
2627 patternMatcher * PatternMatcher
2728 ttl uint32
29+ createPTR bool
2830}
2931
3032func (plugin * PluginCloak ) Name () string {
@@ -42,6 +44,7 @@ func (plugin *PluginCloak) Init(proxy *Proxy) error {
4244 return err
4345 }
4446 plugin .ttl = proxy .cloakTTL
47+ plugin .createPTR = proxy .cloakedPTR
4548 plugin .patternMatcher = NewPatternMatcher ()
4649 cloakedNames := make (map [string ]* CloakedName )
4750 for lineNo , line := range strings .Split (string (bin ), "\n " ) {
@@ -67,7 +70,8 @@ func (plugin *PluginCloak) Init(proxy *Proxy) error {
6770 if ! found {
6871 cloakedName = & CloakedName {}
6972 }
70- if ip := net .ParseIP (target ); ip != nil {
73+ ip := net .ParseIP (target )
74+ if ip != nil {
7175 if ipv4 := ip .To4 (); ipv4 != nil {
7276 cloakedName .ipv4 = append ((* cloakedName ).ipv4 , ipv4 )
7377 } else if ipv6 := ip .To16 (); ipv6 != nil {
@@ -82,6 +86,28 @@ func (plugin *PluginCloak) Init(proxy *Proxy) error {
8286 }
8387 cloakedName .lineNo = lineNo + 1
8488 cloakedNames [line ] = cloakedName
89+
90+ if ! plugin .createPTR || strings .Contains (line , "*" ) || ! cloakedName .isIP == true {
91+ continue
92+ }
93+
94+ var ptrLine string
95+ if ipv4 := ip .To4 (); ipv4 != nil {
96+ reversed , _ := dns .ReverseAddr (ip .To4 ().String ())
97+ ptrLine = strings .TrimSuffix (reversed , "." )
98+ } else {
99+ reversed , _ := dns .ReverseAddr (cloakedName .ipv6 [0 ].To16 ().String ())
100+ ptrLine = strings .TrimSuffix (reversed , "." )
101+ }
102+ ptrQueryLine := ptrEntryToQuery (ptrLine )
103+ ptrCloakedName , found := cloakedNames [ptrQueryLine ]
104+ if ! found {
105+ ptrCloakedName = & CloakedName {}
106+ }
107+ ptrCloakedName .isIP = true
108+ ptrCloakedName .PTR = append ((* ptrCloakedName ).PTR , ptrNameToFQDN (line ))
109+ ptrCloakedName .lineNo = lineNo + 1
110+ cloakedNames [ptrQueryLine ] = ptrCloakedName
85111 }
86112 for line , cloakedName := range cloakedNames {
87113 if err := plugin .patternMatcher .Add (line , cloakedName , cloakedName .lineNo ); err != nil {
@@ -91,6 +117,15 @@ func (plugin *PluginCloak) Init(proxy *Proxy) error {
91117 return nil
92118}
93119
120+ func ptrEntryToQuery (ptrEntry string ) string {
121+ return "=" + ptrEntry
122+ }
123+
124+ func ptrNameToFQDN (ptrLine string ) string {
125+ ptrLine = strings .TrimPrefix (ptrLine , "=" )
126+ return ptrLine + "."
127+ }
128+
94129func (plugin * PluginCloak ) Drop () error {
95130 return nil
96131}
@@ -101,7 +136,7 @@ func (plugin *PluginCloak) Reload() error {
101136
102137func (plugin * PluginCloak ) Eval (pluginsState * PluginsState , msg * dns.Msg ) error {
103138 question := msg .Question [0 ]
104- if question .Qclass != dns .ClassINET || (question .Qtype != dns .TypeA && question .Qtype != dns .TypeAAAA ) {
139+ if question .Qclass != dns .ClassINET || (question .Qtype != dns .TypeA && question .Qtype != dns .TypeAAAA && question . Qtype != dns . TypePTR ) {
105140 return nil
106141 }
107142 now := time .Now ()
@@ -157,13 +192,20 @@ func (plugin *PluginCloak) Eval(pluginsState *PluginsState, msg *dns.Msg) error
157192 rr .A = ip
158193 synth .Answer = append (synth .Answer , rr )
159194 }
160- } else {
195+ } else if question . Qtype == dns . TypeAAAA {
161196 for _ , ip := range cloakedName .ipv6 {
162197 rr := new (dns.AAAA )
163198 rr .Hdr = dns.RR_Header {Name : question .Name , Rrtype : dns .TypeAAAA , Class : dns .ClassINET , Ttl : ttl }
164199 rr .AAAA = ip
165200 synth .Answer = append (synth .Answer , rr )
166201 }
202+ } else if question .Qtype == dns .TypePTR {
203+ for _ , ptr := range cloakedName .PTR {
204+ rr := new (dns.PTR )
205+ rr .Hdr = dns.RR_Header {Name : question .Name , Rrtype : dns .TypePTR , Class : dns .ClassINET , Ttl : ttl }
206+ rr .Ptr = ptr
207+ synth .Answer = append (synth .Answer , rr )
208+ }
167209 }
168210 rand .Shuffle (len (synth .Answer ), func (i , j int ) { synth .Answer [i ], synth .Answer [j ] = synth .Answer [j ], synth .Answer [i ] })
169211 pluginsState .synthResponse = synth
0 commit comments