From cfa1b0eeaac768a585d265176d5d0fc2b87b663f Mon Sep 17 00:00:00 2001 From: lqqyt2423 <974923609@qq.com> Date: Wed, 25 Nov 2020 19:20:36 +0800 Subject: [PATCH] init --- bin/main.go | 8 ++++++++ go.mod | 3 +++ proxy.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 bin/main.go create mode 100644 go.mod create mode 100644 proxy.go diff --git a/bin/main.go b/bin/main.go new file mode 100644 index 0000000..42b79ca --- /dev/null +++ b/bin/main.go @@ -0,0 +1,8 @@ +package main + +import proxy "github.com/lqqyt2423/go-mitmproxy" + +func main() { + proxy.Create() + proxy.Init() +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e779ada --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/lqqyt2423/go-mitmproxy + +go 1.14 diff --git a/proxy.go b/proxy.go new file mode 100644 index 0000000..be7faf6 --- /dev/null +++ b/proxy.go @@ -0,0 +1,42 @@ +package proxy + +import ( + "io" + "log" + "net/http" + "time" +) + +func Create() { + http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) { + if !req.URL.IsAbs() || req.URL.Host == "" { + res.WriteHeader(400) + io.WriteString(res, "此为代理服务器,不能直接发起请求") + return + } + + start := time.Now() + + proxyReq, _ := http.NewRequest(req.Method, req.URL.String(), req.Body) + + // TODO: handle Proxy- header + for key, value := range req.Header { + proxyReq.Header[key] = value + } + proxyRes, _ := http.DefaultClient.Do(proxyReq) + defer proxyRes.Body.Close() + + for key, value := range proxyRes.Header { + res.Header()[key] = value + } + res.WriteHeader(proxyRes.StatusCode) + io.Copy(res, proxyRes.Body) + + log.Printf("%v %v %v - %v ms", req.Method, req.URL.String(), proxyRes.StatusCode, time.Since(start)) + }) +} + +func Init() { + log.Println("server begin listen at :8000") + log.Fatal(http.ListenAndServe(":8000", nil)) +}