优化 log

addon-dailer
lqqyt2423 4 years ago
parent b83457d1ed
commit 80848bcc4b

@ -7,6 +7,8 @@ import (
) )
func main() { func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
opts := &proxy.Options{ opts := &proxy.Options{
Addr: ":8080", Addr: ":8080",
} }

@ -17,7 +17,7 @@ type Proxy struct {
} }
func (proxy *Proxy) Start() error { 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() return proxy.Server.ListenAndServe()
} }
@ -31,7 +31,7 @@ func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(400) res.WriteHeader(400)
_, err := io.WriteString(res, "此为代理服务器,不能直接发起请求") _, err := io.WriteString(res, "此为代理服务器,不能直接发起请求")
if err != nil { if err != nil {
log.Printf("error: %v", err) log.Printf("error: %v, url: %v\n", err, req.URL.String())
} }
return 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) proxyReq, err := http.NewRequest(req.Method, req.URL.String(), req.Body)
if err != nil { if err != nil {
log.Printf("error: %v", err) log.Printf("error: %v, url: %v\n", err, req.URL.String())
res.WriteHeader(502) res.WriteHeader(502)
return return
} }
@ -51,7 +51,7 @@ func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) {
} }
proxyRes, err := http.DefaultClient.Do(proxyReq) proxyRes, err := http.DefaultClient.Do(proxyReq)
if err != nil { if err != nil {
log.Printf("error: %v", err) log.Printf("error: %v, url: %v\n", err, req.URL.String())
res.WriteHeader(502) res.WriteHeader(502)
return return
} }
@ -63,7 +63,7 @@ func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(proxyRes.StatusCode) res.WriteHeader(proxyRes.StatusCode)
_, err = io.Copy(res, proxyRes.Body) _, err = io.Copy(res, proxyRes.Body)
if err != nil { if err != nil {
log.Printf("error: %v", err) log.Printf("error: %v, url: %v\n", err, req.URL.String())
return return
} }
@ -75,7 +75,7 @@ func (proxy *Proxy) handleConnect(res http.ResponseWriter, req *http.Request) {
conn, err := net.Dial("tcp", req.Host) conn, err := net.Dial("tcp", req.Host)
if err != nil { if err != nil {
log.Printf("error: %v", err) log.Printf("error: %v, host: %v\n", err, req.Host)
res.WriteHeader(502) res.WriteHeader(502)
return return
} }
@ -83,7 +83,7 @@ func (proxy *Proxy) handleConnect(res http.ResponseWriter, req *http.Request) {
cconn, _, err := res.(http.Hijacker).Hijack() cconn, _, err := res.(http.Hijacker).Hijack()
if err != nil { if err != nil {
log.Printf("error: %v", err) log.Printf("error: %v, host: %v\n", err, req.Host)
res.WriteHeader(502) res.WriteHeader(502)
return 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") _, err = io.WriteString(cconn, "HTTP/1.1 200 Connection Established\r\n\r\n")
if err != nil { if err != nil {
log.Printf("error: %v", err) log.Printf("error: %v, host: %v\n", err, req.Host)
return return
} }
@ -99,14 +99,14 @@ func (proxy *Proxy) handleConnect(res http.ResponseWriter, req *http.Request) {
go func() { go func() {
_, err := io.Copy(conn, cconn) _, err := io.Copy(conn, cconn)
if err != nil { if err != nil {
log.Printf("error: %v", err) log.Printf("error: %v, host: %v\n", err, req.Host)
} }
ch <- true ch <- true
}() }()
_, err = io.Copy(cconn, conn) _, err = io.Copy(cconn, conn)
if err != nil { if err != nil {
log.Printf("error: %v", err) log.Printf("error: %v, host: %v\n", err, req.Host)
} }
<-ch <-ch

Loading…
Cancel
Save