forked from cghdev/gotunl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
222 lines (209 loc) · 5.25 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
package main
import (
"flag"
"fmt"
"os"
"runtime"
"sort"
"strconv"
"strings"
"time"
"github.com/bug3/gotunl/pkg/gotunl"
"github.com/olekukonko/tablewriter"
"github.com/tidwall/gjson"
)
var version = "1.2.4"
var color = map[string]string{
"red": "\x1b[31;1m",
"green": "\x1b[32;1m",
"reset": "\x1b[0m"}
type connections struct {
id int
name string
status string
timestamp int64
clientAddr string
serverAddr string
}
func listConnections(gt *gotunl.Gotunl, output string) { // add output format as json?
if len(gt.Profiles) == 0 {
fmt.Println("No profiles found in Pritunl")
os.Exit(1)
}
cons := gt.GetConnections()
c := []connections{}
stdis := ""
stcon := ""
anycon := false
for pid, p := range gt.Profiles {
ptmp := connections{}
if runtime.GOOS != "windows" {
stdis = color["red"] + "Disconnected" + color["reset"]
stcon = color["green"] + "Connected" + color["reset"]
} else {
stdis = "Disconnected"
stcon = "Connected"
}
ptmp.status = stdis
ptmp.name = gjson.Get(p.Conf, "name").String()
ptmp.id = p.ID
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()
if ptmp.status == "Connected" {
ptmp.status = stcon
anycon = true
}
}
c = append(c, ptmp)
}
sort.SliceStable(c, func(i, j int) bool {
return c[i].id < c[j].id
})
table := tablewriter.NewWriter(os.Stdout)
if anycon {
table.SetHeader([]string{"ID", "Name", "Status", "Connected for", "Client IP", "Server IP"})
} else {
table.SetHeader([]string{"ID", "Name", "Status"})
}
setOutputFormat(table, output)
for _, p := range c {
since := ""
if p.timestamp > 0 {
ts := time.Unix(p.timestamp, 0)
since = formatSince(ts)
}
pid := strconv.Itoa(p.id)
if anycon {
table.Append([]string{pid, p.name, p.status, since, p.clientAddr, p.serverAddr})
} else {
table.Append([]string{pid, p.name, p.status})
}
}
table.Render()
}
func setOutputFormat(tbl *tablewriter.Table, output string) *tablewriter.Table {
switch output {
case "tsv":
tbl.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
tbl.SetAlignment(tablewriter.ALIGN_LEFT)
tbl.SetCenterSeparator("")
tbl.SetColumnSeparator("\t")
tbl.SetRowSeparator("")
tbl.SetHeaderLine(false)
tbl.SetBorder(false)
tbl.SetTablePadding("\t")
tbl.SetAutoFormatHeaders(true)
tbl.SetAutoWrapText(false)
tbl.SetAutoFormatHeaders(false)
case "table":
tbl.SetAutoFormatHeaders(false)
default:
fmt.Println("Define output format")
os.Exit(1)
}
return tbl
}
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, pin string, otp string, user string, pass string) {
for pid, p := range gt.Profiles {
if id == gjson.Get(p.Conf, "name").String() || id == strconv.Itoa(p.ID) {
gt.ConnectProfile(pid, pin, otp, user, pass)
}
}
}
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 usage(a *flag.Flag) {
if a.Name == "l" || a.Name == "v" {
fmt.Printf(" -%v \t\t%v\n", a.Name, a.Usage)
} else if a.Name == "o" {
fmt.Printf(" -%v <output>\t%v\n", a.Name, a.Usage)
} else if a.Name == "pin" {
fmt.Printf(" -%v <pin>\t%v\n", a.Name, a.Usage)
} else if a.Name == "otp" {
fmt.Printf(" -%v <otp>\t%v\n", a.Name, a.Usage)
} else if a.Name == "un" {
fmt.Printf(" -%v <user>\t%v\n", a.Name, a.Usage)
} else if a.Name == "pass" {
fmt.Printf(" -%v <pass>\t%v\n", a.Name, a.Usage)
} else {
fmt.Printf(" -%v <profile>\t%v\n", a.Name, a.Usage)
}
}
func main() {
gt := *gotunl.New()
flag.Usage = func() {
fmt.Print("Pritunl command line client\n\n")
fmt.Println("Usage:")
flag.VisitAll(usage)
}
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\"")
o := flag.String("o", "table", "Output format table|tsv (default is table)")
pin := flag.String("pin", "", "Pin code")
otp := flag.String("otp", "", "Otp code")
un := flag.String("un", "", "User name")
pass := flag.String("pass", "", "Password")
v := flag.Bool("v", false, "Show version")
flag.Parse()
if len(os.Args) < 2 {
flag.Usage()
os.Exit(1)
}
if *l {
listConnections(>, *o)
os.Exit(0)
}
if *c != "" {
connect(>, *c, *pin, *otp, *un, *pass)
os.Exit(0)
}
if *d != "" {
disconnect(>, *d)
os.Exit(0)
}
if *v {
fmt.Println(version)
}
os.Exit(1)
}