| 1 | package web |
| 2 | |
| 3 | import ( |
| 4 | "log" |
| 5 | "strings" |
| 6 | "time" |
| 7 | |
| 8 | "github.com/gorilla/websocket" |
| 9 | ) |
| 10 | |
| 11 | const ( |
| 12 | SubTypeFull SubscriptionType = iota |
| 13 | SubTypeLite |
| 14 | SubTypeDomain |
| 15 | ) |
| 16 | |
| 17 | type SubscriptionType int |
| 18 | |
| 19 | // client represents a single client's connection to the server. |
| 20 | type client struct { |
| 21 | conn *websocket.Conn |
| 22 | broadcastChan chan []byte |
| 23 | name string |
| 24 | subType SubscriptionType |
| 25 | skippedCerts uint64 |
| 26 | } |
| 27 | |
| 28 | func newClient(conn *websocket.Conn, subType SubscriptionType, name string, certBufferSize int) *client { |
| 29 | return &client{ |
| 30 | conn: conn, |
| 31 | broadcastChan: make(chan []byte, certBufferSize), |
| 32 | name: name, |
| 33 | subType: subType, |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | // Each client has a broadcastHandler that runs in the background and sends out the broadcast messages to the client. |
| 38 | func (c *client) broadcastHandler() { |
| 39 | writeWait := 60 * time.Second |
| 40 | pingTicker := time.NewTicker(30 * time.Second) |
| 41 | |
| 42 | defer func() { |
| 43 | log.Println("Closing broadcast handler for client:", c.conn.RemoteAddr()) |
| 44 | |
| 45 | pingTicker.Stop() |
| 46 | |
| 47 | _ = c.conn.SetWriteDeadline(time.Now().Add(5 * time.Second)) |
| 48 | _ = c.conn.WriteMessage(websocket.CloseMessage, []byte{}) |
| 49 | _ = c.conn.Close() |
| 50 | }() |
| 51 | |
| 52 | for { |
| 53 | select { |
| 54 | case <-pingTicker.C: |
| 55 | _ = c.conn.SetWriteDeadline(time.Now().Add(writeWait)) |
| 56 | |
| 57 | if err := c.conn.WriteMessage(websocket.PingMessage, nil); err != nil { |
| 58 | return |
| 59 | } |
| 60 | case message := <-c.broadcastChan: |
| 61 | _ = c.conn.SetWriteDeadline(time.Now().Add(writeWait)) |
| 62 | |
| 63 | w, err := c.conn.NextWriter(websocket.TextMessage) |
| 64 | if err != nil { |
| 65 | log.Printf("Error while getting next writer: %v\n", err) |
| 66 | return |
| 67 | } |
| 68 | |
| 69 | _, writeErr := w.Write(message) |
| 70 | if writeErr != nil { |
| 71 | log.Printf("Error while writing: %v\n", writeErr) |
| 72 | } |
| 73 | |
| 74 | if closeErr := w.Close(); closeErr != nil { |
| 75 | log.Printf("Error while closing: %v\n", closeErr) |
| 76 | return |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // listenWebsocket is running in the background on a goroutine and listens for messages from the client. |
| 83 | // It responds to ping messages with a pong message. It closes the connection if the client sends |
| 84 | // a close message or no ping is received within 65 seconds. |
| 85 | func (c *client) listenWebsocket() { |
| 86 | defer func() { |
| 87 | _ = c.conn.Close() |
| 88 | ClientHandler.unregisterClient(c) |
| 89 | }() |
| 90 | |
| 91 | readWait := 65 * time.Second |
| 92 | |
| 93 | c.conn.SetReadLimit(512) |
| 94 | _ = c.conn.SetReadDeadline(time.Now().Add(readWait)) |
| 95 | |
| 96 | defaultPingHandler := c.conn.PingHandler() |
| 97 | |
| 98 | c.conn.SetPingHandler(func(appData string) error { |
| 99 | // Ping received - reset the deadline |
| 100 | err := c.conn.SetReadDeadline(time.Now().Add(readWait)) |
| 101 | if err != nil { |
| 102 | return err |
| 103 | } |
| 104 | |
| 105 | return defaultPingHandler(appData) |
| 106 | }) |
| 107 | |
| 108 | c.conn.SetPongHandler(func(string) error { |
| 109 | // Pong received - reset the deadline |
| 110 | err := c.conn.SetReadDeadline(time.Now().Add(readWait)) |
| 111 | return err |
| 112 | }) |
| 113 | |
| 114 | // Handle messages from the client |
| 115 | for { |
| 116 | // ignore any message sent from clients - we only handle errors (aka. disconnects) |
| 117 | _, _, readErr := c.conn.ReadMessage() |
| 118 | if readErr != nil { |
| 119 | if websocket.IsUnexpectedCloseError(readErr, websocket.CloseGoingAway, websocket.CloseNormalClosure) { |
| 120 | log.Printf("Unexpected websocket close error: %v\n", readErr) |
| 121 | } |
| 122 | |
| 123 | if strings.Contains(strings.ToLower(readErr.Error()), "i/o timeout") { |
| 124 | log.Printf("No ping received from client: %v\n", c.conn.RemoteAddr()) |
| 125 | closeMessage := websocket.FormatCloseMessage(websocket.CloseNoStatusReceived, "No ping received!") |
| 126 | c.conn.WriteControl(websocket.CloseMessage, closeMessage, time.Now().Add(5*time.Second)) //nolint:errcheck |
| 127 | } else if strings.Contains(strings.ToLower(readErr.Error()), "an existing connection was forcibly closed by the remote host") { |
| 128 | log.Printf("Connection to client lost: %v\n", c.conn.RemoteAddr()) |
| 129 | } |
| 130 | |
| 131 | log.Printf("Disconnecting client %v!\n", c.conn.RemoteAddr()) |
| 132 | |
| 133 | break |
| 134 | } |
| 135 | } |
| 136 | } |