frp/server/group/tcpmux.go

231 lines
5.8 KiB
Go
Raw Normal View History

2020-04-20 13:35:47 +08:00
// Copyright 2020 guylewin, guy@lewin.co.il
//
// 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 group
import (
"context"
"fmt"
"net"
"sync"
2022-08-29 01:02:53 +08:00
gerr "github.com/fatedier/golib/errors"
2023-09-20 15:18:50 +08:00
v1 "github.com/fatedier/frp/pkg/config/v1"
2020-09-23 13:49:14 +08:00
"github.com/fatedier/frp/pkg/util/tcpmux"
"github.com/fatedier/frp/pkg/util/vhost"
2020-04-20 13:35:47 +08:00
)
2020-05-24 17:48:37 +08:00
// TCPMuxGroupCtl manage all TCPMuxGroups
type TCPMuxGroupCtl struct {
groups map[string]*TCPMuxGroup
2020-04-20 13:35:47 +08:00
// portManager is used to manage port
2020-05-24 17:48:37 +08:00
tcpMuxHTTPConnectMuxer *tcpmux.HTTPConnectTCPMuxer
2020-04-20 13:35:47 +08:00
mu sync.Mutex
}
2020-05-24 17:48:37 +08:00
// NewTCPMuxGroupCtl return a new TCPMuxGroupCtl
func NewTCPMuxGroupCtl(tcpMuxHTTPConnectMuxer *tcpmux.HTTPConnectTCPMuxer) *TCPMuxGroupCtl {
return &TCPMuxGroupCtl{
groups: make(map[string]*TCPMuxGroup),
tcpMuxHTTPConnectMuxer: tcpMuxHTTPConnectMuxer,
2020-04-20 13:35:47 +08:00
}
}
2020-05-24 17:48:37 +08:00
// Listen is the wrapper for TCPMuxGroup's Listen
2020-04-20 13:35:47 +08:00
// If there are no group, we will create one here
func (tmgc *TCPMuxGroupCtl) Listen(
ctx context.Context,
multiplexer, group, groupKey string,
routeConfig vhost.RouteConfig,
) (l net.Listener, err error) {
2020-04-20 13:35:47 +08:00
tmgc.mu.Lock()
tcpMuxGroup, ok := tmgc.groups[group]
if !ok {
2020-05-24 17:48:37 +08:00
tcpMuxGroup = NewTCPMuxGroup(tmgc)
2020-04-20 13:35:47 +08:00
tmgc.groups[group] = tcpMuxGroup
}
tmgc.mu.Unlock()
2023-09-20 15:18:50 +08:00
switch v1.TCPMultiplexerType(multiplexer) {
case v1.TCPMultiplexerHTTPConnect:
return tcpMuxGroup.HTTPConnectListen(ctx, group, groupKey, routeConfig)
2020-04-20 13:35:47 +08:00
default:
err = fmt.Errorf("unknown multiplexer [%s]", multiplexer)
return
}
}
2020-05-24 17:48:37 +08:00
// RemoveGroup remove TCPMuxGroup from controller
func (tmgc *TCPMuxGroupCtl) RemoveGroup(group string) {
2020-04-20 13:35:47 +08:00
tmgc.mu.Lock()
defer tmgc.mu.Unlock()
delete(tmgc.groups, group)
}
2020-05-24 17:48:37 +08:00
// TCPMuxGroup route connections to different proxies
type TCPMuxGroup struct {
group string
groupKey string
domain string
routeByHTTPUser string
2023-03-07 19:53:32 +08:00
username string
password string
2020-04-20 13:35:47 +08:00
acceptCh chan net.Conn
tcpMuxLn net.Listener
2020-05-24 17:48:37 +08:00
lns []*TCPMuxGroupListener
ctl *TCPMuxGroupCtl
2020-04-20 13:35:47 +08:00
mu sync.Mutex
}
2020-05-24 17:48:37 +08:00
// NewTCPMuxGroup return a new TCPMuxGroup
func NewTCPMuxGroup(ctl *TCPMuxGroupCtl) *TCPMuxGroup {
return &TCPMuxGroup{
lns: make([]*TCPMuxGroupListener, 0),
2020-04-20 13:35:47 +08:00
ctl: ctl,
acceptCh: make(chan net.Conn),
}
}
2020-05-24 17:48:37 +08:00
// Listen will return a new TCPMuxGroupListener
// if TCPMuxGroup already has a listener, just add a new TCPMuxGroupListener to the queues
2020-04-20 13:35:47 +08:00
// otherwise, listen on the real address
func (tmg *TCPMuxGroup) HTTPConnectListen(
ctx context.Context,
group, groupKey string,
routeConfig vhost.RouteConfig,
) (ln *TCPMuxGroupListener, err error) {
2020-04-20 13:35:47 +08:00
tmg.mu.Lock()
defer tmg.mu.Unlock()
if len(tmg.lns) == 0 {
// the first listener, listen on the real address
tcpMuxLn, errRet := tmg.ctl.tcpMuxHTTPConnectMuxer.Listen(ctx, &routeConfig)
2020-04-20 13:35:47 +08:00
if errRet != nil {
return nil, errRet
}
2020-05-24 17:48:37 +08:00
ln = newTCPMuxGroupListener(group, tmg, tcpMuxLn.Addr())
2020-04-20 13:35:47 +08:00
tmg.group = group
tmg.groupKey = groupKey
tmg.domain = routeConfig.Domain
tmg.routeByHTTPUser = routeConfig.RouteByHTTPUser
2023-03-07 19:53:32 +08:00
tmg.username = routeConfig.Username
tmg.password = routeConfig.Password
2020-04-20 13:35:47 +08:00
tmg.tcpMuxLn = tcpMuxLn
tmg.lns = append(tmg.lns, ln)
if tmg.acceptCh == nil {
tmg.acceptCh = make(chan net.Conn)
}
go tmg.worker()
} else {
// route config in the same group must be equal
2023-03-07 19:53:32 +08:00
if tmg.group != group || tmg.domain != routeConfig.Domain ||
tmg.routeByHTTPUser != routeConfig.RouteByHTTPUser ||
tmg.username != routeConfig.Username ||
tmg.password != routeConfig.Password {
2020-04-20 13:35:47 +08:00
return nil, ErrGroupParamsInvalid
}
if tmg.groupKey != groupKey {
return nil, ErrGroupAuthFailed
}
2020-05-24 17:48:37 +08:00
ln = newTCPMuxGroupListener(group, tmg, tmg.lns[0].Addr())
2020-04-20 13:35:47 +08:00
tmg.lns = append(tmg.lns, ln)
}
return
}
2020-05-24 17:48:37 +08:00
// worker is called when the real TCP listener has been created
func (tmg *TCPMuxGroup) worker() {
2020-04-20 13:35:47 +08:00
for {
c, err := tmg.tcpMuxLn.Accept()
if err != nil {
return
}
err = gerr.PanicToError(func() {
tmg.acceptCh <- c
})
if err != nil {
return
}
}
}
2020-05-24 17:48:37 +08:00
func (tmg *TCPMuxGroup) Accept() <-chan net.Conn {
2020-04-20 13:35:47 +08:00
return tmg.acceptCh
}
2020-05-24 17:48:37 +08:00
// CloseListener remove the TCPMuxGroupListener from the TCPMuxGroup
func (tmg *TCPMuxGroup) CloseListener(ln *TCPMuxGroupListener) {
2020-04-20 13:35:47 +08:00
tmg.mu.Lock()
defer tmg.mu.Unlock()
for i, tmpLn := range tmg.lns {
if tmpLn == ln {
tmg.lns = append(tmg.lns[:i], tmg.lns[i+1:]...)
break
}
}
if len(tmg.lns) == 0 {
close(tmg.acceptCh)
tmg.tcpMuxLn.Close()
tmg.ctl.RemoveGroup(tmg.group)
}
}
2020-05-24 17:48:37 +08:00
// TCPMuxGroupListener
type TCPMuxGroupListener struct {
2020-04-20 13:35:47 +08:00
groupName string
2020-05-24 17:48:37 +08:00
group *TCPMuxGroup
2020-04-20 13:35:47 +08:00
addr net.Addr
closeCh chan struct{}
}
2020-05-24 17:48:37 +08:00
func newTCPMuxGroupListener(name string, group *TCPMuxGroup, addr net.Addr) *TCPMuxGroupListener {
return &TCPMuxGroupListener{
2020-04-20 13:35:47 +08:00
groupName: name,
group: group,
addr: addr,
closeCh: make(chan struct{}),
}
}
2020-05-24 17:48:37 +08:00
// Accept will accept connections from TCPMuxGroup
func (ln *TCPMuxGroupListener) Accept() (c net.Conn, err error) {
2020-04-20 13:35:47 +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
}
}
2020-05-24 17:48:37 +08:00
func (ln *TCPMuxGroupListener) Addr() net.Addr {
2020-04-20 13:35:47 +08:00
return ln.addr
}
// Close close the listener
2020-05-24 17:48:37 +08:00
func (ln *TCPMuxGroupListener) Close() (err error) {
2020-04-20 13:35:47 +08:00
close(ln.closeCh)
// remove self from TcpMuxGroup
ln.group.CloseListener(ln)
return
}