forked from ayufan/auto-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.go
59 lines (50 loc) · 1.19 KB
/
proxy.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
package main
import (
"fmt"
"net/http"
"time"
)
var defaultTransport http.Transport
type loggingResponseWriter struct {
rw http.ResponseWriter
status int
written int64
started time.Time
Message string
}
func newLoggingResponseWriter(rw http.ResponseWriter) *loggingResponseWriter {
return &loggingResponseWriter{
rw: rw,
started: time.Now(),
}
}
func (l *loggingResponseWriter) Header() http.Header {
return l.rw.Header()
}
func (l *loggingResponseWriter) Write(data []byte) (n int, err error) {
if l.status == 0 {
l.status = http.StatusOK
}
n, err = l.rw.Write(data)
l.written += int64(n)
return
}
func (l *loggingResponseWriter) WriteHeader(status int) {
l.rw.WriteHeader(status)
if l.status == 0 {
l.status = status
}
}
func (l *loggingResponseWriter) Log(r *http.Request) {
duration := time.Since(l.started)
fmt.Printf("%s %s - - [%s] %q %d %d %q %q %f %q\n",
r.Host, r.RemoteAddr, l.started,
fmt.Sprintf("%s %s %s", r.Method, r.RequestURI, r.Proto),
l.status, l.written, r.Referer(), r.UserAgent(),
duration.Seconds(), l.Message,
)
}
func httpServerError(w http.ResponseWriter, r *http.Request, a ...interface{}) {
w.WriteHeader(503)
fmt.Fprintln(w, a...)
}