You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ircconn.go 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package irc
  2. import (
  3. "bufio"
  4. "bytes"
  5. "net"
  6. "unicode/utf8"
  7. "github.com/gorilla/websocket"
  8. "github.com/goshuirc/irc-go/ircmsg"
  9. "github.com/oragono/oragono/irc/utils"
  10. )
  11. const (
  12. maxReadQBytes = ircmsg.MaxlenTagsFromClient + 512 + 1024
  13. )
  14. var (
  15. crlf = []byte{'\r', '\n'}
  16. )
  17. // IRCConn abstracts away the distinction between a regular
  18. // net.Conn (which includes both raw TCP and TLS) and a websocket.
  19. // it doesn't expose the net.Conn, io.Reader, or io.Writer interfaces
  20. // because websockets are message-oriented, not stream-oriented, and
  21. // therefore this abstraction is message-oriented as well.
  22. type IRCConn interface {
  23. UnderlyingConn() *utils.WrappedConn
  24. // these take an IRC line or lines, correctly terminated with CRLF:
  25. WriteLine([]byte) error
  26. WriteLines([][]byte) error
  27. // this returns an IRC line without the terminating CRLF:
  28. ReadLine() (line []byte, err error)
  29. Close() error
  30. }
  31. // IRCStreamConn is an IRCConn over a regular stream connection.
  32. type IRCStreamConn struct {
  33. conn *utils.WrappedConn
  34. reader *bufio.Reader
  35. }
  36. func NewIRCStreamConn(conn *utils.WrappedConn) *IRCStreamConn {
  37. return &IRCStreamConn{
  38. conn: conn,
  39. }
  40. }
  41. func (cc *IRCStreamConn) UnderlyingConn() *utils.WrappedConn {
  42. return cc.conn
  43. }
  44. func (cc *IRCStreamConn) WriteLine(buf []byte) (err error) {
  45. _, err = cc.conn.Write(buf)
  46. return
  47. }
  48. func (cc *IRCStreamConn) WriteLines(buffers [][]byte) (err error) {
  49. // on Linux, with a plaintext TCP or Unix domain socket,
  50. // the Go runtime will optimize this into a single writev(2) call:
  51. _, err = (*net.Buffers)(&buffers).WriteTo(cc.conn)
  52. return
  53. }
  54. func (cc *IRCStreamConn) ReadLine() (line []byte, err error) {
  55. // lazy initialize the reader in case the IP is banned
  56. if cc.reader == nil {
  57. cc.reader = bufio.NewReaderSize(cc.conn, maxReadQBytes)
  58. }
  59. var isPrefix bool
  60. line, isPrefix, err = cc.reader.ReadLine()
  61. if isPrefix {
  62. return nil, errReadQ
  63. }
  64. line = bytes.TrimSuffix(line, crlf)
  65. return
  66. }
  67. func (cc *IRCStreamConn) Close() (err error) {
  68. return cc.conn.Close()
  69. }
  70. // IRCWSConn is an IRCConn over a websocket.
  71. type IRCWSConn struct {
  72. conn *websocket.Conn
  73. }
  74. func NewIRCWSConn(conn *websocket.Conn) IRCWSConn {
  75. return IRCWSConn{conn: conn}
  76. }
  77. func (wc IRCWSConn) UnderlyingConn() *utils.WrappedConn {
  78. // just assume that the type is OK
  79. wConn, _ := wc.conn.UnderlyingConn().(*utils.WrappedConn)
  80. return wConn
  81. }
  82. func (wc IRCWSConn) WriteLine(buf []byte) (err error) {
  83. buf = bytes.TrimSuffix(buf, crlf)
  84. // there's not much we can do about this;
  85. // silently drop the message
  86. if !utf8.Valid(buf) {
  87. return nil
  88. }
  89. return wc.conn.WriteMessage(websocket.TextMessage, buf)
  90. }
  91. func (wc IRCWSConn) WriteLines(buffers [][]byte) (err error) {
  92. for _, buf := range buffers {
  93. err = wc.WriteLine(buf)
  94. if err != nil {
  95. return
  96. }
  97. }
  98. return
  99. }
  100. func (wc IRCWSConn) ReadLine() (line []byte, err error) {
  101. for {
  102. var messageType int
  103. messageType, line, err = wc.conn.ReadMessage()
  104. // on empty message or non-text message, try again, block if necessary
  105. if err != nil || (messageType == websocket.TextMessage && len(line) != 0) {
  106. return
  107. }
  108. }
  109. }
  110. func (wc IRCWSConn) Close() (err error) {
  111. return wc.conn.Close()
  112. }