|
|
@ -3,14 +3,36 @@ package proxy
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/tls"
|
|
|
|
"io"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"net"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var log = _log.WithField("at", "proxy")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var ignoreErr = func(log *_log.Entry, err error) bool {
|
|
|
|
|
|
|
|
errs := err.Error()
|
|
|
|
|
|
|
|
strs := []string{
|
|
|
|
|
|
|
|
"read: connection reset by peer",
|
|
|
|
|
|
|
|
"write: broken pipe",
|
|
|
|
|
|
|
|
"i/o timeout",
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for _, str := range strs {
|
|
|
|
|
|
|
|
if strings.Contains(errs, str) {
|
|
|
|
|
|
|
|
log.Debug(str)
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type Options struct {
|
|
|
|
type Options struct {
|
|
|
|
Addr string
|
|
|
|
Addr string
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -30,7 +52,7 @@ func (proxy *Proxy) Start() error {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
|
|
log.Printf("Proxy start listen at %v\n", proxy.Server.Addr)
|
|
|
|
log.Infof("Proxy start listen at %v\n", proxy.Server.Addr)
|
|
|
|
return proxy.Server.ListenAndServe()
|
|
|
|
return proxy.Server.ListenAndServe()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -40,11 +62,16 @@ func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log := log.WithFields(_log.Fields{
|
|
|
|
|
|
|
|
"in": "ServeHTTP",
|
|
|
|
|
|
|
|
"url": req.URL,
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
if !req.URL.IsAbs() || req.URL.Host == "" {
|
|
|
|
if !req.URL.IsAbs() || req.URL.Host == "" {
|
|
|
|
res.WriteHeader(400)
|
|
|
|
res.WriteHeader(400)
|
|
|
|
_, err := io.WriteString(res, "此为代理服务器,不能直接发起请求")
|
|
|
|
_, err := io.WriteString(res, "此为代理服务器,不能直接发起请求")
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error: %v, url: %v\n", err, req.URL.String())
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -53,7 +80,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, url: %v\n", err, req.URL.String())
|
|
|
|
log.Error(err)
|
|
|
|
res.WriteHeader(502)
|
|
|
|
res.WriteHeader(502)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -65,7 +92,9 @@ func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
proxyRes, err := proxy.Client.Do(proxyReq)
|
|
|
|
proxyRes, err := proxy.Client.Do(proxyReq)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error: %v, url: %v\n", err, req.URL.String())
|
|
|
|
if !ignoreErr(log, err) {
|
|
|
|
|
|
|
|
log.Error(err)
|
|
|
|
|
|
|
|
}
|
|
|
|
res.WriteHeader(502)
|
|
|
|
res.WriteHeader(502)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -78,21 +107,26 @@ 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 && !ignoreErr(log, err) {
|
|
|
|
log.Printf("error: %v, url: %v\n", err, req.URL.String())
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
log.Printf("%v %v %v - %v ms", req.Method, req.URL.String(), proxyRes.StatusCode, time.Since(start).Milliseconds())
|
|
|
|
log.Infof("%v %v %v - %v ms", req.Method, req.URL.String(), proxyRes.StatusCode, time.Since(start).Milliseconds())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (proxy *Proxy) handleConnect(res http.ResponseWriter, req *http.Request) {
|
|
|
|
func (proxy *Proxy) handleConnect(res http.ResponseWriter, req *http.Request) {
|
|
|
|
// log.Printf("CONNECT: %v\n", req.Host)
|
|
|
|
log := log.WithFields(_log.Fields{
|
|
|
|
|
|
|
|
"in": "handleConnect",
|
|
|
|
|
|
|
|
"host": req.Host,
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.Debug("CONNECT")
|
|
|
|
|
|
|
|
|
|
|
|
conn, err := proxy.Mitm.Dial(req.Host)
|
|
|
|
conn, err := proxy.Mitm.Dial(req.Host)
|
|
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error: %v, host: %v\n", err, req.Host)
|
|
|
|
log.Error(err)
|
|
|
|
res.WriteHeader(502)
|
|
|
|
res.WriteHeader(502)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -100,7 +134,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, host: %v\n", err, req.Host)
|
|
|
|
log.Error(err)
|
|
|
|
res.WriteHeader(502)
|
|
|
|
res.WriteHeader(502)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -108,22 +142,22 @@ 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, host: %v\n", err, req.Host)
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ch := make(chan bool)
|
|
|
|
ch := make(chan bool)
|
|
|
|
go func() {
|
|
|
|
go func() {
|
|
|
|
_, err := io.Copy(conn, cconn)
|
|
|
|
_, err := io.Copy(conn, cconn)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil && !ignoreErr(log, err) {
|
|
|
|
log.Printf("error: %v, host: %v\n", err, req.Host)
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ch <- true
|
|
|
|
ch <- true
|
|
|
|
}()
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
|
|
_, err = io.Copy(cconn, conn)
|
|
|
|
_, err = io.Copy(cconn, conn)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil && !ignoreErr(log, err) {
|
|
|
|
log.Printf("error: %v, host: %v\n", err, req.Host)
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
<-ch
|
|
|
|
<-ch
|
|
|
@ -180,7 +214,7 @@ func GetTlsKeyLogWriter() io.Writer {
|
|
|
|
|
|
|
|
|
|
|
|
writer, err := os.OpenFile(logfile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
|
|
|
|
writer, err := os.OpenFile(logfile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("GetTlsKeyLogWriter error: %v\n", err)
|
|
|
|
log.WithField("in", "GetTlsKeyLogWriter").Debug(err)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|