@@ -3,12 +3,14 @@ package main
33import (
44 "fmt"
55 "net"
6+ "net/netip"
67 "strings"
78 "sync"
89 "time"
910
11+ "codeberg.org/miekg/dns"
12+ "codeberg.org/miekg/dns/rdata"
1013 "github.com/jedisct1/dlog"
11- "github.com/miekg/dns"
1214)
1315
1416type CaptivePortalEntryIPs []net.IP
@@ -25,53 +27,56 @@ func (captivePortalHandler *CaptivePortalHandler) Stop() {
2527 captivePortalHandler .wg .Wait ()
2628}
2729
28- func (ipsMap * CaptivePortalMap ) GetEntry (msg * dns.Msg ) (* dns.Question , * CaptivePortalEntryIPs ) {
30+ func (ipsMap * CaptivePortalMap ) GetEntry (msg * dns.Msg ) (dns.RR , * CaptivePortalEntryIPs ) {
2931 if len (msg .Question ) != 1 {
3032 return nil , nil
3133 }
32- question := & msg .Question [0 ]
33- name , err := NormalizeQName (question .Name )
34+ question := msg .Question [0 ]
35+ hdr := question .Header ()
36+ name , err := NormalizeQName (hdr .Name )
3437 if err != nil {
3538 return nil , nil
3639 }
3740 ips , ok := (* ipsMap )[name ]
3841 if ! ok {
3942 return nil , nil
4043 }
41- if question . Qclass != dns .ClassINET {
44+ if hdr . Class != dns .ClassINET {
4245 return nil , nil
4346 }
4447 return question , & ips
4548}
4649
47- func HandleCaptivePortalQuery (msg * dns.Msg , question * dns.Question , ips * CaptivePortalEntryIPs ) * dns.Msg {
50+ func HandleCaptivePortalQuery (msg * dns.Msg , question dns.RR , ips * CaptivePortalEntryIPs ) * dns.Msg {
4851 respMsg := EmptyResponseFromMessage (msg )
4952 ttl := uint32 (1 )
50- if question .Qtype == dns .TypeA {
53+ hdr := question .Header ()
54+ qtype := dns .RRToType (question )
55+ if qtype == dns .TypeA {
5156 for _ , xip := range * ips {
5257 if ip := xip .To4 (); ip != nil {
5358 rr := new (dns.A )
54- rr .Hdr = dns.RR_Header {Name : question .Name , Rrtype : dns . TypeA , Class : dns .ClassINET , Ttl : ttl }
55- rr .A = ip
59+ rr .Hdr = dns.Header {Name : hdr .Name , Class : dns .ClassINET , TTL : ttl }
60+ rr .A = rdata. A { Addr : netip . AddrFrom4 ([ 4 ] byte ( ip ))}
5661 respMsg .Answer = append (respMsg .Answer , rr )
5762 }
5863 }
59- } else if question . Qtype == dns .TypeAAAA {
64+ } else if qtype == dns .TypeAAAA {
6065 for _ , xip := range * ips {
6166 if xip .To4 () == nil {
6267 rr := new (dns.AAAA )
63- rr .Hdr = dns.RR_Header {Name : question .Name , Rrtype : dns . TypeAAAA , Class : dns .ClassINET , Ttl : ttl }
64- rr .AAAA = xip
68+ rr .Hdr = dns.Header {Name : hdr .Name , Class : dns .ClassINET , TTL : ttl }
69+ rr .AAAA = rdata. AAAA { Addr : netip . AddrFrom16 ([ 16 ] byte ( xip . To16 ()))}
6570 respMsg .Answer = append (respMsg .Answer , rr )
6671 }
6772 }
6873 }
6974
70- qType , ok := dns .TypeToString [question . Qtype ]
75+ qTypeStr , ok := dns .TypeToString [qtype ]
7176 if ! ok {
72- qType = fmt .Sprint (question . Qtype )
77+ qTypeStr = fmt .Sprint (qtype )
7378 }
74- dlog .Infof ("Query for captive portal detection: [%v] (%v)" , question .Name , qType )
79+ dlog .Infof ("Query for captive portal detection: [%v] (%v)" , hdr .Name , qTypeStr )
7580 return respMsg
7681}
7782
@@ -97,7 +102,8 @@ func handleColdStartClient(clientPc *net.UDPConn, cancelChannel chan struct{}, i
97102 }
98103 packet := buffer [:length ]
99104 msg := & dns.Msg {}
100- if err := msg .Unpack (packet ); err != nil {
105+ msg .Data = packet
106+ if err := msg .Unpack (); err != nil {
101107 return false
102108 }
103109 question , ips := ipsMap .GetEntry (msg )
@@ -108,8 +114,8 @@ func handleColdStartClient(clientPc *net.UDPConn, cancelChannel chan struct{}, i
108114 if respMsg == nil {
109115 return false
110116 }
111- if response , err := respMsg .Pack (); err == nil {
112- clientPc .WriteTo (response , clientAddr )
117+ if err := respMsg .Pack (); err == nil {
118+ clientPc .WriteTo (respMsg . Data , clientAddr )
113119 }
114120 return false
115121}
0 commit comments