-
Notifications
You must be signed in to change notification settings - Fork 31
/
tunnel.go
60 lines (51 loc) · 1.05 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
package client
import (
"errors"
"fmt"
conn "github.com/inconshreveable/go-tunnel/conn"
proto "github.com/inconshreveable/go-tunnel/proto"
"net"
"sync/atomic"
)
// A Tunnel is a net.Listener that Accept()'s connections from a
// remote machine.
type Tunnel struct {
url string
sess *Session
bindOpts interface{}
bindExtra interface{}
bindResp *proto.BindResp
accept chan conn.Conn
proto string
closed int32
}
func (t *Tunnel) Accept() (net.Conn, error) {
select {
case conn, ok := <-t.accept:
if !ok {
return nil, errors.New("Tunnel closed")
}
return conn, nil
}
return nil, errors.New("Unexpected select condition")
}
func (t *Tunnel) Close() error {
if !atomic.CompareAndSwapInt32(&t.closed, 0, 1) {
return fmt.Errorf("Already closed")
}
close(t.accept)
return t.sess.unlisten(t)
}
func (t *Tunnel) Addr() net.Addr {
return &Addr{net: t.proto, addr: t.url}
}
type Addr struct {
net string
addr string
}
func (a *Addr) Network() string {
return a.net
}
func (a *Addr) String() string {
return a.addr
}