mirror of
https://github.com/bettercap/bettercap
synced 2025-07-14 00:53:46 -07:00
new: module parameters now accept <iface-name> that will be resolved to the interface IP address
This commit is contained in:
parent
16891c4048
commit
d0b5c34763
1 changed files with 34 additions and 0 deletions
|
@ -98,6 +98,8 @@ const ParamIfaceAddress6 = "<interface address6>"
|
||||||
const ParamSubnet = "<entire subnet>"
|
const ParamSubnet = "<entire subnet>"
|
||||||
const ParamRandomMAC = "<random mac>"
|
const ParamRandomMAC = "<random mac>"
|
||||||
|
|
||||||
|
var ParamIfaceNameParser = regexp.MustCompile("<([a-zA-Z0-9]{2,16})>")
|
||||||
|
|
||||||
func (p ModuleParam) parse(s *Session, v string) string {
|
func (p ModuleParam) parse(s *Session, v string) string {
|
||||||
switch v {
|
switch v {
|
||||||
case ParamIfaceName:
|
case ParamIfaceName:
|
||||||
|
@ -112,6 +114,38 @@ func (p ModuleParam) parse(s *Session, v string) string {
|
||||||
hw := make([]byte, 6)
|
hw := make([]byte, 6)
|
||||||
rand.Read(hw)
|
rand.Read(hw)
|
||||||
v = net.HardwareAddr(hw).String()
|
v = net.HardwareAddr(hw).String()
|
||||||
|
default:
|
||||||
|
// check the <iface> case
|
||||||
|
if m := ParamIfaceNameParser.FindStringSubmatch(v); len(m) == 2 {
|
||||||
|
// does it match any interface?
|
||||||
|
name := m[1]
|
||||||
|
if iface, err := net.InterfaceByName(name); err == nil {
|
||||||
|
if addrs, err := iface.Addrs(); err == nil {
|
||||||
|
var ipv4, ipv6 *net.IP
|
||||||
|
// get first ipv4 and ipv6 addresses
|
||||||
|
for _, addr := range addrs {
|
||||||
|
if ipv4 == nil {
|
||||||
|
if ipv4Addr := addr.(*net.IPNet).IP.To4(); ipv4Addr != nil {
|
||||||
|
ipv4 = &ipv4Addr
|
||||||
|
}
|
||||||
|
} else if ipv6 == nil {
|
||||||
|
if ipv6Addr := addr.(*net.IPNet).IP.To16(); ipv6Addr != nil {
|
||||||
|
ipv6 = &ipv6Addr
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// prioritize ipv4, fallback on ipv6 if assigned
|
||||||
|
if ipv4 != nil {
|
||||||
|
v = ipv4.String()
|
||||||
|
} else if ipv6 != nil {
|
||||||
|
v = ipv6.String()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return v
|
return v
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue