| // |
| 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 | "net" |
| 19 | "strconv" |
| 20 |
| 21 | "github.com/fatedier/frp/server/ports" |
| 22 | ) |
| 23 |
| 24 | // TCPGroupCtl manages all TCPGroups. |
| 25 | type TCPGroupCtl struct { |
| 26 | groupRegistry[*TCPGroup] |
| 27 | portManager *ports.Manager |
| 28 | } |
| 29 |
| 30 | // NewTCPGroupCtl returns a new TCPGroupCtl. |
| 31 | func NewTCPGroupCtl(portManager *ports.Manager) *TCPGroupCtl { |
| 32 | return &TCPGroupCtl{ |
| 33 | groupRegistry: newGroupRegistry[*TCPGroup](), |
| 34 | portManager: portManager, |
| 35 | } |
| 36 | } |
| 37 |
| 38 | // Listen is the wrapper for TCPGroup's Listen. |
| 39 | // If there is no group, one will be created. |
| 40 | func (tgc *TCPGroupCtl) Listen(proxyName string, group string, groupKey string, |
| 41 | addr string, port int, |
| 42 | ) (l net.Listener, realPort int, err error) { |
| 43 | for { |
| 44 | tcpGroup := tgc.getOrCreate(group, func() *TCPGroup { |
| 45 | return NewTCPGroup(tgc) |
| 46 | }) |
| 47 | l, realPort, err = tcpGroup.Listen(proxyName, group, groupKey, addr, port) |
| 48 | if err == errGroupStale { |
| 49 | continue |
| 50 | } |
| 51 | return |
| 52 | } |
| 53 | } |
| 54 |
| 55 | // TCPGroup routes connections to different proxies. |
| 56 | type TCPGroup struct { |
| 57 | baseGroup |
| 58 |
| 59 | addr string |
| 60 | port int |
| 61 | realPort int |
| 62 | ctl *TCPGroupCtl |
| 63 | } |
| 64 |
| 65 | // NewTCPGroup returns a new TCPGroup. |
| 66 | func NewTCPGroup(ctl *TCPGroupCtl) *TCPGroup { |
| 67 | return &TCPGroup{ |
| 68 | ctl: ctl, |
| 69 | } |
| 70 | } |
| 71 |
| 72 | // Listen will return a new Listener. |
| 73 | // If TCPGroup already has a listener, just add a new Listener to the queues, |
| 74 | // otherwise listen on the real address. |
| 75 | func (tg *TCPGroup) Listen(proxyName string, group string, groupKey string, addr string, port int) (ln *Listener, realPort int, err error) { |
| 76 | tg.mu.Lock() |
| 77 | defer tg.mu.Unlock() |
| 78 | if !tg.ctl.isCurrent(group, func(cur *TCPGroup) bool { return cur == tg }) { |
| 79 | return nil, 0, errGroupStale |
| 80 | } |
| 81 | if len(tg.lns) == 0 { |
| 82 | // the first listener, listen on the real address |
| 83 | realPort, err = tg.ctl.portManager.Acquire(proxyName, port) |
| 84 | if err != nil { |
| 85 | return |
| 86 | } |
| 87 | tcpLn, errRet := net.Listen("tcp", net.JoinHostPort(addr, strconv.Itoa(realPort))) |
| 88 | if errRet != nil { |
| 89 | tg.ctl.portManager.Release(realPort) |
| 90 | err = errRet |
| 91 | return |
| 92 | } |
| 93 |
| 94 | tg.addr = addr |
| 95 | tg.port = port |
| 96 | tg.realPort = realPort |
| 97 | tg.initBase(group, groupKey, tcpLn, func() { |
| 98 | tg.ctl.portManager.Release(tg.realPort) |
| 99 | tg.ctl.removeIf(tg.group, func(cur *TCPGroup) bool { |
| 100 | return cur == tg |
| 101 | }) |
| 102 | }) |
| 103 | ln = tg.newListener(tcpLn.Addr()) |
| 104 | go tg.worker(tcpLn, tg.acceptCh) |
| 105 | } else { |
| 106 | // address and port in the same group must be equal |
| 107 | if tg.group != group || tg.addr != addr { |
| 108 | err = ErrGroupParamsInvalid |
| 109 | return |
| 110 | } |
| 111 | if tg.port != port { |
| 112 | err = ErrGroupDifferentPort |
| 113 | return |
| 114 | } |
| 115 | if tg.groupKey != groupKey { |
| 116 | err = ErrGroupAuthFailed |
| 117 | return |
| 118 | } |
| 119 | ln = tg.newListener(tg.lns[0].Addr()) |
| 120 | realPort = tg.realPort |
| 121 | } |
| 122 | return |
| 123 | } |