Created
November 11, 2019 19:05
-
-
Save erikdubbelboer/1d52429400792dfbf7db88ec737a9364 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"math/rand" | |
"net" | |
"time" | |
"github.com/valyala/fasthttp" | |
) | |
const charset = "abcdefghijklmnopqrstuvwxyz0123456789" | |
func random() string { | |
b := make([]byte, rand.Intn(100)) | |
for i := 0; i < len(b); i++ { | |
b[i] = charset[rand.Intn(len(charset))] | |
} | |
return string(b) | |
} | |
func main() { | |
go func() { | |
if err := fasthttp.ListenAndServe(":7070", func(ctx *fasthttp.RequestCtx) { | |
o := string(ctx.Request.Header.Peek("OriginalIP")) | |
if o != "141.226.8.217" { | |
println(o) | |
} | |
}); err != nil { | |
panic(err) | |
} | |
}() | |
time.Sleep(time.Second) | |
addr, err := net.ResolveTCPAddr("tcp", "localhost:7070") | |
if err != nil { | |
panic(err) | |
} | |
for i := 0; i < 20; i++ { | |
go func() { | |
conn, err := net.DialTCP("tcp4", nil, addr) | |
if err != nil { | |
panic(err) | |
} | |
// Don't buffer writes to this conn, send it immediately. | |
conn.SetNoDelay(true) | |
for { | |
req := "GET /" + random() + " HTTP/1.1\r\nUser-Agent: Dalvik/2.1.0 (Linux; U; Android 5.1.1; AOSP on p201 Build/LMY47V)\r\nHost: example.com\r\nConnection: Keep-Alive\r\nAccept-Encoding: gzip\r\nOriginalIP: 141.226.8.217 \r\n\r\n" | |
var chunks []string | |
// Split the request in small chunks. | |
for len(req) > 0 { | |
n := rand.Intn(20) | |
if l := len(req); n > l { | |
n = l | |
} | |
chunks = append(chunks, req[:n]) | |
req = req[n:] | |
} | |
// Send each chunk in a separate TCP packet. | |
for _, c := range chunks { | |
if _, err := conn.Write([]byte(c)); err != nil { | |
panic(err) | |
} | |
// With a small delay in between. | |
time.Sleep(time.Duration(rand.Intn(1000)) * time.Microsecond) | |
} | |
// Read the response. | |
b := make([]byte, 4096) | |
if _, err := conn.Read(b); err != nil { | |
panic(err) | |
} | |
} | |
}() | |
} | |
select {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment