mirror of
https://github.com/mickael-kerjean/filestash.git
synced 2025-11-01 10:56:31 +08:00
fix (config): concurrent issue
fatal error: sync: unlock of unlocked mutex goroutine 6 [running]: runtime.throw(0x1420562, 0x1e) /usr/local/go/src/runtime/panic.go:1117 +0x72 fp=0xc0000b5ec8 sp=0xc0000b5e98 pc=0x439e92 sync.throw(0x1420562, 0x1e) /usr/local/go/src/runtime/panic.go:1103 +0x35 fp=0xc0000b5ee8 sp=0xc0000b5ec8 pc=0x46e395 sync.(*Mutex).unlockSlow(0x1f97138, 0xffffffff) /usr/local/go/src/sync/mutex.go:196 +0xd8 fp=0xc0000b5f10 sp=0xc0000b5ee8 pc=0x47d458 sync.(*Mutex).Unlock(...) /usr/local/go/src/sync/mutex.go:190 github.com/mickael-kerjean/filestash/server/common.(*Configuration).Get(0x1f97120, 0x140a506, 0x14, 0x0) /home/mickael/Documents/projects/filestash/server/common/config.go:438 +0x113 fp=0xc0000b5f90 sp=0xc0000b5f10 pc=0x7ba313 github.com/mickael-kerjean/filestash/server/common.init.0.func1(0x0) /home/mickael/Documents/projects/filestash/server/common/api.go:16 +0x46 fp=0xc0000b5fc0 sp=0xc0000b5f90 pc=0x7c7906 github.com/mickael-kerjean/filestash/server/common.init.0.func3() /home/mickael/Documents/projects/filestash/server/common/api.go:40 +0x29 fp=0xc0000b5fe0 sp=0xc0000b5fc0 pc=0x7c7c49 runtime.goexit() /usr/local/go/src/runtime/asm_amd64.s:1371 +0x1 fp=0xc0000b5fe8 sp=0xc0000b5fe0 pc=0x4733e1 created by github.com/mickael-kerjean/filestash/server/common.init.0 /home/mickael/Documents/projects/filestash/server/common/api.go:39 +0x5a goroutine 1 [runnable, locked to thread]: os.(*File).Read(0xc000186000, 0xc000280400, 0x100, 0x100, 0x401, 0xc000280000, 0x400) /usr/local/go/src/os/file.go:113 +0x265 io.ReadAll(0x1677e40, 0xc000186000, 0x0, 0x1ff, 0xc000186000, 0x0, 0x0) /usr/local/go/src/io/io.go:633 +0xdf io/ioutil.ReadAll(...) /usr/local/go/src/io/ioutil/ioutil.go:27 github.com/mickael-kerjean/filestash/server/common.LoadConfig(0x0, 0x0, 0x0, 0x1fc66e0, 0x0) /home/mickael/Documents/projects/filestash/server/common/config_state.go:32 +0x85 github.com/mickael-kerjean/filestash/server/common.(*Configuration).Load(0x1f97120) /home/mickael/Documents/projects/filestash/server/common/config.go:214 +0x34 github.com/mickael-kerjean/filestash/server/common.init.1() /home/mickael/Documents/projects/filestash/server/common/config.go:53 +0x85
This commit is contained in:
@ -6,55 +6,22 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
isApiEnabled func() bool
|
||||
getApiKey func() string
|
||||
)
|
||||
|
||||
func init() {
|
||||
isApiEnabled = func() bool {
|
||||
return Config.Get("features.api.enabled").Schema(func(f *FormElement) *FormElement {
|
||||
if f == nil {
|
||||
f = &FormElement{}
|
||||
}
|
||||
f.Name = "enabled"
|
||||
f.Type = "boolean"
|
||||
f.Description = "Enable/Disable the API"
|
||||
f.Default = true
|
||||
return f
|
||||
}).Bool()
|
||||
}
|
||||
getApiKey = func() string {
|
||||
return Config.Get("features.api.api_key").Schema(func(f *FormElement) *FormElement {
|
||||
if f == nil {
|
||||
f = &FormElement{}
|
||||
}
|
||||
f.Name = "api_key"
|
||||
f.Type = "long_text"
|
||||
f.Description = "Format: '[mandatory:key] [optional:hostname]'. The hostname is used to enabled CORS for your application."
|
||||
f.Placeholder = "foobar *.filestash.app"
|
||||
return f
|
||||
}).String()
|
||||
}
|
||||
go func() {
|
||||
isApiEnabled()
|
||||
getApiKey()
|
||||
}()
|
||||
}
|
||||
|
||||
func VerifyApiKey(api_key string) (host string, err error) {
|
||||
if isApiEnabled() == false {
|
||||
isApiEnabled := Config.Get("features.api.enable").Bool()
|
||||
apiKey := Config.Get("feature.api.api_key").String()
|
||||
|
||||
if isApiEnabled == false {
|
||||
return "", NewError("Api is not enabled", 503)
|
||||
} else if api_key == os.Getenv("API_KEY") {
|
||||
} else if apiKey == os.Getenv("API_KEY") {
|
||||
return "*", nil
|
||||
}
|
||||
lines := strings.Split(getApiKey(), "\n")
|
||||
lines := strings.Split(apiKey, "\n")
|
||||
for _, line := range lines {
|
||||
line = regexp.MustCompile(` #.*`).ReplaceAllString(line, "") // remove comment
|
||||
chunks := strings.SplitN(line, " ", 2)
|
||||
if len(chunks) == 0 {
|
||||
continue
|
||||
} else if chunks[0] != api_key {
|
||||
} else if chunks[0] != apiKey {
|
||||
continue
|
||||
}
|
||||
if len(chunks) == 1 {
|
||||
|
||||
Reference in New Issue
Block a user