1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-22 16:45:58 +01:00

Split handshake into login/join.

For now, join must follow login, but it will make it easier to extend
the protocol for joining multiple groups (think federation).
This commit is contained in:
Juliusz Chroboczek 2020-08-12 11:50:30 +02:00
parent 6bde5f986a
commit 7b51296262
3 changed files with 37 additions and 4 deletions

View file

@ -231,6 +231,9 @@ func addClient(name string, c client) (*group, error) {
func delClient(c client) { func delClient(c client) {
g := c.Group() g := c.Group()
if g == nil {
return
}
g.mu.Lock() g.mu.Lock()
defer g.mu.Unlock() defer g.mu.Unlock()

View file

@ -612,12 +612,15 @@ function serverConnect() {
let up = getUserPass(); let up = getUserPass();
try { try {
send({ send({
type: 'handshake', type: 'login',
id: myid, id: myid,
group: group,
username: up.username, username: up.username,
password: up.password, password: up.password,
}) })
send({
type: 'join',
group: group,
})
sendRequest(document.getElementById('requestselect').value); sendRequest(document.getElementById('requestselect').value);
} catch(e) { } catch(e) {
console.error(e); console.error(e);

View file

@ -619,13 +619,31 @@ func startClient(conn *websocket.Conn) (err error) {
return return
} }
if m.Type != "handshake" { if m.Type != "login" {
conn.WriteMessage(websocket.CloseMessage,
websocket.FormatCloseMessage(
websocket.CloseProtocolError,
"you must login first",
),
)
conn.Close() conn.Close()
return return
} }
if strings.ContainsRune(m.Username, ' ') { if strings.ContainsRune(m.Username, ' ') {
err = userError("don't put spaces in your username") // at this point, the writer is not running yet, so format
// the message ourselves
conn.WriteJSON(clientMessage{
Type: "error",
Value: "don't put spaces in your username",
})
conn.WriteMessage(websocket.CloseMessage,
websocket.FormatCloseMessage(
websocket.CloseProtocolError,
"don't put spaces in your username",
),
)
conn.Close()
return return
} }
@ -665,6 +683,15 @@ func startClient(conn *websocket.Conn) (err error) {
c.writerDone = make(chan struct{}) c.writerDone = make(chan struct{})
go clientWriter(conn, c.writeCh, c.writerDone) go clientWriter(conn, c.writeCh, c.writerDone)
err = conn.ReadJSON(&m)
if err != nil {
return err
}
if m.Type != "join" {
return protocolError("you must join a group first")
}
g, err := addClient(m.Group, c) g, err := addClient(m.Group, c)
if err != nil { if err != nil {
return return