-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
65 lines (56 loc) · 1.32 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"fmt"
"log"
"os"
"os/signal"
"github.com/dropbox/goebpf"
)
func main() {
// Specify Interface Name
interfaceName := "lo"
// IP BlockList
// Add the IPs you want to be blocked
ipList := []string{
"12.12.11.32",
}
// Load XDP Into App
bpf := goebpf.NewDefaultEbpfSystem()
err := bpf.LoadElf("bpf/xdp.elf")
if err != nil {
log.Fatalf("LoadELF() failed: %s", err)
}
blacklist := bpf.GetMapByName("blacklist")
if blacklist == nil {
log.Fatalf("eBPF map 'blacklist' not found\n")
}
xdp := bpf.GetProgramByName("firewall")
if xdp == nil {
log.Fatalln("Program 'firewall' not found in Program")
}
err = xdp.Load()
if err != nil {
fmt.Printf("xdp.Attach(): %v", err)
}
err = xdp.Attach(interfaceName)
if err != nil {
log.Fatalf("Error attaching to Interface: %s", err)
}
BlockIPAddress(ipList, blacklist)
defer xdp.Detach()
ctrlC := make(chan os.Signal, 1)
signal.Notify(ctrlC, os.Interrupt)
log.Println("XDP Program Loaded successfuly into the Kernel.")
log.Println("Press CTRL+C to stop.")
<-ctrlC
}
// The Function That adds the IPs to the blacklist map
func BlockIPAddress(ipAddreses []string, blacklist goebpf.Map) error {
for index, ip := range ipAddreses {
err := blacklist.Insert(goebpf.CreateLPMtrieKey(ip), index)
if err != nil {
return err
}
}
return nil
}