Add Wake On Lan Feature

This commit is contained in:
ned3y2k 2022-02-27 23:51:58 +09:00
parent 7988015983
commit 9d5fc3f72b
9 changed files with 374 additions and 322 deletions

View file

@ -38,12 +38,10 @@ func New(dbPath string) (*JsonDB, error) {
func (o *JsonDB) Init() error {
var clientPath string = path.Join(o.dbPath, "clients")
var serverPath string = path.Join(o.dbPath, "server")
var wakeOnLanListPath string = path.Join(o.dbPath, model.WakeOnLanHostCollectionName)
var serverInterfacePath string = path.Join(serverPath, "interfaces.json")
var serverKeyPairPath string = path.Join(serverPath, "keypair.json")
var globalSettingPath string = path.Join(serverPath, "global_settings.json")
var userPath string = path.Join(serverPath, "users.json")
// create directories if they do not exist
if _, err := os.Stat(clientPath); os.IsNotExist(err) {
os.MkdirAll(clientPath, os.ModePerm)
@ -101,10 +99,6 @@ func (o *JsonDB) Init() error {
o.conn.Write("server", "users", user)
}
if _, err := os.Stat(wakeOnLanListPath); os.IsNotExist(err) {
os.MkdirAll(wakeOnLanListPath, os.ModePerm)
}
return nil
}
@ -227,54 +221,3 @@ func (o *JsonDB) SaveServerKeyPair(serverKeyPair model.ServerKeypair) error {
func (o *JsonDB) SaveGlobalSettings(globalSettings model.GlobalSetting) error {
return o.conn.Write("server", "global_settings", globalSettings)
}
func (o *JsonDB) GetWakeOnLanHosts() ([]model.WakeOnLanHost, error) {
var hosts []model.WakeOnLanHost
// read all client json file in "hosts" directory
records, err := o.conn.ReadAll(model.WakeOnLanHostCollectionName)
if err != nil {
return hosts, err
}
// build the ClientData list
for _, f := range records {
host := model.WakeOnLanHost{}
// get client info
if err := json.Unmarshal([]byte(f), &host); err != nil {
return hosts, fmt.Errorf("cannot decode client json structure: %v", err)
}
// create the list of hosts and their qrcode data
hosts = append(hosts, host)
}
return hosts, nil
}
func (o *JsonDB) GetWakeOnLanHost(macAddress string) (*model.WakeOnLanHost, error) {
host := &model.WakeOnLanHost{
MacAddress: macAddress,
}
resourceName, err := host.ResolveResourceName()
if err != nil {
return nil, err
}
err = o.conn.Read(model.WakeOnLanHostCollectionName, resourceName, host)
return host, err
}
func (o *JsonDB) DeleteWakeOnHostLanHost(macAddress string) error {
return o.conn.Delete(model.WakeOnLanHostCollectionName, macAddress)
}
func (o *JsonDB) SaveWakeOnLanHost(host model.WakeOnLanHost) error {
resourceName, err := host.ResolveResourceName()
if err != nil {
return err
}
return o.conn.Write(model.WakeOnLanHostCollectionName, resourceName, host)
}

View file

@ -0,0 +1,78 @@
package jsondb
import (
"encoding/json"
"fmt"
"github.com/ngoduykhanh/wireguard-ui/model"
)
func (o *JsonDB) GetWakeOnLanHosts() ([]model.WakeOnLanHost, error) {
var hosts []model.WakeOnLanHost
// read all client json file in "hosts" directory
records, err := o.conn.ReadAll(model.WakeOnLanHostCollectionName)
if err != nil {
return hosts, err
}
// build the ClientData list
for _, f := range records {
host := model.WakeOnLanHost{}
// get client info
if err := json.Unmarshal([]byte(f), &host); err != nil {
return hosts, fmt.Errorf("cannot decode client json structure: %v", err)
}
// create the list of hosts and their qrcode data
hosts = append(hosts, host)
}
return hosts, nil
}
func (o *JsonDB) GetWakeOnLanHost(macAddress string) (*model.WakeOnLanHost, error) {
host := &model.WakeOnLanHost{
MacAddress: macAddress,
}
resourceName, err := host.ResolveResourceName()
if err != nil {
return nil, err
}
err = o.conn.Read(model.WakeOnLanHostCollectionName, resourceName, host)
if err != nil {
host = nil
}
return host, err
}
func (o *JsonDB) DeleteWakeOnHostLanHost(macAddress string) error {
host := &model.WakeOnLanHost{
MacAddress: macAddress,
}
resourceName, err := host.ResolveResourceName()
if err != nil {
return err
}
return o.conn.Delete(model.WakeOnLanHostCollectionName, resourceName)
}
func (o *JsonDB) SaveWakeOnLanHost(host model.WakeOnLanHost) error {
resourceName, err := host.ResolveResourceName()
if err != nil {
return err
}
return o.conn.Write(model.WakeOnLanHostCollectionName, resourceName, host)
}
func (o *JsonDB) DeleteWakeOnHost(host model.WakeOnLanHost) error {
resourceName, err := host.ResolveResourceName()
if err != nil {
return err
}
return o.conn.Delete(model.WakeOnLanHostCollectionName, resourceName)
}

View file

@ -20,4 +20,5 @@ type IStore interface {
GetWakeOnLanHost(macAddress string) (*model.WakeOnLanHost, error)
DeleteWakeOnHostLanHost(macAddress string) error
SaveWakeOnLanHost(host model.WakeOnLanHost) error
DeleteWakeOnHost(host model.WakeOnLanHost) error
}