mirror of
https://github.com/ergochat/ergo.git
synced 2024-11-26 05:49:25 +01:00
touchups to irc/websocket.go per review comments
This commit is contained in:
parent
62302ec92e
commit
fce54343ea
@ -232,11 +232,7 @@ func (s *Server) wslisten(addr string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
wsc := WSContainer{
|
s.newConns <- WSContainer{ws}
|
||||||
conn: ws,
|
|
||||||
}
|
|
||||||
|
|
||||||
s.newConns <- wsc
|
|
||||||
})
|
})
|
||||||
go func() {
|
go func() {
|
||||||
Log.info.Printf("%s listening on %s", s, addr)
|
Log.info.Printf("%s listening on %s", s, addr)
|
||||||
|
@ -2,7 +2,6 @@ package irc
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"net"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -10,56 +9,38 @@ import (
|
|||||||
var upgrader = websocket.Upgrader{
|
var upgrader = websocket.Upgrader{
|
||||||
ReadBufferSize: 1024,
|
ReadBufferSize: 1024,
|
||||||
WriteBufferSize: 1024,
|
WriteBufferSize: 1024,
|
||||||
/* If a WS session contains sensitive information, and you choose to use
|
// If a WS session contains sensitive information, and you choose to use
|
||||||
cookies for authentication (during the HTTP(S) upgrade request), then
|
// cookies for authentication (during the HTTP(S) upgrade request), then
|
||||||
you should check that Origin is a domain under your control. If it
|
// 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
|
// isn't, then it is possible for users of your site, visiting a naughty
|
||||||
Origin, to have a WS opened using their credentials. See
|
// Origin, to have a WS opened using their credentials. See
|
||||||
http://www.christian-schneider.net/CrossSiteWebSocketHijacking.html#main.
|
// http://www.christian-schneider.net/CrossSiteWebSocketHijacking.html#main.
|
||||||
We don't care about Origin because the (IRC) authentication is contained
|
// 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.
|
// in the WS stream -- the WS session is not privileged when it is opened.
|
||||||
*/
|
|
||||||
CheckOrigin: func(r *http.Request) bool { return true },
|
CheckOrigin: func(r *http.Request) bool { return true },
|
||||||
}
|
}
|
||||||
|
|
||||||
type WSContainer struct {
|
type WSContainer struct {
|
||||||
conn *websocket.Conn
|
*websocket.Conn
|
||||||
}
|
|
||||||
|
|
||||||
func (this WSContainer) Close() error {
|
|
||||||
return this.conn.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this WSContainer) LocalAddr() net.Addr {
|
|
||||||
return this.conn.LocalAddr()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this WSContainer) RemoteAddr() net.Addr {
|
|
||||||
return this.conn.RemoteAddr()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this WSContainer) Read(msg []byte) (int, error) {
|
func (this WSContainer) Read(msg []byte) (int, error) {
|
||||||
_, tmp, err := this.conn.ReadMessage()
|
ty, bytes, err := this.ReadMessage()
|
||||||
str := (string)(tmp)
|
if ty == websocket.TextMessage {
|
||||||
n := copy(msg, ([]byte)(str+CRLF+CRLF))
|
n := copy(msg, []byte(string(bytes)+CRLF+CRLF))
|
||||||
return n, err
|
return n, err
|
||||||
|
}
|
||||||
|
// Binary, and other kinds of messages, are thrown away.
|
||||||
|
return 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this WSContainer) Write(msg []byte) (int, error) {
|
func (this WSContainer) Write(msg []byte) (int, error) {
|
||||||
err := this.conn.WriteMessage(1, msg)
|
err := this.WriteMessage(websocket.TextMessage, msg)
|
||||||
return len(msg), err
|
return len(msg), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this WSContainer) SetDeadline(t time.Time) error {
|
func (this WSContainer) SetDeadline(t time.Time) error {
|
||||||
err := this.conn.SetWriteDeadline(t)
|
err := this.SetWriteDeadline(t)
|
||||||
err = this.conn.SetReadDeadline(t)
|
err = this.SetReadDeadline(t)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this WSContainer) SetReadDeadline(t time.Time) error {
|
|
||||||
return this.conn.SetReadDeadline(t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this WSContainer) SetWriteDeadline(t time.Time) error {
|
|
||||||
return this.conn.SetWriteDeadline(t)
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user