forked from 99designs/aws-vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathec2alias_windows.go
55 lines (44 loc) · 1.42 KB
/
ec2alias_windows.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
//go:build windows
// +build windows
package server
import (
"fmt"
"os/exec"
"strings"
)
var alreadyRegisteredLocalised = []string{
"The object already exists",
"Das Objekt ist bereits vorhanden",
"El objeto ya existe",
}
var runAsAdministratorLocalised = []string{
"Run as administrator",
// truncate before 'Umlaut' to avoid encoding problems coming from Windows cmd
"Als Administrator ausf",
"Ejecutar como administrador",
}
func msgFound(localised []string, toTest string) bool {
for _, value := range localised {
if strings.Contains(toTest, value) {
return true
}
}
return false
}
func runAndWrapAdminErrors(name string, arg ...string) ([]byte, error) {
out, err := exec.Command(name, arg...).CombinedOutput()
if msgFound(runAsAdministratorLocalised, string(out)) {
err = fmt.Errorf("Creation of network alias for server mode requires elevated permissions, run as administrator", err)
}
return out, err
}
func installEc2EndpointNetworkAlias() ([]byte, error) {
out, err := runAndWrapAdminErrors("netsh", "interface", "ipv4", "add", "address", "Loopback Pseudo-Interface 1", "169.254.169.254", "255.255.0.0")
if msgFound(alreadyRegisteredLocalised, string(out)) {
return []byte{}, nil
}
return out, err
}
func removeEc2EndpointNetworkAlias() ([]byte, error) {
return runAndWrapAdminErrors("netsh", "interface", "ipv4", "delete", "address", "Loopback Pseudo-Interface 1", "169.254.169.254", "255.255.0.0")
}