2018-09-29 12:36:47 +00:00
|
|
|
package websocket
|
2018-05-03 17:31:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
2018-09-29 12:36:47 +00:00
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2021-05-14 14:47:02 +00:00
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
"git.xdrm.io/go/ws/internal/http/upgrade"
|
2018-05-03 17:31:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Represents a client socket utility (reader, writer, ..)
|
|
|
|
type clientIO struct {
|
2018-09-29 12:36:47 +00:00
|
|
|
sock net.Conn
|
|
|
|
reader *bufio.Reader
|
|
|
|
kill chan<- *client // unregisters client
|
|
|
|
closing bool
|
2018-05-05 15:38:07 +00:00
|
|
|
closingMu sync.Mutex
|
2018-09-29 12:36:47 +00:00
|
|
|
reading sync.WaitGroup
|
|
|
|
writing bool
|
2018-05-03 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Represents all channels that need a client
|
2018-09-29 12:36:47 +00:00
|
|
|
type clientChannelSet struct {
|
2018-05-03 17:31:09 +00:00
|
|
|
receive chan Message
|
2018-05-05 16:43:16 +00:00
|
|
|
send chan Message
|
2018-05-03 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Represents a websocket client
|
|
|
|
type client struct {
|
2018-09-29 12:36:47 +00:00
|
|
|
io clientIO
|
|
|
|
iface *Client
|
|
|
|
ch clientChannelSet
|
|
|
|
status MessageError // close status ; 0 = nothing ; else -> must close
|
2018-05-03 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create creates a new client
|
2018-09-29 12:36:47 +00:00
|
|
|
func buildClient(s net.Conn, ctl ControllerSet, serverCh serverChannelSet) (*client, error) {
|
2018-05-03 17:31:09 +00:00
|
|
|
|
|
|
|
/* (1) Manage UPGRADE request
|
|
|
|
---------------------------------------------------------*/
|
2021-05-14 15:19:02 +00:00
|
|
|
// 1. Parse request
|
|
|
|
req, _ := upgrade.Parse(s)
|
2018-05-03 17:50:30 +00:00
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 3. Build response
|
2018-05-03 17:50:30 +00:00
|
|
|
res := req.BuildResponse()
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 4. Write into socket
|
2018-05-03 17:50:30 +00:00
|
|
|
_, err := res.Send(s)
|
2018-05-03 17:31:09 +00:00
|
|
|
if err != nil {
|
2021-06-15 20:13:36 +00:00
|
|
|
return nil, fmt.Errorf("upgrade write: %w", err)
|
2018-05-03 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2018-05-03 17:50:30 +00:00
|
|
|
if res.GetStatusCode() != 101 {
|
2018-05-03 17:31:09 +00:00
|
|
|
s.Close()
|
2021-06-15 20:13:36 +00:00
|
|
|
return nil, fmt.Errorf("upgrade failed (HTTP %d)", res.GetStatusCode())
|
2018-05-03 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (2) Initialise client
|
|
|
|
---------------------------------------------------------*/
|
2021-05-14 15:19:02 +00:00
|
|
|
// 1. Get upgrade data
|
2018-05-03 17:50:30 +00:00
|
|
|
clientURI := req.GetURI()
|
|
|
|
clientProtocol := res.GetProtocol()
|
2018-05-03 17:31:09 +00:00
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 2. Initialise client
|
2018-05-05 15:38:07 +00:00
|
|
|
cli := &client{
|
2018-05-03 17:31:09 +00:00
|
|
|
io: clientIO{
|
2018-09-29 12:36:47 +00:00
|
|
|
sock: s,
|
2018-05-03 17:31:09 +00:00
|
|
|
reader: bufio.NewReader(s),
|
2018-09-29 12:36:47 +00:00
|
|
|
kill: serverCh.unregister,
|
2018-05-03 17:31:09 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
iface: &Client{
|
|
|
|
Protocol: string(clientProtocol),
|
2021-05-14 15:19:02 +00:00
|
|
|
Arguments: [][]string{{clientURI}},
|
2018-05-03 17:31:09 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
ch: clientChannelSet{
|
2018-05-05 16:43:16 +00:00
|
|
|
receive: make(chan Message, 1),
|
|
|
|
send: make(chan Message, 1),
|
2018-05-03 17:31:09 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Find controller by URI
|
|
|
|
---------------------------------------------------------*/
|
2021-05-14 15:19:02 +00:00
|
|
|
// 1. Try to find one
|
2018-09-29 12:36:47 +00:00
|
|
|
controller, arguments := ctl.Match(clientURI)
|
2018-05-03 17:31:09 +00:00
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 2. If nothing found -> error
|
2018-05-03 17:31:09 +00:00
|
|
|
if controller == nil {
|
2021-05-14 15:19:02 +00:00
|
|
|
return nil, fmt.Errorf("No controller found, no default controller set")
|
2018-05-03 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 3. Copy arguments
|
2018-09-29 12:36:47 +00:00
|
|
|
cli.iface.Arguments = arguments
|
2018-05-03 17:31:09 +00:00
|
|
|
|
|
|
|
/* (4) Launch client routines
|
|
|
|
---------------------------------------------------------*/
|
2021-05-14 15:19:02 +00:00
|
|
|
// 1. Launch client controller
|
2018-05-03 19:10:02 +00:00
|
|
|
go controller.Fun(
|
2018-09-29 12:36:47 +00:00
|
|
|
cli.iface, // pass the client
|
|
|
|
cli.ch.receive, // the receiver
|
|
|
|
cli.ch.send, // the sender
|
|
|
|
serverCh.broadcast, // broadcast sender
|
2018-05-03 17:31:09 +00:00
|
|
|
)
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 2. Launch message reader
|
2018-05-05 15:38:07 +00:00
|
|
|
go clientReader(cli)
|
2018-05-03 17:31:09 +00:00
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 3. Launc writer
|
2018-05-05 15:38:07 +00:00
|
|
|
go clientWriter(cli)
|
2018-05-03 17:31:09 +00:00
|
|
|
|
2018-05-05 15:38:07 +00:00
|
|
|
return cli, nil
|
2018-05-03 17:31:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// reader reads and parses messages from the buffer
|
2018-09-29 12:36:47 +00:00
|
|
|
func clientReader(c *client) {
|
2018-05-07 14:35:40 +00:00
|
|
|
var frag *Message
|
2018-05-05 15:38:07 +00:00
|
|
|
|
2021-05-14 14:47:02 +00:00
|
|
|
closeStatus := Normal
|
2018-05-05 15:38:07 +00:00
|
|
|
clientAck := true
|
2018-05-07 14:35:40 +00:00
|
|
|
|
2018-05-05 15:38:07 +00:00
|
|
|
c.io.reading.Add(1)
|
2018-05-03 17:31:09 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 1. if currently closing -> exit
|
2018-05-05 15:38:07 +00:00
|
|
|
if c.io.closing {
|
|
|
|
fmt.Printf("[reader] killed because closing")
|
2018-05-03 19:10:02 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 2. Parse message
|
2018-05-03 17:31:09 +00:00
|
|
|
msg, err := readMessage(c.io.reader)
|
2018-05-07 14:35:40 +00:00
|
|
|
|
2018-05-08 09:52:34 +00:00
|
|
|
if err == ErrUnmaskedFrame || err == ErrReservedBits {
|
2021-05-14 14:47:02 +00:00
|
|
|
closeStatus = ProtocolError
|
2018-05-05 22:18:01 +00:00
|
|
|
}
|
2018-09-29 12:36:47 +00:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2018-05-05 22:18:01 +00:00
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 3. Fail on invalid message
|
2018-05-08 09:15:01 +00:00
|
|
|
msgErr := msg.check(frag != nil)
|
2018-05-08 10:10:24 +00:00
|
|
|
if msgErr != nil {
|
|
|
|
|
|
|
|
mustClose := false
|
|
|
|
|
|
|
|
switch msgErr {
|
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
// Fail
|
|
|
|
case ErrUnexpectedContinuation:
|
2021-05-14 14:47:02 +00:00
|
|
|
closeStatus = None
|
2018-09-29 12:36:47 +00:00
|
|
|
clientAck = false
|
|
|
|
mustClose = true
|
|
|
|
|
|
|
|
// proper close
|
2021-05-14 14:47:02 +00:00
|
|
|
case ErrCloseFrame:
|
|
|
|
closeStatus = Normal
|
2018-09-29 12:36:47 +00:00
|
|
|
clientAck = true
|
|
|
|
mustClose = true
|
|
|
|
|
|
|
|
// invalid payload proper close
|
|
|
|
case ErrInvalidPayload:
|
2021-05-14 14:47:02 +00:00
|
|
|
closeStatus = InvalidPayload
|
2018-09-29 12:36:47 +00:00
|
|
|
clientAck = true
|
|
|
|
mustClose = true
|
|
|
|
|
|
|
|
// any other error -> protocol error
|
|
|
|
default:
|
2021-05-14 14:47:02 +00:00
|
|
|
closeStatus = ProtocolError
|
2018-09-29 12:36:47 +00:00
|
|
|
clientAck = true
|
|
|
|
mustClose = true
|
2018-05-08 10:10:24 +00:00
|
|
|
}
|
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
if mustClose {
|
|
|
|
break
|
|
|
|
}
|
2018-05-08 09:15:01 +00:00
|
|
|
|
2018-05-03 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 4. Ping <-> Pong
|
2021-05-14 14:47:02 +00:00
|
|
|
if msg.Type == Ping && c.io.writing {
|
2018-05-07 14:35:40 +00:00
|
|
|
msg.Final = true
|
2021-05-14 14:47:02 +00:00
|
|
|
msg.Type = Pong
|
2018-05-07 14:35:40 +00:00
|
|
|
c.ch.send <- *msg
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 5. Store first fragment
|
2018-05-07 14:35:40 +00:00
|
|
|
if frag == nil && !msg.Final {
|
|
|
|
frag = &Message{
|
2018-09-29 12:36:47 +00:00
|
|
|
Type: msg.Type,
|
2018-05-07 14:35:40 +00:00
|
|
|
Final: msg.Final,
|
2018-09-29 12:36:47 +00:00
|
|
|
Data: msg.Data,
|
|
|
|
Size: msg.Size,
|
2018-05-05 23:37:02 +00:00
|
|
|
}
|
2018-05-07 14:35:40 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 6. Store fragments
|
2018-05-07 14:35:40 +00:00
|
|
|
if frag != nil {
|
|
|
|
frag.Final = msg.Final
|
|
|
|
frag.Size += msg.Size
|
|
|
|
frag.Data = append(frag.Data, msg.Data...)
|
|
|
|
|
2018-05-08 09:15:01 +00:00
|
|
|
if !frag.Final { // continue if not last fragment
|
2018-05-07 14:35:40 +00:00
|
|
|
continue
|
2018-05-05 21:53:06 +00:00
|
|
|
}
|
2018-05-03 17:31:09 +00:00
|
|
|
|
2018-05-07 14:35:40 +00:00
|
|
|
// check message errors
|
2018-05-08 09:15:01 +00:00
|
|
|
fragErr := frag.check(false)
|
|
|
|
if fragErr == ErrInvalidPayload {
|
2021-05-14 14:47:02 +00:00
|
|
|
closeStatus = InvalidPayload
|
2018-05-08 09:15:01 +00:00
|
|
|
break
|
|
|
|
} else if fragErr != nil {
|
2021-05-14 14:47:02 +00:00
|
|
|
closeStatus = ProtocolError
|
2018-05-07 14:35:40 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = frag
|
|
|
|
frag = nil
|
2018-05-05 15:38:07 +00:00
|
|
|
|
2018-05-03 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 7. Dispatch to receiver
|
2021-05-14 14:47:02 +00:00
|
|
|
if msg.Type == Text || msg.Type == Binary {
|
2018-05-07 14:35:40 +00:00
|
|
|
c.ch.receive <- *msg
|
|
|
|
}
|
2018-05-03 17:31:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-05 16:43:16 +00:00
|
|
|
close(c.ch.receive)
|
2018-05-05 15:38:07 +00:00
|
|
|
c.io.reading.Done()
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 8. close channel (if not already done)
|
2018-05-05 15:38:07 +00:00
|
|
|
// fmt.Printf("[reader] end\n")
|
2018-05-08 09:15:01 +00:00
|
|
|
c.close(closeStatus, clientAck)
|
2018-05-03 17:31:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// writer writes into websocket
|
|
|
|
// and is triggered by client.ch.send channel
|
2018-09-29 12:36:47 +00:00
|
|
|
func clientWriter(c *client) {
|
2018-05-05 15:38:07 +00:00
|
|
|
|
2018-05-05 16:43:16 +00:00
|
|
|
c.io.writing = true // if channel still exists
|
2018-05-03 17:31:09 +00:00
|
|
|
|
|
|
|
for msg := range c.ch.send {
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 2. Send message
|
2018-05-03 17:31:09 +00:00
|
|
|
err := msg.Send(c.io.sock)
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 3. Fail on error
|
2018-05-03 17:31:09 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf(" [writer] %s\n", err)
|
2018-05-05 16:43:16 +00:00
|
|
|
c.io.writing = false
|
2018-05-03 17:31:09 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-05 16:43:16 +00:00
|
|
|
c.io.writing = false
|
2018-05-05 15:38:07 +00:00
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 4. close channel (if not already done)
|
2018-05-05 15:38:07 +00:00
|
|
|
// fmt.Printf("[writer] end\n")
|
2021-05-14 14:47:02 +00:00
|
|
|
c.close(Normal, true)
|
2018-05-03 17:31:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-05 23:37:02 +00:00
|
|
|
// closes the connection
|
|
|
|
// send CLOSE frame is 'status' is not NONE
|
|
|
|
// wait for the next message (CLOSE acknowledge) if 'clientACK'
|
|
|
|
// then delete client
|
2018-09-29 12:36:47 +00:00
|
|
|
func (c *client) close(status MessageError, clientACK bool) {
|
2018-05-05 15:38:07 +00:00
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 1. Fail if already closing
|
2018-05-05 15:38:07 +00:00
|
|
|
alreadyClosing := false
|
|
|
|
c.io.closingMu.Lock()
|
2018-09-29 12:36:47 +00:00
|
|
|
alreadyClosing = c.io.closing
|
|
|
|
c.io.closing = true
|
2018-05-05 15:38:07 +00:00
|
|
|
c.io.closingMu.Unlock()
|
|
|
|
|
|
|
|
if alreadyClosing {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 2. kill writer' if still running
|
2018-05-05 16:43:16 +00:00
|
|
|
if c.io.writing {
|
|
|
|
close(c.ch.send)
|
|
|
|
}
|
2018-05-03 17:31:09 +00:00
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 3. kill reader if still running
|
2018-09-29 12:36:47 +00:00
|
|
|
c.io.sock.SetReadDeadline(time.Now().Add(time.Second * -1))
|
2018-05-05 16:47:38 +00:00
|
|
|
c.io.reading.Wait()
|
|
|
|
|
2021-05-14 14:47:02 +00:00
|
|
|
if status != None {
|
2018-05-05 15:38:07 +00:00
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 3. Build message
|
2018-05-05 23:37:02 +00:00
|
|
|
msg := &Message{
|
|
|
|
Final: true,
|
2021-05-14 14:47:02 +00:00
|
|
|
Type: Close,
|
2018-09-29 12:36:47 +00:00
|
|
|
Size: 2,
|
|
|
|
Data: make([]byte, 2),
|
2018-05-05 23:37:02 +00:00
|
|
|
}
|
|
|
|
binary.BigEndian.PutUint16(msg.Data, uint16(status))
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 4. Send message
|
2018-05-07 14:35:40 +00:00
|
|
|
msg.Send(c.io.sock)
|
|
|
|
// if err != nil {
|
2018-05-08 09:31:37 +00:00
|
|
|
// fmt.Printf("[close] send error (%s0\n", err)
|
2018-05-07 14:35:40 +00:00
|
|
|
// }
|
2018-05-05 15:38:07 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 2. Wait for client CLOSE if needed
|
2018-05-05 15:38:07 +00:00
|
|
|
if clientACK {
|
|
|
|
|
|
|
|
c.io.sock.SetReadDeadline(time.Now().Add(time.Millisecond))
|
|
|
|
|
|
|
|
/* Wait for message */
|
2018-05-07 14:35:40 +00:00
|
|
|
readMessage(c.io.reader)
|
|
|
|
// if err != nil || msg.Type != CLOSE {
|
|
|
|
// if err == nil {
|
|
|
|
// fmt.Printf("[close] received OpCode = %d\n", msg.Type)
|
|
|
|
// } else {
|
|
|
|
// fmt.Printf("[close] read error (%v)\n", err)
|
|
|
|
// }
|
|
|
|
// }
|
2018-05-03 17:31:09 +00:00
|
|
|
|
2018-05-05 15:38:07 +00:00
|
|
|
// fmt.Printf("[close] received ACK\n")
|
2018-05-03 17:31:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 3. Close socket
|
2018-05-03 17:31:09 +00:00
|
|
|
c.io.sock.Close()
|
2018-05-05 15:38:07 +00:00
|
|
|
// fmt.Printf("[close] socket closed\n")
|
2018-05-03 17:31:09 +00:00
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 4. Unregister
|
2018-05-03 17:31:09 +00:00
|
|
|
c.io.kill <- c
|
|
|
|
|
2018-05-05 15:38:07 +00:00
|
|
|
return
|
|
|
|
|
2018-05-05 23:37:02 +00:00
|
|
|
}
|