@@ -7,7 +7,7 @@ package netcheck
7
7
import (
8
8
"bufio"
9
9
"context"
10
- "crypto/tls "
10
+ "encoding/hex "
11
11
"errors"
12
12
"fmt"
13
13
"io"
@@ -678,7 +678,9 @@ func (rs *reportState) addNodeLatency(node *tailcfg.DERPNode, ipp netip.AddrPort
678
678
ret := rs .report
679
679
680
680
ret .UDP = true
681
- updateLatency (ret .RegionLatency , node .RegionID , d )
681
+
682
+ // Coder: don't actually store the latency.
683
+ //updateLatency(ret.RegionLatency, node.RegionID, d)
682
684
683
685
// Once we've heard from enough regions (3), start a timer to
684
686
// give up on the other ones. The timer's duration is a
@@ -696,13 +698,13 @@ func (rs *reportState) addNodeLatency(node *tailcfg.DERPNode, ipp netip.AddrPort
696
698
697
699
switch {
698
700
case ipp .Addr ().Is6 ():
699
- updateLatency (ret .RegionV6Latency , node .RegionID , d )
701
+ // updateLatency(ret.RegionV6Latency, node.RegionID, d)
700
702
ret .IPv6 = true
701
703
ret .GlobalV6 = ipPortStr
702
704
// TODO: track MappingVariesByDestIP for IPv6
703
705
// too? Would be sad if so, but who knows.
704
706
case ipp .Addr ().Is4 ():
705
- updateLatency (ret .RegionV4Latency , node .RegionID , d )
707
+ // updateLatency(ret.RegionV4Latency, node.RegionID, d)
706
708
ret .IPv4 = true
707
709
if rs .gotEP4 == "" {
708
710
rs .gotEP4 = ipPortStr
@@ -1016,7 +1018,9 @@ func (c *Client) GetReport(ctx context.Context, dm *tailcfg.DERPMap) (_ *Report,
1016
1018
// Try HTTPS and ICMP latency check if all STUN probes failed due to
1017
1019
// UDP presumably being blocked.
1018
1020
// TODO: this should be moved into the probePlan, using probeProto probeHTTPS.
1019
- if ! rs .anyUDP () && ctx .Err () == nil {
1021
+ // Coder: always run this because we don't store STUN latency in the report.
1022
+ //if !rs.anyUDP() && ctx.Err() == nil {
1023
+ if ctx .Err () == nil {
1020
1024
var wg sync.WaitGroup
1021
1025
var need []* tailcfg.DERPRegion
1022
1026
for rid , reg := range dm .Regions {
@@ -1039,13 +1043,14 @@ func (c *Client) GetReport(ctx context.Context, dm *tailcfg.DERPMap) (_ *Report,
1039
1043
}()
1040
1044
1041
1045
wg .Add (len (need ))
1042
- c .logf ("netcheck: UDP is blocked, trying HTTPS" )
1046
+ // Coder: this is misleading because we always log it.
1047
+ //c.logf("netcheck: UDP is blocked, trying HTTPS")
1043
1048
}
1044
1049
for _ , reg := range need {
1045
1050
go func (reg * tailcfg.DERPRegion ) {
1046
1051
defer wg .Done ()
1047
- if d , ip , err := c .measureHTTPSLatency (ctx , reg ); err != nil {
1048
- c .logf ("[v1] netcheck: measuring HTTPS latency of %v (%d): %v" , reg .RegionCode , reg .RegionID , err )
1052
+ if d , ip , err := c .measureHTTPLatency (ctx , reg ); err != nil {
1053
+ c .logf ("[v1] netcheck: measuring HTTP(S) latency of %v (%d): %v" , reg .RegionCode , reg .RegionID , err )
1049
1054
} else {
1050
1055
rs .mu .Lock ()
1051
1056
if l , ok := rs .report .RegionLatency [reg .RegionID ]; ! ok {
@@ -1216,7 +1221,9 @@ func (c *Client) runHTTPOnlyChecks(ctx context.Context, last *Report, rs *report
1216
1221
return nil
1217
1222
}
1218
1223
1219
- func (c * Client ) measureHTTPSLatency (ctx context.Context , reg * tailcfg.DERPRegion ) (time.Duration , netip.Addr , error ) {
1224
+ // measureHTTPLatency measures the latency to the given DERP region over HTTP
1225
+ // or HTTPS.
1226
+ func (c * Client ) measureHTTPLatency (ctx context.Context , reg * tailcfg.DERPRegion ) (time.Duration , netip.Addr , error ) {
1220
1227
metricHTTPSend .Add (1 )
1221
1228
var result httpstat.Result
1222
1229
ctx , cancel := context .WithTimeout (httpstat .WithHTTPStat (ctx , & result ), overallProbeTimeout )
@@ -1227,28 +1234,60 @@ func (c *Client) measureHTTPSLatency(ctx context.Context, reg *tailcfg.DERPRegio
1227
1234
dc := derphttp .NewNetcheckClient (c .logf )
1228
1235
defer dc .Close ()
1229
1236
1230
- tlsConn , tcpConn , node , err := dc .DialRegionTLS (ctx , reg )
1237
+ var hasForceHTTPNode = false
1238
+ for _ , node := range reg .Nodes {
1239
+ if node .STUNOnly {
1240
+ continue
1241
+ }
1242
+ if node .ForceHTTP {
1243
+ hasForceHTTPNode = true
1244
+ break
1245
+ }
1246
+ }
1247
+
1248
+ var (
1249
+ conn net.Conn
1250
+ closer io.Closer
1251
+ node * tailcfg.DERPNode
1252
+ err error
1253
+ )
1254
+ if hasForceHTTPNode {
1255
+ conn , node , err = dc .DialRegion (ctx , reg )
1256
+ closer = conn
1257
+ } else {
1258
+ conn , closer , node , err = dc .DialRegionTLS (ctx , reg )
1259
+ }
1231
1260
if err != nil {
1232
1261
return 0 , ip , err
1233
1262
}
1234
- defer tcpConn .Close ()
1235
-
1236
- if ta , ok := tlsConn .RemoteAddr ().(* net.TCPAddr ); ok {
1263
+ defer closer .Close ()
1264
+ if ta , ok := conn .RemoteAddr ().(* net.TCPAddr ); ok {
1237
1265
ip , _ = netip .AddrFromSlice (ta .IP )
1238
1266
ip = ip .Unmap ()
1239
1267
}
1240
1268
if ip == (netip.Addr {}) {
1241
- return 0 , ip , fmt .Errorf ("no unexpected RemoteAddr %#v" , tlsConn .RemoteAddr ())
1269
+ return 0 , ip , fmt .Errorf ("no unexpected RemoteAddr %#v" , conn .RemoteAddr ())
1242
1270
}
1243
1271
1244
- connc := make (chan * tls .Conn , 1 )
1245
- connc <- tlsConn
1272
+ connc := make (chan net .Conn , 1 )
1273
+ connc <- conn
1246
1274
1247
1275
tr := & http.Transport {
1248
1276
DialContext : func (ctx context.Context , network , addr string ) (net.Conn , error ) {
1249
- return nil , errors .New ("unexpected DialContext dial" )
1277
+ if ! hasForceHTTPNode {
1278
+ return nil , errors .New ("unexpected DialContext dial" )
1279
+ }
1280
+ select {
1281
+ case nc := <- connc :
1282
+ return nc , nil
1283
+ default :
1284
+ return nil , errors .New ("only one conn expected" )
1285
+ }
1250
1286
},
1251
1287
DialTLSContext : func (ctx context.Context , network , addr string ) (net.Conn , error ) {
1288
+ if hasForceHTTPNode {
1289
+ return nil , errors .New ("unexpected DialTLSContext dial" )
1290
+ }
1252
1291
select {
1253
1292
case nc := <- connc :
1254
1293
return nc , nil
@@ -1530,6 +1569,12 @@ func (rs *reportState) runProbe(ctx context.Context, dm *tailcfg.DERPMap, probe
1530
1569
return
1531
1570
}
1532
1571
1572
+ // Coder: The address below won't be valid if the node doesn't have a
1573
+ // STUNPort.
1574
+ if node .STUNPort < 1 {
1575
+ return
1576
+ }
1577
+
1533
1578
addr := c .nodeAddr (ctx , node , probe .proto )
1534
1579
if ! addr .IsValid () {
1535
1580
return
@@ -1542,7 +1587,16 @@ func (rs *reportState) runProbe(ctx context.Context, dm *tailcfg.DERPMap, probe
1542
1587
1543
1588
rs .mu .Lock ()
1544
1589
rs .inFlight [txID ] = func (ipp netip.AddrPort ) {
1590
+ // Coder: we don't want to store the latency of STUN netchecks because
1591
+ // Coder doesn't contain a built-in STUN server and customers often use
1592
+ // Google STUN which has extremely low latency everywhere on the planet.
1593
+ // This means that latency checks to any regions containing the Google
1594
+ // STUN server will always muddy that region's latency results.
1595
+ //
1596
+ // rs.addNodeLatency has been updated to not store latency but will
1597
+ // still set the approapriate values in the report.
1545
1598
rs .addNodeLatency (node , ipp , time .Since (sent ))
1599
+ c .logf ("netcheck.runProbe: got STUN response for %s from %s (%s) in %s" , node .Name , ipp .String (), hex .EncodeToString (txID [:]), time .Since (sent ).String ())
1546
1600
cancelSet () // abort other nodes in this set
1547
1601
}
1548
1602
rs .mu .Unlock ()
0 commit comments