summaryrefslogtreecommitdiff
path: root/pkg/server/hook/auth.go
blob: 7f0281671e49c0cde1276d87e7423a990b4293af (plain)
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
package hook

import (
	"crypto/md5"
	"crypto/rand"
	"errors"
	"io"
	"time"
	"tunnel/pkg/netstring"
	"tunnel/pkg/server/env"
	"tunnel/pkg/server/opts"
	"tunnel/pkg/server/queue"
)

const authTimeout = 5 * time.Second
const challengeLen = 16

type auth struct {
	secret string

	challenge struct {
		self string
		peer string
	}

	hash string

	recvChallenge chan struct{}
	recvHash      chan struct{}

	tmr *time.Timer

	fail chan struct{}
	ok   chan struct{}
}

var errDupChallenge = errors.New("peer duplicates challenge")
var errAuthFail = errors.New("peer auth fail")
var errTimeout = errors.New("timeout")

type authHook struct{}

func (a *auth) generateChallenge() error {
	b := make([]byte, challengeLen)
	if _, err := rand.Read(b); err != nil {
		return err
	}

	a.challenge.self = string(b)

	return nil
}

func (a *auth) getHash(c string) string {
	h := md5.New()

	io.WriteString(h, a.secret)
	io.WriteString(h, c)

	return string(h.Sum(nil))
}

func (a *auth) wait(c chan struct{}) error {
	select {
	case <-a.tmr.C:
		return errTimeout
	case <-a.fail:
		return io.EOF
	case <-c:
		return nil
	}
}

func (a *auth) Send(rq, wq queue.Q) error {
	e := netstring.NewEncoder(wq.Writer())

	if err := a.generateChallenge(); err != nil {
		return err
	}

	e.Encode(a.challenge.self)

	if err := a.wait(a.recvChallenge); err != nil {
		return err
	}

	if a.challenge.self == a.challenge.peer {
		return errDupChallenge
	}

	e.Encode(a.getHash(a.challenge.peer))

	if err := a.wait(a.recvHash); err != nil {
		return err
	}

	if a.hash != a.getHash(a.challenge.self) {
		close(a.fail)
		return errAuthFail
	}

	close(a.ok)

	return queue.Copy(rq, wq)
}

func (a *auth) Recv(rq, wq queue.Q) (err error) {
	r := rq.Reader()
	d := netstring.NewDecoder(r)

	if a.challenge.peer, err = d.Decode(); err != nil {
		close(a.fail)
		return
	}

	close(a.recvChallenge)

	if a.hash, err = d.Decode(); err != nil {
		close(a.fail)
		return err
	}

	close(a.recvHash)

	if err = a.wait(a.ok); err != nil {
		return
	}

	a.tmr.Stop()

	return queue.IoCopy(r, wq.Writer())
}

func (authHook) Open(env env.Env) (interface{}, error) {
	a := &auth{
		secret:        getHookVar(env, "secret"),
		recvChallenge: make(chan struct{}),
		recvHash:      make(chan struct{}),
		fail:          make(chan struct{}),
		ok:            make(chan struct{}),
		tmr:           time.NewTimer(authTimeout),
	}
	return a, nil
}

func newAuthHook(opts.Opts, env.Env) (hook, error) {
	return authHook{}, nil
}

func init() {
	register("auth", newAuthHook)
}