mirror of
https://github.com/bettercap/bettercap
synced 2025-08-14 18:57:17 -07:00
new: new -env-file for env persistance (closes #81)
This commit is contained in:
parent
27457ac774
commit
5509a58e52
3 changed files with 50 additions and 3 deletions
|
@ -1,7 +1,9 @@
|
|||
package session
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"sort"
|
||||
"strconv"
|
||||
"sync"
|
||||
|
@ -21,7 +23,7 @@ type Environment struct {
|
|||
sess *Session
|
||||
}
|
||||
|
||||
func NewEnvironment(s *Session) *Environment {
|
||||
func NewEnvironment(s *Session, envFile string) *Environment {
|
||||
env := &Environment{
|
||||
Padding: 0,
|
||||
Data: make(map[string]string),
|
||||
|
@ -29,9 +31,42 @@ func NewEnvironment(s *Session) *Environment {
|
|||
cbs: make(map[string]SetCallback),
|
||||
}
|
||||
|
||||
if envFile != "" {
|
||||
envFile, _ := core.ExpandPath(envFile)
|
||||
if core.Exists(envFile) {
|
||||
if err := env.Load(envFile); err != nil {
|
||||
fmt.Printf("Error while loading %s: %s\n", envFile, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return env
|
||||
}
|
||||
|
||||
func (env *Environment) Load(fileName string) error {
|
||||
env.Lock()
|
||||
defer env.Unlock()
|
||||
|
||||
raw, err := ioutil.ReadFile(fileName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return json.Unmarshal(raw, &env.Data)
|
||||
}
|
||||
|
||||
func (env *Environment) Save(fileName string) error {
|
||||
env.Lock()
|
||||
defer env.Unlock()
|
||||
|
||||
raw, err := json.Marshal(env.Data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return ioutil.WriteFile(fileName, raw, 0644)
|
||||
}
|
||||
|
||||
func (env *Environment) Has(name string) bool {
|
||||
env.Lock()
|
||||
defer env.Unlock()
|
||||
|
|
|
@ -142,7 +142,7 @@ func New() (*Session, error) {
|
|||
}
|
||||
}
|
||||
|
||||
s.Env = NewEnvironment(s)
|
||||
s.Env = NewEnvironment(s, *s.Options.EnvFile)
|
||||
s.Events = NewEventPool(*s.Options.Debug, *s.Options.Silent)
|
||||
|
||||
s.registerCoreHandlers()
|
||||
|
@ -246,6 +246,13 @@ func (s *Session) Close() {
|
|||
s.Firewall.Restore()
|
||||
s.Queue.Stop()
|
||||
|
||||
if *s.Options.EnvFile != "" {
|
||||
envFile, _ := core.ExpandPath(*s.Options.EnvFile)
|
||||
if err := s.Env.Save(envFile); err != nil {
|
||||
fmt.Printf("Error while storing the environment to %s: %s", envFile, err)
|
||||
}
|
||||
}
|
||||
|
||||
if *s.Options.CpuProfile != "" {
|
||||
pprof.StopCPUProfile()
|
||||
}
|
||||
|
@ -304,7 +311,6 @@ func (s *Session) setupSignals() {
|
|||
}
|
||||
|
||||
func (s *Session) setupEnv() {
|
||||
s.Env.Set(PromptVariable, DefaultPrompt)
|
||||
s.Env.Set("iface.index", fmt.Sprintf("%d", s.Interface.Index))
|
||||
s.Env.Set("iface.name", s.Interface.Name())
|
||||
s.Env.Set("iface.ipv4", s.Interface.IpAddress)
|
||||
|
@ -313,6 +319,10 @@ func (s *Session) setupEnv() {
|
|||
s.Env.Set("gateway.address", s.Gateway.IpAddress)
|
||||
s.Env.Set("gateway.mac", s.Gateway.HwAddress)
|
||||
|
||||
if found, v := s.Env.Get(PromptVariable); found == false || v == "" {
|
||||
s.Env.Set(PromptVariable, DefaultPrompt)
|
||||
}
|
||||
|
||||
dbg := "false"
|
||||
if *s.Options.Debug {
|
||||
dbg = "true"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue