2015-05-04 07:47:26 +02:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var upgrader = websocket.Upgrader{
|
|
|
|
ReadBufferSize: 1024,
|
|
|
|
WriteBufferSize: 1024,
|
2015-06-07 01:11:06 +02:00
|
|
|
// If a WS session contains sensitive information, and you choose to use
|
|
|
|
// cookies for authentication (during the HTTP(S) upgrade request), then
|
|
|
|
// you should check that Origin is a domain under your control. If it
|
|
|
|
// isn't, then it is possible for users of your site, visiting a naughty
|
|
|
|
// Origin, to have a WS opened using their credentials. See
|
|
|
|
// http://www.christian-schneider.net/CrossSiteWebSocketHijacking.html#main.
|
|
|
|
// We don't care about Origin because the (IRC) authentication is contained
|
|
|
|
// in the WS stream -- the WS session is not privileged when it is opened.
|
2015-05-04 07:47:26 +02:00
|
|
|
CheckOrigin: func(r *http.Request) bool { return true },
|
|
|
|
}
|
|
|
|
|
|
|
|
type WSContainer struct {
|
2015-06-07 01:11:06 +02:00
|
|
|
*websocket.Conn
|
2015-05-04 07:47:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this WSContainer) Read(msg []byte) (int, error) {
|
2015-06-07 01:11:06 +02:00
|
|
|
ty, bytes, err := this.ReadMessage()
|
|
|
|
if ty == websocket.TextMessage {
|
|
|
|
n := copy(msg, []byte(string(bytes)+CRLF+CRLF))
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
// Binary, and other kinds of messages, are thrown away.
|
|
|
|
return 0, nil
|
2015-05-04 07:47:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this WSContainer) Write(msg []byte) (int, error) {
|
2015-06-07 01:11:06 +02:00
|
|
|
err := this.WriteMessage(websocket.TextMessage, msg)
|
2015-05-04 07:47:26 +02:00
|
|
|
return len(msg), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this WSContainer) SetDeadline(t time.Time) error {
|
2015-06-07 01:11:06 +02:00
|
|
|
err := this.SetWriteDeadline(t)
|
|
|
|
err = this.SetReadDeadline(t)
|
2015-05-04 07:47:26 +02:00
|
|
|
return err
|
|
|
|
}
|