From eb34216c7a73241f4a10b3d5f4ca4093c8fd3c7f Mon Sep 17 00:00:00 2001 From: dingjun Date: Sat, 13 Apr 2019 17:04:18 +0800 Subject: [PATCH] seperate proto listener --- go.mod | 2 +- go.sum | 2 ++ obfsshd/proxy_protocol.go | 50 --------------------------------------- obfsshd/server.go | 9 +++++-- 4 files changed, 10 insertions(+), 53 deletions(-) delete mode 100644 obfsshd/proxy_protocol.go diff --git a/go.mod b/go.mod index a604392..d664b50 100644 --- a/go.mod +++ b/go.mod @@ -5,12 +5,12 @@ go 1.13 require ( github.com/bgentry/speakeasy v0.1.0 github.com/fangdingjun/go-log v4.0.0+incompatible + github.com/fangdingjun/protolistener v0.0.0-20190413090244-355464816fc0 github.com/fangdingjun/socks-go v0.0.0-20180926100003-fc6f0a9ee1f4 github.com/go-yaml/yaml v2.1.0+incompatible github.com/kr/fs v0.1.0 github.com/kr/pretty v0.1.0 // indirect github.com/kr/pty v1.1.4 - github.com/pires/go-proxyproto v0.0.0-20190111085350-4d51b51e3bfc github.com/pkg/errors v0.8.1 // indirect github.com/pkg/sftp v1.10.0 github.com/stretchr/testify v1.3.0 // indirect diff --git a/go.sum b/go.sum index d78df18..c439e95 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fangdingjun/go-log v4.0.0+incompatible h1:h/3S0FRNiFR1A5xkK/ECHtmc2O/DVESKCzjhpgzszfA= github.com/fangdingjun/go-log v4.0.0+incompatible/go.mod h1:Y6ko/KySCpIcAX3mrfiUJtuPY2saPwofv9WYcUatPJY= +github.com/fangdingjun/protolistener v0.0.0-20190413090244-355464816fc0 h1:dafx8KP2yvsX8oAFwJ6Bx54h3YE2P7UXLB453z7LbPo= +github.com/fangdingjun/protolistener v0.0.0-20190413090244-355464816fc0/go.mod h1:bp6oYi3nsUwqGyzikoHJ672i9vX39Qp1h1C/r/vkIIg= github.com/fangdingjun/socks-go v0.0.0-20180926100003-fc6f0a9ee1f4 h1:c3Iw/znf2xe2uut9zUTueO6XHyTTLugrbN9fAE4NAkg= github.com/fangdingjun/socks-go v0.0.0-20180926100003-fc6f0a9ee1f4/go.mod h1:0P4kTlyyh76uY1Li3cyw4pOIKGL9RmXXWTQYFLS1ZaM= github.com/go-yaml/yaml v2.1.0+incompatible h1:RYi2hDdss1u4YE7GwixGzWwVo47T8UQwnTLB6vQiq+o= diff --git a/obfsshd/proxy_protocol.go b/obfsshd/proxy_protocol.go deleted file mode 100644 index 6536fed..0000000 --- a/obfsshd/proxy_protocol.go +++ /dev/null @@ -1,50 +0,0 @@ -package main - -import ( - "bufio" - "net" - - proxyproto "github.com/pires/go-proxyproto" -) - -type protoListener struct { - net.Listener -} - -type protoConn struct { - net.Conn - headerDone bool - r *bufio.Reader - proxy *proxyproto.Header -} - -func (l *protoListener) Accept() (net.Conn, error) { - c, err := l.Listener.Accept() - if err != nil { - return nil, err - } - return &protoConn{Conn: c}, err -} - -func (c *protoConn) Read(buf []byte) (int, error) { - var err error - if !c.headerDone { - c.r = bufio.NewReader(c.Conn) - c.proxy, err = proxyproto.Read(c.r) - if err != nil && err != proxyproto.ErrNoProxyProtocol { - return 0, err - } - c.headerDone = true - return c.r.Read(buf) - } - return c.r.Read(buf) -} - -func (c *protoConn) RemoteAddr() net.Addr { - if c.proxy == nil { - return c.Conn.RemoteAddr() - } - return &net.TCPAddr{ - IP: c.proxy.SourceAddress, - Port: int(c.proxy.SourcePort)} -} diff --git a/obfsshd/server.go b/obfsshd/server.go index b2b8605..d2e431a 100644 --- a/obfsshd/server.go +++ b/obfsshd/server.go @@ -14,6 +14,7 @@ import ( "github.com/fangdingjun/go-log" "github.com/fangdingjun/obfssh" + "github.com/fangdingjun/protolistener" "golang.org/x/crypto/ssh" ) @@ -95,7 +96,9 @@ func main() { AuthLogCallback: func(c ssh.ConnMetadata, method string, err error) { if err != nil { log.Debugf("%s", err.Error()) - log.Errorf("%s auth failed for %s from %s", method, c.User(), c.RemoteAddr()) + if method != "none" { + log.Errorf("%s auth failed for %s from %s", method, c.User(), c.RemoteAddr()) + } } else { log.Printf("Accepted %s for user %s from %s", method, c.User(), c.RemoteAddr()) } @@ -124,12 +127,14 @@ func main() { } defer l.Close() + l = protolistener.New(l) + if lst.Key != "" && lst.Cert != "" { cert, err := tls.LoadX509KeyPair(lst.Cert, lst.Key) if err != nil { log.Fatal(err) } - l = tls.NewListener(&protoListener{l}, &tls.Config{ + l = tls.NewListener(l, &tls.Config{ Certificates: []tls.Certificate{cert}, }) }