Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the feature of delayed packages #170

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ ports:
udp: 5001
ssh: 2222

delays:
global:
min: 50 # milliseconds
max: 200 # milliseconds
per_port:
- port: 5000
min: 100
max: 300
protocols: ["tcp"]
- port: 5001
min: 75
max: 150
protocols: ["udp"]

rules_path: config/rules.yaml

addresses: ["1.2.3.4", "5.4.3.2"]
Expand Down
47 changes: 47 additions & 0 deletions delay/delay.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package delay

import (
"math/rand"
"time"
)

type DelayRange struct {
Min int `yaml:"min"`
Max int `yaml:"max"`
}

type PortDelay struct {
Port int `yaml:"port"`
Min int `yaml:"min"`
Max int `yaml:"max"`
Protocols []string `yaml:"protocols"`
}

type DelayConfig struct {
Global DelayRange `yaml:"global"`
PerPort []PortDelay `yaml:"per_port"`
}

func NewDelayHandler(config DelayConfig) *DelayHandler {
return &DelayHandler{config: config}
}

type DelayHandler struct {
config DelayConfig
}

func (h *DelayHandler) GetDelay(port int, protocol string) time.Duration {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a doc string

// Check port specific delay
for _, pd := range h.config.PerPort {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you solve this without a for loop?

if pd.Port == port {
for _, p := range pd.Protocols {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you solve this without a for loop?

if p == protocol {
return time.Duration(rand.Intn(pd.Max-pd.Min+1)+pd.Min) * time.Millisecond
}
}
}
}

// Fall back to global delay
return time.Duration(rand.Intn(h.config.Global.Max-h.config.Global.Min+1)+h.config.Global.Min) * time.Millisecond
}
55 changes: 55 additions & 0 deletions delay/delay_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package delay

import (
"testing"
"time"
)

func TestDelayHandler(t *testing.T) {
config := DelayConfig{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use the test config

Global: DelayRange{Min: 50, Max: 200},
PerPort: []PortDelay{
{
Port: 5000,
Min: 100,
Max: 300,
Protocols: []string{"tcp"},
},
},
}

handler := NewDelayHandler(config)

tests := []struct {
name string
port int
protocol string
minDelay time.Duration
maxDelay time.Duration
}{
{
name: "TCP port 5000",
port: 5000,
protocol: "tcp",
minDelay: 100 * time.Millisecond,
maxDelay: 300 * time.Millisecond,
},
{
name: "Global fallback",
port: 5001,
protocol: "udp",
minDelay: 50 * time.Millisecond,
maxDelay: 200 * time.Millisecond,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
delay := handler.GetDelay(tt.port, tt.protocol)
if delay < tt.minDelay || delay > tt.maxDelay {
t.Errorf("Delay %v outside expected range [%v, %v]",
delay, tt.minDelay, tt.maxDelay)
}
Comment on lines +49 to +52
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use the require package for tests

})
}
}