-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.go
102 lines (80 loc) · 1.56 KB
/
map.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
package main
import (
"net"
"strconv"
"strings"
"sync"
)
type fromAddr string
func (f fromAddr) parse() (proto, addr string) {
from := string(f)
// Split the string by "://"
parts := strings.Split(from, "://")
if len(parts) != 2 {
return "", ""
}
if parts[0] == "" {
return "", ""
}
// Return the first and second parts
return parts[0], parts[1]
}
func (f fromAddr) build(proto, addr string) string {
return proto + "://" + addr
}
type used map[string]bool
func (u used) add(s string) {
u[s] = true
}
func (u used) has(s string) bool {
return u[s]
}
type fwdMap struct {
mu sync.RWMutex
data map[fromAddr]net.Addr
ports used
}
func newFwdMap() *fwdMap {
return &fwdMap{
data: make(map[fromAddr]net.Addr),
ports: make(used),
}
}
func (m *fwdMap) add(from fromAddr, to net.Addr) {
m.mu.Lock()
defer m.mu.Unlock()
m.data[from] = to
}
func (m *fwdMap) get(from fromAddr) (net.Addr, bool) {
m.mu.RLock()
defer m.mu.RUnlock()
to, exists := m.data[from]
return to, exists
}
func (m *fwdMap) addPort(port string) {
m.mu.Lock()
defer m.mu.Unlock()
m.ports.add(port)
}
func (m *fwdMap) hasPort(port string) bool {
m.mu.RLock()
defer m.mu.RUnlock()
return m.ports.has(port)
}
func (m *fwdMap) findFreePort() string {
m.mu.Lock()
defer m.mu.Unlock()
for port := 30000; port < 40000; port++ {
portStr := strconv.Itoa(port)
if _, exists := m.ports[portStr]; exists {
continue
}
n, err := net.Listen("tcp", "localhost:"+portStr)
if err == nil {
_ = n.Close()
m.ports.add(portStr)
return portStr
}
}
return ""
}