Skip to content

Commit

Permalink
proxy: record failures of remote connection only
Browse files Browse the repository at this point in the history
  • Loading branch information
nadoo committed Sep 14, 2020
1 parent b3b11b4 commit 89114e6
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 15 deletions.
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func main() {
confInit()

// setup a log func
log.F = func(f string, v ...interface{}) {
if conf.Verbose {
if conf.Verbose {
log.F = func(f string, v ...interface{}) {
stdlog.Output(2, fmt.Sprintf(f, v...))
}
}
Expand Down
8 changes: 6 additions & 2 deletions proxy/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"net"
"net/textproto"
"strings"
"time"

"github.com/nadoo/glider/common/conn"
Expand Down Expand Up @@ -103,8 +104,11 @@ func (s *HTTP) servHTTPS(r *request, c net.Conn) {
log.F("[http] %s <-> %s [c] via %s", c.RemoteAddr(), r.uri, dialer.Addr())

if err = conn.Relay(c, rc); err != nil {
log.F("[http] relay error: %v", err)
s.proxy.Record(dialer, false)
log.F("[http] %s <-> %s via %s, relay error: %v", c.RemoteAddr(), r.uri, dialer.Addr(), err)
// record remote conn failure only
if !strings.Contains(err.Error(), s.addr) {
s.proxy.Record(dialer, false)
}
}
}

Expand Down
8 changes: 6 additions & 2 deletions proxy/redir/redir_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"net"
"net/url"
"strings"
"syscall"
"unsafe"

Expand Down Expand Up @@ -114,8 +115,11 @@ func (s *RedirProxy) Serve(c net.Conn) {
log.F("[redir] %s <-> %s via %s", c.RemoteAddr(), tgt, dialer.Addr())

if err = conn.Relay(c, rc); err != nil {
log.F("[redir] relay error: %v", err)
s.proxy.Record(dialer, false)
log.F("[redir] %s <-> %s via %s, relay error: %v", c.RemoteAddr(), tgt, dialer.Addr(), err)
// record remote conn failure only
if !strings.Contains(err.Error(), s.addr) {
s.proxy.Record(dialer, false)
}
}
}

Expand Down
8 changes: 6 additions & 2 deletions proxy/socks5/socks5.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"net"
"net/url"
"strconv"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -142,8 +143,11 @@ func (s *Socks5) Serve(c net.Conn) {
log.F("[socks5] %s <-> %s via %s", c.RemoteAddr(), tgt, dialer.Addr())

if err = conn.Relay(c, rc); err != nil {
log.F("[socks5] relay error: %v", err)
s.proxy.Record(dialer, false)
log.F("[socks5] %s <-> %s via %s, relay error: %v", c.RemoteAddr(), tgt, dialer.Addr(), err)
// record remote conn failure only
if !strings.Contains(err.Error(), s.addr) {
s.proxy.Record(dialer, false)
}
}
}

Expand Down
7 changes: 5 additions & 2 deletions proxy/ss/ss.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,11 @@ func (s *SS) Serve(c net.Conn) {
log.F("[ss] %s <-> %s via %s", c.RemoteAddr(), tgt, dialer.Addr())

if err = conn.Relay(c, rc); err != nil {
log.F("[ss] relay error: %v", err)
s.proxy.Record(dialer, false)
log.F("[ss] %s <-> %s via %s, relay error: %v", c.RemoteAddr(), tgt, dialer.Addr(), err)
// record remote conn failure only
if !strings.Contains(err.Error(), s.addr) {
s.proxy.Record(dialer, false)
}
}
}

Expand Down
7 changes: 5 additions & 2 deletions proxy/tcptun/tcptun.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ func (s *TCPTun) Serve(c net.Conn) {
log.F("[tcptun] %s <-> %s via %s", c.RemoteAddr(), s.raddr, dialer.Addr())

if err = conn.Relay(c, rc); err != nil {
log.F("[tcptun] relay error: %v", err)
s.proxy.Record(dialer, false)
log.F("[tcptun] %s <-> %s via %s, relay error: %v", c.RemoteAddr(), s.raddr, dialer.Addr(), err)
// record remote conn failure only
if !strings.Contains(err.Error(), s.addr) {
s.proxy.Record(dialer, false)
}
}
}
6 changes: 3 additions & 3 deletions proxy/ws/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const (
type frameWriter struct {
io.Writer
buf []byte
maskKey []byte
maskKey [maskKeyLen]byte
}

// FrameWriter returns a frame writer.
Expand All @@ -51,7 +51,7 @@ func FrameWriter(w io.Writer) io.Writer {
return &frameWriter{
Writer: w,
buf: make([]byte, maxFrameHeaderSize+defaultFrameSize),
maskKey: []byte{byte(n), byte(n >> 8), byte(n >> 16), byte(n >> 24)},
maskKey: [...]byte{byte(n), byte(n >> 8), byte(n >> 16), byte(n >> 24)},
}
}

Expand Down Expand Up @@ -93,7 +93,7 @@ func (w *frameWriter) ReadFrom(r io.Reader) (n int64, err error) {
}

// maskkey
_, ew = w.Writer.Write(w.maskKey)
_, ew = w.Writer.Write(w.maskKey[:])
if ew != nil {
err = ew
break
Expand Down

0 comments on commit 89114e6

Please sign in to comment.