-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (54 loc) · 1.33 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"fmt"
"log"
"net/http"
"sync"
"github.com/valyala/fasthttp"
)
func main() {
wg := sync.WaitGroup{}
for i := 7000; i < 10000; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
if err := proxy(
fmt.Sprintf(":%d", i),
fmt.Sprintf("localhost:%d", i+1),
); err != nil {
log.Println(err)
}
}(i)
}
wg.Wait()
//cfg := parseConfig()
//proxy := httputil.NewSingleHostReverseProxy(&cfg.targetURL)
//limiter := newLimiter(cfg, proxy)
//
//http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
// limiter.handle(res, req)
//})
//
//if err := http.ListenAndServe(cfg.listenAddress, nil); err != nil {
// log.Fatalf("Failed to listen and serve %s: %v\n", cfg.listenAddress, err)
//}
}
func proxy(listen string, target string) error {
cli := fasthttp.HostClient{
Addr: target,
DisableHeaderNamesNormalizing: true,
DisablePathNormalizing: true,
}
srv := fasthttp.Server{
Handler: func(ctx *fasthttp.RequestCtx) {
ctx.Request.SetHost(cli.Addr)
if err := cli.Do(&ctx.Request, &ctx.Response); err != nil {
ctx.Response.SetStatusCode(http.StatusInternalServerError)
ctx.Response.SetBodyString(err.Error())
}
},
DisablePreParseMultipartForm: true,
DisableHeaderNamesNormalizing: true,
}
return srv.ListenAndServe(listen)
}