From 51da3506a296b7d5d4185b17364f188292136888 Mon Sep 17 00:00:00 2001 From: Sebastien Douheret Date: Wed, 17 May 2017 16:08:03 +0200 Subject: [PATCH] Use autogenerated Synchting apikey. Except if gui-apikey is set in config.json file. Signed-off-by: Sebastien Douheret --- README.md | 3 ++- lib/syncthing/st.go | 27 ++++++++++++++++++++++++--- lib/xdsconfig/fileconfig.go | 7 ++++--- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 7b60cdc..b910e06 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,8 @@ Supported fields in configuration file are: "syncthing": { "binDir": "syncthing binaries directory (default: executable directory)", "home": "syncthing home directory (usually .../syncthing-config)", - "gui-address": "syncthing gui url (default http://localhost:8384)" + "gui-address": "syncthing gui url (default http://localhost:8384)", + "gui-apikey": "syncthing api-key to use (default auto-generated)" } } ``` diff --git a/lib/syncthing/st.go b/lib/syncthing/st.go index 9452fbd..841901d 100644 --- a/lib/syncthing/st.go +++ b/lib/syncthing/st.go @@ -15,6 +15,10 @@ import ( "io" + "io/ioutil" + + "regexp" + "github.com/Sirupsen/logrus" "github.com/iotbzh/xds-server/lib/common" "github.com/iotbzh/xds-server/lib/xdsconfig" @@ -172,27 +176,44 @@ func (s *SyncThing) Start() (*exec.Cmd, error) { s.STCmd, err = s.startProc("syncthing", args, env, &s.exitSTChan) + // Use autogenerated apikey if not set by config.json + if s.APIKey == "" { + if fd, err := os.Open(filepath.Join(s.Home, "config.xml")); err == nil { + defer fd.Close() + if b, err := ioutil.ReadAll(fd); err == nil { + re := regexp.MustCompile("(.*)") + key := re.FindStringSubmatch(string(b)) + if len(key) >= 1 { + s.APIKey = key[1] + } + } + } + } + return s.STCmd, err } // StartInotify Starts syncthing-inotify process func (s *SyncThing) StartInotify() (*exec.Cmd, error) { var err error + exeName := "syncthing-inotify" - s.log.Infof(" STI home=%s", s.Home) s.log.Infof(" STI url=%s", s.BaseURL) args := []string{ - "--home=" + s.Home, "-target=" + s.BaseURL, } + if s.APIKey != "" { + args = append(args, "-api="+s.APIKey) + s.log.Infof("%s uses apikey=%s", exeName, s.APIKey) + } if s.log.Level == logrus.DebugLevel { args = append(args, "-verbosity=4") } env := []string{} - s.STICmd, err = s.startProc("syncthing-inotify", args, env, &s.exitSTIChan) + s.STICmd, err = s.startProc(exeName, args, env, &s.exitSTIChan) return s.STICmd, err } diff --git a/lib/xdsconfig/fileconfig.go b/lib/xdsconfig/fileconfig.go index 776eb78..d39cae0 100644 --- a/lib/xdsconfig/fileconfig.go +++ b/lib/xdsconfig/fileconfig.go @@ -23,7 +23,7 @@ type SyncThingConf struct { type FileConfig struct { WebAppDir string `json:"webAppDir"` ShareRootDir string `json:"shareRootDir"` - SdkRootDir string `json:"sdkRootDir"` + SdkRootDir string `json:"sdkRootDir"` HTTPPort string `json:"httpPort"` SThgConf *SyncThingConf `json:"syncthing"` LogsDir string `json:"logsDir"` @@ -86,7 +86,8 @@ func updateConfigFromFile(c *Config, confFile string) error { &fCfg.ShareRootDir, &fCfg.SdkRootDir, &fCfg.LogsDir, - &fCfg.SThgConf.Home} { + &fCfg.SThgConf.Home, + &fCfg.SThgConf.BinDir} { rep, err := resolveEnvVar(*field) if err != nil { @@ -94,7 +95,7 @@ func updateConfigFromFile(c *Config, confFile string) error { } *field = path.Clean(rep) } - + // Config file settings overwrite default config if fCfg.WebAppDir != "" { -- 2.16.6