aboutsummaryrefslogtreecommitdiff
path: root/comp_auth.go
blob: eeccc667282a221a069d08dc76691ec5daaa31d0 (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
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
package main

import (
	"github.com/HimbeerserverDE/srp"
	"github.com/dragonfireclient/hydra-dragonfire/convert"
	"github.com/dragonfireclient/mt"
	"github.com/yuin/gopher-lua"
	"strings"
	"time"
)

type authState uint8

const (
	asInit authState = iota
	asRequested
	asVerified
	asActive
	asError
)

type CompAuth struct {
	client            *Client
	username          string
	password          string
	language          string
	version           string
	state             authState
	err               string
	srpBytesA, bytesA []byte
	userdata          *lua.LUserData
}

var compAuthFuncs = map[string]lua.LGFunction{
	"username": l_comp_auth_username,
	"password": l_comp_auth_password,
	"language": l_comp_auth_language,
	"version":  l_comp_auth_version,
	"state":    l_comp_auth_state,
}

func getCompAuth(l *lua.LState) *CompAuth {
	return l.CheckUserData(1).Value.(*CompAuth)
}

func (comp *CompAuth) create(client *Client, l *lua.LState) {
	if client.state != csNew {
		panic("can't add auth component after connect")
	}

	comp.client = client
	comp.language = "en_US"
	comp.version = "hydra-dragonfire"
	comp.state = asInit
	comp.userdata = l.NewUserData()
	comp.userdata.Value = comp
	l.SetMetatable(comp.userdata, l.GetTypeMetatable("hydra.comp.auth"))
}

func (comp *CompAuth) push() lua.LValue {
	return comp.userdata
}

func (comp *CompAuth) connect() {
	if comp.username == "" {
		panic("missing username")
	}

	go func() {
		for comp.client.state == csConnected && comp.state == asInit {
			comp.client.conn.SendCmd(&mt.ToSrvInit{
				SerializeVer: serializeVer,
				MinProtoVer:  protoVer,
				MaxProtoVer:  protoVer,
				PlayerName:   comp.username,
			})
			time.Sleep(500 * time.Millisecond)
		}
	}()
}

func (comp *CompAuth) fail(err string) {
	comp.err = err
	comp.state = asError
	comp.client.closeConn()
}

func (comp *CompAuth) checkState(state authState, pkt *mt.Pkt) bool {
	if comp.state == state {
		return true
	}

	comp.fail("received " + string(convert.PushPktType(pkt)) + " in invalid state")
	return false
}

func (comp *CompAuth) process(pkt *mt.Pkt) {
	if comp.state == asError {
		return
	}

	switch cmd := pkt.Cmd.(type) {
	case *mt.ToCltHello:
		if !comp.checkState(asInit, pkt) {
			return
		}

		if cmd.SerializeVer != 28 {
			comp.fail("unsupported serialize version")
			return
		}

		if cmd.AuthMethods == mt.FirstSRP {
			salt, verifier, err := srp.NewClient([]byte(strings.ToLower(comp.username)), []byte(comp.password))
			if err != nil {
				comp.fail(err.Error())
				return
			}

			comp.client.conn.SendCmd(&mt.ToSrvFirstSRP{
				Salt:        salt,
				Verifier:    verifier,
				EmptyPasswd: comp.password == "",
			})
			comp.state = asVerified
		} else if cmd.AuthMethods == mt.SRP {
			var err error
			comp.srpBytesA, comp.bytesA, err = srp.InitiateHandshake()
			if err != nil {
				comp.fail(err.Error())
				return
			}

			comp.client.conn.SendCmd(&mt.ToSrvSRPBytesA{
				A:      comp.srpBytesA,
				NoSHA1: true,
			})
			comp.state = asRequested
		} else {
			comp.fail("invalid auth methods")
			return
		}

	case *mt.ToCltSRPBytesSaltB:
		if !comp.checkState(asRequested, pkt) {
			return
		}

		srpBytesK, err := srp.CompleteHandshake(comp.srpBytesA, comp.bytesA, []byte(strings.ToLower(comp.username)), []byte(comp.password), cmd.Salt, cmd.B)
		if err != nil {
			comp.fail(err.Error())
			return
		}

		M := srp.ClientProof([]byte(comp.username), cmd.Salt, comp.srpBytesA, cmd.B, srpBytesK)
		comp.srpBytesA = []byte{}
		comp.bytesA = []byte{}

		if M == nil {
			comp.fail("srp safety check fail")
			return
		}

		comp.client.conn.SendCmd(&mt.ToSrvSRPBytesM{
			M: M,
		})
		comp.state = asVerified

	case *mt.ToCltAcceptAuth:
		comp.client.conn.SendCmd(&mt.ToSrvInit2{Lang: comp.language})

	case *mt.ToCltTimeOfDay:
		if comp.state == asActive {
			return
		}

		if !comp.checkState(asVerified, pkt) {
			return
		}

		comp.client.conn.SendCmd(&mt.ToSrvCltReady{
			Major:    5,
			Minor:    6,
			Patch:    0,
			Reserved: 0,
			Formspec: 4,
			Version:  comp.version,
		})
		comp.state = asActive
	}
}

func (comp *CompAuth) accessProperty(l *lua.LState, key string, ptr *string) int {
	if str, ok := l.Get(2).(lua.LString); ok {
		if comp.client.state != csNew {
			panic("can't change " + key + " after connecting")
		}
		*ptr = string(str)
		return 0
	} else {
		l.Push(lua.LString(*ptr))
		return 1
	}
}

func l_comp_auth_username(l *lua.LState) int {
	comp := getCompAuth(l)
	return comp.accessProperty(l, "username", &comp.username)
}

func l_comp_auth_password(l *lua.LState) int {
	comp := getCompAuth(l)
	return comp.accessProperty(l, "password", &comp.password)
}

func l_comp_auth_language(l *lua.LState) int {
	comp := getCompAuth(l)
	return comp.accessProperty(l, "language", &comp.language)
}

func l_comp_auth_version(l *lua.LState) int {
	comp := getCompAuth(l)
	return comp.accessProperty(l, "version", &comp.version)
}

func l_comp_auth_state(l *lua.LState) int {
	comp := getCompAuth(l)

	switch comp.state {
	case asInit:
		l.Push(lua.LString("init"))
	case asRequested:
		l.Push(lua.LString("requested"))
	case asVerified:
		l.Push(lua.LString("verified"))
	case asActive:
		l.Push(lua.LString("active"))
	case asError:
		l.Push(lua.LString("error"))
		l.Push(lua.LString(comp.err))
		return 2
	}

	return 1
}