-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-connectivity.go
283 lines (260 loc) · 8.13 KB
/
test-connectivity.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Copyright 2023 Jigsaw Operations LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"errors"
"io"
"log"
"net"
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"
"github.com/Jigsaw-Code/outline-sdk/dns"
"github.com/Jigsaw-Code/outline-sdk/transport"
"github.com/Jigsaw-Code/outline-sdk/x/config"
"github.com/Jigsaw-Code/outline-sdk/x/connectivity"
"github.com/Jigsaw-Code/outline-sdk/x/report"
)
var debugLog log.Logger = *log.New(io.Discard, "", 0)
// debugLog = *log.New(os.Stderr, "", log.LstdFlags|log.Lmicroseconds|log.Lshortfile)
// var errorLog log.Logger = *log.New(os.Stderr, "[ERROR] ", log.LstdFlags|log.Lmicroseconds|log.Lshortfile)
type connectivityReport struct {
// Inputs
Resolver string `json:"resolver"`
Proto string `json:"proto"`
Transport string `json:"transport"`
// Observations
Time time.Time `json:"time"`
DurationMs int64 `json:"duration_ms"`
Error *errorJSON `json:"error"`
Collected bool `json:"collected"`
}
type errorJSON struct {
Op string `json:"op,omitempty"`
PosixError string `json:"posix_error,omitempty"`
Msg string `json:"msg,omitempty"`
}
func makeErrorRecord(result *connectivity.ConnectivityError) *errorJSON {
if result == nil {
return nil
}
var record = new(errorJSON)
record.Op = result.Op
record.PosixError = result.PosixError
record.Msg = unwrapAll(result.Err).Error()
return record
}
func unwrapAll(err error) error {
for {
unwrapped := errors.Unwrap(err)
if unwrapped == nil {
return err
}
err = unwrapped
}
}
func (r connectivityReport) IsSuccess() bool {
if r.Error == nil {
return true
} else {
return false
}
}
func TestConfigs(setting *AppSettings) {
var wg sync.WaitGroup // Step 1: Create a WaitGroup instance
for i := range setting.Configs {
wg.Add(1) // Increment the WaitGroup counter
go func(i int) { // Step 2: Launch a goroutine
defer wg.Done() // Step 3: Decrement the counter when the goroutine completes
TestSingleConfig(setting, i)
}(i)
}
wg.Wait() // Step 4: Wait for all goroutines to complete
}
func TestSingleConfig(setting *AppSettings, i int) {
var wg sync.WaitGroup
var healthlyMutex sync.Mutex
var healthly []bool
protocols := []string{"tcp", "udp"}
// check if i is within the range of the slice
if i < 0 || i >= len(setting.Configs) {
log.Fatalf("Index %v is out of range", i)
}
cnf := &setting.Configs[i]
c, err := config.SanitizeConfig(cnf.Transport)
if err != nil {
log.Fatalf("Failed to sanitize config: %v", err)
}
configparser := config.NewDefaultConfigParser()
// Clear previous test reports
// maybe make it atomic to prevent losing previous reports if test fails for any reason
// In other words, only clear the reports if the test is fully perfomed....
cnf.TestReports = []*connectivityReport{}
resolverHost := strings.TrimSpace(setting.ResolverHost)
resolverAddress := net.JoinHostPort(resolverHost, "53")
for _, proto := range protocols {
wg.Add(1)
go func(proto string, resolverAddress string) {
defer wg.Done()
var r connectivityReport
var resolver dns.Resolver
r.Transport = c
r.Resolver = resolverAddress
startTime := time.Now()
switch proto {
case "tcp":
streamDialer, err := configparser.WrapStreamDialer(&transport.TCPDialer{}, cnf.Transport)
r.Proto = "tcp"
log.Printf("testing for protocol: %v", r.Proto)
if err != nil {
log.Printf("Failed to create StreamDialer: %v", err)
r.Time = startTime.UTC().Truncate(time.Second)
r.DurationMs = time.Duration(0).Milliseconds()
r.Error = &errorJSON{Msg: err.Error()}
cnf.TestReports = append(cnf.TestReports, &r)
return
}
resolver = dns.NewTCPResolver(streamDialer, resolverAddress)
case "udp":
packetDialer, err := configparser.WrapPacketDialer(&transport.UDPDialer{}, cnf.Transport)
r.Proto = "udp"
log.Printf("testing for protocol: %v", r.Proto)
if err != nil {
log.Printf("Failed to create StreamDialer: %v", err)
r.Time = startTime.UTC().Truncate(time.Second)
r.DurationMs = time.Duration(0).Milliseconds()
r.Error = &errorJSON{Msg: err.Error()}
cnf.TestReports = append(cnf.TestReports, &r)
return
}
resolver = dns.NewUDPResolver(packetDialer, resolverAddress)
default:
log.Fatalf(`Invalid proto %v. Must be "tcp" or "udp"`, proto)
}
result, err := connectivity.TestConnectivityWithResolver(context.Background(), resolver, setting.Domain)
r.Time = startTime.UTC().Truncate(time.Second)
r.DurationMs = time.Duration(0).Milliseconds()
if err != nil {
log.Fatalf("Connectivity test failed to run: %v", err)
r.Error = &errorJSON{Msg: err.Error()}
cnf.TestReports = append(cnf.TestReports, &r)
return
}
r.Error = makeErrorRecord(result)
//log.Printf("Connectivity test result: %v", r)
// collectReport(r, "")
cnf.TestReports = append(cnf.TestReports, &r)
healthlyMutex.Lock()
healthly = append(healthly, r.IsSuccess())
healthlyMutex.Unlock()
}(proto, resolverAddress)
}
wg.Wait()
cnf.Health = CheckHealth(healthly)
}
func submitReports(setting *AppSettings) {
log.Println("Submitting all reports...")
var wg sync.WaitGroup // Create a WaitGroup instance
for i := range setting.Configs {
wg.Add(1)
sumbitOneReport(setting, i)
}
wg.Wait() // Wait for all goroutines to complete
}
func sumbitOneReport(setting *AppSettings, i int) {
var wg sync.WaitGroup
reporterURL := setting.ReporterURL
log.Printf("Reporter URL: %v", reporterURL)
c := setting.Configs[i]
log.Printf("Config: %v", c)
for j := range c.TestReports {
wg.Add(1) // Increment the WaitGroup counter
go func(r *connectivityReport) { // Launch a goroutine
defer wg.Done() // Decrement the counter when the goroutine completes
err := collectReport(r, reporterURL)
if err != nil {
debugLog.Printf("Failed to collect report: %v\n", err)
r.Collected = false
return
}
log.Println("Report collected successfully")
r.Collected = true
log.Printf("Collecting report: %v", r)
}(c.TestReports[j])
}
wg.Wait() // Wait for all goroutines to complete
}
func collectReport(r report.Report, reporterURL string) error {
var reportCollector report.Collector
if strings.TrimSpace(reporterURL) != "" {
log.Println("URL is not empty, using remote collector...")
collectorURL, err := url.Parse(reporterURL)
if err != nil {
debugLog.Printf("Failed to parse collector URL: %v", err)
}
remoteCollector := &report.RemoteCollector{
CollectorURL: collectorURL,
HttpClient: &http.Client{Timeout: 10 * time.Second},
}
retryCollector := &report.RetryCollector{
Collector: remoteCollector,
MaxRetry: 3,
InitialDelay: 1 * time.Second,
}
reportCollector = &report.SamplingCollector{
Collector: retryCollector,
SuccessFraction: 1,
FailureFraction: 1,
}
} else {
log.Println("No collector URL provided, writing to stdout")
reportCollector = &report.WriteCollector{Writer: os.Stdout}
}
log.Println("Collecting report...")
err := reportCollector.Collect(context.Background(), r)
if err != nil {
return err
}
return nil
}
// CheckHealth takes a slice of booleans and returns:
// 1 if all elements are true (all tests have passed),
// 3 if all elements are false (all tests have failed),
// 2 if there is a mix of true and false (some have failed).
func CheckHealth(slice []bool) int {
if len(slice) == 0 {
return 0
}
allTrue := true
allFalse := true
for _, value := range slice {
if value {
allFalse = false
} else {
allTrue = false
}
// Early exit if we already know there's a mix
if !allTrue && !allFalse {
return 2
}
}
if allTrue {
return 1
}
return 3
}