Skip to content

Commit f20486e

Browse files
committed
simplify parser for now
1 parent b44ae25 commit f20486e

File tree

1 file changed

+25
-32
lines changed

1 file changed

+25
-32
lines changed

providers/http/nfqueue/nfqueue.go

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"strings"
1414
"time"
1515

16-
nfqueue "github.com/florianl/go-nfqueue"
16+
gnfqueue "github.com/florianl/go-nfqueue"
1717
"github.com/go-acme/lego/v4/log"
1818
"github.com/google/gopacket"
1919
"github.com/google/gopacket/layers"
@@ -59,57 +59,46 @@ func (w *HTTPProvider) serve(domain, token, keyAuth string) error {
5959
if err != nil {
6060
return err
6161
}
62-
config := nfqueue.Config{
62+
config := gnfqueue.Config{
6363
NfQueue: 8555,
6464
MaxPacketLen: 0xFFFF,
6565
MaxQueueLen: 0xFF,
66-
Copymode: nfqueue.NfQnlCopyPacket,
66+
Copymode: gnfqueue.NfQnlCopyPacket,
6767
WriteTimeout: 15 * time.Millisecond,
6868
}
69-
nf, err := nfqueue.Open(&config)
69+
nf, err := gnfqueue.Open(&config)
7070
if err != nil {
7171
return err
7272
}
7373
defer nf.Close()
7474

7575
//handle Packet
76-
handlepacket := func(a nfqueue.Attribute) int {
77-
log.Infof("handlepacket called!")
76+
handlepacket := func(a gnfqueue.Attribute) int {
7877
id := *a.PacketID
7978
opt := gopacket.DecodeOptions{
8079
NoCopy: true,
8180
Lazy: false,
8281
}
83-
payload := gopacket.NewPacket(*a.Payload, layers.LayerTypeEthernet, opt)
84-
var srcip net.IP
85-
//var ipLayer int
86-
//get from/to ips
87-
if ipv4Layer := payload.Layer(layers.LayerTypeIPv4); ipv4Layer != nil {
88-
//ipv4
89-
90-
ip, _ := ipv4Layer.(*layers.IPv4)
91-
srcip = ip.SrcIP
92-
// ipLayer = 0
93-
} else {
94-
//ipv6
95-
ipv6Layer := payload.Layer(layers.LayerTypeIPv6)
96-
ip, _ := ipv6Layer.(*layers.IPv6)
97-
srcip = ip.SrcIP
98-
}
82+
//assume ipv4 for now, will segfault
83+
payload := gopacket.NewPacket(*a.Payload, layers.LayerTypeIPv4, opt)
84+
ipL := payload.Layer(layers.LayerTypeIPv4)
85+
srcip := ipL.(*layers.IPv4).SrcIP
86+
log.Infof("%s", srcip.String())
9987
if tcpLayer := payload.Layer(layers.LayerTypeTCP); tcpLayer != nil {
100-
log.Infof("This is a TCP packet!")
10188
// Get actual TCP data from this layer
10289
inputTcp, _ := tcpLayer.(*layers.TCP)
10390
log.Infof("From src port %d to dst port %d\n", inputTcp.SrcPort, inputTcp.DstPort)
91+
log.Infof("layer under P %s", inputTcp.Payload)
10492
// this should be HTTP payload
10593
httpPayload, err := http.ReadRequest(bufio.NewReader((bytes.NewReader(inputTcp.LayerPayload()))))
10694
if err != nil {
107-
nf.SetVerdict(id, nfqueue.NfAccept)
95+
nf.SetVerdict(id, gnfqueue.NfAccept)
96+
return 0
10897
}
10998
// check token in http
11099
if strings.Contains(httpPayload.URL.Path, token) {
111-
//we got the token!, block the packet.
112-
nf.SetVerdict(id, nfqueue.NfDrop)
100+
//we got the token!, block the packet to backend server.
101+
nf.SetVerdict(id, gnfqueue.NfDrop)
113102
//forge our new reply
114103
log.Infof("got token packet")
115104
replyhttp := craftkeyauthresponse(keyAuth)
@@ -121,15 +110,18 @@ func (w *HTTPProvider) serve(domain, token, keyAuth string) error {
121110
Ack: inputTcp.Seq + 1,
122111
Window: 100,
123112
}
113+
outputTcp.Payload = replyhttp
124114
//check network layer
125115
outputTcp.SetNetworkLayerForChecksum(&layers.IPv4{})
126-
ouputIPpack := gopacket.NewSerializeBuffer()
116+
replypacket := gopacket.NewSerializeBuffer()
117+
bytes, _ := replypacket.AppendBytes(len(replyhttp))
118+
127119
opts := gopacket.SerializeOptions{
128120
FixLengths: true,
129121
ComputeChecksums: true,
130122
}
131123
// serialize packet
132-
if err := gopacket.SerializeLayers(ouputIPpack, opts, outputTcp, gopacket.Payload(replyhttp)); err != nil {
124+
if err := outputTcp.SerializeTo(replypacket, opts); err != nil {
133125
panic(err)
134126
}
135127
//dial validation agent
@@ -138,17 +130,18 @@ func (w *HTTPProvider) serve(domain, token, keyAuth string) error {
138130
if err != nil {
139131
panic(err)
140132
}
141-
if _, err := conn.Write(ouputIPpack.Bytes()); err != nil {
133+
if _, err := conn.Write(replypacket.Bytes()); err != nil {
142134
panic(err)
143135
}
144-
136+
// packet sent, end of function
137+
return 0
145138
} else {
146-
nf.SetVerdict(id, nfqueue.NfAccept)
139+
nf.SetVerdict(id, gnfqueue.NfAccept)
147140
return 0
148141
}
149142

150143
} else {
151-
nf.SetVerdict(id, nfqueue.NfAccept)
144+
nf.SetVerdict(id, gnfqueue.NfAccept)
152145
}
153146

154147
return 0

0 commit comments

Comments
 (0)