You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
2.3 KiB
Go

4 years ago
package proxy
import (
"bytes"
"io"
2 years ago
"os"
4 years ago
"strings"
2 years ago
"sync"
4 years ago
_log "github.com/sirupsen/logrus"
4 years ago
)
2 years ago
var normalErrMsgs []string = []string{
4 years ago
"read: connection reset by peer",
"write: broken pipe",
"i/o timeout",
"net/http: TLS handshake timeout",
"io: read/write on closed pipe",
"connect: connection refused",
"connect: connection reset by peer",
"use of closed network connection",
4 years ago
}
// 仅打印预料之外的错误信息
2 years ago
func logErr(log *_log.Entry, err error) (loged bool) {
4 years ago
msg := err.Error()
2 years ago
for _, str := range normalErrMsgs {
4 years ago
if strings.Contains(msg, str) {
log.Debug(err)
return
}
}
log.Error(err)
loged = true
return
}
// 转发流量
// Read a => Write b
// Read b => Write a
func transfer(log *_log.Entry, a, b io.ReadWriteCloser) {
4 years ago
done := make(chan struct{})
defer close(done)
forward := func(dst io.WriteCloser, src io.Reader, ec chan<- error) {
4 years ago
_, err := io.Copy(dst, src)
dst.Close() // 当一端读结束时,结束另一端的写
4 years ago
select {
case <-done:
return
case ec <- err:
4 years ago
return
4 years ago
}
}
errChan := make(chan error)
go forward(a, b, errChan)
go forward(b, a, errChan)
for i := 0; i < 2; i++ {
if err := <-errChan; err != nil {
2 years ago
logErr(log, err)
4 years ago
return // 如果有错误,直接返回
}
}
}
4 years ago
// 尝试将 Reader 读取至 buffer 中
4 years ago
// 如果未达到 limit则成功读取进入 buffer
// 否则 buffer 返回 nil且返回新 Reader状态为未读取前
2 years ago
func readerToBuffer(r io.Reader, limit int64) ([]byte, io.Reader, error) {
4 years ago
buf := bytes.NewBuffer(make([]byte, 0))
lr := io.LimitReader(r, limit)
_, err := io.Copy(buf, lr)
if err != nil {
return nil, nil, err
}
// 达到上限
if int64(buf.Len()) == limit {
// 返回新的 Reader
return nil, io.MultiReader(bytes.NewBuffer(buf.Bytes()), r), nil
}
// 返回 buffer
return buf.Bytes(), nil, nil
}
2 years ago
// Wireshark 解析 https 设置
var tlsKeyLogWriter io.Writer
var tlsKeyLogOnce sync.Once
func getTlsKeyLogWriter() io.Writer {
tlsKeyLogOnce.Do(func() {
logfile := os.Getenv("SSLKEYLOGFILE")
if logfile == "" {
return
}
writer, err := os.OpenFile(logfile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.WithField("in", "getTlsKeyLogWriter").Debug(err)
return
}
tlsKeyLogWriter = writer
})
return tlsKeyLogWriter
}