/
main.go
227 lines (213 loc) · 5.19 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package main
import (
"bytes"
"flag"
"fmt"
"log"
"os"
"os/exec"
"sort"
"strconv"
"strings"
"time"
"github.com/cghdev/gotunl/pkg/gotunl"
aw "github.com/deanishe/awgo"
"github.com/tidwall/gjson"
)
var (
iconConnected = &aw.Icon{Value: "icons/locked.png"}
iconDisconnected = &aw.Icon{Value: "icons/unlocked.png"}
iconConnecting = &aw.Icon{Value: "icons/unlocked2.png"}
iconIssue = &aw.Icon{Value: "icons/issue.png"}
iconDisconnect = &aw.Icon{Value: "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/ToolbarDeleteIcon.icns"}
)
var version = "1.4.1"
var wf *aw.Workflow
type connections struct {
status string
clientAddr string
serverAddr string
timestamp int64
id string
name string
}
func listConnections(gt *gotunl.Gotunl, search string) []connections { // add output format as json?
cons := gt.GetConnections()
c := []connections{}
ptmp := connections{}
for pid, p := range gt.Profiles {
ptmp.status = "Disconnected"
if strings.Contains(cons, pid) {
ptmp.status = strings.Title(gjson.Get(cons, pid+".status").String())
ptmp.serverAddr = gjson.Get(cons, pid+".server_addr").String()
ptmp.clientAddr = gjson.Get(cons, pid+".client_addr").String()
ptmp.timestamp = gjson.Get(cons, pid+".timestamp").Int()
}
name := gjson.Get(p.Conf, "name").String()
if search == "" || strings.Contains(strings.ToLower(name), strings.ToLower(search)) {
ptmp.id = strconv.Itoa(p.ID)
ptmp.name = name
c = append(c, ptmp)
}
if ptmp.status == "Connecting" {
wf.Rerun(3)
}
}
sort.Slice(c, func(i, j int) bool {
return c[i].id < c[j].id
})
sort.Slice(c, func(i, j int) bool {
return c[i].status != "Disconnected"
})
return c
}
func disconnect(gt *gotunl.Gotunl, id string) {
if id == "all" {
gt.StopConnections()
} else {
for pid, p := range gt.Profiles {
if id == gjson.Get(p.Conf, "name").String() || id == strconv.Itoa(p.ID) {
gt.DisconnectProfile(pid)
}
}
}
}
func connect(gt *gotunl.Gotunl, id string) {
for pid, p := range gt.Profiles {
name := gjson.Get(p.Conf, "name").String()
if id == name || id == strconv.Itoa(p.ID) {
_, auth := gt.GetProfile(pid)
user := ""
password := ""
if auth != "" {
if auth[len(auth)-3:] == "pin" {
var otp string
user = "pritunl"
pass := popup("Enter the PIN: ", true)
if auth == "otp_pin" {
otp = popup("Enter the OTP code: ", false)
}
password = string(pass) + otp
}
if user == "" {
user = popup("Enter the username: ", false)
}
if password == "" {
password = popup("Enter the password: ", true)
}
}
gt.ConnectProfile(pid, user, password)
}
}
}
func runOsa(command string) bytes.Buffer {
var out bytes.Buffer
cmd := exec.Command("osascript")
cmd.Stdin = bytes.NewBuffer([]byte(command))
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
log.Fatal("There as an error: " + err.Error())
}
return out
}
func popup(msg string, pwd bool) string {
var out bytes.Buffer
hidden := ""
if pwd {
hidden = "with hidden answer"
}
command := fmt.Sprintf(`Tell application "System Events" to display dialog "%v" default answer "" %v`, msg, hidden)
out = runOsa(command)
res := strings.Split(out.String(), ":")[2]
return strings.Trim(res, "\n")
}
func formatSince(t time.Time) string {
Day := 24 * time.Hour
ts := time.Since(t)
sign := time.Duration(1)
var days, hours, minutes, seconds string
if ts < 0 {
sign = -1
ts = -ts
}
d := sign * (ts / Day)
ts = ts % Day
h := ts / time.Hour
ts = ts % time.Hour
m := ts / time.Minute
ts = ts % time.Minute
s := ts / time.Second
if d > 0 {
days = fmt.Sprintf("%d days ", d)
}
if h > 0 {
hours = fmt.Sprintf("%d hrs ", h)
}
if m > 0 {
minutes = fmt.Sprintf("%d mins ", m)
}
seconds = fmt.Sprintf("%d secs", s)
return fmt.Sprintf("%v%v%v%v", days, hours, minutes, seconds)
}
func run() {
gt := *gotunl.New()
l := flag.Bool("l", false, "List connections")
c := flag.String("c", "", "Connect to profile ID or Name")
d := flag.String("d", "", "Disconnect profile or \"all\"")
s := flag.String("s", "", "Search for profile")
flag.Parse()
if len(os.Args) < 2 {
flag.Usage()
os.Exit(1)
}
if *l {
conns := listConnections(>, *s)
ic := &aw.Icon{}
action := ""
sub := ""
submod := ""
for _, co := range conns {
switch co.status {
case "Connected":
ic = iconConnected
action = "-d " + co.id
sub = "↩ to disconnect, ⌘ for details"
ts := time.Unix(co.timestamp, 0)
since := formatSince(ts)
submod = "Server: " + co.serverAddr + " | Client: " + co.clientAddr + " | " + since
case "Disconnected":
ic = iconDisconnected
action = "-c " + co.id
sub = "↩ to connect"
case "Connecting":
ic = iconConnecting
action = "-d " + co.id
sub = "Connecting... ↩ to cancel"
}
it := wf.NewItem(co.name).
Icon(ic).
Subtitle(sub).
Arg(action).
Valid(true)
if co.status == "Connected" {
it.NewModifier("cmd").Subtitle(submod)
}
}
if len(conns) == 0 {
wf.NewItem(`No connections found ¯\_(ツ)_/¯`).
Icon(aw.IconWarning)
}
}
if *c != "" {
connect(>, *c)
}
if *d != "" {
disconnect(>, *d)
}
wf.SendFeedback()
}
func main() {
wf = aw.New()
wf.Run(run)
}