|
@@ -32,12 +32,16 @@ var (
|
|
|
HttpOpen = false
|
|
HttpOpen = false
|
|
|
UploadSecret string
|
|
UploadSecret string
|
|
|
confMutex sync.Mutex
|
|
confMutex sync.Mutex
|
|
|
|
|
+ versMutex sync.Mutex
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
+// try initializing from system.json on disk
|
|
|
func init() {
|
|
func init() {
|
|
|
- confPath := filepath.Join(basePath, "settings", "system2.json")
|
|
|
|
|
|
|
+ // try loading existing config
|
|
|
|
|
+ confPath := filepath.Join(basePath, "settings", "system.json")
|
|
|
file, err := os.Open(confPath)
|
|
file, err := os.Open(confPath)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
|
|
+ // create a default if it doesn't exist
|
|
|
err = createDefaultConf()
|
|
err = createDefaultConf()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
errmsg := fmt.Sprintf("Unable to create config! %v", err)
|
|
errmsg := fmt.Sprintf("Unable to create config! %v", err)
|
|
@@ -53,30 +57,38 @@ func init() {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// return the global conf var
|
|
|
func Conf() structs.SysConfig {
|
|
func Conf() structs.SysConfig {
|
|
|
|
|
+ confMutex.Lock()
|
|
|
|
|
+ defer confMutex.Unlock()
|
|
|
return globalConfig
|
|
return globalConfig
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// update by passing in a map of key:values you want to modify
|
|
|
func UpdateConf(values map[string]interface{}) error {
|
|
func UpdateConf(values map[string]interface{}) error {
|
|
|
|
|
+ // mutex lock to avoid race conditions
|
|
|
confMutex.Lock()
|
|
confMutex.Lock()
|
|
|
defer confMutex.Unlock()
|
|
defer confMutex.Unlock()
|
|
|
- confPath := filepath.Join(basePath, "settings", "system2.json")
|
|
|
|
|
|
|
+ confPath := filepath.Join(basePath, "settings", "system.json")
|
|
|
file, err := ioutil.ReadFile(confPath)
|
|
file, err := ioutil.ReadFile(confPath)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
errmsg := fmt.Sprintf("Unable to load config: %v", err)
|
|
errmsg := fmt.Sprintf("Unable to load config: %v", err)
|
|
|
logger.Error(errmsg)
|
|
logger.Error(errmsg)
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
|
|
+ // unmarshal the config to struct
|
|
|
var configMap map[string]interface{}
|
|
var configMap map[string]interface{}
|
|
|
if err := json.Unmarshal(file, &configMap); err != nil {
|
|
if err := json.Unmarshal(file, &configMap); err != nil {
|
|
|
errmsg := fmt.Sprintf("Error decoding JSON: %v", err)
|
|
errmsg := fmt.Sprintf("Error decoding JSON: %v", err)
|
|
|
logger.Error(errmsg)
|
|
logger.Error(errmsg)
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
|
|
+ // update our unmarshaled struct
|
|
|
for key, value := range values {
|
|
for key, value := range values {
|
|
|
configMap[key] = value
|
|
configMap[key] = value
|
|
|
}
|
|
}
|
|
|
- updatedJSON, err := json.Marshal(configMap)
|
|
|
|
|
|
|
+ // marshal and persist it
|
|
|
|
|
+ updatedJSON, err := json.MarshalIndent(configMap, "", " ")
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
errmsg := fmt.Sprintf("Error encoding JSON: %v", err)
|
|
errmsg := fmt.Sprintf("Error encoding JSON: %v", err)
|
|
|
logger.Error(errmsg)
|
|
logger.Error(errmsg)
|
|
@@ -95,6 +107,7 @@ func UpdateConf(values map[string]interface{}) error {
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// write a default conf to disk
|
|
|
func createDefaultConf() error {
|
|
func createDefaultConf() error {
|
|
|
defaultConfig := structs.SysConfig{
|
|
defaultConfig := structs.SysConfig{
|
|
|
Setup: "start",
|
|
Setup: "start",
|
|
@@ -139,7 +152,7 @@ func createDefaultConf() error {
|
|
|
Privkey: "",
|
|
Privkey: "",
|
|
|
Salt: "",
|
|
Salt: "",
|
|
|
}
|
|
}
|
|
|
- path := filepath.Join(basePath, "settings", "system2.json")
|
|
|
|
|
|
|
+ path := filepath.Join(basePath, "settings", "system.json")
|
|
|
if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
|
|
if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
@@ -149,12 +162,15 @@ func createDefaultConf() error {
|
|
|
}
|
|
}
|
|
|
defer file.Close()
|
|
defer file.Close()
|
|
|
encoder := json.NewEncoder(file)
|
|
encoder := json.NewEncoder(file)
|
|
|
|
|
+ encoder.SetIndent("", " ")
|
|
|
if err := encoder.Encode(&defaultConfig); err != nil {
|
|
if err := encoder.Encode(&defaultConfig); err != nil {
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// check outbound tcp connectivity
|
|
|
|
|
+// takes ip:port
|
|
|
func NetCheck(netCheck string) bool {
|
|
func NetCheck(netCheck string) bool {
|
|
|
logger.Info("Checking internet access")
|
|
logger.Info("Checking internet access")
|
|
|
internet := false
|
|
internet := false
|
|
@@ -170,7 +186,10 @@ func NetCheck(netCheck string) bool {
|
|
|
return internet
|
|
return internet
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// check the version server and return unmarshaled result
|
|
|
func CheckVersion() (structs.Version, bool) {
|
|
func CheckVersion() (structs.Version, bool) {
|
|
|
|
|
+ versMutex.Lock()
|
|
|
|
|
+ defer versMutex.Unlock()
|
|
|
const retries = 10
|
|
const retries = 10
|
|
|
const delay = time.Second
|
|
const delay = time.Second
|
|
|
url := globalConfig.UpdateUrl
|
|
url := globalConfig.UpdateUrl
|
|
@@ -186,6 +205,7 @@ func CheckVersion() (structs.Version, bool) {
|
|
|
return VersionInfo, false
|
|
return VersionInfo, false
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ // read the body bytes
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
resp.Body.Close()
|
|
resp.Body.Close()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -198,8 +218,8 @@ func CheckVersion() (structs.Version, bool) {
|
|
|
return VersionInfo, false
|
|
return VersionInfo, false
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ // unmarshal values into Version struct
|
|
|
err = json.Unmarshal(body, &VersionInfo)
|
|
err = json.Unmarshal(body, &VersionInfo)
|
|
|
- fmt.Println(string(body))
|
|
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
errmsg := fmt.Sprintf("Error unmarshalling JSON: %v", err)
|
|
errmsg := fmt.Sprintf("Error unmarshalling JSON: %v", err)
|
|
|
logger.Warn(errmsg)
|
|
logger.Warn(errmsg)
|
|
@@ -210,6 +230,7 @@ func CheckVersion() (structs.Version, bool) {
|
|
|
return VersionInfo, false
|
|
return VersionInfo, false
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ // debug: re-marshal and write to disk
|
|
|
confPath := filepath.Join(basePath, "settings", "version_info.json")
|
|
confPath := filepath.Join(basePath, "settings", "version_info.json")
|
|
|
file, err := os.Create(confPath)
|
|
file, err := os.Create(confPath)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -219,11 +240,11 @@ func CheckVersion() (structs.Version, bool) {
|
|
|
}
|
|
}
|
|
|
defer file.Close()
|
|
defer file.Close()
|
|
|
encoder := json.NewEncoder(file)
|
|
encoder := json.NewEncoder(file)
|
|
|
|
|
+ encoder.SetIndent("", " ")
|
|
|
if err := encoder.Encode(&VersionInfo); err != nil {
|
|
if err := encoder.Encode(&VersionInfo); err != nil {
|
|
|
errmsg := fmt.Sprintf("Failed to write JSON: %v", err)
|
|
errmsg := fmt.Sprintf("Failed to write JSON: %v", err)
|
|
|
logger.Error(errmsg)
|
|
logger.Error(errmsg)
|
|
|
}
|
|
}
|
|
|
- fmt.Println(VersionInfo)
|
|
|
|
|
return VersionInfo, true
|
|
return VersionInfo, true
|
|
|
}
|
|
}
|
|
|
return VersionInfo, false
|
|
return VersionInfo, false
|