Skip to content

Commit 47bb312

Browse files
committed
Allow listening on ALL interfaces
1 parent fb9cf68 commit 47bb312

File tree

3 files changed

+165
-89
lines changed

3 files changed

+165
-89
lines changed

README.md

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Subscribe to the [quarterly newsletter](https://tinyletter.com/gor) to stay info
2626
### Capture traffic from port
2727
```bash
2828
# Run on servers where you want to catch traffic. You can run it on each `web` machine.
29-
sudo gor --input-raw 127.0.0.1:80 --output-tcp replay.local:28020
29+
sudo gor --input-raw :80 --output-tcp replay.local:28020
3030

3131
# Replay server (replay.local).
3232
gor --input-tcp replay.local:28020 --output-http http://staging.com
@@ -38,7 +38,7 @@ Since Gor use raw sockets to capture traffic it require `sudo` access. Alternati
3838
It's recommended to use separate server for replaying traffic, but if you have enough CPU resources you can use single Gor instance.
3939

4040
```
41-
sudo gor --input-raw 127.0.0.1:80 --output-http "http://staging.com"
41+
sudo gor --input-raw :80 --output-http "http://staging.com"
4242
```
4343

4444
## Configuration
@@ -98,18 +98,18 @@ gor --input-tcp :28020 --output-http "http://staging.com|10"
9898
```
9999
# replay server will not get more than 10% of requests
100100
# useful for high-load environments
101-
gor --input-raw 127.0.0.1:80 --output-tcp "replay.local:28020|10%"
101+
gor --input-raw :80 --output-tcp "replay.local:28020|10%"
102102
```
103103

104104
#### Limiting based on Header or URL param value
105105
If you have unique user id (like API key) stored in header or URL you can consistently forward specified percent of traffic only for fraction of this users.
106106
Basic formula looks like this: `FNV32-1A_hashing(value) % 100 >= chance`. Examples:
107107
```
108108
# Limit based on header value
109-
gor --input-raw 127.0.0.1:80 --output-tcp "replay.local:28020|10%" --http-header-limiter "X-API-KEY: 10%"
109+
gor --input-raw :80 --output-tcp "replay.local:28020|10%" --http-header-limiter "X-API-KEY: 10%"
110110
111111
# Limit based on header value
112-
gor --input-raw 127.0.0.1:80 --output-tcp "replay.local:28020|10%" --http-param-limiter "api_key: 10%"
112+
gor --input-raw :80 --output-tcp "replay.local:28020|10%" --http-param-limiter "api_key: 10%"
113113
```
114114
Only percentage based limiting supported.
115115

@@ -118,29 +118,29 @@ Only percentage based limiting supported.
118118
#### Allow url regexp
119119
```
120120
# only forward requests being sent to the /api endpoint
121-
gor --input-raw 127.0.0.1:8080 --output-http staging.com --http-allow-url /api
121+
gor --input-raw :8080 --output-http staging.com --http-allow-url /api
122122
```
123123

124124
#### Disallow url regexp
125125
```
126126
# only forward requests NOT being sent to the /api... endpoint
127-
gor --input-raw 127.0.0.1:8080 --output-http staging.com --http-disallow-url /api
127+
gor --input-raw :8080 --output-http staging.com --http-disallow-url /api
128128
```
129129
#### Filter based on regexp of header
130130

131131
```
132132
# only forward requests with an api version of 1.0x
133-
gor --input-raw 127.0.0.1:8080 --output-http staging.com --http-allow-header api-version:^1\.0\d
133+
gor --input-raw :8080 --output-http staging.com --http-allow-header api-version:^1\.0\d
134134
135135
# only forward requests NOT containing User-Agent header value "Replayed by Gor"
136-
gor --input-raw 127.0.0.1:8080 --output-http staging.com --http-disallow-header "User-Agent: Replayed by Gor"
136+
gor --input-raw :8080 --output-http staging.com --http-disallow-header "User-Agent: Replayed by Gor"
137137
```
138138

139139
#### Filter based on http method
140140
Requests not matching a specified whitelist can be filtered out. For example to strip non-nullipotent requests:
141141

142142
```
143-
gor --input-raw 127.0.0.1:80 --output-http "http://staging.server" \
143+
gor --input-raw :80 --output-http "http://staging.server" \
144144
--http-allow-method GET \
145145
--http-allow-method OPTIONS
146146
```
@@ -151,20 +151,20 @@ Gor supports some basic request rewriting support. For complex logic you can use
151151
#### Rewrite URL based on a mapping
152152
```
153153
# rewrite url to match the following
154-
gor --input-raw 127.0.0.1:8080 --output-http staging.com --http-rewrite-url /v1/user/([^\\/]+)/ping:/v2/user/$1/ping
154+
gor --input-raw :8080 --output-http staging.com --http-rewrite-url /v1/user/([^\\/]+)/ping:/v2/user/$1/ping
155155
```
156156

157157
#### Set URL param
158158
Set request url param, if param already exists it will be overwritten
159159
```
160-
gor --input-raw 127.0.0.1:8080 --output-http staging.com --http-set-param api_key=1
160+
gor --input-raw :8080 --output-http staging.com --http-set-param api_key=1
161161
```
162162

163163
#### Set Header
164164
Set request header, if header already exists it will be overwritten. This may be useful if you need to identify requests generated by Gor or enable feature flagged functionality in an application:
165165

166166
```
167-
gor --input-raw 127.0.0.1:80 --output-http "http://staging.server" \
167+
gor --input-raw :80 --output-http "http://staging.server" \
168168
--http-header "User-Agent: Replayed by Gor" \
169169
--http-header "Enable-Feature-X: true"
170170
```
@@ -204,7 +204,7 @@ end
204204
205205
Middleware can be enabled using `--middleware` option, by specifying path to executable file:
206206
```
207-
gor --input-raw 127.0.0.1:80 --middleware "/opt/middleware_executable" --output-http "http://staging.server"
207+
gor --input-raw :80 --middleware "/opt/middleware_executable" --output-http "http://staging.server"
208208
```
209209
210210
#### Communication protocol
@@ -246,7 +246,7 @@ Imagine that you have auth system that randomly generate access tokens, which us
246246
You can save requests to file, and replay them later:
247247
```
248248
# write to file
249-
gor --input-raw 127.0.0.1:80 --output-file requests.gor
249+
gor --input-raw :80 --output-file requests.gor
250250
251251
# read from file
252252
gor --input-file requests.gor --output-http "http://staging.com"
@@ -268,7 +268,7 @@ gor --input-file "requests.gor|200%" --output-http "staging.com"
268268
If your development or staging environment is protected by Basic Authentication then those credentials can be injected in during the replay:
269269
270270
```
271-
gor --input-raw 127.0.0.1:80 --output-http "http://user:pass@staging .com"
271+
gor --input-raw :80 --output-http "http://user:pass@staging .com"
272272
```
273273
274274
Note: This will overwrite any Authorization headers in the original request.
@@ -277,7 +277,7 @@ Note: This will overwrite any Authorization headers in the original request.
277277
By default Gor use `libpcap` for intercepting traffic. If you have any troubles with it, you may try alternative engine: `raw_socket`.
278278
279279
```
280-
sudo gor --input-raw 127.0.0.1:80 --input-raw-engine "libpcap" --output-http "http://staging.com"
280+
sudo gor --input-raw :80 --input-raw-engine "libpcap" --output-http "http://staging.com"
281281
```
282282
283283
@@ -345,53 +345,53 @@ https://github.com/buger/gor/releases
345345
`gor -h` output:
346346
```
347347
-http-allow-header=[]: A regexp to match a specific header against. Requests with non-matching headers will be dropped:
348-
gor --input-raw 127.0.0.1:8080 --output-http staging.com --http-allow-header api-version:^v1
348+
gor --input-raw :8080 --output-http staging.com --http-allow-header api-version:^v1
349349
-http-disallow-header=[]: A regexp to match a specific header against. Requests with matching headers will be dropped:
350-
gor --input-raw 127.0.0.1:8080 --output-http staging.com --http-disallow-header "User-Agent: Replayed by Gor"
350+
gor --input-raw :8080 --output-http staging.com --http-disallow-header "User-Agent: Replayed by Gor"
351351
-http-allow-method=[]: Whitelist of HTTP methods to replay. Anything else will be dropped:
352-
gor --input-raw 127.0.0.1:8080 --output-http staging.com --http-allow-method GET --http-allow-method OPTIONS
352+
gor --input-raw :8080 --output-http staging.com --http-allow-method GET --http-allow-method OPTIONS
353353
-http-allow-url=[]: A regexp to match requests against. Filter get matched agains full url with domain. Anything else will be dropped:
354-
gor --input-raw 127.0.0.1:8080 --output-http staging.com --http-allow-url ^www.
354+
gor --input-raw :8080 --output-http staging.com --http-allow-url ^www.
355355
-http-disallow-url=[]: A regexp to match requests against. Filter get matched agains full url with domain. Anything else will be forwarded:
356-
gor --input-raw 127.0.0.1:8080 --output-http staging.com --http-disallow-url ^www.
356+
gor --input-raw :8080 --output-http staging.com --http-disallow-url ^www.
357357
-http-header-limiter=[]: Takes a fraction of requests, consistently taking or rejecting a request based on the FNV32-1A hash of a specific header:
358-
gor --input-raw 127.0.0.1:8080 --output-http staging.com --http-header-imiter user-id:25%
358+
gor --input-raw :8080 --output-http staging.com --http-header-imiter user-id:25%
359359
-http-param-limiter=[]: Takes a fraction of requests, consistently taking or rejecting a request based on the FNV32-1A hash of a specific GET param:
360-
gor --input-raw 127.0.0.1:8080 --output-http staging.com --http-param-limiter user_id:25%
360+
gor --input-raw :8080 --output-http staging.com --http-param-limiter user_id:25%
361361
-http-rewrite-url=[]: Rewrite the request url based on a mapping:
362-
gor --input-raw 127.0.0.1:8080 --output-http staging.com --http-rewrite-url /v1/user/([^\/]+)/ping:/v2/user/$1/ping
362+
gor --input-raw :8080 --output-http staging.com --http-rewrite-url /v1/user/([^\/]+)/ping:/v2/user/$1/ping
363363
-http-set-header=[]: Inject additional headers to http reqest:
364-
gor --input-raw 127.0.0.1:8080 --output-http staging.com --http-set-header 'User-Agent: Gor'
364+
gor --input-raw :8080 --output-http staging.com --http-set-header 'User-Agent: Gor'
365365
-http-set-param=[]: Set request url param, if param already exists it will be overwritten:
366-
gor --input-raw 127.0.0.1:8080 --output-http staging.com --http-set-param api_key=1
366+
gor --input-raw :8080 --output-http staging.com --http-set-param api_key=1
367367
-input-dummy=[]: Used for testing outputs. Emits 'Get /' request every 1s
368368
-input-file=[]: Read requests from file:
369369
gor --input-file ./requests.gor --output-http staging.com
370370
-input-http=[]: Read requests from HTTP, should be explicitly sent from your application:
371371
# Listen for http on 9000
372-
gor --input-http 127.0.0.1:9000 --output-http staging.com
372+
gor --input-http :9000 --output-http staging.com
373373
-input-raw=[]: Capture traffic from given port (use RAW sockets and require *sudo* access):
374374
# Capture traffic from 8080 port
375-
gor --input-raw 127.0.0.1:8080 --output-http staging.com
375+
gor --input-raw :8080 --output-http staging.com
376376
-input-tcp=[]: Used for internal communication between Gor instances. Example:
377377
# Receive requests from other Gor instances on 28020 port, and redirect output to staging
378378
gor --input-tcp :28020 --output-http staging.com
379379
-memprofile="": write memory profile to this file
380380
-output-dummy=[]: Used for testing inputs. Just prints data coming from inputs.
381381
-output-file=[]: Write incoming requests to file:
382-
gor --input-raw 127.0.0.1:80 --output-file ./requests.gor
382+
gor --input-raw :80 --output-file ./requests.gor
383383
-output-http=[]: Forwards incoming requests to given http address.
384384
# Redirect all incoming requests to staging.com address
385-
gor --input-raw 127.0.0.1:80 --output-http http://staging.com
385+
gor --input-raw :80 --output-http http://staging.com
386386
-output-http-elasticsearch="": Send request and response stats to ElasticSearch:
387-
gor --input-raw 127.0.0.1:8080 --output-http staging.com --output-http-elasticsearch 'es_host:api_port/index_name'
387+
gor --input-raw :8080 --output-http staging.com --output-http-elasticsearch 'es_host:api_port/index_name'
388388
-output-http-header-filter=[]: WARNING: `--output-http-header-filter` DEPRECATED, use `--http-allow-header` instead
389389
-output-http-redirects=0: Enable how often redirects should be followed.
390390
-output-http-stats=false: Report http output queue stats to console every 5 seconds. Remember to include also `--stats`
391391
-output-http-workers=0: Gor uses dynamic worker scaling by default. Enter a number to run a set number of workers.
392392
-output-tcp=[]: Used for internal communication between Gor instances. Example:
393393
# Listen for requests on 80 port and forward them to other Gor instance on 28020 port
394-
gor --input-raw 127.0.0.1:80 --output-tcp replay.local:28020
394+
gor --input-raw :80 --output-tcp replay.local:28020
395395
-output-tcp-stats=false: Report TCP output queue stats to console every 5 seconds. Remember to include also `--stats`
396396
-split-output=false: By default each output gets same traffic. If set to `true` it splits traffic equally among all outputs.
397397
-stats=false: Turn on queue stats output. Use in combination with the other *-stats flags.

input_raw_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,62 @@ func TestRAWInput(t *testing.T) {
2626
wg := new(sync.WaitGroup)
2727
quit := make(chan int)
2828

29+
listener, err := net.Listen("tcp", ":0")
30+
if err != nil {
31+
t.Fatal(err)
32+
}
33+
origin := &http.Server{
34+
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}),
35+
ReadTimeout: 10 * time.Second,
36+
WriteTimeout: 10 * time.Second,
37+
}
38+
go origin.Serve(listener)
39+
defer listener.Close()
40+
41+
originAddr := listener.Addr().String()
42+
43+
44+
var respCounter, reqCounter int64
45+
46+
input := NewRAWInput(originAddr, EnginePcap, testRawExpire)
47+
defer input.Close()
48+
49+
output := NewTestOutput(func(data []byte) {
50+
if data[0] == '1' {
51+
atomic.AddInt64(&reqCounter, 1)
52+
} else {
53+
atomic.AddInt64(&respCounter, 1)
54+
}
55+
56+
if Settings.debug {
57+
log.Println(reqCounter, respCounter)
58+
}
59+
60+
wg.Done()
61+
})
62+
63+
Plugins.Inputs = []io.Reader{input}
64+
Plugins.Outputs = []io.Writer{output}
65+
66+
client := NewHTTPClient("http://" + listener.Addr().String(), &HTTPClientConfig{})
67+
68+
go Start(quit)
69+
70+
for i := 0; i < 100; i++ {
71+
// request + response
72+
wg.Add(2)
73+
client.Get("/")
74+
time.Sleep(2 * time.Millisecond)
75+
}
76+
77+
wg.Wait()
78+
close(quit)
79+
}
80+
81+
func TestRAWInputIPv6(t *testing.T) {
82+
wg := new(sync.WaitGroup)
83+
quit := make(chan int)
84+
2985
listener, err := net.Listen("tcp", "[::1]:0")
3086
if err != nil {
3187
t.Fatal(err)

0 commit comments

Comments
 (0)