forked from spr-networks/super
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[logs] update packet logs to use gopacket
- Loading branch information
lts-po
committed
Aug 26, 2022
1 parent
aed2389
commit 8cf6343
Showing
11 changed files
with
341 additions
and
354 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module main | ||
|
||
go 1.18 | ||
|
||
require github.com/spr-networks/sprbus v0.0.0-20220811094515-092f7af34fb2 | ||
|
||
require ( | ||
github.com/golang/protobuf v1.5.2 // indirect | ||
github.com/moby/moby v20.10.17+incompatible // indirect | ||
golang.org/x/net v0.0.0-20201021035429-f5854403a974 // indirect | ||
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4 // indirect | ||
golang.org/x/text v0.3.3 // indirect | ||
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect | ||
google.golang.org/grpc v1.48.0 // indirect | ||
google.golang.org/protobuf v1.27.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/spr-networks/sprbus" | ||
) | ||
|
||
var ServerEventSock = "/state/plugins/packet_logs/server.sock" | ||
|
||
var pipeFile = "/state/plugins/packet_logs/ulogd.json" | ||
|
||
type nftEntry struct { | ||
//Timestamp time.Time `json:"timestamp"` | ||
//TODO cannot parse "+0200\"" as | ||
Timestamp string `json:"timestamp"` | ||
Action string `json:"action"` | ||
InterfaceIn string `json:"oob.in"` | ||
InterfaceOut string `json:"oob.out"` | ||
} | ||
|
||
// runs in a thread | ||
func startUlogd() { | ||
cmd := exec.Command("ulogd", "-c", "/etc/ulogd.conf") | ||
|
||
err := cmd.Run() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func sprServer() { | ||
_, err := sprbus.NewServer(ServerEventSock) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func main() { | ||
log.Println("starting ulogd2...") | ||
go startUlogd() | ||
|
||
go sprServer() | ||
|
||
client, err := sprbus.NewClient(ServerEventSock) | ||
defer client.Close() | ||
|
||
if err != nil { | ||
log.Fatal("err", err) | ||
} | ||
|
||
fmt.Println("sprbus client connected") | ||
|
||
log.Println("open ulogd named pipe for reading") | ||
file, err := os.OpenFile(pipeFile, os.O_RDONLY, os.ModeNamedPipe) | ||
if err != nil { | ||
log.Fatal("open error:", err) | ||
} | ||
|
||
reader := bufio.NewReader(file) | ||
|
||
log.Println("entering main loop") | ||
|
||
for { | ||
line, err := reader.ReadBytes('\n') | ||
if err != nil { | ||
log.Println("read error:", err) | ||
continue | ||
} | ||
|
||
var logEntry netfilterEntry | ||
if err := json.Unmarshal(line, &logEntry); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fmt.Printf("## [%v] %v == %v\n", *logEntry.OobPrefix, *logEntry.Timestamp, *logEntry.Action) | ||
if logEntry.SrcIp != nil && logEntry.DestIp != nil && logEntry.SrcPort != nil && logEntry.DestPort != nil { | ||
fmt.Printf("## %v:%v -> %v:%v\n", *logEntry.SrcIp, *logEntry.SrcPort, *logEntry.DestIp, *logEntry.DestPort) | ||
} | ||
|
||
//action := "" | ||
/*if logEntry.Action != nil { | ||
action = *logEntry.Action | ||
}*/ | ||
|
||
prefix := "" | ||
if logEntry.OobPrefix != nil { | ||
prefix = strings.TrimSpace(strings.ToLower(*logEntry.OobPrefix)) | ||
} | ||
|
||
registeredPrefix := regexp.MustCompile(`^(lan|wan|drop):(in|out|forward|input|mac|pfw)$`).MatchString | ||
|
||
topic := fmt.Sprintf("nft:%s", prefix) | ||
|
||
if registeredPrefix(prefix) { | ||
//fmt.Printf("%% publish. #subscribers: %d, hasClients= %v\n", len(server.Subscribers(topic)), hasClients) | ||
//log.Printf("subscribers: %v == %v\n", topic, server) | ||
|
||
client.Publish(topic, string(line)) | ||
} else { | ||
topic = fmt.Sprintf("nft:ip") | ||
client.Publish(topic, string(line)) | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.