This implements a new debug/trace infrastructure based on a slightly
hacked up logger. Instead of the traditional "if debug { ... }" I've
rewritten the logger to have no-op Debugln and Debugf, unless debugging
has been enabled for a given "facility". The "facility" is just a
string, typically a package name.
This will be slightly slower than before; but not that much as it's
mostly a function call that returns immediately. For the cases where it
matters (the Debugln takes a hex.Dump() of something for example, and
it's not in a very occasional "if err != nil" branch) there is an
l.ShouldDebug(facility) that is fast enough to be used like the old "if
debug".
The point of all this is that we can now toggle debugging for the
various packages on and off at runtime. There's a new method
/rest/system/debug that can be POSTed a set of facilities to enable and
disable debug for, or GET from to get a list of facilities with
descriptions and their current debug status.
Similarly a /rest/system/log?since=... can grab the latest log entries,
up to 250 of them (hardcoded constant in main.go) plus the initial few.
Not implemented in this commit (but planned) is a simple debug GUI
available on /debug that shows the current log in an easily pasteable
format and has checkboxes to enable the various debug facilities.
The debug instructions to a user then becomes "visit this URL, check
these boxes, reproduce your problem, copy and paste the log". The actual
log viewer on the hypothetical /debug URL can poll regularly for new log
entries and this bypass the 250 line limit.
The existing STTRACE=foo variable is still obeyed and just sets the
start state of the system.
133 lines
3.3 KiB
Go
133 lines
3.3 KiB
Go
// Copyright (C) 2015 The Syncthing Authors.
|
|
//
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
|
"github.com/syncthing/syncthing/lib/events"
|
|
"github.com/syncthing/syncthing/lib/sync"
|
|
"github.com/syncthing/syncthing/lib/upnp"
|
|
)
|
|
|
|
// The UPnP service runs a loop for discovery of IGDs (Internet Gateway
|
|
// Devices) and setup/renewal of a port mapping.
|
|
type upnpSvc struct {
|
|
cfg *config.Wrapper
|
|
localPort int
|
|
extPort int
|
|
extPortMut sync.Mutex
|
|
stop chan struct{}
|
|
}
|
|
|
|
func newUPnPSvc(cfg *config.Wrapper, localPort int) *upnpSvc {
|
|
return &upnpSvc{
|
|
cfg: cfg,
|
|
localPort: localPort,
|
|
extPortMut: sync.NewMutex(),
|
|
}
|
|
}
|
|
|
|
func (s *upnpSvc) Serve() {
|
|
foundIGD := true
|
|
s.stop = make(chan struct{})
|
|
|
|
for {
|
|
igds := upnp.Discover(time.Duration(s.cfg.Options().UPnPTimeoutS) * time.Second)
|
|
if len(igds) > 0 {
|
|
foundIGD = true
|
|
s.extPortMut.Lock()
|
|
oldExtPort := s.extPort
|
|
s.extPortMut.Unlock()
|
|
|
|
newExtPort := s.tryIGDs(igds, oldExtPort)
|
|
|
|
s.extPortMut.Lock()
|
|
s.extPort = newExtPort
|
|
s.extPortMut.Unlock()
|
|
} else if foundIGD {
|
|
// Only print a notice if we've previously found an IGD or this is
|
|
// the first time around.
|
|
foundIGD = false
|
|
l.Infof("No UPnP device detected")
|
|
}
|
|
|
|
d := time.Duration(s.cfg.Options().UPnPRenewalM) * time.Minute
|
|
if d == 0 {
|
|
// We always want to do renewal so lets just pick a nice sane number.
|
|
d = 30 * time.Minute
|
|
}
|
|
|
|
select {
|
|
case <-s.stop:
|
|
return
|
|
case <-time.After(d):
|
|
}
|
|
}
|
|
}
|
|
|
|
func (s *upnpSvc) Stop() {
|
|
close(s.stop)
|
|
}
|
|
|
|
func (s *upnpSvc) ExternalPort() int {
|
|
s.extPortMut.Lock()
|
|
port := s.extPort
|
|
s.extPortMut.Unlock()
|
|
return port
|
|
}
|
|
|
|
func (s *upnpSvc) tryIGDs(igds []upnp.IGD, prevExtPort int) int {
|
|
// Lets try all the IGDs we found and use the first one that works.
|
|
// TODO: Use all of them, and sort out the resulting mess to the
|
|
// discovery announcement code...
|
|
for _, igd := range igds {
|
|
extPort, err := s.tryIGD(igd, prevExtPort)
|
|
if err != nil {
|
|
l.Warnf("Failed to set UPnP port mapping: external port %d on device %s.", extPort, igd.FriendlyIdentifier())
|
|
continue
|
|
}
|
|
|
|
if extPort != prevExtPort {
|
|
l.Infof("New UPnP port mapping: external port %d to local port %d.", extPort, s.localPort)
|
|
events.Default.Log(events.ExternalPortMappingChanged, map[string]int{"port": extPort})
|
|
}
|
|
l.Debugf("Created/updated UPnP port mapping for external port %d on device %s.", extPort, igd.FriendlyIdentifier())
|
|
return extPort
|
|
}
|
|
|
|
return 0
|
|
}
|
|
|
|
func (s *upnpSvc) tryIGD(igd upnp.IGD, suggestedPort int) (int, error) {
|
|
var err error
|
|
leaseTime := s.cfg.Options().UPnPLeaseM * 60
|
|
|
|
if suggestedPort != 0 {
|
|
// First try renewing our existing mapping.
|
|
name := fmt.Sprintf("syncthing-%d", suggestedPort)
|
|
err = igd.AddPortMapping(upnp.TCP, suggestedPort, s.localPort, name, leaseTime)
|
|
if err == nil {
|
|
return suggestedPort, nil
|
|
}
|
|
}
|
|
|
|
for i := 0; i < 10; i++ {
|
|
// Then try up to ten random ports.
|
|
extPort := 1024 + predictableRandom.Intn(65535-1024)
|
|
name := fmt.Sprintf("syncthing-%d", extPort)
|
|
err = igd.AddPortMapping(upnp.TCP, extPort, s.localPort, name, leaseTime)
|
|
if err == nil {
|
|
return extPort, nil
|
|
}
|
|
}
|
|
|
|
return 0, err
|
|
}
|