-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtool.go
95 lines (81 loc) · 1.91 KB
/
tool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package ri
import (
"errors"
"fmt"
"log"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
)
const (
CMD_RoutingInfo string = "RoutingInformation"
CMD_RequestPairing string = "RequestPairing"
CMD_HolePunching string = "HolePunching"
)
func CheckError(err error) {
if err != nil {
fmt.Println("Error: ", err)
os.Exit(0)
}
}
func GetNetworkMask(deviceName string) string {
switch runtime.GOOS {
case "darwin":
cmd := exec.Command("ipconfig", "getoption", deviceName, "subnet_mask")
out, err := cmd.CombinedOutput()
if err != nil {
return ""
}
nm := strings.Replace(string(out), "\n", "", -1)
log.Println("netmask=", nm, " OS=", runtime.GOOS)
return nm
default:
//return 255.255.255.0 as default
return "255.255.255.0"
}
}
func DecodeIpPort(localAddr string) (string, int) {
if len(localAddr) <= 0 {
return "", 0
}
ip := localAddr[:strings.Index(localAddr, ":")]
log.Println("ip:", ip)
//Get Port
iport := localAddr[strings.Index(localAddr, ":")+1:]
log.Println("port:", iport)
nPort, err := strconv.Atoi(iport)
if err != nil {
return "", 0
}
return ip, nPort
}
func EncodeRoutingInfo(id string, n *NetworkInfo) string {
if n == nil {
return ""
}
return fmt.Sprintf("%s %s,%s,%s,%d,%s", CMD_RoutingInfo, id, n.IIPv4, n.IIPv6, n.IPort, n.INetmask)
}
func DecodeRoutingInfo(ri string) (*NetworkInfo, error) {
log.Println("DecodeRoutingInfo:", ri)
if len(ri) == 0 || !strings.Contains(ri, CMD_RoutingInfo) {
return nil, errors.New("Invalid Input.")
}
var id, iip4, iip6, imask string
var iport int
ri = strings.Replace(ri, ",", " ", -1)
n, err := fmt.Sscanf(ri, CMD_RoutingInfo+" %s %s %s %d %s", &id, &iip4, &iip6, &iport, &imask)
if err != nil {
log.Println(n, err)
return nil, err
}
log.Println("Got:", n, "=>", id, iip4, iip6, imask, iport)
ni := new(NetworkInfo)
ni.IIPv4 = iip4
ni.IIPv6 = iip6
ni.Id = id
ni.IPort = iport
ni.INetmask = imask
return ni, nil
}