mirror of
https://gitee.com/IrisVega/frp.git
synced 2024-11-01 22:31:29 +08:00
vendor: add package golib/net
This commit is contained in:
parent
a27e3dda88
commit
20fcb58437
6
Gopkg.lock
generated
6
Gopkg.lock
generated
@ -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
|
||||
|
@ -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 {
|
||||
|
@ -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"
|
||||
)
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
@ -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]))
|
||||
}
|
@ -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) {
|
||||
|
@ -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
|
||||
}
|
||||
|
64
vendor/github.com/fatedier/golib/net/conn.go
generated
vendored
Normal file
64
vendor/github.com/fatedier/golib/net/conn.go
generated
vendored
Normal file
@ -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
|
||||
}
|
23
utils/net/mux/mux.go → vendor/github.com/fatedier/golib/net/mux/mux.go
generated
vendored
23
utils/net/mux/mux.go → vendor/github.com/fatedier/golib/net/mux/mux.go
generated
vendored
@ -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()
|
0
utils/net/mux/rule.go → vendor/github.com/fatedier/golib/net/mux/rule.go
generated
vendored
0
utils/net/mux/rule.go → vendor/github.com/fatedier/golib/net/mux/rule.go
generated
vendored
Loading…
Reference in New Issue
Block a user