2013-12-15 10:43:31 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-01-01 21:31:04 +00:00
|
|
|
"compress/gzip"
|
2013-12-15 10:43:31 +00:00
|
|
|
"crypto/tls"
|
2014-01-26 13:28:41 +00:00
|
|
|
"flag"
|
2014-01-08 13:37:33 +00:00
|
|
|
"fmt"
|
2013-12-15 10:43:31 +00:00
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
_ "net/http/pprof"
|
|
|
|
"os"
|
2014-02-17 07:47:21 +00:00
|
|
|
"os/exec"
|
2013-12-15 10:43:31 +00:00
|
|
|
"path"
|
2014-01-09 23:09:27 +00:00
|
|
|
"runtime"
|
|
|
|
"runtime/debug"
|
2013-12-15 10:43:31 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/calmh/ini"
|
|
|
|
"github.com/calmh/syncthing/discover"
|
|
|
|
"github.com/calmh/syncthing/protocol"
|
|
|
|
)
|
|
|
|
|
2014-02-01 19:23:19 +00:00
|
|
|
var cfg Configuration
|
2013-12-30 15:05:27 +00:00
|
|
|
var Version string = "unknown-dev"
|
2013-12-18 18:36:28 +00:00
|
|
|
|
2013-12-15 10:43:31 +00:00
|
|
|
var (
|
2014-02-01 19:23:19 +00:00
|
|
|
myID string
|
|
|
|
config ini.Config
|
2013-12-15 10:43:31 +00:00
|
|
|
)
|
|
|
|
|
2014-01-26 13:28:41 +00:00
|
|
|
var (
|
2014-02-12 11:10:44 +00:00
|
|
|
showVersion bool
|
|
|
|
confDir string
|
|
|
|
trace string
|
|
|
|
profiler string
|
|
|
|
verbose bool
|
|
|
|
startupDelay int
|
2014-01-26 13:28:41 +00:00
|
|
|
)
|
|
|
|
|
2013-12-15 10:43:31 +00:00
|
|
|
func main() {
|
2014-01-26 13:28:41 +00:00
|
|
|
flag.StringVar(&confDir, "home", "~/.syncthing", "Set configuration directory")
|
|
|
|
flag.StringVar(&trace, "debug.trace", "", "(connect,net,idx,file,pull)")
|
|
|
|
flag.StringVar(&profiler, "debug.profiler", "", "(addr)")
|
|
|
|
flag.BoolVar(&showVersion, "version", false, "Show version")
|
2014-02-03 15:01:17 +00:00
|
|
|
flag.BoolVar(&verbose, "v", false, "Be more verbose")
|
2014-02-12 11:10:44 +00:00
|
|
|
flag.IntVar(&startupDelay, "delay", 0, "Startup delay (s)")
|
2014-01-26 13:28:41 +00:00
|
|
|
flag.Usage = usageFor(flag.CommandLine, "syncthing [options]")
|
|
|
|
flag.Parse()
|
2014-01-08 13:37:33 +00:00
|
|
|
|
2014-02-12 11:10:44 +00:00
|
|
|
if startupDelay > 0 {
|
|
|
|
time.Sleep(time.Duration(startupDelay) * time.Second)
|
|
|
|
}
|
|
|
|
|
2014-01-26 13:28:41 +00:00
|
|
|
if showVersion {
|
2014-01-08 13:37:33 +00:00
|
|
|
fmt.Println(Version)
|
2013-12-18 18:36:28 +00:00
|
|
|
os.Exit(0)
|
2013-12-15 10:43:31 +00:00
|
|
|
}
|
2014-01-08 13:37:33 +00:00
|
|
|
|
2014-01-09 23:09:27 +00:00
|
|
|
if len(os.Getenv("GOGC")) == 0 {
|
|
|
|
debug.SetGCPercent(25)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(os.Getenv("GOMAXPROCS")) == 0 {
|
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
|
|
|
}
|
|
|
|
|
2014-01-26 13:28:41 +00:00
|
|
|
if len(trace) > 0 {
|
2014-01-13 17:29:23 +00:00
|
|
|
log.SetFlags(log.Lshortfile | log.Ldate | log.Ltime | log.Lmicroseconds)
|
|
|
|
logger.SetFlags(log.Lshortfile | log.Ldate | log.Ltime | log.Lmicroseconds)
|
2013-12-31 02:21:31 +00:00
|
|
|
}
|
2014-01-26 13:28:41 +00:00
|
|
|
confDir = expandTilde(confDir)
|
2013-12-21 23:16:49 +00:00
|
|
|
|
2013-12-15 10:43:31 +00:00
|
|
|
// Ensure that our home directory exists and that we have a certificate and key.
|
|
|
|
|
2014-01-26 13:28:41 +00:00
|
|
|
ensureDir(confDir, 0700)
|
|
|
|
cert, err := loadCert(confDir)
|
2013-12-15 10:43:31 +00:00
|
|
|
if err != nil {
|
2014-01-26 13:28:41 +00:00
|
|
|
newCertificate(confDir)
|
|
|
|
cert, err = loadCert(confDir)
|
2013-12-15 10:43:31 +00:00
|
|
|
fatalErr(err)
|
|
|
|
}
|
|
|
|
|
2014-01-12 22:19:03 +00:00
|
|
|
myID = string(certId(cert.Certificate[0]))
|
2014-01-20 21:22:27 +00:00
|
|
|
log.SetPrefix("[" + myID[0:5] + "] ")
|
|
|
|
logger.SetPrefix("[" + myID[0:5] + "] ")
|
2013-12-15 10:43:31 +00:00
|
|
|
|
2014-02-03 15:01:17 +00:00
|
|
|
infoln("Version", Version)
|
|
|
|
infoln("My ID:", myID)
|
|
|
|
|
2014-02-01 19:23:19 +00:00
|
|
|
// Prepare to be able to save configuration
|
|
|
|
|
|
|
|
cfgFile := path.Join(confDir, "config.xml")
|
|
|
|
go saveConfigLoop(cfgFile)
|
|
|
|
|
2014-01-22 13:28:14 +00:00
|
|
|
// Load the configuration file, if it exists.
|
|
|
|
// If it does not, create a template.
|
|
|
|
|
|
|
|
cf, err := os.Open(cfgFile)
|
2014-02-01 19:23:19 +00:00
|
|
|
if err == nil {
|
|
|
|
// Read config.xml
|
|
|
|
cfg, err = readConfigXML(cf)
|
2014-01-22 13:28:14 +00:00
|
|
|
if err != nil {
|
|
|
|
fatalln(err)
|
|
|
|
}
|
2014-02-01 19:23:19 +00:00
|
|
|
cf.Close()
|
|
|
|
} else {
|
|
|
|
// No config.xml, let's try the old syncthing.ini
|
|
|
|
iniFile := path.Join(confDir, "syncthing.ini")
|
|
|
|
cf, err := os.Open(iniFile)
|
|
|
|
if err == nil {
|
|
|
|
infoln("Migrating syncthing.ini to config.xml")
|
|
|
|
iniCfg := ini.Parse(cf)
|
|
|
|
cf.Close()
|
|
|
|
os.Rename(iniFile, path.Join(confDir, "migrated_syncthing.ini"))
|
|
|
|
|
|
|
|
cfg, _ = readConfigXML(nil)
|
|
|
|
cfg.Repositories = []RepositoryConfiguration{
|
|
|
|
{Directory: iniCfg.Get("repository", "dir")},
|
|
|
|
}
|
|
|
|
readConfigINI(iniCfg.OptionMap("settings"), &cfg.Options)
|
|
|
|
for name, addrs := range iniCfg.OptionMap("nodes") {
|
|
|
|
n := NodeConfiguration{
|
|
|
|
NodeID: name,
|
|
|
|
Addresses: strings.Fields(addrs),
|
|
|
|
}
|
|
|
|
cfg.Repositories[0].Nodes = append(cfg.Repositories[0].Nodes, n)
|
|
|
|
}
|
2014-01-26 13:28:41 +00:00
|
|
|
|
2014-02-01 19:23:19 +00:00
|
|
|
saveConfig()
|
|
|
|
}
|
2014-01-22 13:28:14 +00:00
|
|
|
}
|
|
|
|
|
2014-02-01 19:23:19 +00:00
|
|
|
if len(cfg.Repositories) == 0 {
|
|
|
|
infoln("No config file; starting with empty defaults")
|
|
|
|
|
|
|
|
cfg, err = readConfigXML(nil)
|
|
|
|
cfg.Repositories = []RepositoryConfiguration{
|
|
|
|
{
|
|
|
|
Directory: "~/Sync",
|
|
|
|
Nodes: []NodeConfiguration{
|
|
|
|
{NodeID: myID, Addresses: []string{"dynamic"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2014-01-22 13:28:14 +00:00
|
|
|
|
2014-02-01 19:23:19 +00:00
|
|
|
saveConfig()
|
|
|
|
infof("Edit %s to taste or use the GUI\n", cfgFile)
|
|
|
|
}
|
2014-01-26 13:28:41 +00:00
|
|
|
|
2014-02-10 19:54:37 +00:00
|
|
|
// Make sure the local node is in the node list.
|
|
|
|
cfg.Repositories[0].Nodes = cleanNodeList(cfg.Repositories[0].Nodes, myID)
|
|
|
|
|
2014-02-01 19:23:19 +00:00
|
|
|
var dir = expandTilde(cfg.Repositories[0].Directory)
|
2014-01-22 13:28:14 +00:00
|
|
|
|
2014-01-26 13:28:41 +00:00
|
|
|
if len(profiler) > 0 {
|
2013-12-15 10:43:31 +00:00
|
|
|
go func() {
|
2014-01-26 13:28:41 +00:00
|
|
|
err := http.ListenAndServe(profiler, nil)
|
2013-12-18 18:36:28 +00:00
|
|
|
if err != nil {
|
|
|
|
warnln(err)
|
|
|
|
}
|
2013-12-15 10:43:31 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// The TLS configuration is used for both the listening socket and outgoing
|
|
|
|
// connections.
|
|
|
|
|
2014-02-01 19:23:19 +00:00
|
|
|
tlsCfg := &tls.Config{
|
2014-01-09 08:28:08 +00:00
|
|
|
Certificates: []tls.Certificate{cert},
|
|
|
|
NextProtos: []string{"bep/1.0"},
|
|
|
|
ServerName: myID,
|
|
|
|
ClientAuth: tls.RequestClientCert,
|
|
|
|
SessionTicketsDisabled: true,
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
MinVersion: tls.VersionTLS12,
|
2013-12-15 10:43:31 +00:00
|
|
|
}
|
|
|
|
|
2013-12-21 23:16:49 +00:00
|
|
|
ensureDir(dir, -1)
|
2014-02-12 22:18:41 +00:00
|
|
|
m := NewModel(dir, cfg.Options.MaxChangeKbps*1000)
|
2014-01-26 13:28:41 +00:00
|
|
|
for _, t := range strings.Split(trace, ",") {
|
2014-01-06 10:11:18 +00:00
|
|
|
m.Trace(t)
|
|
|
|
}
|
2014-02-01 19:23:19 +00:00
|
|
|
if cfg.Options.MaxSendKbps > 0 {
|
|
|
|
m.LimitRate(cfg.Options.MaxSendKbps)
|
2014-01-12 15:59:35 +00:00
|
|
|
}
|
2013-12-15 10:43:31 +00:00
|
|
|
|
2014-01-05 22:54:57 +00:00
|
|
|
// GUI
|
2014-02-01 19:23:19 +00:00
|
|
|
if cfg.Options.GUIEnabled && cfg.Options.GUIAddress != "" {
|
|
|
|
host, port, err := net.SplitHostPort(cfg.Options.GUIAddress)
|
2014-01-09 09:40:12 +00:00
|
|
|
if err != nil {
|
2014-02-01 19:23:19 +00:00
|
|
|
warnf("Cannot start GUI on %q: %v", cfg.Options.GUIAddress, err)
|
2014-01-09 09:40:12 +00:00
|
|
|
} else {
|
|
|
|
if len(host) > 0 {
|
2014-02-01 19:23:19 +00:00
|
|
|
infof("Starting web GUI on http://%s", cfg.Options.GUIAddress)
|
2014-01-09 09:40:12 +00:00
|
|
|
} else {
|
|
|
|
infof("Starting web GUI on port %s", port)
|
|
|
|
}
|
2014-02-01 19:23:19 +00:00
|
|
|
startGUI(cfg.Options.GUIAddress, m)
|
2014-01-09 09:40:12 +00:00
|
|
|
}
|
2014-01-05 22:54:57 +00:00
|
|
|
}
|
|
|
|
|
2013-12-15 10:43:31 +00:00
|
|
|
// Walk the repository and update the local model before establishing any
|
|
|
|
// connections to other nodes.
|
|
|
|
|
2014-02-03 15:01:17 +00:00
|
|
|
if verbose {
|
|
|
|
infoln("Populating repository index")
|
|
|
|
}
|
2014-01-29 21:02:38 +00:00
|
|
|
loadIndex(m)
|
2013-12-15 10:43:31 +00:00
|
|
|
updateLocalModel(m)
|
|
|
|
|
2014-02-09 22:13:06 +00:00
|
|
|
connOpts := map[string]string{
|
|
|
|
"clientId": "syncthing",
|
|
|
|
"clientVersion": Version,
|
|
|
|
"clusterHash": clusterHash(cfg.Repositories[0].Nodes),
|
|
|
|
}
|
|
|
|
|
2013-12-15 10:43:31 +00:00
|
|
|
// Routine to listen for incoming connections
|
2014-02-03 15:01:17 +00:00
|
|
|
if verbose {
|
|
|
|
infoln("Listening for incoming connections")
|
|
|
|
}
|
2014-02-05 22:17:17 +00:00
|
|
|
for _, addr := range cfg.Options.ListenAddress {
|
2014-02-09 22:13:06 +00:00
|
|
|
go listen(myID, addr, m, tlsCfg, connOpts)
|
2014-02-05 22:17:17 +00:00
|
|
|
}
|
2013-12-15 10:43:31 +00:00
|
|
|
|
|
|
|
// Routine to connect out to configured nodes
|
2014-02-03 15:01:17 +00:00
|
|
|
if verbose {
|
|
|
|
infoln("Attempting to connect to other nodes")
|
|
|
|
}
|
2014-02-05 22:17:17 +00:00
|
|
|
disc := discovery(cfg.Options.ListenAddress[0])
|
2014-02-09 22:13:06 +00:00
|
|
|
go connect(myID, disc, m, tlsCfg, connOpts)
|
2013-12-15 10:43:31 +00:00
|
|
|
|
|
|
|
// Routine to pull blocks from other nodes to synchronize the local
|
|
|
|
// repository. Does not run when we are in read only (publish only) mode.
|
2014-02-01 19:23:19 +00:00
|
|
|
if !cfg.Options.ReadOnly {
|
2014-02-03 15:01:17 +00:00
|
|
|
if verbose {
|
|
|
|
if cfg.Options.AllowDelete {
|
|
|
|
infoln("Deletes from peer nodes are allowed")
|
|
|
|
} else {
|
|
|
|
infoln("Deletes from peer nodes will be ignored")
|
|
|
|
}
|
|
|
|
okln("Ready to synchronize (read-write)")
|
2014-01-06 20:41:29 +00:00
|
|
|
}
|
2014-02-01 19:23:19 +00:00
|
|
|
m.StartRW(cfg.Options.AllowDelete, cfg.Options.ParallelRequests)
|
2014-02-03 15:01:17 +00:00
|
|
|
} else if verbose {
|
2014-01-06 20:41:29 +00:00
|
|
|
okln("Ready to synchronize (read only; no external updates accepted)")
|
2013-12-15 10:43:31 +00:00
|
|
|
}
|
|
|
|
|
2014-02-12 22:18:41 +00:00
|
|
|
// Periodically scan the repository and update the local
|
2013-12-15 10:43:31 +00:00
|
|
|
// XXX: Should use some fsnotify mechanism.
|
|
|
|
go func() {
|
2014-02-01 19:23:19 +00:00
|
|
|
td := time.Duration(cfg.Options.RescanIntervalS) * time.Second
|
2013-12-15 10:43:31 +00:00
|
|
|
for {
|
2014-02-01 19:23:19 +00:00
|
|
|
time.Sleep(td)
|
|
|
|
if m.LocalAge() > (td / 2).Seconds() {
|
2014-01-20 21:22:27 +00:00
|
|
|
updateLocalModel(m)
|
|
|
|
}
|
2013-12-15 10:43:31 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2014-02-03 15:01:17 +00:00
|
|
|
if verbose {
|
|
|
|
// Periodically print statistics
|
|
|
|
go printStatsLoop(m)
|
|
|
|
}
|
2014-01-05 15:16:37 +00:00
|
|
|
|
2013-12-15 10:43:31 +00:00
|
|
|
select {}
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:10:44 +00:00
|
|
|
func restart() {
|
|
|
|
infoln("Restarting")
|
|
|
|
args := os.Args
|
|
|
|
doAppend := true
|
|
|
|
for _, arg := range args {
|
|
|
|
if arg == "-delay" {
|
|
|
|
doAppend = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if doAppend {
|
|
|
|
args = append(args, "-delay", "2")
|
|
|
|
}
|
2014-02-17 07:47:21 +00:00
|
|
|
pgm, err := exec.LookPath(os.Args[0])
|
|
|
|
if err != nil {
|
|
|
|
warnln(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
proc, err := os.StartProcess(pgm, args, &os.ProcAttr{
|
2014-02-12 11:10:44 +00:00
|
|
|
Env: os.Environ(),
|
|
|
|
Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
fatalln(err)
|
|
|
|
}
|
|
|
|
proc.Release()
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2014-02-01 19:23:19 +00:00
|
|
|
var saveConfigCh = make(chan struct{})
|
|
|
|
|
|
|
|
func saveConfigLoop(cfgFile string) {
|
|
|
|
for _ = range saveConfigCh {
|
|
|
|
fd, err := os.Create(cfgFile + ".tmp")
|
|
|
|
if err != nil {
|
|
|
|
warnln(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err = writeConfigXML(fd, cfg)
|
|
|
|
if err != nil {
|
|
|
|
warnln(err)
|
|
|
|
fd.Close()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err = fd.Close()
|
|
|
|
if err != nil {
|
|
|
|
warnln(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Rename(cfgFile+".tmp", cfgFile)
|
|
|
|
if err != nil {
|
|
|
|
warnln(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveConfig() {
|
|
|
|
saveConfigCh <- struct{}{}
|
|
|
|
}
|
|
|
|
|
2014-02-12 22:18:41 +00:00
|
|
|
func printStatsLoop(m *Model) {
|
2014-01-05 15:16:37 +00:00
|
|
|
var lastUpdated int64
|
2014-02-12 22:18:41 +00:00
|
|
|
var lastStats = make(map[string]ConnectionInfo)
|
2014-01-05 15:16:37 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
time.Sleep(60 * time.Second)
|
|
|
|
|
|
|
|
for node, stats := range m.ConnectionStats() {
|
|
|
|
secs := time.Since(lastStats[node].At).Seconds()
|
|
|
|
inbps := 8 * int(float64(stats.InBytesTotal-lastStats[node].InBytesTotal)/secs)
|
|
|
|
outbps := 8 * int(float64(stats.OutBytesTotal-lastStats[node].OutBytesTotal)/secs)
|
|
|
|
|
|
|
|
if inbps+outbps > 0 {
|
2014-01-20 21:22:27 +00:00
|
|
|
infof("%s: %sb/s in, %sb/s out", node[0:5], MetricPrefix(inbps), MetricPrefix(outbps))
|
2014-01-05 15:16:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lastStats[node] = stats
|
|
|
|
}
|
|
|
|
|
|
|
|
if lu := m.Generation(); lu > lastUpdated {
|
|
|
|
lastUpdated = lu
|
2014-01-05 22:54:57 +00:00
|
|
|
files, _, bytes := m.GlobalSize()
|
2014-01-05 15:16:37 +00:00
|
|
|
infof("%6d files, %9sB in cluster", files, BinaryPrefix(bytes))
|
2014-01-05 22:54:57 +00:00
|
|
|
files, _, bytes = m.LocalSize()
|
2014-01-05 15:16:37 +00:00
|
|
|
infof("%6d files, %9sB in local repo", files, BinaryPrefix(bytes))
|
2014-01-05 22:54:57 +00:00
|
|
|
needFiles, bytes := m.NeedFiles()
|
2014-01-06 10:11:18 +00:00
|
|
|
infof("%6d files, %9sB to synchronize", len(needFiles), BinaryPrefix(bytes))
|
2014-01-05 15:16:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 22:18:41 +00:00
|
|
|
func listen(myID string, addr string, m *Model, tlsCfg *tls.Config, connOpts map[string]string) {
|
2014-02-05 22:17:17 +00:00
|
|
|
if strings.Contains(trace, "connect") {
|
|
|
|
debugln("NET: Listening on", addr)
|
|
|
|
}
|
2014-02-01 19:23:19 +00:00
|
|
|
l, err := tls.Listen("tcp", addr, tlsCfg)
|
2013-12-15 10:43:31 +00:00
|
|
|
fatalErr(err)
|
|
|
|
|
|
|
|
listen:
|
|
|
|
for {
|
|
|
|
conn, err := l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
warnln(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-01-26 13:28:41 +00:00
|
|
|
if strings.Contains(trace, "connect") {
|
2013-12-15 10:43:31 +00:00
|
|
|
debugln("NET: Connect from", conn.RemoteAddr())
|
|
|
|
}
|
|
|
|
|
|
|
|
tc := conn.(*tls.Conn)
|
|
|
|
err = tc.Handshake()
|
|
|
|
if err != nil {
|
|
|
|
warnln(err)
|
|
|
|
tc.Close()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
remoteID := certId(tc.ConnectionState().PeerCertificates[0].Raw)
|
|
|
|
|
|
|
|
if remoteID == myID {
|
|
|
|
warnf("Connect from myself (%s) - should not happen", remoteID)
|
|
|
|
conn.Close()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.ConnectedTo(remoteID) {
|
|
|
|
warnf("Connect from connected node (%s)", remoteID)
|
|
|
|
}
|
|
|
|
|
2014-02-01 19:23:19 +00:00
|
|
|
for _, nodeCfg := range cfg.Repositories[0].Nodes {
|
|
|
|
if nodeCfg.NodeID == remoteID {
|
2014-01-23 12:12:45 +00:00
|
|
|
protoConn := protocol.NewConnection(remoteID, conn, conn, m, connOpts)
|
2014-01-09 12:58:35 +00:00
|
|
|
m.AddConnection(conn, protoConn)
|
2013-12-15 10:43:31 +00:00
|
|
|
continue listen
|
|
|
|
}
|
|
|
|
}
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-05 22:17:17 +00:00
|
|
|
func discovery(addr string) *discover.Discoverer {
|
2013-12-15 10:43:31 +00:00
|
|
|
_, portstr, err := net.SplitHostPort(addr)
|
|
|
|
fatalErr(err)
|
|
|
|
port, _ := strconv.Atoi(portstr)
|
|
|
|
|
2014-02-01 19:23:19 +00:00
|
|
|
if !cfg.Options.LocalAnnEnabled {
|
2013-12-22 21:29:23 +00:00
|
|
|
port = -1
|
2014-02-03 15:01:17 +00:00
|
|
|
} else if verbose {
|
2013-12-22 21:29:23 +00:00
|
|
|
infoln("Sending local discovery announcements")
|
|
|
|
}
|
|
|
|
|
2014-02-01 19:23:19 +00:00
|
|
|
if !cfg.Options.GlobalAnnEnabled {
|
|
|
|
cfg.Options.GlobalAnnServer = ""
|
2014-02-03 15:01:17 +00:00
|
|
|
} else if verbose {
|
2013-12-22 21:29:23 +00:00
|
|
|
infoln("Sending external discovery announcements")
|
|
|
|
}
|
|
|
|
|
2014-02-01 19:23:19 +00:00
|
|
|
disc, err := discover.NewDiscoverer(myID, port, cfg.Options.GlobalAnnServer)
|
2013-12-22 21:29:23 +00:00
|
|
|
|
2013-12-15 10:43:31 +00:00
|
|
|
if err != nil {
|
2013-12-22 21:29:23 +00:00
|
|
|
warnf("No discovery possible (%v)", err)
|
2013-12-15 10:43:31 +00:00
|
|
|
}
|
|
|
|
|
2014-02-05 22:17:17 +00:00
|
|
|
return disc
|
|
|
|
}
|
|
|
|
|
2014-02-12 22:18:41 +00:00
|
|
|
func connect(myID string, disc *discover.Discoverer, m *Model, tlsCfg *tls.Config, connOpts map[string]string) {
|
2013-12-15 10:43:31 +00:00
|
|
|
for {
|
|
|
|
nextNode:
|
2014-02-01 19:23:19 +00:00
|
|
|
for _, nodeCfg := range cfg.Repositories[0].Nodes {
|
|
|
|
if nodeCfg.NodeID == myID {
|
2013-12-15 10:43:31 +00:00
|
|
|
continue
|
|
|
|
}
|
2014-02-01 19:23:19 +00:00
|
|
|
if m.ConnectedTo(nodeCfg.NodeID) {
|
2013-12-15 10:43:31 +00:00
|
|
|
continue
|
|
|
|
}
|
2014-02-01 19:23:19 +00:00
|
|
|
for _, addr := range nodeCfg.Addresses {
|
2013-12-15 10:43:31 +00:00
|
|
|
if addr == "dynamic" {
|
|
|
|
var ok bool
|
|
|
|
if disc != nil {
|
2014-02-01 19:23:19 +00:00
|
|
|
addr, ok = disc.Lookup(nodeCfg.NodeID)
|
2013-12-15 10:43:31 +00:00
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-26 13:28:41 +00:00
|
|
|
if strings.Contains(trace, "connect") {
|
2014-02-01 19:23:19 +00:00
|
|
|
debugln("NET: Dial", nodeCfg.NodeID, addr)
|
2013-12-15 10:43:31 +00:00
|
|
|
}
|
2014-02-01 19:23:19 +00:00
|
|
|
conn, err := tls.Dial("tcp", addr, tlsCfg)
|
2013-12-15 10:43:31 +00:00
|
|
|
if err != nil {
|
2014-01-26 13:28:41 +00:00
|
|
|
if strings.Contains(trace, "connect") {
|
2013-12-15 10:43:31 +00:00
|
|
|
debugln("NET:", err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
remoteID := certId(conn.ConnectionState().PeerCertificates[0].Raw)
|
2014-02-01 19:23:19 +00:00
|
|
|
if remoteID != nodeCfg.NodeID {
|
|
|
|
warnln("Unexpected nodeID", remoteID, "!=", nodeCfg.NodeID)
|
2013-12-15 10:43:31 +00:00
|
|
|
conn.Close()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-01-23 12:12:45 +00:00
|
|
|
protoConn := protocol.NewConnection(remoteID, conn, conn, m, connOpts)
|
2014-01-09 12:58:35 +00:00
|
|
|
m.AddConnection(conn, protoConn)
|
2013-12-15 10:43:31 +00:00
|
|
|
continue nextNode
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-01 19:23:19 +00:00
|
|
|
time.Sleep(time.Duration(cfg.Options.ReconnectIntervalS) * time.Second)
|
2013-12-15 10:43:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 22:18:41 +00:00
|
|
|
func updateLocalModel(m *Model) {
|
2014-02-01 19:23:19 +00:00
|
|
|
files, _ := m.Walk(cfg.Options.FollowSymlinks)
|
2013-12-15 10:43:31 +00:00
|
|
|
m.ReplaceLocal(files)
|
|
|
|
saveIndex(m)
|
|
|
|
}
|
|
|
|
|
2014-02-12 22:18:41 +00:00
|
|
|
func saveIndex(m *Model) {
|
2014-01-06 10:11:18 +00:00
|
|
|
name := m.RepoID() + ".idx.gz"
|
2014-01-26 13:28:41 +00:00
|
|
|
fullName := path.Join(confDir, name)
|
2013-12-31 03:04:30 +00:00
|
|
|
idxf, err := os.Create(fullName + ".tmp")
|
2013-12-15 10:43:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-01-01 21:31:04 +00:00
|
|
|
|
|
|
|
gzw := gzip.NewWriter(idxf)
|
|
|
|
|
|
|
|
protocol.WriteIndex(gzw, m.ProtocolIndex())
|
|
|
|
gzw.Close()
|
2013-12-15 10:43:31 +00:00
|
|
|
idxf.Close()
|
2013-12-31 03:04:30 +00:00
|
|
|
os.Rename(fullName+".tmp", fullName)
|
2013-12-15 10:43:31 +00:00
|
|
|
}
|
|
|
|
|
2014-02-12 22:18:41 +00:00
|
|
|
func loadIndex(m *Model) {
|
2014-01-06 10:11:18 +00:00
|
|
|
name := m.RepoID() + ".idx.gz"
|
2014-01-26 13:28:41 +00:00
|
|
|
idxf, err := os.Open(path.Join(confDir, name))
|
2013-12-15 10:43:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer idxf.Close()
|
|
|
|
|
2014-01-01 21:31:04 +00:00
|
|
|
gzr, err := gzip.NewReader(idxf)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer gzr.Close()
|
|
|
|
|
|
|
|
idx, err := protocol.ReadIndex(gzr)
|
2013-12-15 10:43:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-01-06 10:11:18 +00:00
|
|
|
m.SeedLocal(idx)
|
2013-12-15 10:43:31 +00:00
|
|
|
}
|
|
|
|
|
2013-12-21 23:16:49 +00:00
|
|
|
func ensureDir(dir string, mode int) {
|
2013-12-15 10:43:31 +00:00
|
|
|
fi, err := os.Stat(dir)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err := os.MkdirAll(dir, 0700)
|
|
|
|
fatalErr(err)
|
2013-12-21 23:16:49 +00:00
|
|
|
} else if mode >= 0 && err == nil && int(fi.Mode()&0777) != mode {
|
|
|
|
err := os.Chmod(dir, os.FileMode(mode))
|
2013-12-15 10:43:31 +00:00
|
|
|
fatalErr(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-06 18:37:26 +00:00
|
|
|
func expandTilde(p string) string {
|
|
|
|
if strings.HasPrefix(p, "~/") {
|
|
|
|
return strings.Replace(p, "~", getHomeDir(), 1)
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2013-12-15 10:43:31 +00:00
|
|
|
func getHomeDir() string {
|
|
|
|
home := os.Getenv("HOME")
|
|
|
|
if home == "" {
|
|
|
|
fatalln("No home directory?")
|
|
|
|
}
|
|
|
|
return home
|
|
|
|
}
|