-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
171 additions
and
171 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
// +build windows | ||
|
||
package conf | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package util | ||
|
||
import ( | ||
"net" | ||
) | ||
|
||
// Iface is net interface address info | ||
type Iface struct { | ||
net.HardwareAddr | ||
net.IP | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// +build !windows | ||
|
||
package util | ||
|
||
import ( | ||
"errors" | ||
"net" | ||
) | ||
|
||
// PickInterface pick the first active net interface | ||
func PickInterface() (*Iface, error) { | ||
ifaces, err := net.Interfaces() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for i := range ifaces { | ||
if len(ifaces[i].HardwareAddr) == 0 { | ||
continue | ||
} | ||
|
||
addrs, _ := ifaces[i].Addrs() | ||
for _, addr := range addrs { | ||
if ip := addr.(*net.IPNet).IP.To4(); ip != nil { | ||
return &Iface{ifaces[i].HardwareAddr, ip}, nil | ||
} | ||
} | ||
} | ||
return nil, errors.New("no valid interface") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package util | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestPickInterface(t *testing.T) { | ||
t.Skip("skip for some enviroment not have net interface") | ||
|
||
got, err := PickInterface() | ||
if err != nil { | ||
t.Errorf("PickInterface() error = %v", err) | ||
} else { | ||
t.Logf("PickInterface() got: MAC: %s IP: %s", got.HardwareAddr, got.IP) | ||
} | ||
} |
Oops, something went wrong.