-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
177 lines (155 loc) · 3.78 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"bytes"
_ "embed"
"fmt"
"os"
"os/exec"
"os/signal"
"os/user"
"strconv"
"sync"
"syscall"
"time"
"github.com/DataDog/dev/konamicode-ebpf/pkg/sound"
manager "github.com/DataDog/ebpf-manager"
"github.com/sirupsen/logrus"
)
//go:embed ebpf/bin/probe.o
var Probe []byte
var m = &manager.Manager{
Probes: []*manager.Probe{
{
ProbeIdentificationPair: manager.ProbeIdentificationPair{
EBPFSection: "kprobe/input_handle_event",
EBPFFuncName: "kprobe_input_handle_event",
},
},
},
PerfMaps: []*manager.PerfMap{
{
Map: manager.Map{Name: "notes"},
},
},
}
type KonamiCodeCtx struct {
sp *sound.SineWavePlayer
}
func checkKonamicode() (uint32, error) {
m, _, err := m.GetMap("konamicode_activation_counter")
if err != nil {
logrus.Printf("checkKonamicode error: %v\n", err)
return 0, err
}
var key, val uint32
err = m.Lookup(&key, &val)
if err != nil {
logrus.Printf("checkKonamicode error: %v\n", err)
return 0, err
}
return val, nil
}
func openURL(url string) {
cmd := exec.Command("xdg-open", url)
// get the initial user to open the browser
sudo_user := os.Getenv("SUDO_USER")
if sudo_user == "" {
sudo_user = "root" // try to open with root, but should not works
} else {
os.Setenv("HOME", "/home/"+sudo_user)
}
u, err := user.Lookup(sudo_user)
if err != nil {
logrus.Printf("user lookup failed: %s\n", err)
return
}
uid, _ := strconv.Atoi(u.Uid)
gid, _ := strconv.Atoi(u.Gid)
cmd.SysProcAttr = &syscall.SysProcAttr{}
cmd.SysProcAttr.Credential = &syscall.Credential{Uid: uint32(uid), Gid: uint32(gid)}
_, err = cmd.CombinedOutput()
if err != nil {
logrus.Printf("exec failed: %s\n", err)
}
}
func playSong(sp *sound.SineWavePlayer) {
const (
freqC = 523
freqE = 659
freqG = 784
)
sp.QueueNote(sound.Note{Freq: freqC, Duration: 500})
sp.QueueNote(sound.Note{Freq: freqE, Duration: 500})
sp.QueueNote(sound.Note{Freq: freqG, Duration: 500})
}
func start_konamicode_watcher(sp *sound.SineWavePlayer) {
go func() {
konamicode_check := time.NewTicker(time.Second)
for {
select {
case _ = <-konamicode_check.C:
val, err := checkKonamicode()
if err != nil {
continue
} else if val != 0 {
logrus.Printf("KONAMI CODE ACTIVATED \\o/ !\n")
playSong(sp)
openURL("https://en.wikipedia.org/wiki/Konami_Code")
return
}
}
}
}()
}
func (kcCtx *KonamiCodeCtx) NotesPerfMapHandler(cpu int, data []byte, perfmap *manager.PerfMap, manager *manager.Manager) {
dataLen := uint64(len(data))
if dataLen < 16 {
logrus.Println("got note with wrong size %d\n", dataLen)
return
}
freq := ByteOrder.Uint64(data[0:8])
duration := ByteOrder.Uint64(data[8:16])
n := sound.Note{Freq: int64(freq), Duration: int64(duration)}
kcCtx.sp.QueueNote(n)
}
func main() {
kcCtx := &KonamiCodeCtx{}
sp, ready, err := sound.NewSineWavePlayer(48000, 2, sound.FormatSignedInt16LE)
if err != nil {
panic(fmt.Errorf(err.Error()))
}
<-ready
var wg sync.WaitGroup
wg.Add(1)
go sp.PlayLoop(&wg)
kcCtx.sp = sp
notesPerfMap, ok := m.GetPerfMap("notes")
if !ok {
panic(fmt.Errorf("failed to get notes perfmap"))
}
notesPerfMap.PerfMapOptions = manager.PerfMapOptions{
DataHandler: kcCtx.NotesPerfMapHandler,
}
// Initialize the managers
if err := m.Init(bytes.NewReader(Probe)); err != nil {
panic(fmt.Errorf("failed to init manager: %w", err))
}
// Start
if err := m.Start(); err != nil {
panic(fmt.Errorf("failed to start manager: %w", err))
}
defer m.Stop(manager.CleanAll)
logrus.Println("manager successfully started")
start_konamicode_watcher(sp)
logrus.Println("=> Cmd+C to stop")
wait()
sp.Close()
wg.Wait()
}
// wait - Waits until an interrupt or kill signal is sent
func wait() {
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, os.Kill)
<-sig
fmt.Println()
}