-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.go
168 lines (138 loc) · 3.25 KB
/
main.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"encoding/csv"
"fmt"
"io"
"log"
"net"
"os"
"sync"
"time"
)
type ForwardRule struct {
SourcePort int
Targets []Target
mu sync.RWMutex
bestTarget *Target
}
type Target struct {
IP string
Datacenter string
}
func main() {
rules, err := readRulesFromCSV("ip.csv")
if err != nil {
log.Fatalf("Error reading rules: %v", err)
}
for datacenter, rule := range rules {
go startForwarding(datacenter, rule)
}
select {}
}
func readRulesFromCSV(filename string) (map[string]*ForwardRule, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
reader := csv.NewReader(file)
records, err := reader.ReadAll()
if err != nil {
return nil, err
}
rules := make(map[string]*ForwardRule)
for _, record := range records[1:] { // Skip header
ip := record[0]
datacenter := record[1]
target := Target{
IP: ip,
Datacenter: datacenter,
}
if rule, ok := rules[datacenter]; ok {
rule.Targets = append(rule.Targets, target)
} else {
rules[datacenter] = &ForwardRule{
SourcePort: 10000 + len(rules),
Targets: []Target{target},
}
}
}
return rules, nil
}
func startForwarding(datacenter string, rule *ForwardRule) {
go updateBestTarget(rule)
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", rule.SourcePort))
if err != nil {
log.Printf("Error listening on port %d for %s: %v", rule.SourcePort, datacenter, err)
return
}
defer listener.Close()
log.Printf("Forwarding from :%d for datacenter %s", rule.SourcePort, datacenter)
for {
conn, err := listener.Accept()
if err != nil {
log.Printf("Error accepting connection for %s: %v", datacenter, err)
continue
}
go handleConnection(conn, rule)
}
}
func updateBestTarget(rule *ForwardRule) {
ticker := time.NewTicker(1 * time.Minute)
defer ticker.Stop()
for {
bestTarget := selectTarget(rule.Targets)
rule.mu.Lock()
rule.bestTarget = &bestTarget
rule.mu.Unlock()
<-ticker.C
}
}
func handleConnection(source net.Conn, rule *ForwardRule) {
defer source.Close()
rule.mu.RLock()
target := rule.bestTarget
rule.mu.RUnlock()
if target == nil {
log.Printf("No available target")
return
}
destination, err := net.Dial("tcp", fmt.Sprintf("%s:443", target.IP))
if err != nil {
log.Printf("Error connecting to target %s:443: %v", target.IP, err)
return
}
defer destination.Close()
// Use io.Copy for bidirectional data transfer
go func() {
_, err := io.Copy(destination, source)
if err != nil {
log.Printf("Error copying data from source to target: %v", err)
}
}()
_, err = io.Copy(source, destination)
if err != nil {
log.Printf("Error copying data from target to source: %v", err)
}
}
func selectTarget(targets []Target) Target {
var bestTarget Target
lowestLatency := time.Hour
for _, target := range targets {
latency := measureLatency(target.IP)
if latency < lowestLatency {
lowestLatency = latency
bestTarget = target
}
}
return bestTarget
}
func measureLatency(ip string) time.Duration {
start := time.Now()
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:80", ip), 5*time.Second)
if err != nil {
return time.Hour // Return a high latency if the connection fails
}
defer conn.Close()
return time.Since(start)
}