mirror of https://github.com/namecoin/ncdns
Add cookie support
©! I, Hugo Landau <hlandau@devever.net>, hereby licence these changes under the ©! licence with SHA256 hash ©! fd80a26fbb3f644af1fa994134446702932968519797227e07a1368dea80f0bc.pull/18/head
parent
aa564761b5
commit
1e6ccf7405
@ -0,0 +1,59 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func readCookieFile(path string) (username, password string, err error) {
|
||||
b, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
s := strings.TrimSpace(string(b))
|
||||
parts := strings.SplitN(s, ":", 2)
|
||||
if len(parts) != 2 {
|
||||
err = fmt.Errorf("malformed cookie file")
|
||||
return
|
||||
}
|
||||
|
||||
username, password = parts[0], parts[1]
|
||||
return
|
||||
}
|
||||
|
||||
func cookieRetriever(path string) func() (username, password string, err error) {
|
||||
lastCheckTime := time.Time{}
|
||||
lastModTime := time.Time{}
|
||||
|
||||
curUsername, curPassword := "", ""
|
||||
var curError error
|
||||
|
||||
doUpdate := func() {
|
||||
if !lastCheckTime.IsZero() && time.Now().Before(lastCheckTime.Add(30*time.Second)) {
|
||||
return
|
||||
}
|
||||
|
||||
lastCheckTime = time.Now()
|
||||
|
||||
st, err := os.Stat(path)
|
||||
if err != nil {
|
||||
curError = err
|
||||
return
|
||||
}
|
||||
|
||||
modTime := st.ModTime()
|
||||
if !modTime.Equal(lastModTime) {
|
||||
lastModTime = modTime
|
||||
curUsername, curPassword, curError = readCookieFile(path)
|
||||
}
|
||||
}
|
||||
|
||||
return func() (username, password string, err error) {
|
||||
doUpdate()
|
||||
return curUsername, curPassword, curError
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue