Browse Source

first commit

张延森 3 years ago
commit
a478272ea0
5 changed files with 75 additions and 0 deletions
  1. 3
    0
      .gitignore
  2. 8
    0
      README.md
  3. 5
    0
      go.mod
  4. 4
    0
      go.sum
  5. 55
    0
      main.go

+ 3
- 0
.gitignore View File

@@ -0,0 +1,3 @@
1
+*.log
2
+*.exe
3
+log.txt

+ 8
- 0
README.md View File

@@ -0,0 +1,8 @@
1
+## Http 代理
2
+
3
+### 启动命令
4
+
5
+```cmd
6
+# -p 端口, 默认 7080
7
+httpproxy -p 7080
8
+```

+ 5
- 0
go.mod View File

@@ -0,0 +1,5 @@
1
+module njyunzhi.com/httpproxy
2
+
3
+go 1.17
4
+
5
+require github.com/elazarl/goproxy v0.0.0-20220417044921-416226498f94 // indirect

+ 4
- 0
go.sum View File

@@ -0,0 +1,4 @@
1
+github.com/elazarl/goproxy v0.0.0-20220417044921-416226498f94 h1:VIy7cdK7ufs7ctpTFkXJHm1uP3dJSnCGSPysEICB1so=
2
+github.com/elazarl/goproxy v0.0.0-20220417044921-416226498f94/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM=
3
+github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2/go.mod h1:gNh8nYJoAm43RfaxurUnxr+N1PwuFV3ZMl/efxlIlY8=
4
+github.com/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4/go.mod h1:qgYeAmZ5ZIpBWTGllZSQnw97Dj+woV0toclVaRGI8pc=

+ 55
- 0
main.go View File

@@ -0,0 +1,55 @@
1
+package main
2
+
3
+import (
4
+	"flag"
5
+	"fmt"
6
+	"log"
7
+	"net/http"
8
+	"os"
9
+
10
+	"github.com/elazarl/goproxy"
11
+)
12
+
13
+var f *os.File
14
+
15
+type Logger struct{}
16
+
17
+func (l *Logger) Printf(format string, v ...interface{}) {
18
+	log.Printf(format, v...)
19
+}
20
+
21
+func initLog() {
22
+	var err error
23
+	f, err = os.OpenFile("log.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
24
+	if err != nil {
25
+		log.Fatal(err)
26
+	}
27
+
28
+	log.SetOutput(f)
29
+}
30
+
31
+func main() {
32
+	initLog()
33
+	defer func() {
34
+		if f != nil {
35
+			f.Close()
36
+		}
37
+	}()
38
+
39
+	var port = flag.String("p", "7080", "代理端口")
40
+	flag.Parse()
41
+	log.Println(fmt.Sprintf("开始监听 %s:%s", "127.0.0.1", *port))
42
+
43
+	proxy := goproxy.NewProxyHttpServer()
44
+	proxy.Logger = new(Logger)
45
+
46
+	// proxy.OnRequest(goproxy.ReqHostMatches(regexp.MustCompile(".*:443$"))).DoFunc(func(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
47
+	// 	log.Println("==========================")
48
+	// 	log.Println(fmt.Sprintf("==========[%s] %s", r.Method, r.RequestURI))
49
+
50
+	// 	return r, nil
51
+	// })
52
+
53
+	proxy.Verbose = true
54
+	log.Fatal(http.ListenAndServe(":"+*port, proxy))
55
+}