| // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | package group |
| 16 |
| 17 | import ( |
| 18 | "context" |
| 19 | "fmt" |
| 20 | "net" |
| 21 |
| 22 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 23 | "github.com/fatedier/frp/pkg/util/tcpmux" |
| 24 | "github.com/fatedier/frp/pkg/util/vhost" |
| 25 | ) |
| 26 |
| 27 | // TCPMuxGroupCtl manages all TCPMuxGroups. |
| 28 | type TCPMuxGroupCtl struct { |
| 29 | groupRegistry[*TCPMuxGroup] |
| 30 | tcpMuxHTTPConnectMuxer *tcpmux.HTTPConnectTCPMuxer |
| 31 | } |
| 32 |
| 33 | // NewTCPMuxGroupCtl returns a new TCPMuxGroupCtl. |
| 34 | func NewTCPMuxGroupCtl(tcpMuxHTTPConnectMuxer *tcpmux.HTTPConnectTCPMuxer) *TCPMuxGroupCtl { |
| 35 | return &TCPMuxGroupCtl{ |
| 36 | groupRegistry: newGroupRegistry[*TCPMuxGroup](), |
| 37 | tcpMuxHTTPConnectMuxer: tcpMuxHTTPConnectMuxer, |
| 38 | } |
| 39 | } |
| 40 |
| 41 | // Listen is the wrapper for TCPMuxGroup's Listen. |
| 42 | // If there is no group, one will be created. |
| 43 | func (tmgc *TCPMuxGroupCtl) Listen( |
| 44 | ctx context.Context, |
| 45 | multiplexer, group, groupKey string, |
| 46 | routeConfig vhost.RouteConfig, |
| 47 | ) (l net.Listener, err error) { |
| 48 | for { |
| 49 | tcpMuxGroup := tmgc.getOrCreate(group, func() *TCPMuxGroup { |
| 50 | return NewTCPMuxGroup(tmgc) |
| 51 | }) |
| 52 |
| 53 | switch v1.TCPMultiplexerType(multiplexer) { |
| 54 | case v1.TCPMultiplexerHTTPConnect: |
| 55 | l, err = tcpMuxGroup.HTTPConnectListen(ctx, group, groupKey, routeConfig) |
| 56 | if err == errGroupStale { |
| 57 | continue |
| 58 | } |
| 59 | return |
| 60 | default: |
| 61 | return nil, fmt.Errorf("unknown multiplexer [%s]", multiplexer) |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 |
| 66 | // TCPMuxGroup routes connections to different proxies. |
| 67 | type TCPMuxGroup struct { |
| 68 | baseGroup |
| 69 |
| 70 | domain string |
| 71 | routeByHTTPUser string |
| 72 | username string |
| 73 | password string |
| 74 | ctl *TCPMuxGroupCtl |
| 75 | } |
| 76 |
| 77 | // NewTCPMuxGroup returns a new TCPMuxGroup. |
| 78 | func NewTCPMuxGroup(ctl *TCPMuxGroupCtl) *TCPMuxGroup { |
| 79 | return &TCPMuxGroup{ |
| 80 | ctl: ctl, |
| 81 | } |
| 82 | } |
| 83 |
| 84 | // HTTPConnectListen will return a new Listener. |
| 85 | // If TCPMuxGroup already has a listener, just add a new Listener to the queues, |
| 86 | // otherwise listen on the real address. |
| 87 | func (tmg *TCPMuxGroup) HTTPConnectListen( |
| 88 | ctx context.Context, |
| 89 | group, groupKey string, |
| 90 | routeConfig vhost.RouteConfig, |
| 91 | ) (ln *Listener, err error) { |
| 92 | tmg.mu.Lock() |
| 93 | defer tmg.mu.Unlock() |
| 94 | if !tmg.ctl.isCurrent(group, func(cur *TCPMuxGroup) bool { return cur == tmg }) { |
| 95 | return nil, errGroupStale |
| 96 | } |
| 97 | if len(tmg.lns) == 0 { |
| 98 | // the first listener, listen on the real address |
| 99 | tcpMuxLn, errRet := tmg.ctl.tcpMuxHTTPConnectMuxer.Listen(ctx, &routeConfig) |
| 100 | if errRet != nil { |
| 101 | return nil, errRet |
| 102 | } |
| 103 |
| 104 | tmg.domain = routeConfig.Domain |
| 105 | tmg.routeByHTTPUser = routeConfig.RouteByHTTPUser |
| 106 | tmg.username = routeConfig.Username |
| 107 | tmg.password = routeConfig.Password |
| 108 | tmg.initBase(group, groupKey, tcpMuxLn, func() { |
| 109 | tmg.ctl.removeIf(tmg.group, func(cur *TCPMuxGroup) bool { |
| 110 | return cur == tmg |
| 111 | }) |
| 112 | }) |
| 113 | ln = tmg.newListener(tcpMuxLn.Addr()) |
| 114 | go tmg.worker(tcpMuxLn, tmg.acceptCh) |
| 115 | } else { |
| 116 | // route config in the same group must be equal |
| 117 | if tmg.group != group || tmg.domain != routeConfig.Domain || |
| 118 | tmg.routeByHTTPUser != routeConfig.RouteByHTTPUser || |
| 119 | tmg.username != routeConfig.Username || |
| 120 | tmg.password != routeConfig.Password { |
| 121 | return nil, ErrGroupParamsInvalid |
| 122 | } |
| 123 | if tmg.groupKey != groupKey { |
| 124 | return nil, ErrGroupAuthFailed |
| 125 | } |
| 126 | ln = tmg.newListener(tmg.lns[0].Addr()) |
| 127 | } |
| 128 | return |
| 129 | } |