Merge pull request #35 from Akilan1999/viewinterfaces

adds ipv6 index to config and a cli command to view network interface
This commit is contained in:
Akilan Selvacoumar
2021-06-25 22:08:43 +04:00
committed by GitHub
6 changed files with 53 additions and 2 deletions

View File

@@ -109,6 +109,15 @@ var CliAction = func(ctx *cli.Context) error {
}
}
//If the network interface flag is called
// Then all the network interfaces are displayed
if NetworkInterface {
err := p2p.ViewNetworkInterface()
if err != nil {
fmt.Print(err)
}
}
return nil
}

View File

@@ -19,6 +19,7 @@ var (
UpdateServerList bool
ServerList bool
SetDefaultConfig bool
NetworkInterface bool
)
var AppConfigFlags = []cli.Flag{
@@ -103,4 +104,10 @@ var AppConfigFlags = []cli.Flag{
EnvVars: []string{"SET_DEFAULT_CONFIG"},
Destination: &SetDefaultConfig,
},
&cli.BoolFlag{
Name: "NetworkInterfaces",
Usage: "Shows the network interface in your computer",
EnvVars: []string{"NETWORK_INTERFACE"},
Destination: &NetworkInterface,
},
}

View File

@@ -13,6 +13,7 @@ var (
"DefaultDockerFile": "/home/akilan/Documents/p2prendering/p2p-redering-computation/server/docker/containers/docker-ubuntu-sshd/",
"SpeedTestFile":"/etc/p2p-rendering/50.bin",
"NetworkInterface": "wlp0s20f3",
"NetworkInterfaceIPV6Index": "1",
}
configName = "config"
configType = "json"
@@ -26,6 +27,7 @@ type Config struct {
DefaultDockerFile string
SpeedTestFile string
NetworkInterface string
NetworkInterfaceIPV6Index int
}
// Exists reports whether the named file or directory exists.
@@ -53,6 +55,7 @@ func SetDefaults() error {
defaults["DockerContainers"] = defaultPath + "server/docker/containers/"
defaults["SpeedTestFile"] = defaultPath + "p2p/50.bin"
defaults["NetworkInterface"] = "wlp0s20f3"
defaults["NetworkInterfaceIPV6Index"] = "2"
//Paths to search for config file
configPaths = append(configPaths, defaultPath)

View File

@@ -3,7 +3,7 @@
{
"ipv4": "",
"ipv6": "2001:8f8:172d:ee93:7588:ad57:c351:3309",
"latency": 462096,
"latency": 1213198,
"download": 0,
"upload": 0
}

View File

@@ -186,7 +186,7 @@ func GetCurrentIPV6()(string,error){
if addresses[1].String() == "" {
return "",errors.New("IPV6 address not detected")
}
IP,_,err := net.ParseCIDR(addresses[1].String())
IP,_,err := net.ParseCIDR(addresses[Config.NetworkInterfaceIPV6Index].String())
if err != nil {
return "",err
}
@@ -194,6 +194,31 @@ func GetCurrentIPV6()(string,error){
return IP.String(), nil
}
// ViewNetworkInterface This function is created to view the network interfaces available
func ViewNetworkInterface() error {
ifaces, err := net.Interfaces()
if err != nil {
return err
}
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
return err
}
for index, a := range addrs {
switch v := a.(type) {
case *net.IPAddr:
fmt.Printf("(%v) %v : %s (%s)\n", index, i.Name, v, v.IP.DefaultMask())
case *net.IPNet:
fmt.Printf("(%v) %v : %s \n", index, i.Name, v)
}
}
}
return nil
}
// Ip4or6 Helper function to check if the IP address is IPV4 or
//IPV6 (https://socketloop.com/tutorials/golang-check-if-ip-address-is-version-4-or-6)
func Ip4or6(s string) string {

View File

@@ -58,3 +58,10 @@ func TestIpAddresses_RemoveDuplicates(t *testing.T) {
}
}
func TestViewNetworkInterface(t *testing.T) {
err := ViewNetworkInterface()
if err != nil {
t.Error()
}
}