-
Notifications
You must be signed in to change notification settings - Fork 4
/
localtunnel.go
247 lines (207 loc) · 5.13 KB
/
localtunnel.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
236
237
238
239
240
241
242
243
244
245
246
247
// Package localtunnel implements a client library for https://localtunnel.me
//
// localtunnel allows you to expose your localhost to the world for easy testing and sharing!
//
// Exposing a local port:
//
// import "github.com/jweslley/localtunnel"
//
// ...
//
// var port := 8000
// var tunnel := localtunnel.NewLocalTunnel(port)
// var err := tunnel.Open()
// if (err != nil) {
// fmt.Printf("your url is: %s\n", tunnel.URL())
// }
//
// ...
//
// tunnel.Close()
//
//
package localtunnel
import (
"encoding/json"
"fmt"
"net"
"net/http"
"strconv"
"sync"
)
// A Client is an localtunnel client.
type Client struct {
endPoint string
}
// NewLocalTunnel create a tunnel for a server in a given port from localhost.
func (c *Client) NewLocalTunnel(port int) *Tunnel {
return c.NewTunnel("localhost", port)
}
// NewTunnel create a tunnel for a server in a given host and port.
func (c *Client) NewTunnel(host string, port int) *Tunnel {
return &Tunnel{c: c, localHost: host, localPort: port}
}
// NewClient returns a client using the given end point.
func NewClient(url string) *Client {
return &Client{endPoint: url}
}
// DefaultClient is the default Client and is used by NewLocalTunnel and NewTunnel.
var DefaultClient = NewClient("https://localtunnel.me")
// NewLocalTunnel create a tunnel for a server in a given port from localhost using the DefaultClient.
func NewLocalTunnel(port int) *Tunnel {
return DefaultClient.NewTunnel("localhost", port)
}
// NewTunnel create a tunnel for a server in a given host and port using the DefaultClient.
func NewTunnel(host string, port int) *Tunnel {
return DefaultClient.NewTunnel(host, port)
}
// Tunnel forwards remote requests to another server, typically to a port on localhost.
type Tunnel struct {
c *Client
m sync.Mutex
closeCh chan struct{}
remoteHost string
remotePort int
localHost string
localPort int
subdomain string
url string
maxConn int
}
func (t *Tunnel) RemoteHost() string { return t.remoteHost }
func (t *Tunnel) RemotePort() int { return t.remotePort }
func (t *Tunnel) LocalHost() string { return t.localHost }
func (t *Tunnel) LocalPort() int { return t.localPort }
func (t *Tunnel) Subdomain() string { return t.subdomain }
// URL at which the localtunnel is exposed.
func (t *Tunnel) URL() string { return t.url }
// MaxConn is the maximum number of connections allowed.
func (t *Tunnel) MaxConn() int { return t.maxConn }
// Open setup the tunnel creating connections between the remote and local servers.
func (t *Tunnel) Open() error {
return t.OpenAs("?new")
}
// Open setup the tunnel creating connections between the remote and local servers with a custom subdomain.
func (t *Tunnel) OpenAs(subdomain string) error {
t.m.Lock()
defer t.m.Unlock()
err := t.setup(subdomain)
if err != nil {
return err
}
t.closeCh = make(chan struct{})
t.establish()
return nil
}
// Close closes all tunnel's connections.
func (t *Tunnel) Close() {
t.m.Lock()
defer t.m.Unlock()
t.remoteHost = ""
t.remotePort = 0
t.maxConn = 0
t.subdomain = ""
t.url = ""
close(t.closeCh)
}
// Closing is a channel which is closed when the tunnel is closed.
func (t *Tunnel) Closing() <-chan struct{} {
return t.closeCh
}
func (t *Tunnel) setup(subdomain string) error {
url := fmt.Sprintf(t.c.endPoint+"/%s", subdomain)
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
var i struct {
ID string `json:"id,omitempty"`
URL string `json:"url,omitempty"`
Port int `json:"port,omitempty"`
MaxConn int `json:"max_conn_count,omitempty"`
}
d := json.NewDecoder(resp.Body)
err = d.Decode(&i)
if err != nil {
return err
}
t.remoteHost = resp.Request.URL.Host
t.remotePort = i.Port
t.maxConn = i.MaxConn
t.subdomain = i.ID
t.url = i.URL
return nil
}
func (t *Tunnel) establish() {
for i := 0; i < t.MaxConn(); i++ {
c := &conn{t: t}
go c.open()
}
}
type conn struct {
t *Tunnel
remoteConn net.Conn
localConn net.Conn
}
func (c *conn) open() {
var err error
c.remoteConn, err = net.Dial("tcp", net.JoinHostPort(c.t.RemoteHost(), strconv.Itoa(c.t.RemotePort())))
if err != nil {
c.t.Close()
return
}
c.localConn, err = net.Dial("tcp", net.JoinHostPort(c.t.LocalHost(), strconv.Itoa(c.t.LocalPort())))
if err != nil {
c.t.Close()
return
}
c.pipe()
}
func (c *conn) close() {
if c.localConn != nil {
c.localConn.Close()
}
if c.remoteConn != nil {
c.remoteConn.Close()
}
}
func (c *conn) pipe() {
errorCh := make(chan error)
remoteCh := chanFromConn(c.remoteConn, errorCh)
localCh := chanFromConn(c.localConn, errorCh)
for {
select {
case b := <-remoteCh:
c.localConn.Write(b)
case b := <-localCh:
c.remoteConn.Write(b)
case <-errorCh:
c.close()
c.open()
return
case <-c.t.closeCh:
c.close()
return
}
}
}
func chanFromConn(conn net.Conn, errorCh chan error) chan []byte {
c := make(chan []byte)
go func() {
b := make([]byte, 1024)
for {
n, err := conn.Read(b)
if n > 0 {
res := make([]byte, n)
copy(res, b[:n])
c <- res
}
if err != nil {
errorCh <- err
break
}
}
}()
return c
}