package common import ( "encoding/json" "fmt" "github.com/tidwall/gjson" "github.com/tidwall/sjson" "io/ioutil" "os" "os/exec" "os/user" "path/filepath" "sync" "strings" ) var ( Config Configuration configPath string = filepath.Join(GetCurrentDir(), CONFIG_PATH + "config.json") ) type Configuration struct { onChange []ChangeListener mu sync.Mutex currentElement *FormElement cache KeyValueStore form []Form Conn []map[string]interface{} } type Form struct { Title string Form []Form Elmnts []FormElement } type FormElement struct { Id string `json:"id,omitempty"` Name string `json:"label"` Type string `json:"type"` Description string `json:"description,omitempty"` Placeholder string `json:"placeholder,omitempty"` Opts []string `json:"options,omitempty"` Target []string `json:"target,omitempty"` ReadOnly bool `json:"readonly"` Default interface{} `json:"default"` Value interface{} `json:"value"` MultiValue bool `json:"multi,omitempty"` Datalist []string `json:"datalist,omitempty"` Order int `json:"-"` Required bool `json:"required"` } func init() { Config = NewConfiguration() Config.Load() Config.Save() Config.Initialise() } func NewConfiguration() Configuration { return Configuration{ onChange: make([]ChangeListener, 0), mu: sync.Mutex{}, cache: NewKeyValueStore(), form: []Form{ Form{ Title: "general", Elmnts: []FormElement{ FormElement{Name: "name", Type: "text", Default: "Filestash", Description: "Name has shown in the UI", Placeholder: "Default: \"Filestash\""}, FormElement{Name: "port", Type: "number", Default: 8334, Description: "Port on which the application is available.", Placeholder: "Default: 8334"}, FormElement{Name: "host", Type: "text", Description: "The host people need to use to access this server", Placeholder: "Eg: \"demo.filestash.app\""}, FormElement{Name: "secret_key", Type: "password", Description: "The key that's used to encrypt and decrypt content. Update this settings will invalidate existing user sessions and shared links, use with caution!"}, FormElement{Name: "force_ssl", Type: "boolean", Description: "Enable the web security mechanism called 'Strict Transport Security'"}, FormElement{Name: "editor", Type: "select", Default: "emacs", Opts: []string{"base", "emacs", "vim"}, Description: "Keybinding to be use in the editor. Default: \"emacs\""}, FormElement{Name: "fork_button", Type: "boolean", Default: true, Description: "Display the fork button in the login screen"}, FormElement{Name: "display_hidden", Type: "boolean", Default: false, Description: "Should files starting with a dot be visible by default?"}, FormElement{Name: "auto_connect", Type: "boolean", Default: false, Description: "User don't have to click on the login button if an admin is prefilling a unique backend"}, FormElement{Name: "remember_me", Type: "boolean", Default: true, Description: "Visiblity of the remember me button on the login screen"}, FormElement{Name: "upload_button", Type: "boolean", Default: false, Description: "Display the upload button on any device"}, }, }, Form{ Title: "features", Form: []Form{ Form{ Title: "share", Elmnts: []FormElement{ FormElement{Name: "enable", Type: "boolean", Default: true, Description: "Enable/Disable the share feature"}, }, }, }, }, Form{ Title: "log", Elmnts: []FormElement{ FormElement{Name: "enable", Type: "enable", Target: []string{"log_level"}, Default: true}, FormElement{Name: "level", Type: "select", Default: "INFO", Opts: []string{"DEBUG", "INFO", "WARNING", "ERROR"}, Id: "log_level", Description: "Default: \"INFO\". This setting determines the level of detail at which log events are written to the log file"}, FormElement{Name: "telemetry", Type: "boolean", Default: false, Description: "We won't share anything with any third party. This will only to be used to improve Filestash"}, }, }, Form{ Title: "email", Elmnts: []FormElement{ FormElement{Name: "server", Type: "text", Default: "smtp.gmail.com", Description: "Address of the SMTP email server.", Placeholder: "Default: smtp.gmail.com"}, FormElement{Name: "port", Type: "number", Default: 587, Description: "Port of the SMTP email server. Eg: 587", Placeholder: "Default: 587"}, FormElement{Name: "username", Type: "text", Description: "The username for authenticating to the SMTP server.", Placeholder: "Eg: username@gmail.com"}, FormElement{Name: "password", Type: "password", Description: "The password associated with the SMTP username.", Placeholder: "Eg: Your google password"}, FormElement{Name: "from", Type: "text", Description: "Email address visible on sent messages.", Placeholder: "Eg: username@gmail.com"}, }, }, Form{ Title: "auth", Elmnts: []FormElement{ FormElement{Name: "admin", Type: "bcrypt", Default: "", Description: "Password of the admin section."}, }, Form: []Form{ Form{ Title: "custom", Elmnts: []FormElement{ FormElement{Name: "client_secret", Type: "password"}, FormElement{Name: "client_id", Type: "text"}, FormElement{Name: "sso_domain", Type: "text"}, }, }, }, }, }, Conn: make([]map[string]interface{}, 0), } } func (this Form) MarshalJSON() ([]byte, error) { return []byte(this.toJSON(func(el FormElement) string { a, e := json.Marshal(el) if e != nil { return "" } return string(a) })), nil } func (this Form) toJSON(fn func(el FormElement) string) string { formatKey := func(str string) string { return strings.Replace(str, " ", "_", -1) } ret := "" if this.Title != "" { ret = fmt.Sprintf("%s\"%s\":", ret, formatKey(this.Title)) } for i := 0; i < len(this.Elmnts); i++ { if i == 0 { ret = fmt.Sprintf("%s{", ret) } ret = fmt.Sprintf("%s\"%s\":%s", ret, formatKey(this.Elmnts[i].Name), fn(this.Elmnts[i])) if i == len(this.Elmnts) - 1 && len(this.Form) == 0 { ret = fmt.Sprintf("%s}", ret) } if i != len(this.Elmnts) - 1 || len(this.Form) != 0 { ret = fmt.Sprintf("%s,", ret) } } for i := 0; i < len(this.Form); i++ { if i == 0 && len(this.Elmnts) == 0 { ret = fmt.Sprintf("%s{", ret) } ret = ret + this.Form[i].toJSON(fn) if i == len(this.Form) - 1 { ret = fmt.Sprintf("%s}", ret) } if i != len(this.Form) - 1 { ret = fmt.Sprintf("%s,", ret) } } if len(this.Form) == 0 && len(this.Elmnts) == 0 { ret = fmt.Sprintf("%s{}", ret) } return ret } type FormIterator struct { Path string *FormElement } func (this *Form) Iterator() []FormIterator { slice := make([]FormIterator, 0) for i, _ := range this.Elmnts { slice = append(slice, FormIterator{ strings.ToLower(this.Title), &this.Elmnts[i], }) } for _, node := range this.Form { r := node.Iterator() if this.Title != "" { for i := range r { r[i].Path = strings.ToLower(this.Title) + "." + r[i].Path } } slice = append(r, slice...) } return slice } func (this *Configuration) Load() { file, err := os.OpenFile(configPath, os.O_RDONLY, os.ModePerm) if err != nil { Log.Warning("Can't read from config file") return } defer file.Close() cFile, err := ioutil.ReadAll(file) if err != nil { Log.Warning("Can't parse config file") return } // Extract enabled backends this.Conn = func(cFile []byte) []map[string]interface{} { var d struct { Connections []map[string]interface{} `json:"connections"` } json.Unmarshal(cFile, &d) return d.Connections }(cFile) // Hydrate Config with data coming from the config file d := JsonIterator(string(cFile)) for i := range d { this = this.Get(d[i].Path) if this.Interface() != d[i].Value { this.currentElement.Value = d[i].Value } } this.cache.Clear() Log.SetVisibility(this.Get("log.level").String()) go func() { // Trigger all the event listeners for i:=0; i %+v", this.currentElement) } } this.mu.Unlock() return this } func (this *Configuration) Set(value interface{}) *Configuration { if this.currentElement == nil { return this } this.mu.Lock() this.cache.Clear() if this.currentElement.Value != value { this.currentElement.Value = value this.Save() } this.mu.Unlock() return this } func (this Configuration) String() string { val := this.Interface() switch val.(type) { case string: return val.(string) case []byte: return string(val.([]byte)) } return "" } func (this Configuration) Int() int { val := this.Interface() switch val.(type) { case float64: return int(val.(float64)) case int64: return int(val.(int64)) case int: return val.(int) } return 0 } func (this Configuration) Bool() bool { val := this.Interface() switch val.(type) { case bool: return val.(bool) } return false } func (this Configuration) Interface() interface{} { if this.currentElement == nil { return nil } val := this.currentElement.Value if val == nil { val = this.currentElement.Default } return val } func (this Configuration) MarshalJSON() ([]byte, error) { form := this.form form = append(form, Form{ Title: "constant", Elmnts: []FormElement{ FormElement{Name: "user", Type: "boolean", ReadOnly: true, Value: func() string{ if u, err := user.Current(); err == nil { if u.Username != "" { return u.Username } return u.Name } return "n/a" }()}, FormElement{Name: "emacs", Type: "boolean", ReadOnly: true, Value: func() bool { if _, err := exec.LookPath("emacs"); err == nil { return true } return false }()}, FormElement{Name: "pdftotext", Type: "boolean", ReadOnly: true, Value: func() bool { if _, err := exec.LookPath("pdftotext"); err == nil { return true } return false }()}, }, }) return Form{ Form: form, }.MarshalJSON() } func (this *Configuration) ListenForChange() ChangeListener { this.mu.Lock() change := ChangeListener{ Id: QuickString(20), Listener: make(chan interface{}, 0), } this.onChange = append(this.onChange, change) this.mu.Unlock() return change } func (this *Configuration) UnlistenForChange(c ChangeListener) { this.mu.Lock() for i:=0; i= 0 { close(this.onChange[i].Listener) this.onChange[i] = this.onChange[len(this.onChange)-1] this.onChange = this.onChange[:len(this.onChange)-1] } break } } this.mu.Unlock() } type ChangeListener struct { Id string Listener chan interface{} }