├── .gitignore ├── go.mod ├── README.md ├── module.go ├── go.sum ├── module_test.go └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | # If you prefer the allow list template instead of the deny list, see community template: 2 | # https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore 3 | # 4 | # Binaries for programs and plugins 5 | *.exe 6 | *.exe~ 7 | *.dll 8 | *.so 9 | *.dylib 10 | 11 | # Test binary, built with `go test -c` 12 | *.test 13 | 14 | # Output of the go coverage tool, specifically when used with LiteIDE 15 | *.out 16 | 17 | # Dependency directories (remove the comment below to include it) 18 | # vendor/ 19 | 20 | # Go workspace file 21 | go.work 22 | go.work.sum 23 | 24 | # env file 25 | .env 26 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/mohammed90/caddy-throttle-listener 2 | 3 | go 1.22.0 4 | 5 | require ( 6 | github.com/caddyserver/caddy/v2 v2.8.4 7 | github.com/conduitio/bwlimit v0.1.0 8 | github.com/dustin/go-humanize v1.0.1 9 | ) 10 | 11 | require ( 12 | github.com/beorn7/perks v1.0.1 // indirect 13 | github.com/caddyserver/certmagic v0.21.3 // indirect 14 | github.com/caddyserver/zerossl v0.1.3 // indirect 15 | github.com/cespare/xxhash/v2 v2.2.0 // indirect 16 | github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect 17 | github.com/google/pprof v0.0.0-20231212022811-ec68065c825e // indirect 18 | github.com/google/uuid v1.6.0 // indirect 19 | github.com/klauspost/cpuid/v2 v2.2.7 // indirect 20 | github.com/libdns/libdns v0.2.2 // indirect 21 | github.com/mholt/acmez/v2 v2.0.1 // indirect 22 | github.com/miekg/dns v1.1.59 // indirect 23 | github.com/onsi/ginkgo/v2 v2.13.2 // indirect 24 | github.com/prometheus/client_golang v1.19.1 // indirect 25 | github.com/prometheus/client_model v0.5.0 // indirect 26 | github.com/prometheus/common v0.48.0 // indirect 27 | github.com/prometheus/procfs v0.12.0 // indirect 28 | github.com/quic-go/qpack v0.4.0 // indirect 29 | github.com/quic-go/quic-go v0.44.0 // indirect 30 | github.com/zeebo/blake3 v0.2.3 // indirect 31 | go.uber.org/mock v0.4.0 // indirect 32 | go.uber.org/multierr v1.11.0 // indirect 33 | go.uber.org/zap v1.27.0 // indirect 34 | go.uber.org/zap/exp v0.2.0 // indirect 35 | golang.org/x/crypto v0.23.0 // indirect 36 | golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect 37 | golang.org/x/mod v0.17.0 // indirect 38 | golang.org/x/net v0.25.0 // indirect 39 | golang.org/x/sync v0.7.0 // indirect 40 | golang.org/x/sys v0.20.0 // indirect 41 | golang.org/x/term v0.20.0 // indirect 42 | golang.org/x/text v0.15.0 // indirect 43 | golang.org/x/time v0.5.0 // indirect 44 | golang.org/x/tools v0.21.0 // indirect 45 | google.golang.org/protobuf v1.34.1 // indirect 46 | ) 47 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Caddy Throttle Listener 2 | ========================== 3 | 4 | A Caddy module that provides bandwidth throttling and chaos engineering features for incoming connections. This module allows you to simulate real-world network conditions by adding bandwidth limits, latency, and jitter. Note that the throttling applies for all bytes transmitted on the connection, including TLS handshake and HTTP headers. 5 | 6 | ## Overview 7 | 8 | This module allows you to throttle incoming connections to your Caddy server to prevent abuse and simulate chaotic network conditions for testing purposes. It provides: 9 | 10 | - **Bandwidth Throttling**: Limit upload/download speeds per connection 11 | - **Latency Injection**: Add fixed delays to simulate slow networks 12 | - **Jitter**: Add random delays to simulate unstable networks 13 | 14 | Perfect for preventing abuse, ensuring fair resource usage, and testing how your applications handle real-world network conditions. 15 | 16 | ## Install 17 | 18 | Follow the xcaddy install process [here](https://github.com/caddyserver/xcaddy#install). 19 | 20 | Then, build Caddy with this Go module plugged in. For example: 21 | 22 | ```shell 23 | xcaddy build --with github.com/mohammed90/caddy-throttle-listener 24 | ``` 25 | 26 | ## Caddyfile Example 27 | 28 | 29 | ```caddyfile 30 | { 31 | servers { 32 | listener_wrappers { 33 | throttle { 34 | down 1MiB 35 | up 1MiB 36 | } 37 | tls 38 | } 39 | } 40 | } 41 | example.com { 42 | root * /var/www/html 43 | file_server 44 | } 45 | ``` 46 | 47 | ## Configuration Options 48 | 49 | ### Basic Throttling 50 | 51 | ```caddyfile 52 | { 53 | servers { 54 | listener_wrappers { 55 | throttle { 56 | down 512KB # Download limit per connection 57 | up 1MB # Upload limit per connection 58 | } 59 | } 60 | } 61 | } 62 | ``` 63 | 64 | ### Chaos Engineering Features 65 | 66 | Add network latency and jitter to test application resilience: 67 | 68 | ```caddyfile 69 | { 70 | servers { 71 | listener_wrappers { 72 | throttle { 73 | # Bandwidth limits 74 | down 1MiB 75 | up 512KB 76 | 77 | # Chaos engineering 78 | latency 100ms # Fixed delay per read/write 79 | jitter 50ms # Random delay (0-50ms) per operation 80 | } 81 | } 82 | } 83 | } 84 | ``` 85 | 86 | ### Testing Scenarios 87 | 88 | #### Slow Network Simulation 89 | ```caddyfile 90 | throttle { 91 | down 64KB 92 | up 32KB 93 | latency 200ms 94 | jitter 100ms 95 | } 96 | ``` 97 | 98 | #### High-Latency Connection (Satellite/Cellular) 99 | ```caddyfile 100 | throttle { 101 | latency 800ms 102 | jitter 200ms 103 | } 104 | ``` 105 | 106 | #### Unstable Connection 107 | ```caddyfile 108 | throttle { 109 | jitter 500ms # High jitter without base latency 110 | } 111 | ``` 112 | 113 | -------------------------------------------------------------------------------- /module.go: -------------------------------------------------------------------------------- 1 | package caddythrottlelistener 2 | 3 | import ( 4 | "math/rand" 5 | "net" 6 | "strings" 7 | "time" 8 | 9 | "github.com/caddyserver/caddy/v2" 10 | "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" 11 | "github.com/conduitio/bwlimit" 12 | "github.com/dustin/go-humanize" 13 | ) 14 | 15 | func init() { 16 | caddy.RegisterModule(Listener{}) 17 | } 18 | 19 | // The `throttle` listener limits the bandwidth of the connection to the 20 | // given values and can inject chaos engineering features like latency and jitter. 21 | type Listener struct { 22 | // Up is the maximum upload speed. If not set, there is no limit. 23 | // The value is parsed using the `go-humanize` package and accepts 24 | // both the SI and the IEC prefixes. Values without units are interepreted 25 | // as bytes. 26 | Up string `json:"up,omitempty"` 27 | 28 | // Down is the maximum upload speed. If not set, there is no limit. 29 | // The value is parsed using the `go-humanize` package and accepts 30 | // both the SI and the IEC prefixes. Values without units are interepreted 31 | // as bytes. 32 | Down string `json:"down,omitempty"` 33 | 34 | // Latency adds a fixed delay to each read/write operation. 35 | // Accepts duration strings like "10ms", "1s", "500us". 36 | Latency caddy.Duration `json:"latency,omitempty"` 37 | 38 | // Jitter adds random delay (0 to jitter value) to each read/write operation. 39 | // Accepts duration strings like "5ms", "100ms", "1s". 40 | Jitter caddy.Duration `json:"jitter,omitempty"` 41 | 42 | up bwlimit.Byte 43 | down bwlimit.Byte 44 | latency time.Duration 45 | jitter time.Duration 46 | } 47 | 48 | // CaddyModule returns the Caddy module information. 49 | func (Listener) CaddyModule() caddy.ModuleInfo { 50 | return caddy.ModuleInfo{ 51 | ID: "caddy.listeners.throttle", 52 | New: func() caddy.Module { return new(Listener) }, 53 | } 54 | } 55 | 56 | // Provision implements caddy.Provisioner. 57 | func (l *Listener) Provision(caddy.Context) error { 58 | l.up, l.down = 0, 0 59 | l.latency, l.jitter = 0, 0 60 | 61 | if up := strings.TrimSpace(l.Up); up != "" { 62 | u, err := humanize.ParseBytes(l.Up) 63 | if err != nil { 64 | return err 65 | } 66 | l.up = bwlimit.Byte(u) 67 | } 68 | if down := strings.TrimSpace(l.Down); down != "" { 69 | d, err := humanize.ParseBytes(l.Down) 70 | if err != nil { 71 | return err 72 | } 73 | l.down = bwlimit.Byte(d) 74 | } 75 | l.latency = time.Duration(l.Latency) 76 | l.jitter = time.Duration(l.Jitter) 77 | return nil 78 | } 79 | 80 | // WrapListener implements caddy.ListenerWrapper. 81 | func (l *Listener) WrapListener(parent net.Listener) net.Listener { 82 | throttled := bwlimit.NewListener(parent, l.down, l.up) 83 | if l.latency > 0 || l.jitter > 0 { 84 | return &chaoticListener{ 85 | Listener: throttled, 86 | latency: l.latency, 87 | jitter: l.jitter, 88 | } 89 | } 90 | return throttled 91 | } 92 | 93 | // chaoticListener wraps a listener to add chaos engineering features 94 | type chaoticListener struct { 95 | net.Listener 96 | latency time.Duration 97 | jitter time.Duration 98 | } 99 | 100 | func (cl *chaoticListener) Accept() (net.Conn, error) { 101 | conn, err := cl.Listener.Accept() 102 | if err != nil { 103 | return nil, err 104 | } 105 | return &chaoticConn{ 106 | Conn: conn, 107 | latency: cl.latency, 108 | jitter: cl.jitter, 109 | }, nil 110 | } 111 | 112 | // chaoticConn wraps a connection to inject latency and jitter 113 | type chaoticConn struct { 114 | net.Conn 115 | latency time.Duration 116 | jitter time.Duration 117 | } 118 | 119 | func (cc *chaoticConn) Read(b []byte) (int, error) { 120 | cc.addDelay() 121 | return cc.Conn.Read(b) 122 | } 123 | 124 | func (cc *chaoticConn) Write(b []byte) (int, error) { 125 | cc.addDelay() 126 | return cc.Conn.Write(b) 127 | } 128 | 129 | func (cc *chaoticConn) addDelay() { 130 | totalDelay := cc.latency 131 | if cc.jitter > 0 { 132 | jitterDelay := time.Duration(rand.Int63n(int64(cc.jitter))) 133 | totalDelay += jitterDelay 134 | } 135 | if totalDelay > 0 { 136 | time.Sleep(totalDelay) 137 | } 138 | } 139 | 140 | // UnmarshalCaddyfile implements caddyfile.Unmarshaler. 141 | func (l *Listener) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { 142 | d.Next() 143 | for nesting := d.Nesting(); d.NextBlock(nesting); { 144 | switch d.Val() { 145 | case "up": 146 | if !d.NextArg() { 147 | return d.ArgErr() 148 | } 149 | l.Up = d.Val() 150 | case "down": 151 | if !d.NextArg() { 152 | return d.ArgErr() 153 | } 154 | l.Down = d.Val() 155 | case "latency": 156 | if !d.NextArg() { 157 | return d.ArgErr() 158 | } 159 | latency, err := caddy.ParseDuration(d.Val()) 160 | if err != nil { 161 | return d.Errf("invalid latency duration %s: %v", d.Val(), err) 162 | } 163 | l.Latency = caddy.Duration(latency) 164 | case "jitter": 165 | if !d.NextArg() { 166 | return d.ArgErr() 167 | } 168 | jitter, err := caddy.ParseDuration(d.Val()) 169 | if err != nil { 170 | return d.Errf("invalid jitter duration %s: %v", d.Val(), err) 171 | } 172 | l.Jitter = caddy.Duration(jitter) 173 | default: 174 | return d.Errf("unrecognized subdirective %s", d.Val()) 175 | } 176 | } 177 | return nil 178 | } 179 | 180 | var ( 181 | _ caddy.Module = (*Listener)(nil) 182 | _ caddy.Provisioner = (*Listener)(nil) 183 | _ caddy.ListenerWrapper = (*Listener)(nil) 184 | _ caddyfile.Unmarshaler = (*Listener)(nil) 185 | ) 186 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= 2 | github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= 3 | github.com/caddyserver/caddy/v2 v2.8.4 h1:q3pe0wpBj1OcHFZ3n/1nl4V4bxBrYoSoab7rL9BMYNk= 4 | github.com/caddyserver/caddy/v2 v2.8.4/go.mod h1:vmDAHp3d05JIvuhc24LmnxVlsZmWnUwbP5WMjzcMPWw= 5 | github.com/caddyserver/certmagic v0.21.3 h1:pqRRry3yuB4CWBVq9+cUqu+Y6E2z8TswbhNx1AZeYm0= 6 | github.com/caddyserver/certmagic v0.21.3/go.mod h1:Zq6pklO9nVRl3DIFUw9gVUfXKdpc/0qwTUAQMBlfgtI= 7 | github.com/caddyserver/zerossl v0.1.3 h1:onS+pxp3M8HnHpN5MMbOMyNjmTheJyWRaZYwn+YTAyA= 8 | github.com/caddyserver/zerossl v0.1.3/go.mod h1:CxA0acn7oEGO6//4rtrRjYgEoa4MFw/XofZnrYwGqG4= 9 | github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= 10 | github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= 11 | github.com/conduitio/bwlimit v0.1.0 h1:x3ijON0TSghQob4tFKaEvKixFmYKfVJQeSpXluC2JvE= 12 | github.com/conduitio/bwlimit v0.1.0/go.mod h1:E+ASZ1/5L33MTb8hJTERs5Xnmh6Ulq3jbRh7LrdbXWU= 13 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 14 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 15 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 16 | github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= 17 | github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= 18 | github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= 19 | github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= 20 | github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI= 21 | github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls= 22 | github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= 23 | github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= 24 | github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= 25 | github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= 26 | github.com/google/pprof v0.0.0-20231212022811-ec68065c825e h1:bwOy7hAFd0C91URzMIEBfr6BAz29yk7Qj0cy6S7DJlU= 27 | github.com/google/pprof v0.0.0-20231212022811-ec68065c825e/go.mod h1:czg5+yv1E0ZGTi6S6vVK1mke0fV+FaUhNGcd6VRS9Ik= 28 | github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= 29 | github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 30 | github.com/klauspost/cpuid/v2 v2.0.12/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c= 31 | github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM= 32 | github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= 33 | github.com/libdns/libdns v0.2.2 h1:O6ws7bAfRPaBsgAYt8MDe2HcNBGC29hkZ9MX2eUSX3s= 34 | github.com/libdns/libdns v0.2.2/go.mod h1:4Bj9+5CQiNMVGf87wjX4CY3HQJypUHRuLvlsfsZqLWQ= 35 | github.com/mholt/acmez/v2 v2.0.1 h1:3/3N0u1pLjMK4sNEAFSI+bcvzbPhRpY383sy1kLHJ6k= 36 | github.com/mholt/acmez/v2 v2.0.1/go.mod h1:fX4c9r5jYwMyMsC+7tkYRxHibkOTgta5DIFGoe67e1U= 37 | github.com/miekg/dns v1.1.59 h1:C9EXc/UToRwKLhK5wKU/I4QVsBUc8kE6MkHBkeypWZs= 38 | github.com/miekg/dns v1.1.59/go.mod h1:nZpewl5p6IvctfgrckopVx2OlSEHPRO/U4SYkRklrEk= 39 | github.com/onsi/ginkgo/v2 v2.13.2 h1:Bi2gGVkfn6gQcjNjZJVO8Gf0FHzMPf2phUei9tejVMs= 40 | github.com/onsi/ginkgo/v2 v2.13.2/go.mod h1:XStQ8QcGwLyF4HdfcZB8SFOS/MWCgDuXMSBe6zrvLgM= 41 | github.com/onsi/gomega v1.29.0 h1:KIA/t2t5UBzoirT4H9tsML45GEbo3ouUnBHsCfD2tVg= 42 | github.com/onsi/gomega v1.29.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ= 43 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 44 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 45 | github.com/prometheus/client_golang v1.19.1 h1:wZWJDwK+NameRJuPGDhlnFgx8e8HN3XHQeLaYJFJBOE= 46 | github.com/prometheus/client_golang v1.19.1/go.mod h1:mP78NwGzrVks5S2H6ab8+ZZGJLZUq1hoULYBAYBw1Ho= 47 | github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw= 48 | github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI= 49 | github.com/prometheus/common v0.48.0 h1:QO8U2CdOzSn1BBsmXJXduaaW+dY/5QLjfB8svtSzKKE= 50 | github.com/prometheus/common v0.48.0/go.mod h1:0/KsvlIEfPQCQ5I2iNSAWKPZziNCvRs5EC6ILDTlAPc= 51 | github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo= 52 | github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo= 53 | github.com/quic-go/qpack v0.4.0 h1:Cr9BXA1sQS2SmDUWjSofMPNKmvF6IiIfDRmgU0w1ZCo= 54 | github.com/quic-go/qpack v0.4.0/go.mod h1:UZVnYIfi5GRk+zI9UMaCPsmZ2xKJP7XBUvVyT1Knj9A= 55 | github.com/quic-go/quic-go v0.44.0 h1:So5wOr7jyO4vzL2sd8/pD9Kesciv91zSk8BoFngItQ0= 56 | github.com/quic-go/quic-go v0.44.0/go.mod h1:z4cx/9Ny9UtGITIPzmPTXh1ULfOyWh4qGQlpnPcWmek= 57 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 58 | github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 59 | github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= 60 | github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= 61 | github.com/zeebo/assert v1.1.0 h1:hU1L1vLTHsnO8x8c9KAR5GmM5QscxHg5RNU5z5qbUWY= 62 | github.com/zeebo/assert v1.1.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0= 63 | github.com/zeebo/blake3 v0.2.3 h1:TFoLXsjeXqRNFxSbk35Dk4YtszE/MQQGK10BH4ptoTg= 64 | github.com/zeebo/blake3 v0.2.3/go.mod h1:mjJjZpnsyIVtVgTOSpJ9vmRE4wgDeyt2HU3qXvvKCaQ= 65 | github.com/zeebo/pcg v1.0.1 h1:lyqfGeWiv4ahac6ttHs+I5hwtH/+1mrhlCtVNQM2kHo= 66 | github.com/zeebo/pcg v1.0.1/go.mod h1:09F0S9iiKrwn9rlI5yjLkmrug154/YRW6KnnXVDM/l4= 67 | go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= 68 | go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= 69 | go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU= 70 | go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc= 71 | go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= 72 | go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= 73 | go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= 74 | go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= 75 | go.uber.org/zap/exp v0.2.0 h1:FtGenNNeCATRB3CmB/yEUnjEFeJWpB/pMcy7e2bKPYs= 76 | go.uber.org/zap/exp v0.2.0/go.mod h1:t0gqAIdh1MfKv9EwN/dLwfZnJxe9ITAZN78HEWPFWDQ= 77 | golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= 78 | golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= 79 | golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 h1:vr/HnozRka3pE4EsMEg1lgkXJkTFJCVUX+S/ZT6wYzM= 80 | golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc= 81 | golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA= 82 | golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= 83 | golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= 84 | golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= 85 | golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= 86 | golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= 87 | golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 88 | golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= 89 | golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 90 | golang.org/x/term v0.20.0 h1:VnkxpohqXaOBYJtBmEppKUG6mXpi+4O6purfc2+sMhw= 91 | golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY= 92 | golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= 93 | golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= 94 | golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= 95 | golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= 96 | golang.org/x/tools v0.21.0 h1:qc0xYgIbsSDt9EyWz05J5wfa7LOVW0YTLOXrqdLAWIw= 97 | golang.org/x/tools v0.21.0/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= 98 | google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg= 99 | google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= 100 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 101 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 102 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= 103 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 104 | -------------------------------------------------------------------------------- /module_test.go: -------------------------------------------------------------------------------- 1 | package caddythrottlelistener 2 | 3 | import ( 4 | "io" 5 | "net" 6 | "testing" 7 | "time" 8 | 9 | "github.com/caddyserver/caddy/v2" 10 | ) 11 | 12 | func TestThrottling(t *testing.T) { 13 | listener := Listener{ 14 | Up: "1KiB", 15 | Down: "1KiB", 16 | } 17 | ctx := caddy.Context{} 18 | err := listener.Provision(ctx) 19 | if err != nil { 20 | t.Errorf("expected no error, got %s", err) 21 | } 22 | 23 | ln, err := net.Listen("tcp", "localhost:0") 24 | if err != nil { 25 | t.Errorf("expected no error, got %s", err) 26 | } 27 | defer ln.Close() 28 | 29 | wrappedLn := listener.WrapListener(ln) 30 | 31 | go func() { 32 | conn, err := net.Dial("tcp", ln.Addr().String()) 33 | if err != nil { 34 | t.Errorf("expected no error, got %s", err) 35 | } 36 | defer conn.Close() 37 | 38 | // Send 10KB of data to the server 39 | data := make([]byte, 10*1024) 40 | _, err = conn.Write(data) 41 | if err != nil { 42 | t.Errorf("expected no error, got %s", err) 43 | } 44 | }() 45 | 46 | // Wait for the connection to be established 47 | time.Sleep(100 * time.Millisecond) 48 | 49 | // Accept the connection 50 | conn, err := wrappedLn.Accept() 51 | if err != nil { 52 | t.Errorf("expected no error, got %s", err) 53 | } 54 | defer conn.Close() 55 | 56 | // Read 1KB of data from the connection 57 | buf := make([]byte, 1024) 58 | n, err := conn.Read(buf) 59 | if err != nil && err != io.EOF { 60 | t.Errorf("expected no error, got %s", err) 61 | } 62 | if n != 1024 { 63 | t.Errorf("expected to read 1024 bytes, got %d", n) 64 | } 65 | 66 | // Wait for the throttling to kick in 67 | time.Sleep(100 * time.Millisecond) 68 | 69 | // Read the remaining 9KB of data from the connection 70 | buf = make([]byte, 9*1024) 71 | n, err = conn.Read(buf) 72 | if err != nil && err != io.EOF { 73 | t.Errorf("expected no error, got %s", err) 74 | } 75 | if n != 1024 { 76 | t.Errorf("expected to read 1024 bytes, got %d", n) 77 | } 78 | } 79 | 80 | func TestThrottlingUpDown(t *testing.T) { 81 | listener := Listener{ 82 | Up: "1KiB", 83 | Down: "2KiB", 84 | } 85 | ctx := caddy.Context{} 86 | err := listener.Provision(ctx) 87 | if err != nil { 88 | t.Errorf("expected no error, got %s", err) 89 | } 90 | 91 | ln, err := net.Listen("tcp", "localhost:0") 92 | if err != nil { 93 | t.Errorf("expected no error, got %s", err) 94 | } 95 | defer ln.Close() 96 | 97 | wrappedLn := listener.WrapListener(ln) 98 | 99 | go func() { 100 | conn, err := net.Dial("tcp", ln.Addr().String()) 101 | if err != nil { 102 | t.Errorf("expected no error, got %s", err) 103 | } 104 | defer conn.Close() 105 | 106 | // Send 2KB of data to the server 107 | data := make([]byte, 2*1024) 108 | _, err = conn.Write(data) 109 | if err != nil { 110 | t.Errorf("expected no error, got %s", err) 111 | } 112 | }() 113 | 114 | // Wait for the connection to be established 115 | time.Sleep(100 * time.Millisecond) 116 | 117 | // Accept the connection 118 | conn, err := wrappedLn.Accept() 119 | if err != nil { 120 | t.Errorf("expected no error, got %s", err) 121 | } 122 | defer conn.Close() 123 | 124 | // Read 1KB of data from the connection 125 | buf := make([]byte, 1024) 126 | n, err := conn.Read(buf) 127 | if err != nil && err != io.EOF { 128 | t.Errorf("expected no error, got %s", err) 129 | } 130 | if n != 1024 { 131 | t.Errorf("expected to read 1024 bytes, got %d", n) 132 | } 133 | 134 | // Send 1KB of data back to the client 135 | _, err = conn.Write(buf) 136 | if err != nil { 137 | t.Errorf("expected no error, got %s", err) 138 | } 139 | } 140 | 141 | func TestLatency(t *testing.T) { 142 | latency, _ := caddy.ParseDuration("50ms") 143 | listener := Listener{ 144 | Latency: caddy.Duration(latency), 145 | } 146 | ctx := caddy.Context{} 147 | err := listener.Provision(ctx) 148 | if err != nil { 149 | t.Errorf("expected no error, got %s", err) 150 | } 151 | 152 | ln, err := net.Listen("tcp", "localhost:0") 153 | if err != nil { 154 | t.Errorf("expected no error, got %s", err) 155 | } 156 | defer ln.Close() 157 | 158 | wrappedLn := listener.WrapListener(ln) 159 | 160 | go func() { 161 | conn, err := net.Dial("tcp", ln.Addr().String()) 162 | if err != nil { 163 | t.Errorf("expected no error, got %s", err) 164 | } 165 | defer conn.Close() 166 | 167 | // Send a small message 168 | _, err = conn.Write([]byte("test")) 169 | if err != nil { 170 | t.Errorf("expected no error, got %s", err) 171 | } 172 | }() 173 | 174 | // Wait for connection 175 | time.Sleep(10 * time.Millisecond) 176 | 177 | conn, err := wrappedLn.Accept() 178 | if err != nil { 179 | t.Errorf("expected no error, got %s", err) 180 | } 181 | defer conn.Close() 182 | 183 | // Measure time for read operation (should include latency) 184 | start := time.Now() 185 | buf := make([]byte, 4) 186 | _, err = conn.Read(buf) 187 | duration := time.Since(start) 188 | 189 | if err != nil && err != io.EOF { 190 | t.Errorf("expected no error, got %s", err) 191 | } 192 | 193 | // Should take at least 50ms due to latency 194 | if duration < 40*time.Millisecond { 195 | t.Errorf("expected latency of at least 40ms, got %v", duration) 196 | } 197 | } 198 | 199 | func TestJitter(t *testing.T) { 200 | jitter, _ := caddy.ParseDuration("30ms") 201 | listener := Listener{ 202 | Jitter: caddy.Duration(jitter), 203 | } 204 | ctx := caddy.Context{} 205 | err := listener.Provision(ctx) 206 | if err != nil { 207 | t.Errorf("expected no error, got %s", err) 208 | } 209 | 210 | ln, err := net.Listen("tcp", "localhost:0") 211 | if err != nil { 212 | t.Errorf("expected no error, got %s", err) 213 | } 214 | defer ln.Close() 215 | 216 | wrappedLn := listener.WrapListener(ln) 217 | 218 | // Collect multiple timing samples to verify jitter variance 219 | var durations []time.Duration 220 | 221 | for i := 0; i < 5; i++ { 222 | go func() { 223 | conn, err := net.Dial("tcp", ln.Addr().String()) 224 | if err != nil { 225 | t.Errorf("expected no error, got %s", err) 226 | } 227 | defer conn.Close() 228 | 229 | _, err = conn.Write([]byte("test")) 230 | if err != nil { 231 | t.Errorf("expected no error, got %s", err) 232 | } 233 | }() 234 | 235 | time.Sleep(10 * time.Millisecond) 236 | 237 | conn, err := wrappedLn.Accept() 238 | if err != nil { 239 | t.Errorf("expected no error, got %s", err) 240 | } 241 | 242 | start := time.Now() 243 | buf := make([]byte, 4) 244 | _, err = conn.Read(buf) 245 | duration := time.Since(start) 246 | conn.Close() 247 | 248 | if err != nil && err != io.EOF { 249 | t.Errorf("expected no error, got %s", err) 250 | } 251 | 252 | durations = append(durations, duration) 253 | } 254 | 255 | // Verify that we have some variance in timing due to jitter 256 | // At least one should be significantly different from another 257 | hasVariance := false 258 | for i := 0; i < len(durations)-1; i++ { 259 | diff := durations[i] - durations[i+1] 260 | if diff < 0 { 261 | diff = -diff 262 | } 263 | if diff > 5*time.Millisecond { 264 | hasVariance = true 265 | break 266 | } 267 | } 268 | 269 | if !hasVariance { 270 | t.Errorf("expected jitter to create timing variance, but all durations were similar: %v", durations) 271 | } 272 | } 273 | 274 | func TestLatencyAndJitter(t *testing.T) { 275 | latency, _ := caddy.ParseDuration("20ms") 276 | jitter, _ := caddy.ParseDuration("10ms") 277 | listener := Listener{ 278 | Latency: caddy.Duration(latency), 279 | Jitter: caddy.Duration(jitter), 280 | } 281 | ctx := caddy.Context{} 282 | err := listener.Provision(ctx) 283 | if err != nil { 284 | t.Errorf("expected no error, got %s", err) 285 | } 286 | 287 | ln, err := net.Listen("tcp", "localhost:0") 288 | if err != nil { 289 | t.Errorf("expected no error, got %s", err) 290 | } 291 | defer ln.Close() 292 | 293 | wrappedLn := listener.WrapListener(ln) 294 | 295 | go func() { 296 | conn, err := net.Dial("tcp", ln.Addr().String()) 297 | if err != nil { 298 | t.Errorf("expected no error, got %s", err) 299 | } 300 | defer conn.Close() 301 | 302 | _, err = conn.Write([]byte("test")) 303 | if err != nil { 304 | t.Errorf("expected no error, got %s", err) 305 | } 306 | }() 307 | 308 | time.Sleep(10 * time.Millisecond) 309 | 310 | conn, err := wrappedLn.Accept() 311 | if err != nil { 312 | t.Errorf("expected no error, got %s", err) 313 | } 314 | defer conn.Close() 315 | 316 | start := time.Now() 317 | buf := make([]byte, 4) 318 | _, err = conn.Read(buf) 319 | duration := time.Since(start) 320 | 321 | if err != nil && err != io.EOF { 322 | t.Errorf("expected no error, got %s", err) 323 | } 324 | 325 | // Should take at least the base latency (20ms) 326 | if duration < 15*time.Millisecond { 327 | t.Errorf("expected delay of at least 15ms (base latency), got %v", duration) 328 | } 329 | 330 | // Should not exceed latency + jitter (30ms) by much 331 | if duration > 50*time.Millisecond { 332 | t.Errorf("expected delay of less than 50ms (latency + jitter + margin), got %v", duration) 333 | } 334 | } 335 | 336 | func TestThrottlingWithChaos(t *testing.T) { 337 | latency, _ := caddy.ParseDuration("10ms") 338 | jitter, _ := caddy.ParseDuration("5ms") 339 | listener := Listener{ 340 | Up: "1KiB", 341 | Down: "1KiB", 342 | Latency: caddy.Duration(latency), 343 | Jitter: caddy.Duration(jitter), 344 | } 345 | ctx := caddy.Context{} 346 | err := listener.Provision(ctx) 347 | if err != nil { 348 | t.Errorf("expected no error, got %s", err) 349 | } 350 | 351 | ln, err := net.Listen("tcp", "localhost:0") 352 | if err != nil { 353 | t.Errorf("expected no error, got %s", err) 354 | } 355 | defer ln.Close() 356 | 357 | wrappedLn := listener.WrapListener(ln) 358 | 359 | go func() { 360 | conn, err := net.Dial("tcp", ln.Addr().String()) 361 | if err != nil { 362 | t.Errorf("expected no error, got %s", err) 363 | } 364 | defer conn.Close() 365 | 366 | data := make([]byte, 1024) 367 | _, err = conn.Write(data) 368 | if err != nil { 369 | t.Errorf("expected no error, got %s", err) 370 | } 371 | }() 372 | 373 | time.Sleep(10 * time.Millisecond) 374 | 375 | conn, err := wrappedLn.Accept() 376 | if err != nil { 377 | t.Errorf("expected no error, got %s", err) 378 | } 379 | defer conn.Close() 380 | 381 | // Measure combined effect of throttling + chaos 382 | start := time.Now() 383 | buf := make([]byte, 1024) 384 | n, err := conn.Read(buf) 385 | duration := time.Since(start) 386 | 387 | if err != nil && err != io.EOF { 388 | t.Errorf("expected no error, got %s", err) 389 | } 390 | if n != 1024 { 391 | t.Errorf("expected to read 1024 bytes, got %d", n) 392 | } 393 | 394 | // Should have some delay from chaos features 395 | if duration < 5*time.Millisecond { 396 | t.Errorf("expected some delay from chaos features, got %v", duration) 397 | } 398 | } 399 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2024 [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------