diff --git a/session/module_param.go b/session/module_param.go index 43aaeaa3..e2aacdb6 100644 --- a/session/module_param.go +++ b/session/module_param.go @@ -98,6 +98,8 @@ const ParamIfaceAddress6 = "" const ParamSubnet = "" const ParamRandomMAC = "" +var ParamIfaceNameParser = regexp.MustCompile("<([a-zA-Z0-9]{2,16})>") + func (p ModuleParam) parse(s *Session, v string) string { switch v { case ParamIfaceName: @@ -112,6 +114,38 @@ func (p ModuleParam) parse(s *Session, v string) string { hw := make([]byte, 6) rand.Read(hw) v = net.HardwareAddr(hw).String() + default: + // check the 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