mirror of
https://github.com/fatedier/frp.git
synced 2024-12-13 10:11:04 +01:00
76 lines
1.3 KiB
Go
76 lines
1.3 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
)
|
|
|
|
type ControlManager struct {
|
|
// controls indexed by run id
|
|
ctlsByRunId map[string]*Control
|
|
|
|
mu sync.RWMutex
|
|
}
|
|
|
|
func NewControlManager() *ControlManager {
|
|
return &ControlManager{
|
|
ctlsByRunId: make(map[string]*Control),
|
|
}
|
|
}
|
|
|
|
func (cm *ControlManager) Add(runId string, ctl *Control) (oldCtl *Control) {
|
|
cm.mu.Lock()
|
|
defer cm.mu.Unlock()
|
|
|
|
oldCtl, ok := cm.ctlsByRunId[runId]
|
|
if ok {
|
|
oldCtl.Replaced(ctl)
|
|
}
|
|
cm.ctlsByRunId[runId] = ctl
|
|
return
|
|
}
|
|
|
|
func (cm *ControlManager) GetById(runId string) (ctl *Control, ok bool) {
|
|
cm.mu.RLock()
|
|
defer cm.mu.RUnlock()
|
|
ctl, ok = cm.ctlsByRunId[runId]
|
|
return
|
|
}
|
|
|
|
type ProxyManager struct {
|
|
// proxies indexed by proxy name
|
|
pxys map[string]Proxy
|
|
|
|
mu sync.RWMutex
|
|
}
|
|
|
|
func NewProxyManager() *ProxyManager {
|
|
return &ProxyManager{
|
|
pxys: make(map[string]Proxy),
|
|
}
|
|
}
|
|
|
|
func (pm *ProxyManager) Add(name string, pxy Proxy) error {
|
|
pm.mu.Lock()
|
|
defer pm.mu.Unlock()
|
|
if _, ok := pm.pxys[name]; ok {
|
|
return fmt.Errorf("proxy name [%s] is already in use", name)
|
|
}
|
|
|
|
pm.pxys[name] = pxy
|
|
return nil
|
|
}
|
|
|
|
func (pm *ProxyManager) Del(name string) {
|
|
pm.mu.Lock()
|
|
defer pm.mu.Unlock()
|
|
delete(pm.pxys, name)
|
|
}
|
|
|
|
func (pm *ProxyManager) GetByName(name string) (pxy Proxy, ok bool) {
|
|
pm.mu.RLock()
|
|
defer pm.mu.RUnlock()
|
|
pxy, ok = pm.pxys[name]
|
|
return
|
|
}
|