-
Notifications
You must be signed in to change notification settings - Fork 1
/
tunnel.go
235 lines (190 loc) · 5.42 KB
/
tunnel.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
228
229
230
231
232
233
234
235
package lokal
import (
"errors"
"fmt"
"math/rand"
"strings"
"time"
"github.com/fatih/color"
)
type TunnelType string
const (
TunnelTypeHTTP TunnelType = "HTTP"
)
type Tunnel struct {
Lokal *Lokal `json:",omitempty"`
ID string `json:",omitempty"`
Name string `json:"name"`
TunnelType TunnelType `json:"tunnel_type"`
LocalAddress string `json:"local_address"`
ServerID string `json:"server_id"`
AddressTunnel string `json:"address_tunnel"`
AddressTunnelPort int64 `json:"address_tunnel_port"`
AddressPublic string `json:"address_public"`
AddressMdns string `json:"address_mdns"`
Inspect bool `json:"inspect"`
Options Options `json:"options"`
ignoreDuplicate bool
startupBanner bool
}
type Options struct {
BasicAuth []string `json:"basic_auth"`
CIDRAllow []string `json:"cidr_allow"`
CIDRDeny []string `json:"cidr_deny"`
RequestHeaderAdd []string `json:"request_header_add"`
RequestHeaderRemove []string `json:"request_header_remove"`
ResponseHeaderAdd []string `json:"response_header_add"`
ResponseHeaderRemove []string `json:"response_header_remove"`
HeaderKey []string `json:"header_key"`
}
func (l *Lokal) NewTunnel() *Tunnel {
return &Tunnel{Lokal: l}
}
func (t *Tunnel) SetLocalAddress(localAddress string) *Tunnel {
t.LocalAddress = localAddress
return t
}
func (t *Tunnel) SetTunnelType(tunnelType TunnelType) *Tunnel {
t.TunnelType = tunnelType
return t
}
func (t *Tunnel) SetInspection(inspect bool) *Tunnel {
t.Inspect = inspect
return t
}
func (t *Tunnel) SetLANAddress(lanAddress string) *Tunnel {
lanAddress = strings.TrimSuffix(lanAddress, ".local")
t.AddressMdns = lanAddress
return t
}
func (t *Tunnel) SetPublicAddress(publicAddress string) *Tunnel {
t.AddressPublic = publicAddress
return t
}
func (t *Tunnel) SetName(name string) *Tunnel {
t.Name = name
return t
}
func (t *Tunnel) IgnoreDuplicate() *Tunnel {
t.ignoreDuplicate = true
return t
}
func (t *Tunnel) ShowStartupBanner() *Tunnel {
t.startupBanner = true
return t
}
func (t *Tunnel) Create() (*Tunnel, error) {
if t.AddressMdns == "" && t.AddressPublic == "" {
return nil, errors.New("please enable either lan address or random/custom public url")
}
resp := struct {
Success bool `json:"success"`
Message string `json:"message"`
Tunnel []Tunnel `json:"data"`
}{}
_, err := t.Lokal.rest.
R().
SetBody(t).
SetResult(&resp).
SetError(&resp).
Post("/api/tunnel/start")
if err != nil {
return nil, err
}
if len(resp.Tunnel) == 0 {
return nil, errors.New("tunnel creation failing")
}
if !resp.Success {
if t.ignoreDuplicate && strings.HasSuffix(resp.Message, "address is already being used") {
t.AddressPublic = resp.Tunnel[0].AddressPublic
t.AddressMdns = resp.Tunnel[0].AddressMdns
t.ID = resp.Tunnel[0].ID
t.showStartupBanner()
return t, nil
}
return nil, errors.New(resp.Message)
}
t.AddressPublic = resp.Tunnel[0].AddressPublic
t.AddressMdns = resp.Tunnel[0].AddressMdns
t.ID = resp.Tunnel[0].ID
t.showStartupBanner()
return t, nil
}
func (t *Tunnel) GetLANAddress() (string, error) {
if t.AddressMdns == "" {
return "", errors.New("lan address is not being set")
}
if !strings.HasSuffix(t.AddressMdns, ".local") {
return t.AddressMdns + ".local", nil
}
return t.AddressMdns, nil
}
func (t *Tunnel) GetPublicAddress() (string, error) {
if t.AddressPublic == "" {
return "", errors.New("public address is not requested by client")
}
if t.AddressPublic == "" {
return "", errors.New("unable to assign public address")
}
if t.TunnelType != "HTTP" {
if !strings.Contains(t.AddressPublic, ":") {
go t.updatePublicURLPort()
return "", errors.New("tunnel is using a random port, but it has not been assigned yet. please try again later")
}
}
return t.AddressPublic, nil
}
func (t *Tunnel) updatePublicURLPort() error {
resp := struct {
Success bool `json:"success"`
Message string `json:"message"`
Tunnel []Tunnel `json:"data"`
}{}
_, err := t.Lokal.rest.R().
SetResult(&resp).
SetError(&resp).
Get("/api/tunnel/info/" + t.ID)
if err != nil {
return err
}
if !resp.Success {
return errors.New(resp.Message)
}
if len(resp.Tunnel) == 0 {
return errors.New("could not get tunnel info")
}
if !strings.Contains(resp.Tunnel[0].AddressPublic, ":") {
return errors.New("could not get assigned port")
}
t.AddressPublic = resp.Tunnel[0].AddressPublic
return nil
}
func (t *Tunnel) showStartupBanner() {
if !t.startupBanner {
return
}
var banner = `
__ _ _
/ / ___ | | ____ _| | ___ ___
/ / / _ \| |/ / _ | | / __|/ _ \
/ /__| (_) | < (_| | |_\__ \ (_) |
\____/\___/|_|\_\__,_|_(_)___/\___/ `
colors := []func(format string, a ...interface{}) string{
color.HiMagentaString,
color.HiBlueString,
color.HiCyanString,
color.HiGreenString,
color.HiRedString,
}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
fmt.Println(colors[r.Intn(len(colors))](banner))
fmt.Println()
fmt.Println(color.RedString("Minimum Lokal Client"), "\t"+ServerMinVersion)
if val, err := t.GetPublicAddress(); err == nil {
fmt.Println(color.CyanString("Public Address"), "\t\thttps://"+val)
}
if val, err := t.GetLANAddress(); err == nil {
fmt.Println(color.GreenString("LAN Address"), "\t\thttps://"+val)
}
fmt.Println()
}