Skip to content

Commit

Permalink
Gracefully handle panic in jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
arriven committed Mar 2, 2022
1 parent 5215f02 commit 895495e
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ func parseStringTemplate(input string) string {
}

func httpJob(ctx context.Context, l *logs.Logger, args JobArgs) error {
defer panicHandler()
type HTTPClientConfig struct {
TLSClientConfig *tls.Config `json:"tls_config,omitempty"`
Timeout *time.Duration `json:"timeout"`
Expand Down Expand Up @@ -305,6 +306,7 @@ type RawNetJobConfig struct {
}

func tcpJob(ctx context.Context, l *logs.Logger, args JobArgs) error {
defer panicHandler()
type tcpJobConfig struct {
RawNetJobConfig
}
Expand Down Expand Up @@ -343,6 +345,7 @@ func tcpJob(ctx context.Context, l *logs.Logger, args JobArgs) error {
}

func udpJob(ctx context.Context, l *logs.Logger, args JobArgs) error {
defer panicHandler()
type udpJobConfig struct {
RawNetJobConfig
}
Expand Down Expand Up @@ -380,6 +383,7 @@ func udpJob(ctx context.Context, l *logs.Logger, args JobArgs) error {
}

func synFloodJob(ctx context.Context, l *logs.Logger, args JobArgs) error {
defer panicHandler()
type synFloodJobConfig struct {
BasicJobConfig
Host string
Expand All @@ -403,6 +407,7 @@ func synFloodJob(ctx context.Context, l *logs.Logger, args JobArgs) error {
}

func slowLoris(ctx context.Context, l *logs.Logger, args JobArgs) error {
defer panicHandler()
var jobConfig *slowloris.Config
err := json.Unmarshal(args, &jobConfig)
if err != nil {
Expand Down Expand Up @@ -446,6 +451,7 @@ func slowLoris(ctx context.Context, l *logs.Logger, args JobArgs) error {
}

func packetgenJob(ctx context.Context, l *logs.Logger, args JobArgs) error {
defer panicHandler()
type packetgenJobConfig struct {
BasicJobConfig
Packet json.RawMessage
Expand Down Expand Up @@ -554,6 +560,12 @@ func dumpMetrics(l *logs.Logger, path, name, clientID string) {
}
}

func panicHandler() {
if err := recover(); err != nil {
logs.Default.Warning("caught panic: %v\n some of the attacks may be unsupported on your system", err)
}
}

func main() {
var configPath string
var backupConfigPath string
Expand Down

0 comments on commit 895495e

Please sign in to comment.