This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtunnel.go
74 lines (63 loc) · 1.49 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
package secretun
import (
"fmt"
"reflect"
)
type ClientChan struct {
R chan *Packet
W chan *Packet
End chan error
}
func NewClientChan() (c ClientChan) {
c.R = make(chan *Packet)
c.W = make(chan *Packet)
c.End = make(chan error)
return c
}
func (c *ClientChan) Close() {
close(c.R)
close(c.W)
close(c.End)
}
type ClientTunnel interface {
Init(Config) error
Start(ClientChan) error
Shutdown() error
}
type ServerTunnel interface {
Init(Config) error
Accept() (ClientChan, error)
Shutdown() error
}
var clientTunnels = map[string]reflect.Type{}
var serverTunnels = map[string]reflect.Type{}
func RegisterClientTunnel(name string, i interface{}) {
t := reflect.TypeOf(i)
if _, ok := reflect.New(t).Interface().(ClientTunnel); !ok {
panic(fmt.Errorf("invalid ClientTunnel: %s", name))
}
clientTunnels[name] = t
}
func NewClientTunnel(name string) (c ClientTunnel, err error) {
t, ok := clientTunnels[name]
if !ok {
err = fmt.Errorf("invalid ClientTunnel: %s", name)
return
}
return reflect.New(t).Interface().(ClientTunnel), nil
}
func RegisterServerTunnel(name string, i interface{}) {
t := reflect.TypeOf(i)
if _, ok := reflect.New(t).Interface().(ServerTunnel); !ok {
panic(fmt.Errorf("invalid ServerTunnel: %s", name))
}
serverTunnels[name] = t
}
func NewServerTunnel(name string) (c ServerTunnel, err error) {
t, ok := serverTunnels[name]
if !ok {
err = fmt.Errorf("invalid ServerTunnel: %s", name)
return
}
return reflect.New(t).Interface().(ServerTunnel), nil
}