-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.go
98 lines (92 loc) · 2.52 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
package main
import (
"encoding/json"
"fmt"
"log"
"net"
"os"
"os/signal"
"runtime"
"strconv"
"strings"
"syscall"
)
// ActionMessage is a struct representing a message from CSM
type ActionMessage struct {
Version int `json:"Version"`
ClientID string `json:"ClientId"`
Type string `json:"Type"`
Service string `json:"Service"`
Action string `json:"Api"`
Timestamp int `json:"Timestamp"`
AttemptLatency int `json:"AttemptLatency"`
Fqdn string `json:"Fqdn"`
UserAgent string `json:"UserAgent"`
AccessKey string `json:"AccessKey"`
Region string `json:"Region"`
HTTPStatusCode int `json:"HttpStatusCode"`
FinalHTTPStatusCode int `json:"FinalHttpStatusCode"`
XAmzRequestID string `json:"XAmzRequestId"`
XAmzID2 string `json:"XAmzId2"`
}
func listen(connection *net.UDPConn, quit chan struct{}) {
buffer := make([]byte, 4096)
n, _, err := 0, new(net.UDPAddr), error(nil)
var message ActionMessage
for err == nil {
n, _, err = connection.ReadFromUDP(buffer)
err := json.Unmarshal(buffer[:n], &message)
if err != nil {
log.Println(err)
}
//Each action taken sends two json messages. The first has a type of "ApiCallAttempt" this filters for the API call itself
if message.Type == "ApiCall" {
fmt.Println(strings.ToLower(message.Service) + ":" + message.Action)
}
}
fmt.Println("listener failed - ", err)
quit <- struct{}{}
}
//SetupCloseHandler Displays a message when the user closes the program
func SetupCloseHandler() {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
fmt.Println("\rCtrl+C pressed, Stopping...")
os.Exit(0)
}()
}
func main() {
var port = 31000
var err error
var addr = net.UDPAddr{
Port: port,
IP: net.IP{127, 0, 0, 1},
}
if os.Getenv("AWS_CSM_PORT") != "" {
port, err = strconv.Atoi(os.Getenv("AWS_CSM_PORT"))
if err != nil {
fmt.Println("Could not parse value of AWS_CSM_PORT Exiting...")
os.Exit(1)
}
}
if os.Getenv("IN_DOCKER") == "True" {
addr = net.UDPAddr{
Port: port,
IP: net.IP{0, 0, 0, 0},
}
}
connection, err := net.ListenUDP("udp", &addr)
if err != nil {
fmt.Println("Could not start Action hero on the specified port, Exiting...")
os.Exit(1)
}
fmt.Println("Action Hero Starting...")
SetupCloseHandler()
quit := make(chan struct{})
for i := 0; i < runtime.NumCPU(); i++ {
go listen(connection, quit)
}
<-quit // hang until an error
}