Browse code

bump hashicorp/go-sockaddr v1.0.2

full diff: https://github.com/hashicorp/go-sockaddr/compare/6d291a969b86c4b633730bfc6b8b9d64c3aafed9...v1.0.2

Relevant changes:
- hashicorp/go-sockaddr#25 Add android os
- hashicorp/go-sockaddr#28 Add go.mod

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 492945c2d553c1e8a13ad9c06aa2eb000d03f631)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2019/09/03 18:54:32
Showing 5 changed files
... ...
@@ -47,7 +47,7 @@ github.com/hashicorp/go-msgpack                     71c2886f5a673a35f909803f38ec
47 47
 github.com/hashicorp/memberlist                     3d8438da9589e7b608a83ffac1ef8211486bcb7c
48 48
 github.com/sean-/seed                               e2103e2c35297fb7e17febb81e49b312087a2372
49 49
 github.com/hashicorp/errwrap                        8a6fb523712970c966eefc6b39ed2c5e74880354 # v1.0.0
50
-github.com/hashicorp/go-sockaddr                    6d291a969b86c4b633730bfc6b8b9d64c3aafed9
50
+github.com/hashicorp/go-sockaddr                    c7188e74f6acae5a989bdc959aa779f8b9f42faf # v1.0.2
51 51
 github.com/hashicorp/go-multierror                  886a7fbe3eb1c874d46f623bfa70af45f425b3d1 # v1.0.0
52 52
 github.com/hashicorp/serf                           598c54895cc5a7b1a24a398d635e8c0ea0959870
53 53
 github.com/docker/libkv                             458977154600b9f23984d9f4b82e79570b5ae12b
54 54
new file mode 100644
... ...
@@ -0,0 +1,8 @@
0
+module github.com/hashicorp/go-sockaddr
1
+
2
+require (
3
+	github.com/hashicorp/errwrap v1.0.0
4
+	github.com/mitchellh/cli v1.0.0
5
+	github.com/mitchellh/go-wordwrap v1.0.0
6
+	github.com/ryanuber/columnize v2.1.0+incompatible
7
+)
... ...
@@ -1197,23 +1197,46 @@ func parseDefaultIfNameFromRoute(routeOut string) (string, error) {
1197 1197
 // parseDefaultIfNameFromIPCmd parses the default interface from ip(8) for
1198 1198
 // Linux.
1199 1199
 func parseDefaultIfNameFromIPCmd(routeOut string) (string, error) {
1200
+	parsedLines := parseIfNameFromIPCmd(routeOut)
1201
+	for _, parsedLine := range parsedLines {
1202
+		if parsedLine[0] == "default" &&
1203
+			parsedLine[1] == "via" &&
1204
+			parsedLine[3] == "dev" {
1205
+			ifName := strings.TrimSpace(parsedLine[4])
1206
+			return ifName, nil
1207
+		}
1208
+	}
1209
+
1210
+	return "", errors.New("No default interface found")
1211
+}
1212
+
1213
+// parseDefaultIfNameFromIPCmdAndroid parses the default interface from ip(8) for
1214
+// Android.
1215
+func parseDefaultIfNameFromIPCmdAndroid(routeOut string) (string, error) {
1216
+	parsedLines := parseIfNameFromIPCmd(routeOut)
1217
+	if (len(parsedLines) > 0) {
1218
+		ifName := strings.TrimSpace(parsedLines[0][4])
1219
+		return ifName, nil
1220
+	}
1221
+
1222
+	return "", errors.New("No default interface found")
1223
+}
1224
+
1225
+
1226
+// parseIfNameFromIPCmd parses interfaces from ip(8) for
1227
+// Linux.
1228
+func parseIfNameFromIPCmd(routeOut string) [][]string {
1200 1229
 	lines := strings.Split(routeOut, "\n")
1201 1230
 	re := whitespaceRE.Copy()
1231
+	parsedLines := make([][]string, 0, len(lines))
1202 1232
 	for _, line := range lines {
1203 1233
 		kvs := re.Split(line, -1)
1204 1234
 		if len(kvs) < 5 {
1205 1235
 			continue
1206 1236
 		}
1207
-
1208
-		if kvs[0] == "default" &&
1209
-			kvs[1] == "via" &&
1210
-			kvs[3] == "dev" {
1211
-			ifName := strings.TrimSpace(kvs[4])
1212
-			return ifName, nil
1213
-		}
1237
+		parsedLines = append(parsedLines, kvs)
1214 1238
 	}
1215
-
1216
-	return "", errors.New("No default interface found")
1239
+	return parsedLines
1217 1240
 }
1218 1241
 
1219 1242
 // parseDefaultIfNameWindows parses the default interface from `netstat -rn` and
1220 1243
new file mode 100644
... ...
@@ -0,0 +1,34 @@
0
+package sockaddr
1
+
2
+import (
3
+	"errors"
4
+	"os/exec"
5
+)
6
+
7
+type routeInfo struct {
8
+	cmds map[string][]string
9
+}
10
+
11
+// NewRouteInfo returns a Android-specific implementation of the RouteInfo
12
+// interface.
13
+func NewRouteInfo() (routeInfo, error) {
14
+	return routeInfo{
15
+		cmds: map[string][]string{"ip": {"/system/bin/ip", "route", "get", "8.8.8.8"}},
16
+	}, nil
17
+}
18
+
19
+// GetDefaultInterfaceName returns the interface name attached to the default
20
+// route on the default interface.
21
+func (ri routeInfo) GetDefaultInterfaceName() (string, error) {
22
+	out, err := exec.Command(ri.cmds["ip"][0], ri.cmds["ip"][1:]...).Output()
23
+	if err != nil {
24
+		return "", err
25
+	}
26
+
27
+
28
+	var ifName string
29
+	if ifName, err = parseDefaultIfNameFromIPCmdAndroid(string(out)); err != nil {
30
+		return "", errors.New("No default interface found")
31
+	}
32
+	return ifName, nil
33
+}
... ...
@@ -1,3 +1,5 @@
1
+// +build !android
2
+
1 3
 package sockaddr
2 4
 
3 5
 import (