-
Notifications
You must be signed in to change notification settings - Fork 12
/
iperf.go
89 lines (80 loc) · 2 KB
/
iperf.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
package iperf
import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
"runtime"
)
var (
Debug = false
binaryDir = ""
binaryLocation = ""
)
func init() {
// Extract the binaries
if runtime.GOOS == "windows" {
err := extractWindowsEmbeddedBinaries()
if err != nil {
log.Fatalf("error initializing iperf: %v", err)
}
} else if runtime.GOOS == "darwin" {
err := extractMacEmbeddedBinaries()
if err != nil {
log.Fatalf("error initializing iperf: %v\n", err)
}
} else {
err := extractLinuxEmbeddedBinaries()
if err != nil {
log.Fatalf("error initializing iperf: %v", err)
}
}
}
func Cleanup() {
os.RemoveAll(binaryDir)
}
func ExtractBinaries() (err error) {
files := []string{"cygwin1.dll", "iperf3.exe", "iperf3", "iperf3.app"}
err = extractEmbeddedBinaries(files)
fmt.Printf("files extracted to %s\n", binaryDir)
return err
}
func extractWindowsEmbeddedBinaries() (err error) {
files := []string{"cygwin1.dll", "iperf3.exe"}
err = extractEmbeddedBinaries(files)
binaryLocation = path.Join(binaryDir, "iperf3.exe")
return err
}
func extractLinuxEmbeddedBinaries() (err error) {
files := []string{"iperf3"}
err = extractEmbeddedBinaries(files)
binaryLocation = path.Join(binaryDir, "iperf3")
return err
}
func extractMacEmbeddedBinaries() (err error) {
files := []string{"iperf3.app"}
err = extractEmbeddedBinaries(files)
binaryLocation = path.Join(binaryDir, "iperf3.app")
return err
}
func extractEmbeddedBinaries(files []string) (err error) {
binaryDir, err = ioutil.TempDir("", "goiperf")
if err != nil {
return fmt.Errorf("failed to create temporary iperf directory: %v", err)
}
for _, file := range files {
data, err := Asset(file)
if err != nil {
return fmt.Errorf("failed to extract embedded iperf: %v", err)
}
err = ioutil.WriteFile(path.Join(binaryDir, file), data, 0755)
if err != nil {
return fmt.Errorf("failed to save embedded iperf: %v", err)
}
if Debug {
log.Printf("extracted file: %s\n", path.Join(binaryDir, file))
}
}
return nil
}