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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. if globalUtf8EnforcementSetting && !utf8.Valid(line) {
  65. err = errInvalidUtf8
  66. }
  67. return
  68. }
  69. func (cc *IRCStreamConn) Close() (err error) {
  70. return cc.conn.Close()
  71. }
  72. // IRCWSConn is an IRCConn over a websocket.
  73. type IRCWSConn struct {
  74. conn *websocket.Conn
  75. }
  76. func NewIRCWSConn(conn *websocket.Conn) IRCWSConn {
  77. return IRCWSConn{conn: conn}
  78. }
  79. func (wc IRCWSConn) UnderlyingConn() *utils.WrappedConn {
  80. // just assume that the type is OK
  81. wConn, _ := wc.conn.UnderlyingConn().(*utils.WrappedConn)
  82. return wConn
  83. }
  84. func (wc IRCWSConn) WriteLine(buf []byte) (err error) {
  85. buf = bytes.TrimSuffix(buf, crlf)
  86. if !globalUtf8EnforcementSetting && !utf8.Valid(buf) {
  87. // there's not much we can do about this;
  88. // silently drop the message
  89. return nil
  90. }
  91. return wc.conn.WriteMessage(websocket.TextMessage, buf)
  92. }
  93. func (wc IRCWSConn) WriteLines(buffers [][]byte) (err error) {
  94. for _, buf := range buffers {
  95. err = wc.WriteLine(buf)
  96. if err != nil {
  97. return
  98. }
  99. }
  100. return
  101. }
  102. func (wc IRCWSConn) ReadLine() (line []byte, err error) {
  103. for {
  104. var messageType int
  105. messageType, line, err = wc.conn.ReadMessage()
  106. // on empty message or non-text message, try again, block if necessary
  107. if err != nil || (messageType == websocket.TextMessage && len(line) != 0) {
  108. return
  109. }
  110. }
  111. }
  112. func (wc IRCWSConn) Close() (err error) {
  113. return wc.conn.Close()
  114. }