Browse Source

touchups to irc/websocket.go per review comments

tags/v0.1.0
Edmund Huber 9 years ago
parent
commit
fce54343ea
2 changed files with 20 additions and 43 deletions
  1. 1
    5
      irc/server.go
  2. 19
    38
      irc/websocket.go

+ 1
- 5
irc/server.go View File

232
 			return
232
 			return
233
 		}
233
 		}
234
 
234
 
235
-		wsc := WSContainer{
236
-			conn: ws,
237
-		}
238
-
239
-		s.newConns <- wsc
235
+		s.newConns <- WSContainer{ws}
240
 	})
236
 	})
241
 	go func() {
237
 	go func() {
242
 		Log.info.Printf("%s listening on %s", s, addr)
238
 		Log.info.Printf("%s listening on %s", s, addr)

+ 19
- 38
irc/websocket.go View File

2
 
2
 
3
 import (
3
 import (
4
 	"github.com/gorilla/websocket"
4
 	"github.com/gorilla/websocket"
5
-	"net"
6
 	"net/http"
5
 	"net/http"
7
 	"time"
6
 	"time"
8
 )
7
 )
10
 var upgrader = websocket.Upgrader{
9
 var upgrader = websocket.Upgrader{
11
 	ReadBufferSize:  1024,
10
 	ReadBufferSize:  1024,
12
 	WriteBufferSize: 1024,
11
 	WriteBufferSize: 1024,
13
-	/* If a WS session contains sensitive information, and you choose to use
14
-	   cookies for authentication (during the HTTP(S) upgrade request), then
15
-	   you should check that Origin is a domain under your control. If it
16
-	   isn't, then it is possible for users of your site, visiting a naughty
17
-	   Origin, to have a WS opened using their credentials. See
18
-	   http://www.christian-schneider.net/CrossSiteWebSocketHijacking.html#main.
19
-	   We don't care about Origin because the (IRC) authentication is contained
20
-	   in the WS stream -- the WS session is not privileged when it is opened.
21
-	*/
12
+	// If a WS session contains sensitive information, and you choose to use
13
+	// cookies for authentication (during the HTTP(S) upgrade request), then
14
+	// you should check that Origin is a domain under your control. If it
15
+	// isn't, then it is possible for users of your site, visiting a naughty
16
+	// Origin, to have a WS opened using their credentials. See
17
+	// http://www.christian-schneider.net/CrossSiteWebSocketHijacking.html#main.
18
+	// We don't care about Origin because the (IRC) authentication is contained
19
+	// in the WS stream -- the WS session is not privileged when it is opened.
22
 	CheckOrigin:     func(r *http.Request) bool { return true },
20
 	CheckOrigin:     func(r *http.Request) bool { return true },
23
 }
21
 }
24
 
22
 
25
 type WSContainer struct {
23
 type WSContainer struct {
26
-	conn *websocket.Conn
27
-}
28
-
29
-func (this WSContainer) Close() error {
30
-	return this.conn.Close()
31
-}
32
-
33
-func (this WSContainer) LocalAddr() net.Addr {
34
-	return this.conn.LocalAddr()
35
-}
36
-
37
-func (this WSContainer) RemoteAddr() net.Addr {
38
-	return this.conn.RemoteAddr()
24
+	*websocket.Conn
39
 }
25
 }
40
 
26
 
41
 func (this WSContainer) Read(msg []byte) (int, error) {
27
 func (this WSContainer) Read(msg []byte) (int, error) {
42
-	_, tmp, err := this.conn.ReadMessage()
43
-	str := (string)(tmp)
44
-	n := copy(msg, ([]byte)(str+CRLF+CRLF))
45
-	return n, err
28
+	ty, bytes, err := this.ReadMessage()
29
+	if ty == websocket.TextMessage {
30
+		n := copy(msg, []byte(string(bytes)+CRLF+CRLF))
31
+		return n, err
32
+	}
33
+	// Binary, and other kinds of messages, are thrown away.
34
+	return 0, nil
46
 }
35
 }
47
 
36
 
48
 func (this WSContainer) Write(msg []byte) (int, error) {
37
 func (this WSContainer) Write(msg []byte) (int, error) {
49
-	err := this.conn.WriteMessage(1, msg)
38
+	err := this.WriteMessage(websocket.TextMessage, msg)
50
 	return len(msg), err
39
 	return len(msg), err
51
 }
40
 }
52
 
41
 
53
 func (this WSContainer) SetDeadline(t time.Time) error {
42
 func (this WSContainer) SetDeadline(t time.Time) error {
54
-	err := this.conn.SetWriteDeadline(t)
55
-	err = this.conn.SetReadDeadline(t)
43
+	err := this.SetWriteDeadline(t)
44
+	err = this.SetReadDeadline(t)
56
 	return err
45
 	return err
57
 }
46
 }
58
-
59
-func (this WSContainer) SetReadDeadline(t time.Time) error {
60
-	return this.conn.SetReadDeadline(t)
61
-}
62
-
63
-func (this WSContainer) SetWriteDeadline(t time.Time) error {
64
-	return this.conn.SetWriteDeadline(t)
65
-}

Loading…
Cancel
Save