2018-05-22 23:59:35 +08:00
|
|
|
// 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.
|
|
|
|
|
2018-05-23 14:39:12 +08:00
|
|
|
package group
|
2018-05-22 23:59:35 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2022-02-09 15:19:35 +08:00
|
|
|
"strconv"
|
2018-05-22 23:59:35 +08:00
|
|
|
"sync"
|
|
|
|
|
2018-05-23 14:39:12 +08:00
|
|
|
"github.com/fatedier/frp/server/ports"
|
2018-05-22 23:59:35 +08:00
|
|
|
|
2018-05-23 14:39:12 +08:00
|
|
|
gerr "github.com/fatedier/golib/errors"
|
2018-05-22 23:59:35 +08:00
|
|
|
)
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
// TCPGroupCtl manage all TCPGroups
|
|
|
|
type TCPGroupCtl struct {
|
|
|
|
groups map[string]*TCPGroup
|
2018-05-22 23:59:35 +08:00
|
|
|
|
2019-07-31 00:41:58 +08:00
|
|
|
// portManager is used to manage port
|
2020-05-24 17:48:37 +08:00
|
|
|
portManager *ports.Manager
|
2019-07-31 00:41:58 +08:00
|
|
|
mu sync.Mutex
|
2018-05-22 23:59:35 +08:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
// NewTCPGroupCtl return a new TcpGroupCtl
|
|
|
|
func NewTCPGroupCtl(portManager *ports.Manager) *TCPGroupCtl {
|
|
|
|
return &TCPGroupCtl{
|
|
|
|
groups: make(map[string]*TCPGroup),
|
2019-07-31 00:41:58 +08:00
|
|
|
portManager: portManager,
|
2018-05-22 23:59:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
// Listen is the wrapper for TCPGroup's Listen
|
2019-07-31 00:41:58 +08:00
|
|
|
// If there are no group, we will create one here
|
2020-05-24 17:48:37 +08:00
|
|
|
func (tgc *TCPGroupCtl) Listen(proxyName string, group string, groupKey string,
|
2019-07-31 00:41:58 +08:00
|
|
|
addr string, port int) (l net.Listener, realPort int, err error) {
|
|
|
|
|
|
|
|
tgc.mu.Lock()
|
|
|
|
tcpGroup, ok := tgc.groups[group]
|
|
|
|
if !ok {
|
2020-05-24 17:48:37 +08:00
|
|
|
tcpGroup = NewTCPGroup(tgc)
|
2019-07-31 00:41:58 +08:00
|
|
|
tgc.groups[group] = tcpGroup
|
2018-05-22 23:59:35 +08:00
|
|
|
}
|
2019-07-31 00:41:58 +08:00
|
|
|
tgc.mu.Unlock()
|
2018-05-22 23:59:35 +08:00
|
|
|
|
2019-07-31 00:41:58 +08:00
|
|
|
return tcpGroup.Listen(proxyName, group, groupKey, addr, port)
|
2018-05-22 23:59:35 +08:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
// RemoveGroup remove TCPGroup from controller
|
|
|
|
func (tgc *TCPGroupCtl) RemoveGroup(group string) {
|
2019-07-31 00:41:58 +08:00
|
|
|
tgc.mu.Lock()
|
|
|
|
defer tgc.mu.Unlock()
|
|
|
|
delete(tgc.groups, group)
|
2018-05-22 23:59:35 +08:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
// TCPGroup route connections to different proxies
|
|
|
|
type TCPGroup struct {
|
2018-05-22 23:59:35 +08:00
|
|
|
group string
|
|
|
|
groupKey string
|
|
|
|
addr string
|
|
|
|
port int
|
|
|
|
realPort int
|
|
|
|
|
|
|
|
acceptCh chan net.Conn
|
|
|
|
index uint64
|
|
|
|
tcpLn net.Listener
|
2020-05-24 17:48:37 +08:00
|
|
|
lns []*TCPGroupListener
|
|
|
|
ctl *TCPGroupCtl
|
2018-05-22 23:59:35 +08:00
|
|
|
mu sync.Mutex
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
// NewTCPGroup return a new TCPGroup
|
|
|
|
func NewTCPGroup(ctl *TCPGroupCtl) *TCPGroup {
|
|
|
|
return &TCPGroup{
|
|
|
|
lns: make([]*TCPGroupListener, 0),
|
2018-05-22 23:59:35 +08:00
|
|
|
ctl: ctl,
|
|
|
|
acceptCh: make(chan net.Conn),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
// Listen will return a new TCPGroupListener
|
|
|
|
// if TCPGroup already has a listener, just add a new TCPGroupListener to the queues
|
2019-07-31 00:41:58 +08:00
|
|
|
// otherwise, listen on the real address
|
2020-05-24 17:48:37 +08:00
|
|
|
func (tg *TCPGroup) Listen(proxyName string, group string, groupKey string, addr string, port int) (ln *TCPGroupListener, realPort int, err error) {
|
2018-05-22 23:59:35 +08:00
|
|
|
tg.mu.Lock()
|
|
|
|
defer tg.mu.Unlock()
|
|
|
|
if len(tg.lns) == 0 {
|
2019-07-31 00:41:58 +08:00
|
|
|
// the first listener, listen on the real address
|
2018-05-22 23:59:35 +08:00
|
|
|
realPort, err = tg.ctl.portManager.Acquire(proxyName, port)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-02-09 15:19:35 +08:00
|
|
|
tcpLn, errRet := net.Listen("tcp", net.JoinHostPort(addr, strconv.Itoa(port)))
|
2018-05-22 23:59:35 +08:00
|
|
|
if errRet != nil {
|
|
|
|
err = errRet
|
|
|
|
return
|
|
|
|
}
|
2020-05-24 17:48:37 +08:00
|
|
|
ln = newTCPGroupListener(group, tg, tcpLn.Addr())
|
2018-05-22 23:59:35 +08:00
|
|
|
|
|
|
|
tg.group = group
|
|
|
|
tg.groupKey = groupKey
|
|
|
|
tg.addr = addr
|
|
|
|
tg.port = port
|
|
|
|
tg.realPort = realPort
|
|
|
|
tg.tcpLn = tcpLn
|
|
|
|
tg.lns = append(tg.lns, ln)
|
|
|
|
if tg.acceptCh == nil {
|
|
|
|
tg.acceptCh = make(chan net.Conn)
|
|
|
|
}
|
|
|
|
go tg.worker()
|
|
|
|
} else {
|
2019-07-31 00:41:58 +08:00
|
|
|
// address and port in the same group must be equal
|
2018-12-07 17:05:36 +08:00
|
|
|
if tg.group != group || tg.addr != addr {
|
2018-05-22 23:59:35 +08:00
|
|
|
err = ErrGroupParamsInvalid
|
|
|
|
return
|
|
|
|
}
|
2018-12-07 17:05:36 +08:00
|
|
|
if tg.port != port {
|
|
|
|
err = ErrGroupDifferentPort
|
|
|
|
return
|
|
|
|
}
|
2018-05-22 23:59:35 +08:00
|
|
|
if tg.groupKey != groupKey {
|
|
|
|
err = ErrGroupAuthFailed
|
|
|
|
return
|
|
|
|
}
|
2020-05-24 17:48:37 +08:00
|
|
|
ln = newTCPGroupListener(group, tg, tg.lns[0].Addr())
|
2018-05-22 23:59:35 +08:00
|
|
|
realPort = tg.realPort
|
|
|
|
tg.lns = append(tg.lns, ln)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-31 00:41:58 +08:00
|
|
|
// worker is called when the real tcp listener has been created
|
2020-05-24 17:48:37 +08:00
|
|
|
func (tg *TCPGroup) worker() {
|
2018-05-22 23:59:35 +08:00
|
|
|
for {
|
|
|
|
c, err := tg.tcpLn.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = gerr.PanicToError(func() {
|
|
|
|
tg.acceptCh <- c
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (tg *TCPGroup) Accept() <-chan net.Conn {
|
2018-05-22 23:59:35 +08:00
|
|
|
return tg.acceptCh
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
// CloseListener remove the TCPGroupListener from the TCPGroup
|
|
|
|
func (tg *TCPGroup) CloseListener(ln *TCPGroupListener) {
|
2018-05-22 23:59:35 +08:00
|
|
|
tg.mu.Lock()
|
|
|
|
defer tg.mu.Unlock()
|
|
|
|
for i, tmpLn := range tg.lns {
|
|
|
|
if tmpLn == ln {
|
|
|
|
tg.lns = append(tg.lns[:i], tg.lns[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(tg.lns) == 0 {
|
|
|
|
close(tg.acceptCh)
|
|
|
|
tg.tcpLn.Close()
|
|
|
|
tg.ctl.portManager.Release(tg.realPort)
|
|
|
|
tg.ctl.RemoveGroup(tg.group)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
// TCPGroupListener
|
|
|
|
type TCPGroupListener struct {
|
2019-07-31 00:41:58 +08:00
|
|
|
groupName string
|
2020-05-24 17:48:37 +08:00
|
|
|
group *TCPGroup
|
2018-05-22 23:59:35 +08:00
|
|
|
|
2019-07-31 00:41:58 +08:00
|
|
|
addr net.Addr
|
|
|
|
closeCh chan struct{}
|
2018-05-22 23:59:35 +08:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func newTCPGroupListener(name string, group *TCPGroup, addr net.Addr) *TCPGroupListener {
|
|
|
|
return &TCPGroupListener{
|
2019-07-31 00:41:58 +08:00
|
|
|
groupName: name,
|
|
|
|
group: group,
|
|
|
|
addr: addr,
|
|
|
|
closeCh: make(chan struct{}),
|
2018-05-22 23:59:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
// Accept will accept connections from TCPGroup
|
|
|
|
func (ln *TCPGroupListener) Accept() (c net.Conn, err error) {
|
2019-07-31 00:41:58 +08:00
|
|
|
var ok bool
|
|
|
|
select {
|
|
|
|
case <-ln.closeCh:
|
|
|
|
return nil, ErrListenerClosed
|
|
|
|
case c, ok = <-ln.group.Accept():
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrListenerClosed
|
|
|
|
}
|
|
|
|
return c, nil
|
2018-05-22 23:59:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (ln *TCPGroupListener) Addr() net.Addr {
|
2019-07-31 00:41:58 +08:00
|
|
|
return ln.addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close close the listener
|
2020-05-24 17:48:37 +08:00
|
|
|
func (ln *TCPGroupListener) Close() (err error) {
|
2019-07-31 00:41:58 +08:00
|
|
|
close(ln.closeCh)
|
|
|
|
|
|
|
|
// remove self from TcpGroup
|
|
|
|
ln.group.CloseListener(ln)
|
|
|
|
return
|
2018-05-22 23:59:35 +08:00
|
|
|
}
|