/
proxy1.go
63 lines (55 loc) · 1.7 KB
/
proxy1.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
63
package main
import (
"io"
"fmt"
"log"
"net"
// "net/http"
"github.com/valyala/fasthttp"
)
func transfer(destination io.WriteCloser, source io.ReadCloser) {
fmt.Println("transfer")
defer destination.Close()
defer source.Close()
fmt.Println("-------------------")
io.Copy(destination, source)
fmt.Println("===================")
}
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
requestHandler := func(ctx *fasthttp.RequestCtx) {
fmt.Println("host: ", string(ctx.Host()))
dest_conn, err := net.Dial("tcp", string(ctx.Host()), )
if err != nil {
log.Println(err)
return
}
hijackHandler := func(client_conn net.Conn) {
fmt.Println("333333")
go transfer(dest_conn, client_conn)
go transfer(client_conn, dest_conn)
}
ctx.Hijack(hijackHandler)
}
h := func(ctx *fasthttp.RequestCtx) {
fmt.Println(string(ctx.Method()))
fmt.Println("123")
// ctx.SetBody([]byte("this is completely new body contents"))
// ctx.SetStatusCode(200)
// fmt.Fprintln(ctx, "Connection Established\r\n\r\n")
fmt.Println(ctx.Request.String())
fmt.Println(ctx.Response.String())
ctx.Response.Header.SetContentLength(-1)
requestHandler(ctx)
ctx.Response.Header.SetContentLength(-1)
fmt.Println("content length", ctx.Response.Header.ContentLength())
fmt.Println("321")
}
s := &fasthttp.Server{
// Handler: requestHandler,
Handler: h,
}
if err := s.ListenAndServe("127.0.0.1:8080"); err != nil {
log.Fatalf("error in ListenAndServe: %s", err)
}
}