Skip to content

Commit

Permalink
#15 minor improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
mikan committed Jan 27, 2021
1 parent b22900f commit 328d15f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
28 changes: 14 additions & 14 deletions resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ import (
"strings"
)

func ip2mac(ip string, adapter adapter) (string, error) {
func ip2mac(ip string) (string, error) {
var pingCmd, arpCmd *exec.Cmd
switch runtime.GOOS {
case "windows":
pingCmd = exec.Command("ping", "-n", "1", ip)
prepareBackgroundCommand(pingCmd)
arpCmd = exec.Command("arp", "-a", ip, "-N", adapter.ip)
arpCmd = exec.Command("arp", "-a", ip)
prepareBackgroundCommand(arpCmd)
case "darwin":
pingCmd = exec.Command("ping", "-b", adapter.name, "-c", "1", ip)
arpCmd = exec.Command("arp", "-i", adapter.name, ip)
pingCmd = exec.Command("ping", "-c", "1", ip)
arpCmd = exec.Command("arp", ip)
default:
pingCmd = exec.Command("ping", "-I", adapter.name, "-c", "1", ip)
pingCmd = exec.Command("ping", "-c", "1", ip)
if _, err := exec.LookPath("ip"); err == nil {
arpCmd = exec.Command("ip", "neigh", "show", "dev", adapter.name, ip)
arpCmd = exec.Command("ip", "neigh", "show", ip)
} else {
arpCmd = exec.Command("arp", "-i", adapter.name, ip)
arpCmd = exec.Command("arp", "-i", ip)
}
}
if err := pingCmd.Run(); err != nil {
Expand All @@ -43,23 +43,23 @@ func ip2mac(ip string, adapter adapter) (string, error) {
return strings.ReplaceAll(strings.ToLower(string(matches[0])), "-", ":"), nil
}

func ip2macWithoutAdapterSelect(ip string) (string, error) {
func ip2macWithAdapter(ip string, adapter adapter) (string, error) {
var pingCmd, arpCmd *exec.Cmd
switch runtime.GOOS {
case "windows":
pingCmd = exec.Command("ping", "-n", "1", ip)
prepareBackgroundCommand(pingCmd)
arpCmd = exec.Command("arp", "-a", ip)
arpCmd = exec.Command("arp", "-a", ip, "-N", adapter.ip)
prepareBackgroundCommand(arpCmd)
case "darwin":
pingCmd = exec.Command("ping", "-c", "1", ip)
arpCmd = exec.Command("arp", ip)
pingCmd = exec.Command("ping", "-b", adapter.name, "-c", "1", ip)
arpCmd = exec.Command("arp", "-i", adapter.name, ip)
default:
pingCmd = exec.Command("ping", "-c", "1", ip)
pingCmd = exec.Command("ping", "-I", adapter.name, "-c", "1", ip)
if _, err := exec.LookPath("ip"); err == nil {
arpCmd = exec.Command("ip", "neigh", "show", ip)
arpCmd = exec.Command("ip", "neigh", "show", "dev", adapter.name, ip)
} else {
arpCmd = exec.Command("arp", "-i", ip)
arpCmd = exec.Command("arp", "-i", adapter.name, ip)
}
}
if err := pingCmd.Run(); err != nil {
Expand Down
7 changes: 3 additions & 4 deletions ui_ip2mac.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

func newIP2MACTab(w fyne.Window, adapters []adapter) fyne.CanvasObject {
// adapter select
adapterLabel := widget.NewLabel("Network adapter:")
adapterNames := adapterNames(adapters)
adapterEntry := widget.NewSelectEntry(adapterNames)
if len(adapterNames) > 0 {
Expand Down Expand Up @@ -53,9 +52,9 @@ func newIP2MACTab(w fyne.Window, adapters []adapter) fyne.CanvasObject {
var mac string
var err error
if adapterAutoCheck.Checked {
mac, err = ip2macWithoutAdapterSelect(ipEntry.Text)
mac, err = ip2mac(ipEntry.Text)
} else {
mac, err = ip2mac(ipEntry.Text, findAdapter(adapters, adapterEntry.Text))
mac, err = ip2macWithAdapter(ipEntry.Text, findAdapter(adapters, adapterEntry.Text))
}
if err != nil {
macEntry.SetText("ERROR: " + err.Error())
Expand All @@ -80,7 +79,7 @@ func newIP2MACTab(w fyne.Window, adapters []adapter) fyne.CanvasObject {
return container.NewVBox(
widget.NewLabel("Target IP address:"),
ipEntry,
adapterLabel,
widget.NewLabel("Network adapter:"),
adapterAutoCheck,
adapterEntry,
resolveButton,
Expand Down

0 comments on commit 328d15f

Please sign in to comment.