mirror of
https://github.com/fatedier/frp.git
synced 2025-08-04 01:01:07 +07:00
vendor: add package golib/net
This commit is contained in:
224
vendor/github.com/fatedier/golib/net/mux/mux.go
generated
vendored
Normal file
224
vendor/github.com/fatedier/golib/net/mux/mux.go
generated
vendored
Normal file
@ -0,0 +1,224 @@
|
||||
// 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 (
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/fatedier/golib/errors"
|
||||
gnet "github.com/fatedier/golib/net"
|
||||
)
|
||||
|
||||
const (
|
||||
// DefaultTimeout is the default length of time to wait for bytes we need.
|
||||
DefaultTimeout = 10 * time.Second
|
||||
)
|
||||
|
||||
type Mux struct {
|
||||
ln net.Listener
|
||||
|
||||
defaultLn *listener
|
||||
lns []*listener
|
||||
maxNeedBytesNum uint32
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
func NewMux() (mux *Mux) {
|
||||
mux = &Mux{
|
||||
lns: make([]*listener, 0),
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (mux *Mux) Listen(priority int, needBytesNum uint32, fn MatchFunc) net.Listener {
|
||||
ln := &listener{
|
||||
c: make(chan net.Conn),
|
||||
mux: mux,
|
||||
needBytesNum: needBytesNum,
|
||||
matchFn: fn,
|
||||
}
|
||||
|
||||
mux.mu.Lock()
|
||||
defer mux.mu.Unlock()
|
||||
if needBytesNum > mux.maxNeedBytesNum {
|
||||
mux.maxNeedBytesNum = needBytesNum
|
||||
}
|
||||
|
||||
newlns := append(mux.copyLns(), ln)
|
||||
sort.Slice(newlns, func(i, j int) bool {
|
||||
return newlns[i].needBytesNum < newlns[j].needBytesNum
|
||||
})
|
||||
mux.lns = newlns
|
||||
return ln
|
||||
}
|
||||
|
||||
func (mux *Mux) ListenHttp(priority int) net.Listener {
|
||||
return mux.Listen(priority, HttpNeedBytesNum, HttpMatchFunc)
|
||||
}
|
||||
|
||||
func (mux *Mux) ListenHttps(priority int) net.Listener {
|
||||
return mux.Listen(priority, HttpsNeedBytesNum, HttpsMatchFunc)
|
||||
}
|
||||
|
||||
func (mux *Mux) DefaultListener() net.Listener {
|
||||
mux.mu.Lock()
|
||||
defer mux.mu.Unlock()
|
||||
if mux.defaultLn == nil {
|
||||
mux.defaultLn = &listener{
|
||||
c: make(chan net.Conn),
|
||||
mux: mux,
|
||||
}
|
||||
}
|
||||
return mux.defaultLn
|
||||
}
|
||||
|
||||
func (mux *Mux) release(ln *listener) bool {
|
||||
result := false
|
||||
mux.mu.Lock()
|
||||
defer mux.mu.Unlock()
|
||||
lns := mux.copyLns()
|
||||
|
||||
for i, l := range lns {
|
||||
if l == ln {
|
||||
lns = append(lns[:i], lns[i+1:]...)
|
||||
result = true
|
||||
}
|
||||
}
|
||||
mux.lns = lns
|
||||
return result
|
||||
}
|
||||
|
||||
func (mux *Mux) copyLns() []*listener {
|
||||
lns := make([]*listener, 0, len(mux.lns))
|
||||
for _, l := range mux.lns {
|
||||
lns = append(lns, l)
|
||||
}
|
||||
return lns
|
||||
}
|
||||
|
||||
// Serve handles connections from ln and multiplexes then across registered listeners.
|
||||
func (mux *Mux) Serve(ln net.Listener) error {
|
||||
mux.mu.Lock()
|
||||
mux.ln = ln
|
||||
mux.mu.Unlock()
|
||||
for {
|
||||
// Wait for the next connection.
|
||||
// If it returns a temporary error then simply retry.
|
||||
// If it returns any other error then exit immediately.
|
||||
conn, err := ln.Accept()
|
||||
if err, ok := err.(interface {
|
||||
Temporary() bool
|
||||
}); ok && err.Temporary() {
|
||||
continue
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
go mux.handleConn(conn)
|
||||
}
|
||||
}
|
||||
|
||||
func (mux *Mux) handleConn(conn net.Conn) {
|
||||
mux.mu.RLock()
|
||||
maxNeedBytesNum := mux.maxNeedBytesNum
|
||||
lns := mux.lns
|
||||
defaultLn := mux.defaultLn
|
||||
mux.mu.RUnlock()
|
||||
|
||||
sharedConn, rd := gnet.NewSharedConnSize(conn, int(maxNeedBytesNum))
|
||||
data := make([]byte, maxNeedBytesNum)
|
||||
|
||||
conn.SetReadDeadline(time.Now().Add(DefaultTimeout))
|
||||
_, err := io.ReadFull(rd, data)
|
||||
if err != nil {
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
conn.SetReadDeadline(time.Time{})
|
||||
|
||||
for _, ln := range lns {
|
||||
if match := ln.matchFn(data); match {
|
||||
err = errors.PanicToError(func() {
|
||||
ln.c <- sharedConn
|
||||
})
|
||||
if err != nil {
|
||||
conn.Close()
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// No match listeners
|
||||
if defaultLn != nil {
|
||||
err = errors.PanicToError(func() {
|
||||
defaultLn.c <- sharedConn
|
||||
})
|
||||
if err != nil {
|
||||
conn.Close()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// No listeners for this connection, close it.
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
|
||||
type listener struct {
|
||||
mux *Mux
|
||||
|
||||
needBytesNum uint32
|
||||
matchFn MatchFunc
|
||||
|
||||
c chan net.Conn
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
// Accept waits for and returns the next connection to the listener.
|
||||
func (ln *listener) Accept() (net.Conn, error) {
|
||||
conn, ok := <-ln.c
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("network connection closed")
|
||||
}
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
// Close removes this listener from the parent mux and closes the channel.
|
||||
func (ln *listener) Close() error {
|
||||
if ok := ln.mux.release(ln); ok {
|
||||
// Close done to signal to any RLock holders to release their lock.
|
||||
close(ln.c)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ln *listener) Addr() net.Addr {
|
||||
if ln.mux == nil {
|
||||
return nil
|
||||
}
|
||||
ln.mux.mu.RLock()
|
||||
defer ln.mux.mu.RUnlock()
|
||||
if ln.mux.ln == nil {
|
||||
return nil
|
||||
}
|
||||
return ln.mux.ln.Addr()
|
||||
}
|
55
vendor/github.com/fatedier/golib/net/mux/rule.go
generated
vendored
Normal file
55
vendor/github.com/fatedier/golib/net/mux/rule.go
generated
vendored
Normal file
@ -0,0 +1,55 @@
|
||||
package mux
|
||||
|
||||
type MatchFunc func(data []byte) (match bool)
|
||||
|
||||
var (
|
||||
HttpsNeedBytesNum uint32 = 1
|
||||
HttpNeedBytesNum uint32 = 3
|
||||
YamuxNeedBytesNum uint32 = 2
|
||||
)
|
||||
|
||||
var HttpsMatchFunc MatchFunc = func(data []byte) bool {
|
||||
if len(data) < int(HttpsNeedBytesNum) {
|
||||
return false
|
||||
}
|
||||
|
||||
if data[0] == 0x16 {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// From https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
|
||||
var httpHeadBytes = map[string]struct{}{
|
||||
"GET": struct{}{},
|
||||
"HEA": struct{}{},
|
||||
"POS": struct{}{},
|
||||
"PUT": struct{}{},
|
||||
"DEL": struct{}{},
|
||||
"CON": struct{}{},
|
||||
"OPT": struct{}{},
|
||||
"TRA": struct{}{},
|
||||
"PAT": struct{}{},
|
||||
}
|
||||
|
||||
var HttpMatchFunc MatchFunc = func(data []byte) bool {
|
||||
if len(data) < int(HttpNeedBytesNum) {
|
||||
return false
|
||||
}
|
||||
|
||||
_, ok := httpHeadBytes[string(data[:3])]
|
||||
return ok
|
||||
}
|
||||
|
||||
// From https://github.com/hashicorp/yamux/blob/master/spec.md
|
||||
var YamuxMatchFunc MatchFunc = func(data []byte) bool {
|
||||
if len(data) < int(YamuxNeedBytesNum) {
|
||||
return false
|
||||
}
|
||||
|
||||
if data[0] == 0 && data[1] >= 0x0 && data[1] <= 0x3 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
Reference in New Issue
Block a user