diff --git a/bin/main.go b/bin/main.go index 8700a72..9bd8a28 100644 --- a/bin/main.go +++ b/bin/main.go @@ -7,6 +7,8 @@ import ( ) func main() { + log.SetFlags(log.LstdFlags | log.Lshortfile) + opts := &proxy.Options{ Addr: ":8080", } diff --git a/proxy.go b/proxy.go index bf80065..6086fc2 100644 --- a/proxy.go +++ b/proxy.go @@ -17,7 +17,7 @@ type Proxy struct { } func (proxy *Proxy) Start() error { - log.Printf("Proxy start listen at :8080") + log.Printf("Proxy start listen at %v\n", proxy.Server.Addr) return proxy.Server.ListenAndServe() } @@ -31,7 +31,7 @@ func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) { res.WriteHeader(400) _, err := io.WriteString(res, "此为代理服务器,不能直接发起请求") if err != nil { - log.Printf("error: %v", err) + log.Printf("error: %v, url: %v\n", err, req.URL.String()) } return } @@ -40,7 +40,7 @@ func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) { proxyReq, err := http.NewRequest(req.Method, req.URL.String(), req.Body) if err != nil { - log.Printf("error: %v", err) + log.Printf("error: %v, url: %v\n", err, req.URL.String()) res.WriteHeader(502) return } @@ -51,7 +51,7 @@ func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) { } proxyRes, err := http.DefaultClient.Do(proxyReq) if err != nil { - log.Printf("error: %v", err) + log.Printf("error: %v, url: %v\n", err, req.URL.String()) res.WriteHeader(502) return } @@ -63,7 +63,7 @@ func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) { res.WriteHeader(proxyRes.StatusCode) _, err = io.Copy(res, proxyRes.Body) if err != nil { - log.Printf("error: %v", err) + log.Printf("error: %v, url: %v\n", err, req.URL.String()) return } @@ -75,7 +75,7 @@ func (proxy *Proxy) handleConnect(res http.ResponseWriter, req *http.Request) { conn, err := net.Dial("tcp", req.Host) if err != nil { - log.Printf("error: %v", err) + log.Printf("error: %v, host: %v\n", err, req.Host) res.WriteHeader(502) return } @@ -83,7 +83,7 @@ func (proxy *Proxy) handleConnect(res http.ResponseWriter, req *http.Request) { cconn, _, err := res.(http.Hijacker).Hijack() if err != nil { - log.Printf("error: %v", err) + log.Printf("error: %v, host: %v\n", err, req.Host) res.WriteHeader(502) return } @@ -91,7 +91,7 @@ func (proxy *Proxy) handleConnect(res http.ResponseWriter, req *http.Request) { _, err = io.WriteString(cconn, "HTTP/1.1 200 Connection Established\r\n\r\n") if err != nil { - log.Printf("error: %v", err) + log.Printf("error: %v, host: %v\n", err, req.Host) return } @@ -99,14 +99,14 @@ func (proxy *Proxy) handleConnect(res http.ResponseWriter, req *http.Request) { go func() { _, err := io.Copy(conn, cconn) if err != nil { - log.Printf("error: %v", err) + log.Printf("error: %v, host: %v\n", err, req.Host) } ch <- true }() _, err = io.Copy(cconn, conn) if err != nil { - log.Printf("error: %v", err) + log.Printf("error: %v, host: %v\n", err, req.Host) } <-ch