@@ -14,13 +14,16 @@ import (
1414 "time"
1515
1616 nfqueue "github.com/florianl/go-nfqueue"
17+ "github.com/go-acme/lego/v4/log"
1718 "github.com/google/gopacket"
1819 "github.com/google/gopacket/layers"
1920)
2021
2122// HTTPProvider implements HTTPProvider for `http-01` challenge.
2223type HTTPProvider struct {
23- port string
24+ port string
25+ context context.Context
26+ cancel context.CancelFunc
2427}
2528
2629// NewHttpDpiProvider returns a HTTPProvider instance with a configured port.
@@ -45,13 +48,13 @@ func craftkeyauthresponse(keyAuth string) []byte {
4548 return reply
4649}
4750
48- // Present runs server by sniffing packets on firewall and inject response into it.
51+ // serve runs server by sniffing packets on firewall and inject response into it.
4952// iptables ://
50- func (w * HTTPProvider ) Present (domain , token , keyAuth string ) error {
53+ func (w * HTTPProvider ) serve (domain , token , keyAuth string ) error {
5154 //run nfqueue start
5255 cmd := exec .Command ("iptables" , "-I" , "INPUT" , "-p" , "tcp" , "--dport" , w .port , "-j" , "NFQUEUE" , "--queue-num" , "8555" )
5356 err := cmd .Run ()
54- // run this down when
57+ // run this down when this server come down
5558 defer exec .Command ("iptables" , "-D" , "INPUT" , "-p" , "tcp" , "--dport" , w .port , "-j" , "NFQUEUE" , "--queue-num" , "8555" ).Run ()
5659 if err != nil {
5760 return err
@@ -68,11 +71,10 @@ func (w *HTTPProvider) Present(domain, token, keyAuth string) error {
6871 return err
6972 }
7073 defer nf .Close ()
71- ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
72- defer cancel ()
7374
7475 //handle Packet
7576 handlepacket := func (a nfqueue.Attribute ) int {
77+ log .Infof ("handlepacket called!" )
7678 id := * a .PacketID
7779 opt := gopacket.DecodeOptions {
7880 NoCopy : true ,
@@ -95,10 +97,10 @@ func (w *HTTPProvider) Present(domain, token, keyAuth string) error {
9597 srcip = ip .SrcIP
9698 }
9799 if tcpLayer := payload .Layer (layers .LayerTypeTCP ); tcpLayer != nil {
98- fmt . Println ("This is a TCP packet!" )
100+ log . Infof ("This is a TCP packet!" )
99101 // Get actual TCP data from this layer
100102 inputTcp , _ := tcpLayer .(* layers.TCP )
101- fmt . Printf ("From src port %d to dst port %d\n " , inputTcp .SrcPort , inputTcp .DstPort )
103+ log . Infof ("From src port %d to dst port %d\n " , inputTcp .SrcPort , inputTcp .DstPort )
102104 // this should be HTTP payload
103105 httpPayload , err := http .ReadRequest (bufio .NewReader ((bytes .NewReader (inputTcp .LayerPayload ()))))
104106 if err != nil {
@@ -109,6 +111,7 @@ func (w *HTTPProvider) Present(domain, token, keyAuth string) error {
109111 //we got the token!, block the packet.
110112 nf .SetVerdict (id , nfqueue .NfDrop )
111113 //forge our new reply
114+ log .Infof ("got token packet" )
112115 replyhttp := craftkeyauthresponse (keyAuth )
113116 outputTcp := & layers.TCP {
114117 Seq : 3398127 ,
@@ -152,23 +155,30 @@ func (w *HTTPProvider) Present(domain, token, keyAuth string) error {
152155 }
153156
154157 // Register your function to listen on nflqueue queue 100
155- err = nf .Register (ctx , handlepacket )
158+ err = nf .Register (w . context , handlepacket )
156159 if err != nil {
157160 fmt .Println (err )
158161 return nil
159162 }
160163
161164 // Block till the context expires
162- <- ctx .Done ()
165+ <- w .context .Done ()
166+ return nil
167+ }
168+
169+ func (w * HTTPProvider ) Present (domain , token , keyAuth string ) error {
170+ w .context , w .cancel = context .WithCancel (context .Background ())
171+ go w .serve (domain , token , keyAuth )
163172 return nil
164173}
165174
166175// CleanUp removes the firewall rule created for the challenge.
167- // Present should removed it already but just do be safe:
176+ // solve should removed it already but just do be safe:
168177// iptables -D INPUT -p tcp --dport Port -j NFQUEUE --queue-num 8555
169178func (w * HTTPProvider ) CleanUp (domain , token , keyAuth string ) error {
170- // we can't afford .
171179 cmd := exec .Command ("iptables" , "-D" , "INPUT" , "-p" , "tcp" , "--dport" , w .port , "-j" , "NFQUEUE" , "--queue-num" , "8555" )
172180 cmd .Run ()
181+ // tell nfqueue to shut down
182+ w .cancel ()
173183 return nil
174184}
0 commit comments