-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sumire (菫) <[email protected]>
- Loading branch information
1 parent
e04b16f
commit 5f3249b
Showing
13 changed files
with
738 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ outline.json | |
go-mod/ | ||
node_modules/ | ||
*.log | ||
.build_tags |
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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
[submodule "control/kern/headers"] | ||
path = control/kern/headers | ||
url = https://github.com/daeuniverse/dae_bpf_headers | ||
[submodule "trace/kern/headers"] | ||
path = trace/kern/headers | ||
url = https://github.com/daeuniverse/dae_bpf_headers |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
submodule_paths=control/kern/headers | ||
submodule_paths=trace/kern/headers |
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,72 @@ | ||
//go:build trace | ||
// +build trace | ||
|
||
/* | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
* Copyright (c) 2022-2024, daeuniverse Organization <[email protected]> | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"context" | ||
"os/signal" | ||
"syscall" | ||
|
||
"github.com/daeuniverse/dae/cmd/internal" | ||
"github.com/daeuniverse/dae/trace" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
IPv4, IPv6 bool | ||
L4Proto string | ||
Port int | ||
OutputFile string | ||
) | ||
|
||
func init() { | ||
traceCmd := &cobra.Command{ | ||
Use: "trace", | ||
Short: "To trace traffic", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
internal.AutoSu() | ||
|
||
if IPv4 && IPv6 { | ||
logrus.Fatalln("IPv4 and IPv6 cannot be set at the same time") | ||
} | ||
if !IPv4 && !IPv6 { | ||
IPv4 = true | ||
} | ||
IPVersion := 4 | ||
if IPv6 { | ||
IPVersion = 6 | ||
} | ||
|
||
var L4ProtoNo uint16 | ||
switch L4Proto { | ||
case "tcp": | ||
L4ProtoNo = syscall.IPPROTO_TCP | ||
case "udp": | ||
L4ProtoNo = syscall.IPPROTO_UDP | ||
default: | ||
logrus.Fatalf("Unknown L4 protocol: %s\n", L4Proto) | ||
} | ||
|
||
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) | ||
defer cancel() | ||
if err := trace.StartTrace(ctx, IPVersion, L4ProtoNo, Port, OutputFile); err != nil { | ||
logrus.Fatalln(err) | ||
} | ||
}, | ||
} | ||
|
||
traceCmd.PersistentFlags().BoolVarP(&IPv4, "ipv4", "4", false, "Capture IPv4 traffic") | ||
traceCmd.PersistentFlags().BoolVarP(&IPv6, "ipv6", "6", false, "Capture IPv6 traffic") | ||
traceCmd.PersistentFlags().StringVarP(&L4Proto, "l4-proto", "p", "tcp", "Layer 4 protocol") | ||
traceCmd.PersistentFlags().IntVarP(&Port, "port", "P", 80, "Port") | ||
traceCmd.PersistentFlags().StringVarP(&OutputFile, "output", "o", "/dev/stdout", "Output file") | ||
|
||
rootCmd.AddCommand(traceCmd) | ||
} |
Submodule headers
updated
5 files
+2 −2 | bpf_core_read.h | |
+64 −16 | bpf_helper_defs.h | |
+121 −7 | bpf_helpers.h | |
+289 −37 | bpf_tracing.h | |
+2 −2 | update_libbpf.sh |
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
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,71 @@ | ||
/* | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
* Copyright (c) 2022-2024, daeuniverse Organization <[email protected]> | ||
*/ | ||
|
||
package trace | ||
|
||
import ( | ||
"bufio" | ||
"os" | ||
"sort" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/sirupsen/logrus" | ||
"golang.org/x/exp/slices" | ||
) | ||
|
||
type Symbol struct { | ||
Type string | ||
Name string | ||
Addr uint64 | ||
} | ||
|
||
var kallsyms []Symbol | ||
var kallsymsByName map[string]Symbol = make(map[string]Symbol) | ||
var kallsymsByAddr map[uint64]Symbol = make(map[uint64]Symbol) | ||
|
||
func init() { | ||
readKallsyms() | ||
} | ||
|
||
func readKallsyms() { | ||
file, err := os.Open("/proc/kallsyms") | ||
if err != nil { | ||
logrus.Fatalf("failed to open /proc/kallsyms: %v", err) | ||
} | ||
scanner := bufio.NewScanner(file) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
parts := strings.Fields(line) | ||
if len(parts) < 3 { | ||
continue | ||
} | ||
addr, err := strconv.ParseUint(parts[0], 16, 64) | ||
if err != nil { | ||
continue | ||
} | ||
typ, name := parts[1], parts[2] | ||
kallsyms = append(kallsyms, Symbol{typ, name, addr}) | ||
kallsymsByName[name] = Symbol{typ, name, addr} | ||
kallsymsByAddr[addr] = Symbol{typ, name, addr} | ||
} | ||
sort.Slice(kallsyms, func(i, j int) bool { | ||
return kallsyms[i].Addr < kallsyms[j].Addr | ||
}) | ||
} | ||
|
||
func NearestSymbol(addr uint64) Symbol { | ||
idx, _ := slices.BinarySearchFunc(kallsyms, addr, func(x Symbol, addr uint64) int { return int(x.Addr - addr) }) | ||
if idx == len(kallsyms) { | ||
return kallsyms[idx-1] | ||
} | ||
if kallsyms[idx].Addr == addr { | ||
return kallsyms[idx] | ||
} | ||
if idx == 0 { | ||
return kallsyms[0] | ||
} | ||
return kallsyms[idx-1] | ||
} |
Oops, something went wrong.