-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.go
124 lines (114 loc) · 2.6 KB
/
manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"errors"
"log"
"sync"
)
var proxyManager Manager
func init() {
proxyManager = newProxyManager()
}
func newProxyManager() Manager {
return Manager{
Mutex: sync.RWMutex{},
ProxyMap: make(map[string]*UDPProxy),
PortsOccupied: make(map[int]bool),
}
}
func (m *Manager) Add(pr ProxyRecord) (bool, error) {
m.Mutex.RLock()
// check if already exists on same port
if _, ok := m.PortsOccupied[pr.Port]; ok {
// check if proxy is already present
n := pr.String()
if _, ok := m.ProxyMap[n]; ok {
return true, nil
} else {
return false, errors.New("port already occupied")
}
}
m.Mutex.RUnlock()
// create a new proxy
m.Mutex.Lock()
defer m.Mutex.Unlock()
p, err := NewUDPProxy(pr.Port, pr.TargetPort, pr.Service)
if err != nil {
return false, errors.New("failed to create proxy")
}
// add the proxy to the map
m.ProxyMap[pr.String()] = p
m.PortsOccupied[pr.Port] = true
// store record in file
ll := UDPProxyToRecordList(m.ProxyMap)
err = StoreRecordsInFile(ll)
if err != nil {
delete(m.ProxyMap, pr.String())
return false, errors.New("failed to update records")
}
// start the proxy
go p.Run()
// return success
return true, nil
}
func (m *Manager) Remove(pr ProxyRecord) (bool, error) {
// handle panic
defer func() {
if r := recover(); r != nil {
log.Println("Recovered from panic in remove:", r)
return
}
}()
m.Mutex.RLock()
// check if the proxy exists
if _, ok := m.ProxyMap[pr.String()]; !ok {
m.Mutex.RUnlock()
return true, nil
}
m.Mutex.RUnlock()
m.Mutex.Lock()
defer m.Mutex.Unlock()
// close the proxy
m.ProxyMap[pr.String()].Close()
// remove the proxy from the map
delete(m.ProxyMap, pr.String())
// remove the port from the occupied ports
delete(m.PortsOccupied, pr.Port)
// store record in file
ll := UDPProxyToRecordList(m.ProxyMap)
err := StoreRecordsInFile(ll)
if err != nil {
return false, errors.New("failed to update records")
}
// return success
return true, nil
}
func (m *Manager) Exist(pr ProxyRecord) bool {
m.Mutex.RLock()
// check if the proxy exists
_, ok := m.ProxyMap[pr.String()]
m.Mutex.RUnlock()
return ok
}
func (m *Manager) PortOccupied(port int) bool {
m.Mutex.RLock()
// check if the port is occupied
_, ok := m.PortsOccupied[port]
m.Mutex.RUnlock()
return ok
}
func (m *Manager) List() []string {
m.Mutex.RLock()
defer m.Mutex.RUnlock()
var list []string = make([]string, 0, len(m.ProxyMap))
for k := range m.ProxyMap {
list = append(list, k)
}
return list
}
func (m *Manager) Close() {
m.Mutex.Lock()
defer m.Mutex.Unlock()
for _, p := range m.ProxyMap {
p.Close()
}
}