mirror of
https://github.com/bettercap/bettercap
synced 2025-08-20 21:43:18 -07:00
misc: small fix or general refactoring i did not bother commenting
This commit is contained in:
parent
dfd139cab1
commit
3e8ae0339b
3 changed files with 125 additions and 81 deletions
85
network/aliases.go
Normal file
85
network/aliases.go
Normal file
|
@ -0,0 +1,85 @@
|
|||
package network
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/evilsocket/bettercap-ng/core"
|
||||
)
|
||||
|
||||
var fileName, _ = core.ExpandPath("~/bettercap.aliases")
|
||||
|
||||
type Aliases struct {
|
||||
sync.Mutex
|
||||
|
||||
data map[string]string
|
||||
}
|
||||
|
||||
func LoadAliases() (err error, aliases *Aliases) {
|
||||
aliases = &Aliases{
|
||||
data: make(map[string]string),
|
||||
}
|
||||
|
||||
if core.Exists(fileName) {
|
||||
var file *os.File
|
||||
|
||||
file, err = os.Open(fileName)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
parts := strings.SplitN(line, " ", 2)
|
||||
mac := core.Trim(parts[0])
|
||||
alias := core.Trim(parts[1])
|
||||
aliases.data[mac] = alias
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (a *Aliases) saveUnlocked() error {
|
||||
data := ""
|
||||
for mac, alias := range a.data {
|
||||
data += fmt.Sprintf("%s %s\n", mac, alias)
|
||||
}
|
||||
return ioutil.WriteFile(fileName, []byte(data), 0644)
|
||||
}
|
||||
|
||||
func (a *Aliases) Save() error {
|
||||
a.Lock()
|
||||
defer a.Unlock()
|
||||
|
||||
return a.saveUnlocked()
|
||||
}
|
||||
|
||||
func (a *Aliases) Get(mac string) string {
|
||||
a.Lock()
|
||||
defer a.Unlock()
|
||||
|
||||
if alias, found := a.data[mac]; found == true {
|
||||
return alias
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (a *Aliases) Set(mac, alias string) error {
|
||||
a.Lock()
|
||||
defer a.Unlock()
|
||||
|
||||
if alias != "" {
|
||||
a.data[mac] = alias
|
||||
} else {
|
||||
delete(a.data, mac)
|
||||
}
|
||||
|
||||
return a.saveUnlocked()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue