From 20fcb5843789f1b9a3dfc09d2b5efde93e3a732d Mon Sep 17 00:00:00 2001 From: fatedier Date: Wed, 9 May 2018 00:23:42 +0800 Subject: [PATCH] vendor: add package golib/net --- Gopkg.lock | 6 +- models/plugin/http_proxy.go | 3 +- server/service.go | 2 +- utils/net/conn.go | 44 --------- utils/net/mux/mux_test.go | 95 ------------------- utils/vhost/http.go | 15 +-- utils/vhost/https.go | 10 +- vendor/github.com/fatedier/golib/net/conn.go | 64 +++++++++++++ .../github.com/fatedier/golib}/net/mux/mux.go | 23 ++++- .../fatedier/golib}/net/mux/rule.go | 0 10 files changed, 103 insertions(+), 159 deletions(-) delete mode 100644 utils/net/mux/mux_test.go create mode 100644 vendor/github.com/fatedier/golib/net/conn.go rename {utils => vendor/github.com/fatedier/golib}/net/mux/mux.go (83%) rename {utils => vendor/github.com/fatedier/golib}/net/mux/rule.go (100%) diff --git a/Gopkg.lock b/Gopkg.lock index dcfd8d7..9790b9d 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -25,9 +25,11 @@ "crypto", "errors", "io", + "net", + "net/mux", "pool" ] - revision = "d7011355f61fc8a2d1e38114604b5c14397841c4" + revision = "99af68ddbf3e0c57f3386fcfae251b67f901bf0d" [[projects]] branch = "frp" @@ -160,6 +162,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "8a34960c52b0b575f8e5cb799e6af7288fe0289cb3b76b9346a2b69b677a3977" + inputs-digest = "d4f8f1e8dd5c07302832144eed2288be10b8061eb60712540ff0c569774e216f" solver-name = "gps-cdcl" solver-version = 1 diff --git a/models/plugin/http_proxy.go b/models/plugin/http_proxy.go index 5ce2e67..a9ff6ef 100644 --- a/models/plugin/http_proxy.go +++ b/models/plugin/http_proxy.go @@ -25,6 +25,7 @@ import ( frpNet "github.com/fatedier/frp/utils/net" frpIo "github.com/fatedier/golib/io" + gnet "github.com/fatedier/golib/net" ) const PluginHttpProxy = "http_proxy" @@ -66,7 +67,7 @@ func (hp *HttpProxy) Name() string { func (hp *HttpProxy) Handle(conn io.ReadWriteCloser, realConn frpNet.Conn) { wrapConn := frpNet.WrapReadWriteCloserToConn(conn, realConn) - sc, rd := frpNet.NewShareConn(wrapConn) + sc, rd := gnet.NewSharedConn(wrapConn) firstBytes := make([]byte, 7) _, err := rd.Read(firstBytes) if err != nil { diff --git a/server/service.go b/server/service.go index 496fd55..cccfcb6 100644 --- a/server/service.go +++ b/server/service.go @@ -26,11 +26,11 @@ import ( "github.com/fatedier/frp/models/msg" "github.com/fatedier/frp/utils/log" frpNet "github.com/fatedier/frp/utils/net" - "github.com/fatedier/frp/utils/net/mux" "github.com/fatedier/frp/utils/util" "github.com/fatedier/frp/utils/version" "github.com/fatedier/frp/utils/vhost" + "github.com/fatedier/golib/net/mux" fmux "github.com/hashicorp/yamux" ) diff --git a/utils/net/conn.go b/utils/net/conn.go index e18d9c4..c6f3504 100644 --- a/utils/net/conn.go +++ b/utils/net/conn.go @@ -15,7 +15,6 @@ package net import ( - "bytes" "errors" "fmt" "io" @@ -133,49 +132,6 @@ func ConnectServerByProxy(proxyUrl string, protocol string, addr string) (c Conn } } -type SharedConn struct { - Conn - buf *bytes.Buffer -} - -// the bytes you read in io.Reader, will be reserved in SharedConn -func NewShareConn(conn Conn) (*SharedConn, io.Reader) { - sc := &SharedConn{ - Conn: conn, - buf: bytes.NewBuffer(make([]byte, 0, 1024)), - } - return sc, io.TeeReader(conn, sc.buf) -} - -func NewShareConnSize(conn Conn, bufSize int) (*SharedConn, io.Reader) { - sc := &SharedConn{ - Conn: conn, - buf: bytes.NewBuffer(make([]byte, 0, bufSize)), - } - return sc, io.TeeReader(conn, sc.buf) -} - -// Not thread safety. -func (sc *SharedConn) Read(p []byte) (n int, err error) { - if sc.buf == nil { - return sc.Conn.Read(p) - } - n, err = sc.buf.Read(p) - if err == io.EOF { - sc.buf = nil - var n2 int - n2, err = sc.Conn.Read(p[n:]) - n += n2 - } - return -} - -func (sc *SharedConn) WriteBuff(buffer []byte) (err error) { - sc.buf.Reset() - _, err = sc.buf.Write(buffer) - return err -} - type StatsConn struct { Conn diff --git a/utils/net/mux/mux_test.go b/utils/net/mux/mux_test.go deleted file mode 100644 index fd3a9e2..0000000 --- a/utils/net/mux/mux_test.go +++ /dev/null @@ -1,95 +0,0 @@ -package mux - -import ( - "bufio" - "io/ioutil" - "net" - "net/http" - "net/http/httptest" - "testing" - "time" - - "github.com/stretchr/testify/assert" -) - -func runHttpSvr(ln net.Listener) *httptest.Server { - svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("http service")) - })) - svr.Listener = ln - svr.Start() - return svr -} - -func runHttpsSvr(ln net.Listener) *httptest.Server { - svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("https service")) - })) - svr.Listener = ln - svr.StartTLS() - return svr -} - -func runEchoSvr(ln net.Listener) { - go func() { - for { - conn, err := ln.Accept() - if err != nil { - return - } - rd := bufio.NewReader(conn) - data, err := rd.ReadString('\n') - if err != nil { - return - } - conn.Write([]byte(data)) - conn.Close() - } - }() -} - -func TestMux(t *testing.T) { - assert := assert.New(t) - - ln, err := net.Listen("tcp", "127.0.0.1:") - assert.NoError(err) - - mux := NewMux() - httpLn := mux.ListenHttp(0) - httpsLn := mux.ListenHttps(0) - defaultLn := mux.DefaultListener() - go mux.Serve(ln) - time.Sleep(100 * time.Millisecond) - - httpSvr := runHttpSvr(httpLn) - defer httpSvr.Close() - httpsSvr := runHttpsSvr(httpsLn) - defer httpsSvr.Close() - runEchoSvr(defaultLn) - defer ln.Close() - - // test http service - resp, err := http.Get(httpSvr.URL) - assert.NoError(err) - data, err := ioutil.ReadAll(resp.Body) - assert.NoError(err) - assert.Equal("http service", string(data)) - - // test https service - client := httpsSvr.Client() - resp, err = client.Get(httpsSvr.URL) - assert.NoError(err) - data, err = ioutil.ReadAll(resp.Body) - assert.NoError(err) - assert.Equal("https service", string(data)) - - // test echo service - conn, err := net.Dial("tcp", ln.Addr().String()) - assert.NoError(err) - _, err = conn.Write([]byte("test echo\n")) - assert.NoError(err) - data = make([]byte, 1024) - n, err := conn.Read(data) - assert.NoError(err) - assert.Equal("test echo\n", string(data[:n])) -} diff --git a/utils/vhost/http.go b/utils/vhost/http.go index 98aa9dc..9fc05bd 100644 --- a/utils/vhost/http.go +++ b/utils/vhost/http.go @@ -27,6 +27,7 @@ import ( frpNet "github.com/fatedier/frp/utils/net" + gnet "github.com/fatedier/golib/net" "github.com/fatedier/golib/pool" ) @@ -36,11 +37,11 @@ type HttpMuxer struct { func GetHttpRequestInfo(c frpNet.Conn) (_ frpNet.Conn, _ map[string]string, err error) { reqInfoMap := make(map[string]string, 0) - sc, rd := frpNet.NewShareConn(c) + sc, rd := gnet.NewSharedConn(c) request, err := http.ReadRequest(bufio.NewReader(rd)) if err != nil { - return sc, reqInfoMap, err + return nil, reqInfoMap, err } // hostName tmpArr := strings.Split(request.Host, ":") @@ -54,7 +55,7 @@ func GetHttpRequestInfo(c frpNet.Conn) (_ frpNet.Conn, _ map[string]string, err reqInfoMap["Authorization"] = authStr } request.Body.Close() - return sc, reqInfoMap, nil + return frpNet.WrapConn(sc), reqInfoMap, nil } func NewHttpMuxer(listener frpNet.Listener, timeout time.Duration) (*HttpMuxer, error) { @@ -63,14 +64,14 @@ func NewHttpMuxer(listener frpNet.Listener, timeout time.Duration) (*HttpMuxer, } func ModifyHttpRequest(c frpNet.Conn, rewriteHost string) (_ frpNet.Conn, err error) { - sc, rd := frpNet.NewShareConn(c) + sc, rd := gnet.NewSharedConn(c) var buff []byte remoteIP := strings.Split(c.RemoteAddr().String(), ":")[0] if buff, err = hostNameRewrite(rd, rewriteHost, remoteIP); err != nil { - return sc, err + return nil, err } - err = sc.WriteBuff(buff) - return sc, err + err = sc.ResetBuf(buff) + return frpNet.WrapConn(sc), err } func hostNameRewrite(request io.Reader, rewriteHost string, remoteIP string) (_ []byte, err error) { diff --git a/utils/vhost/https.go b/utils/vhost/https.go index 490be40..12fc8d0 100644 --- a/utils/vhost/https.go +++ b/utils/vhost/https.go @@ -21,6 +21,8 @@ import ( "time" frpNet "github.com/fatedier/frp/utils/net" + + gnet "github.com/fatedier/golib/net" "github.com/fatedier/golib/pool" ) @@ -180,14 +182,14 @@ func readHandshake(rd io.Reader) (host string, err error) { return } -func GetHttpsHostname(c frpNet.Conn) (sc frpNet.Conn, _ map[string]string, err error) { +func GetHttpsHostname(c frpNet.Conn) (_ frpNet.Conn, _ map[string]string, err error) { reqInfoMap := make(map[string]string, 0) - sc, rd := frpNet.NewShareConn(c) + sc, rd := gnet.NewSharedConn(c) host, err := readHandshake(rd) if err != nil { - return sc, reqInfoMap, err + return nil, reqInfoMap, err } reqInfoMap["Host"] = host reqInfoMap["Scheme"] = "https" - return sc, reqInfoMap, nil + return frpNet.WrapConn(sc), reqInfoMap, nil } diff --git a/vendor/github.com/fatedier/golib/net/conn.go b/vendor/github.com/fatedier/golib/net/conn.go new file mode 100644 index 0000000..185dc8b --- /dev/null +++ b/vendor/github.com/fatedier/golib/net/conn.go @@ -0,0 +1,64 @@ +// Copyright 2018 fatedier, fatedier@gmail.com +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package net + +import ( + "bytes" + "io" + "net" +) + +type SharedConn struct { + net.Conn + buf *bytes.Buffer +} + +// the bytes you read in io.Reader, will be reserved in SharedConn +func NewSharedConn(conn net.Conn) (*SharedConn, io.Reader) { + sc := &SharedConn{ + Conn: conn, + buf: bytes.NewBuffer(make([]byte, 0, 1024)), + } + return sc, io.TeeReader(conn, sc.buf) +} + +func NewSharedConnSize(conn net.Conn, bufSize int) (*SharedConn, io.Reader) { + sc := &SharedConn{ + Conn: conn, + buf: bytes.NewBuffer(make([]byte, 0, bufSize)), + } + return sc, io.TeeReader(conn, sc.buf) +} + +// Not thread safety. +func (sc *SharedConn) Read(p []byte) (n int, err error) { + if sc.buf == nil { + return sc.Conn.Read(p) + } + n, err = sc.buf.Read(p) + if err == io.EOF { + sc.buf = nil + var n2 int + n2, err = sc.Conn.Read(p[n:]) + n += n2 + } + return +} + +func (sc *SharedConn) ResetBuf(buffer []byte) (err error) { + sc.buf.Reset() + _, err = sc.buf.Write(buffer) + return err +} diff --git a/utils/net/mux/mux.go b/vendor/github.com/fatedier/golib/net/mux/mux.go similarity index 83% rename from utils/net/mux/mux.go rename to vendor/github.com/fatedier/golib/net/mux/mux.go index 7e7edb1..ebf5717 100644 --- a/utils/net/mux/mux.go +++ b/vendor/github.com/fatedier/golib/net/mux/mux.go @@ -1,3 +1,17 @@ +// Copyright 2018 fatedier, fatedier@gmail.com +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package mux import ( @@ -8,9 +22,8 @@ import ( "sync" "time" - frpNet "github.com/fatedier/frp/utils/net" - "github.com/fatedier/golib/errors" + gnet "github.com/fatedier/golib/net" ) const ( @@ -131,7 +144,7 @@ func (mux *Mux) handleConn(conn net.Conn) { defaultLn := mux.defaultLn mux.mu.RUnlock() - shareConn, rd := frpNet.NewShareConnSize(frpNet.WrapConn(conn), int(maxNeedBytesNum)) + sharedConn, rd := gnet.NewSharedConnSize(conn, int(maxNeedBytesNum)) data := make([]byte, maxNeedBytesNum) conn.SetReadDeadline(time.Now().Add(DefaultTimeout)) @@ -145,7 +158,7 @@ func (mux *Mux) handleConn(conn net.Conn) { for _, ln := range lns { if match := ln.matchFn(data); match { err = errors.PanicToError(func() { - ln.c <- shareConn + ln.c <- sharedConn }) if err != nil { conn.Close() @@ -157,7 +170,7 @@ func (mux *Mux) handleConn(conn net.Conn) { // No match listeners if defaultLn != nil { err = errors.PanicToError(func() { - defaultLn.c <- shareConn + defaultLn.c <- sharedConn }) if err != nil { conn.Close() diff --git a/utils/net/mux/rule.go b/vendor/github.com/fatedier/golib/net/mux/rule.go similarity index 100% rename from utils/net/mux/rule.go rename to vendor/github.com/fatedier/golib/net/mux/rule.go