Merge branch 'main' into v2

* main:
  chore(fs): speed up case normalization (#10013)
  chore(config): remove discontinued secondary STUN servers (fixes #10011) (#10012)
  chore(gui, man, authors): update docs, translations, and contributors
  fix(stun): better error handling (ref #10008) (#10010)
  fix(config): remove discontinued primary STUN server (fixes #10008) (#10009)
  fix(gui): validate device ID in canonical form (fixes #7291) (#10006)
This commit is contained in:
Jakob Borg
2025-04-01 13:43:33 +02:00
16 changed files with 135 additions and 67 deletions
+2 -5
View File
@@ -71,22 +71,19 @@ var (
// DefaultPrimaryStunServers are servers provided by us (to avoid causing the public servers burden)
DefaultPrimaryStunServers = []string{
"stun.syncthing.net:3478",
// Discontinued because of misuse. See https://forum.syncthing.net/t/stun-server-misuse/23319
//"stun.syncthing.net:3478",
}
DefaultSecondaryStunServers = []string{
"stun.callwithus.com:3478",
"stun.counterpath.com:3478",
"stun.counterpath.net:3478",
"stun.ekiga.net:3478",
"stun.hitv.com:3478",
"stun.ideasip.com:3478",
"stun.internetcalls.com:3478",
"stun.miwifi.com:3478",
"stun.schlund.de:3478",
"stun.sipgate.net:10000",
"stun.sipgate.net:3478",
"stun.voip.aebc.com:3478",
"stun.voiparound.com:3478",
"stun.voipbuster.com:3478",
"stun.voipstunt.com:3478",
"stun.xten.com:3478",
+57 -4
View File
@@ -17,6 +17,52 @@ import (
// UnicodeLowercaseNormalized returns the Unicode lower case variant of s,
// having also normalized it to normalization form C.
func UnicodeLowercaseNormalized(s string) string {
if isASCII, isLower := isASCII(s); isASCII {
if isLower {
return s
}
return toLowerASCII(s)
}
return toLowerUnicode(s)
}
func isASCII(s string) (bool, bool) {
isLower := true
for _, b := range []byte(s) {
if b > unicode.MaxASCII {
return false, isLower
}
if 'A' <= b && b <= 'Z' {
isLower = false
}
}
return true, isLower
}
func toLowerASCII(s string) string {
var (
b strings.Builder
pos int
)
b.Grow(len(s))
for i, c := range []byte(s) {
if c < 'A' || 'Z' < c {
continue
}
if pos < i {
b.WriteString(s[pos:i])
}
pos = i + 1
b.WriteByte(c + 'a' - 'A')
}
if pos != len(s) {
b.WriteString(s[pos:])
}
return b.String()
}
func toLowerUnicode(s string) string {
i := firstCaseChange(s)
if i == -1 {
return norm.NFC.String(s)
@@ -30,7 +76,11 @@ func UnicodeLowercaseNormalized(s string) string {
rs.WriteString(s[:i])
for _, r := range s[i:] {
rs.WriteRune(unicode.ToLower(unicode.ToUpper(r)))
if r <= unicode.MaxLatin1 && r != 'µ' {
rs.WriteRune(unicode.ToLower(r))
} else {
rs.WriteRune(unicode.To(unicode.LowerCase, unicode.To(unicode.UpperCase, r)))
}
}
return norm.NFC.String(rs.String())
}
@@ -38,10 +88,13 @@ func UnicodeLowercaseNormalized(s string) string {
// Byte index of the first rune r s.t. lower(upper(r)) != r.
func firstCaseChange(s string) int {
for i, r := range s {
if r <= unicode.MaxASCII && (r < 'A' || r > 'Z') {
continue
if r <= unicode.MaxASCII {
if r < 'A' || r > 'Z' {
continue
}
return i
}
if unicode.ToLower(unicode.ToUpper(r)) != r {
if unicode.To(unicode.LowerCase, unicode.To(unicode.UpperCase, r)) != r {
return i
}
}
+20 -17
View File
@@ -49,6 +49,18 @@ var caseCases = [][2]string{
{"a\xCC\x88", "\xC3\xA4"}, // ä
}
var benchmarkCases = [][2]string{
{"img_202401241010.jpg", "ASCII lowercase"},
{"IMG_202401241010.jpg", "ASCII mixedcase start"},
{"img_202401241010.JPG", "ASCII mixedcase end"},
{"wir_kinder_aus_bullerbü.epub", "Latin1 lowercase"},
{"Wir_Kinder_aus_Bullerbü.epub", "Latin1 mixedcase start"},
{"wir_kinder_aus_bullerbü.EPUB", "Latin1 mixedcase end"},
{"translated_ウェブの国際化.html", "Unicode lowercase"},
{"Translated_ウェブの国際化.html", "Unicode mixedcase start"},
{"translated_ウェブの国際化.HTML", "Unicode mixedcase end"},
}
func TestUnicodeLowercaseNormalized(t *testing.T) {
for _, tc := range caseCases {
res := UnicodeLowercaseNormalized(tc[0])
@@ -58,22 +70,13 @@ func TestUnicodeLowercaseNormalized(t *testing.T) {
}
}
func BenchmarkUnicodeLowercaseMaybeChange(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for _, s := range caseCases {
UnicodeLowercaseNormalized(s[0])
}
}
}
func BenchmarkUnicodeLowercaseNoChange(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for _, s := range caseCases {
UnicodeLowercaseNormalized(s[1])
}
func BenchmarkUnicodeLowercase(b *testing.B) {
for _, c := range benchmarkCases {
b.Run(c[1], func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
UnicodeLowercaseNormalized(c[0])
}
})
}
}
+25 -23
View File
@@ -8,6 +8,8 @@ package stun
import (
"context"
"errors"
"fmt"
"net"
"time"
@@ -38,6 +40,8 @@ const (
NATSymmetricUDPFirewall = stun.NATSymmetricUDPFirewall
)
var errNotPunchable = errors.New("not punchable")
type Subscriber interface {
OnNATTypeChanged(natType NATType)
OnExternalAddressChanged(address *Host, via string)
@@ -110,10 +114,11 @@ func (s *Service) Serve(ctx context.Context) error {
l.Debugf("Starting stun for %s", s)
for _, addr := range s.cfg.Options().StunServers() {
// This blocks until we hit an exit condition or there are issues with the STUN server.
// This returns a boolean signifying if a different STUN server should be tried (oppose to the whole thing
// shutting down and this winding itself down.
s.runStunForServer(ctx, addr)
// This blocks until we hit an exit condition or there are
// issues with the STUN server.
if err := s.runStunForServer(ctx, addr); errors.Is(err, errNotPunchable) {
break // we will sleep for a while
}
// Have we been asked to stop?
select {
@@ -129,11 +134,6 @@ func (s *Service) Serve(ctx context.Context) error {
s.setExternalAddress(nil, "")
goto disabled
}
// Unpunchable NAT? Chillout for some time.
if !s.isCurrentNATTypePunchable() {
break
}
}
// We failed to contact all provided stun servers or the nat is not punchable.
@@ -142,7 +142,7 @@ func (s *Service) Serve(ctx context.Context) error {
}
}
func (s *Service) runStunForServer(ctx context.Context, addr string) {
func (s *Service) runStunForServer(ctx context.Context, addr string) error {
l.Debugf("Running stun for %s via %s", s, addr)
// Resolve the address, so that in case the server advertises two
@@ -153,7 +153,7 @@ func (s *Service) runStunForServer(ctx context.Context, addr string) {
udpAddr, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
l.Debugf("%s stun addr resolution on %s: %s", s, addr, err)
return
return err
}
s.client.SetServerAddr(udpAddr.String())
@@ -163,15 +163,18 @@ func (s *Service) runStunForServer(ctx context.Context, addr string) {
natType, extAddr, err = s.client.Discover()
return err
})
if err != nil || extAddr == nil {
l.Debugf("%s stun discovery on %s: %s", s, addr, err)
return
if err != nil {
l.Debugf("%s stun discovery on %s: %v", s, addr, err)
return err
} else if extAddr == nil {
l.Debugf("%s stun discovery on %s resulted in no address", s, addr)
return fmt.Errorf("%s: no address", addr)
}
// The stun server is most likely borked, try another one.
if natType == NATError || natType == NATUnknown || natType == NATBlocked {
l.Debugf("%s stun discovery on %s resolved to %s", s, addr, natType)
return
return fmt.Errorf("%s: bad result: %v", addr, natType)
}
s.setNATType(natType)
@@ -181,15 +184,14 @@ func (s *Service) runStunForServer(ctx context.Context, addr string) {
// and such, just let the caller check the nat type and work it out themselves.
if !s.isCurrentNATTypePunchable() {
l.Debugf("%s cannot punch %s, skipping", s, natType)
return
return errNotPunchable
}
s.setExternalAddress(extAddr, addr)
s.stunKeepAlive(ctx, addr, extAddr)
return s.stunKeepAlive(ctx, addr, extAddr)
}
func (s *Service) stunKeepAlive(ctx context.Context, addr string, extAddr *Host) {
func (s *Service) stunKeepAlive(ctx context.Context, addr string, extAddr *Host) error {
var err error
nextSleep := time.Duration(s.cfg.Options().StunKeepaliveStartS) * time.Second
@@ -211,7 +213,7 @@ func (s *Service) stunKeepAlive(ctx context.Context, addr string, extAddr *Host)
minSleep := time.Duration(s.cfg.Options().StunKeepaliveMinS) * time.Second
if nextSleep < minSleep {
l.Debugf("%s keepalive aborting, sleep below min: %s < %s", s, nextSleep, minSleep)
return
return fmt.Errorf("unreasonably low keepalive: %v", minSleep)
}
}
@@ -238,13 +240,13 @@ func (s *Service) stunKeepAlive(ctx context.Context, addr string, extAddr *Host)
case <-time.After(sleepFor):
case <-ctx.Done():
l.Debugf("%s stopping, aborting stun", s)
return
return ctx.Err()
}
if s.cfg.Options().IsStunDisabled() {
// Disabled, give up
l.Debugf("%s disabled, aborting stun ", s)
return
return errors.New("disabled")
}
// Check if any writes happened while we were sleeping, if they did, sleep again
@@ -259,7 +261,7 @@ func (s *Service) stunKeepAlive(ctx context.Context, addr string, extAddr *Host)
extAddr, err = s.client.Keepalive()
if err != nil {
l.Debugf("%s stun keepalive on %s: %s (%v)", s, addr, err, extAddr)
return
return err
}
ourLastWrite = time.Now()
}