mirror of
https://github.com/ngoduykhanh/wireguard-ui.git
synced 2025-05-05 22:22:21 +03:00
Update util.go
This commit is contained in:
parent
885d6ba558
commit
bec3187367
1 changed files with 41 additions and 0 deletions
41
util/util.go
41
util/util.go
|
@ -4,9 +4,14 @@ import (
|
|||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/ngoduykhanh/wireguard-ui/store"
|
||||
"golang.org/x/mod/sumdb/dirhash"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
@ -462,3 +467,39 @@ func LookupEnvOrStrings(key string, defaultVal []string) []string {
|
|||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
// GetCurrentHash returns current hashes
|
||||
func GetCurrentHash(db store.IStore) (string, string) {
|
||||
hashClients, _ := dirhash.HashDir(path.Join(db.GetPath(), "clients"), "prefix", dirhash.Hash1)
|
||||
files := append([]string(nil), "prefix/global_settings.json", "prefix/interfaces.json", "prefix/keypair.json")
|
||||
|
||||
osOpen := func(name string) (io.ReadCloser, error) {
|
||||
return os.Open(filepath.Join(path.Join(db.GetPath(), "server"), strings.TrimPrefix(name, "prefix")))
|
||||
}
|
||||
hashServer, _ := dirhash.Hash1(files, osOpen)
|
||||
|
||||
return hashClients, hashServer
|
||||
}
|
||||
|
||||
func HashesChanged(db store.IStore) bool {
|
||||
old, _ := db.GetHashes()
|
||||
oldClient := old.Client
|
||||
oldServer := old.Server
|
||||
newClient, newServer := GetCurrentHash(db)
|
||||
|
||||
if oldClient != newClient {
|
||||
fmt.Println("Hash for client differs")
|
||||
return true
|
||||
}
|
||||
if oldServer != newServer {
|
||||
fmt.Println("Hash for server differs")
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func UpdateHashes(db store.IStore) error {
|
||||
var clientServerHashes model.ClientServerHashes
|
||||
clientServerHashes.Client, clientServerHashes.Server = GetCurrentHash(db)
|
||||
return db.SaveHashes(clientServerHashes)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue