-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
113 lines (91 loc) · 2.71 KB
/
main.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"regexp"
"strings"
"github.com/keybase/go-keychain"
"github.com/mdp/qrterminal"
)
const airportBin = "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport"
func main() {
auth, ssid := getWifiInfo()
if ssid == "" {
fmt.Fprintln(os.Stderr, "Could not retrieve SSID.")
os.Exit(1)
}
fmt.Printf("\033[90m … getting password for \"%s\". \033[39m\n", ssid)
fmt.Println("\033[90m … keychain prompt incoming. \033[39m")
password, err := getWifiPassword(ssid)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not retrieve password. %v\n", err)
os.Exit(1)
}
fmt.Printf("\033[96m ✓ \"%s\" \033[39m\n\n", password)
qr := composeWifiString(auth, ssid, password)
displayQRCode(qr)
}
func getWifiPassword(ssid string) (password string, err error) {
query := keychain.NewItem()
query.SetSecClass(keychain.SecClassGenericPassword)
query.SetDescription("AirPort network password")
query.SetAccount(ssid)
query.SetMatchLimit(keychain.MatchLimitOne)
query.SetReturnData(true)
results, err := keychain.QueryItem(query)
if err != nil {
return "", err
}
if len(results) != 1 {
return "", errors.New("keychain has no password")
}
return string(results[0].Data), err
}
func getWifiInfo() (authenticationType string, ssid string) {
output, _ := exec.Command(airportBin, "-I").Output()
authenticationType = getWifiInfoFromRegex(string(output), "link auth: (.*)")
if strings.Contains(authenticationType, "wpa") {
authenticationType = "WPA"
} else if strings.Contains(authenticationType, "wep") {
authenticationType = "WEP"
}
ssid = getWifiInfoFromRegex(string(output), " SSID: (.*)")
return
}
func getWifiInfoFromRegex(output string, expr string) string {
regex, _ := regexp.Compile(expr)
match := regex.FindStringSubmatch(output)
return match[1]
}
func displayQRCode(qr string) {
config := qrterminal.Config{
Level: qrterminal.M,
Writer: os.Stdout,
BlackChar: qrterminal.BLACK,
WhiteChar: qrterminal.WHITE,
QuietZone: 1,
}
qrterminal.GenerateWithConfig(qr, config)
}
func composeWifiString(authenticationType string, ssid string, password string) string {
if authenticationType == "WPA" || authenticationType == "WEP" {
authenticationType = "T:" + authenticationType
} else {
authenticationType = ""
}
ssid = escapeMECARDString(ssid)
ssid = "S:" + ssid
password = escapeMECARDString(password)
if authenticationType != "" {
password = "P:" + password
}
return fmt.Sprintf("WIFI:%s;%s;%s;", authenticationType, ssid, password)
}
func escapeMECARDString(s string) string {
s = strings.Replace(s, `\`, `\\`, -1)
s = strings.Replace(s, `;`, `\;`, -1)
s = strings.Replace(s, `"`, `\"`, -1)
return s
}