Skip to content

Commit 0e13b17

Browse files
committed
Initial commit
0 parents  commit 0e13b17

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+3118
-0
lines changed

.github/workflows/ci.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
8+
jobs:
9+
test:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
- uses: actions/setup-go@v5
14+
with:
15+
go-version: '1.25.1'
16+
- run: go test -v ./...

.github/workflows/release.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v*.*.*'
7+
8+
jobs:
9+
release:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
- uses: actions/setup-go@v5
14+
with:
15+
go-version: '1.25.1'
16+
- name: Run tests
17+
run: go test -v ./...
18+
- name: Set up GoReleaser
19+
uses: goreleaser/goreleaser-action@v5
20+
with:
21+
version: latest
22+
args: release --clean
23+
env:
24+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Binaries and build output
2+
dist/
3+
bin/
4+
*.exe
5+
*.dll
6+
*.so
7+
*.dylib
8+
*.test
9+
10+
# Go modules
11+
vendor/
12+
13+
# IDE/editor files
14+
.vscode/
15+
.idea/
16+
*.swp
17+
*.swo
18+
*.bak
19+
*.tmp
20+
21+
# OS files
22+
.DS_Store
23+
Thumbs.db
24+
25+
# Logs
26+
*.log
27+
28+
# Dependency directories
29+
node_modules/
30+
31+
# Coverage and test output
32+
coverage.out
33+
*.coverprofile
34+
35+
# GoReleaser
36+
release/
37+
38+
# Misc
39+
.env
40+
.env.*
41+
42+
# Go workspace file
43+
go.work

.goreleaser.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
project_name: chaos-proxy-go
2+
builds:
3+
- main: ./main.go
4+
goos: [linux, windows, darwin]
5+
goarch: [amd64, arm64]
6+
archives:
7+
- format: tar.gz
8+
name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
9+
checksum:
10+
name_template: "checksums.txt"
11+
release:
12+
github:
13+
owner: fetch-kit
14+
name: chaos-proxy-go
15+
ldflags:
16+
- "-X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}"

.pre-commit-config.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
repos:
2+
- repo: https://github.com/dnephin/pre-commit-golang
3+
rev: v0.5.1
4+
hooks:
5+
# - id: go-fmt
6+
# - id: go-imports
7+
- id: go-lint
8+
- id: go-vet
9+
10+
- repo: https://github.com/golangci/golangci-lint
11+
rev: v2.5.0
12+
hooks:
13+
- id: golangci-lint
14+
15+
- repo: local
16+
hooks:
17+
- id: go-test
18+
name: go test
19+
entry: go test ./...
20+
language: system
21+
pass_filenames: false

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
## [0.0.1] - 2025-10-09
6+
### Added
7+
- Initial release: Go port of [fetch-kit/chaos-proxy](https://github.com/fetch-kit/chaos-proxy)
8+
- Core proxy server and middleware registry
9+
- Middleware: latency, fail, headerTransform, bodyTransformJSON, rateLimit, cors, throttle, dropConnection, etc.
10+
- CLI and programmatic API
11+
- YAML configuration support
12+
- Full integration and unit test suite

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 chaos-proxy-go contributors
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
[![Build](https://github.com/your-org/chaos-proxy-go/actions/workflows/ci.yml/badge.svg)](https://github.com/your-org/chaos-proxy-go/actions)
2+
[![GitHub stars](https://img.shields.io/github/stars/your-org/chaos-proxy-go?style=social)](https://github.com/your-org/chaos-proxy-go)
3+
4+
# chaos-proxy-go
5+
6+
**chaos-proxy-go** is a Go port of [fetch-kit/chaos-proxy](https://github.com/fetch-kit/chaos-proxy): a proxy server for injecting configurable network chaos (latency, failures, connection drops, rate-limiting, etc.) into any HTTP or HTTPS traffic. Use it via CLI or programmatically to apply ordered middleware (global and per-route) and forward requests to your target server, preserving method, path, headers, query, and body.
7+
8+
---
9+
10+
## Features
11+
12+
- Simple configuration via a single `chaos.yaml` file
13+
- Programmatic API and CLI usage
14+
- Built-in middleware primitives: latency, latencyRange, fail, failRandomly, failNth, dropConnection, rateLimit, cors, throttle, bodyTransform
15+
- Extensible registry for custom middleware
16+
- Supports both request and response interception/modification
17+
- Method+path route support (e.g., `GET /api/users`)
18+
- Robust short-circuiting: middlewares halt further processing when sending a response or dropping a connection
19+
20+
---
21+
22+
## Installation
23+
24+
Download the latest release from [GitHub Releases](https://github.com/your-org/chaos-proxy-go/releases) or build from source:
25+
26+
```sh
27+
go build -o chaos-proxy-go .
28+
```
29+
30+
---
31+
32+
## Usage
33+
34+
### CLI
35+
36+
```sh
37+
./chaos-proxy-go --config chaos.yaml [--verbose]
38+
```
39+
- `--config <path>`: YAML config file (default `./chaos.yaml`)
40+
- `--verbose`: print loaded middlewares and request logs
41+
42+
### Programmatic API
43+
44+
```go
45+
import "your-org/chaos-proxy-go/internal/proxy"
46+
47+
// Load config and start server
48+
cfg := config.Load("chaos.yaml")
49+
server := proxy.New(cfg, false)
50+
server.Start()
51+
// ...
52+
```
53+
54+
---
55+
56+
## Configuration (`chaos.yaml`)
57+
58+
See the [original chaos-proxy README](https://github.com/fetch-kit/chaos-proxy) for detailed config options. This Go port supports a compatible YAML structure.
59+
60+
---
61+
62+
## Middleware Primitives
63+
64+
- `latency(ms)` — delay every request
65+
- `latencyRange(minMs, maxMs)` — random delay
66+
- `fail({ status, body })` — always fail
67+
- `failRandomly({ rate, status, body })` — fail with probability
68+
- `failNth({ n, status, body })` — fail every nth request
69+
- `dropConnection({ prob })` — randomly drop connection
70+
- `rateLimit({ limit, windowMs, key })` — rate limiting (by IP, header, or custom)
71+
- `cors({ origin, methods, headers })` — enable and configure CORS headers
72+
- `throttle({ rate, chunkSize, burst, key })` — throttles bandwidth per request
73+
- `bodyTransform({ transform })` — parse and mutate request body with a custom function
74+
75+
---
76+
77+
## Extensibility
78+
79+
Register custom middleware in Go. See the `internal/middleware` package for examples.
80+
81+
---
82+
83+
## Security & Limitations
84+
85+
- Proxy forwards all headers; be careful with sensitive tokens.
86+
- Intended for local/dev/test only.
87+
- HTTPS pass-through requires TLS termination; not supported out-of-the-box.
88+
- Not intended for stress testing; connection limits apply.
89+
90+
---
91+
92+
## License
93+
94+
MIT
95+
96+
---
97+
98+
> This is a Go port of [fetch-kit/chaos-proxy](https://github.com/fetch-kit/chaos-proxy).

chaos.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
target: "https://httpbin.org"
2+
port: 5000
3+
routes:
4+
"GET /get":
5+
- latency:
6+
ms: 5000
7+
"GET /post":
8+
- dropConnection:

cmd/cmd.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
)
6+
7+
var (
8+
configPath string
9+
verbose bool
10+
)
11+
12+
var rootCmd = &cobra.Command{
13+
Use: "chaos-proxy",
14+
Short: "A proxy server for injecting configurable network chaos",
15+
Long: `Chaos Proxy is a proxy server for injecting configurable network chaos
16+
(latency, failures, connection drops, rate-limiting, etc.) into any HTTP or HTTPS traffic.`,
17+
}
18+
19+
func init() {
20+
rootCmd.Flags().StringVar(&configPath, "config", "chaos.yaml", "YAML config file path")
21+
rootCmd.Flags().BoolVar(&verbose, "verbose", false, "Print loaded middlewares and request logs")
22+
}
23+
24+
// Execute runs the root command.
25+
func Execute() error {
26+
return rootCmd.Execute()
27+
}
28+
29+
// GetConfigPath returns the config file path.
30+
func GetConfigPath() string {
31+
return configPath
32+
}
33+
34+
// GetVerbose returns whether verbose logging is enabled.
35+
func GetVerbose() bool {
36+
return verbose
37+
}

0 commit comments

Comments
 (0)