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
|
package hook
import (
"crypto/md5"
"crypto/rand"
"errors"
"io"
"sync"
"time"
"tunnel/pkg/server/env"
"tunnel/pkg/server/queue"
)
const authTimeout = 5 * time.Second
const saltSize = 16
const hashSize = md5.Size
type authHook struct {
m sync.Map
}
type auth struct {
h *authHook
secret string
salt struct {
self string
peer string
}
hash string
readSaltDone chan struct{}
readHashDone chan struct{}
ready chan struct{}
cancel chan struct{}
tmr *time.Timer
}
var errDupSalt = errors.New("peer repeats salt")
var errAuthFail = errors.New("peer auth fail")
var errTimeout = errors.New("timeout")
func (a *auth) Init() error {
b := make([]byte, saltSize)
if _, err := rand.Read(b); err != nil {
return err
}
a.salt.self = string(b)
a.h.m.Store(a.salt.self, struct{}{})
return nil
}
func (a *auth) hashSum(c string) string {
h := md5.New()
io.WriteString(h, c)
io.WriteString(h, a.secret)
return string(h.Sum(nil))
}
func (a *auth) sync(c chan struct{}) error {
select {
case <-a.tmr.C:
return errTimeout
case <-a.cancel:
return io.EOF
case <-c:
return nil
}
}
func (a *auth) Send(rq, wq queue.Q) error {
w := wq.Writer()
io.WriteString(w, a.salt.self)
if err := a.sync(a.readSaltDone); err != nil {
return err
}
if _, ok := a.h.m.Load(a.salt.peer); ok {
return errDupSalt
}
io.WriteString(w, a.hashSum(a.salt.peer))
if err := a.sync(a.readHashDone); err != nil {
return err
}
if a.hash != a.hashSum(a.salt.self) {
close(a.cancel)
return errAuthFail
}
close(a.ready)
return queue.IoCopy(rq.Reader(), w)
}
func (a *auth) read(r io.Reader, n int, s *string) error {
b := make([]byte, n)
if _, err := io.ReadFull(r, b); err != nil {
close(a.cancel)
return err
}
*s = string(b)
return nil
}
func (a *auth) Recv(rq, wq queue.Q) error {
r := rq.Reader()
if err := a.read(r, saltSize, &a.salt.peer); err != nil {
return err
}
close(a.readSaltDone)
if err := a.read(r, hashSize, &a.hash); err != nil {
return err
}
close(a.readHashDone)
if err := a.sync(a.ready); err != nil {
return err
}
a.tmr.Stop()
return queue.IoCopy(r, wq.Writer())
}
func (a *auth) Close() {
a.h.m.Delete(a.salt.self)
}
func (h *authHook) New(env env.Env) (interface{}, error) {
a := &auth{
h: h,
secret: env.Value("secret"),
tmr: time.NewTimer(authTimeout),
cancel: make(chan struct{}),
ready: make(chan struct{}),
readSaltDone: make(chan struct{}),
readHashDone: make(chan struct{}),
}
if err := a.Init(); err != nil {
return nil, err
}
return a, nil
}
func init() {
register("auth", authHook{})
}
|