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.

gateways.go 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright (c) 2012-2014 Jeremy Latt
  2. // Copyright (c) 2014-2015 Edmund Huber
  3. // Copyright (c) 2017 Daniel Oaks <daniel@danieloaks.net>
  4. // released under the MIT license
  5. package irc
  6. import (
  7. "errors"
  8. "fmt"
  9. "net"
  10. "strings"
  11. "time"
  12. "github.com/oragono/oragono/irc/modes"
  13. "github.com/oragono/oragono/irc/utils"
  14. )
  15. var (
  16. errBadGatewayAddress = errors.New("PROXY/WEBIRC commands are not accepted from this IP address")
  17. errBadProxyLine = errors.New("Invalid PROXY/WEBIRC command")
  18. )
  19. const (
  20. // https://www.haproxy.org/download/1.8/doc/proxy-protocol.txt
  21. // "a 108-byte buffer is always enough to store all the line and a trailing zero
  22. // for string processing."
  23. maxProxyLineLen = 107
  24. )
  25. type webircConfig struct {
  26. PasswordString string `yaml:"password"`
  27. Password []byte `yaml:"password-bytes"`
  28. Fingerprint string
  29. Hosts []string
  30. allowedNets []net.IPNet
  31. }
  32. // Populate fills out our password or fingerprint.
  33. func (wc *webircConfig) Populate() (err error) {
  34. if wc.Fingerprint == "" && wc.PasswordString == "" {
  35. err = ErrNoFingerprintOrPassword
  36. }
  37. if err == nil && wc.PasswordString != "" {
  38. wc.Password, err = decodeLegacyPasswordHash(wc.PasswordString)
  39. }
  40. if err == nil && wc.Fingerprint != "" {
  41. wc.Fingerprint, err = utils.NormalizeCertfp(wc.Fingerprint)
  42. }
  43. if err == nil {
  44. wc.allowedNets, err = utils.ParseNetList(wc.Hosts)
  45. }
  46. return err
  47. }
  48. // ApplyProxiedIP applies the given IP to the client.
  49. func (client *Client) ApplyProxiedIP(session *Session, proxiedIP string, tls bool) (err error, quitMsg string) {
  50. // PROXY and WEBIRC are never accepted from a Tor listener, even if the address itself
  51. // is whitelisted:
  52. if client.isTor {
  53. return errBadProxyLine, ""
  54. }
  55. // ensure IP is sane
  56. parsedProxiedIP := net.ParseIP(proxiedIP).To16()
  57. if parsedProxiedIP == nil {
  58. return errBadProxyLine, fmt.Sprintf(client.t("Proxied IP address is not valid: [%s]"), proxiedIP)
  59. }
  60. isBanned, banMsg := client.server.checkBans(parsedProxiedIP)
  61. if isBanned {
  62. return errBanned, banMsg
  63. }
  64. // successfully added a limiter entry for the proxied IP;
  65. // remove the entry for the real IP if applicable (#197)
  66. client.server.connectionLimiter.RemoveClient(session.realIP)
  67. // given IP is sane! override the client's current IP
  68. client.server.logger.Info("localconnect-ip", "Accepted proxy IP for client", parsedProxiedIP.String())
  69. client.stateMutex.Lock()
  70. defer client.stateMutex.Unlock()
  71. client.proxiedIP = parsedProxiedIP
  72. session.proxiedIP = parsedProxiedIP
  73. // nickmask will be updated when the client completes registration
  74. // set tls info
  75. client.certfp = ""
  76. client.SetMode(modes.TLS, tls)
  77. return nil, ""
  78. }
  79. // handle the PROXY command: http://www.haproxy.org/download/1.8/doc/proxy-protocol.txt
  80. // PROXY must be sent as the first message in the session and has the syntax:
  81. // PROXY TCP[46] SOURCEIP DESTIP SOURCEPORT DESTPORT\r\n
  82. // unfortunately, an ipv6 SOURCEIP can start with a double colon; in this case,
  83. // the message is invalid IRC and can't be parsed normally, hence the special handling.
  84. func handleProxyCommand(server *Server, client *Client, session *Session, line string) (err error) {
  85. var quitMsg string
  86. defer func() {
  87. if err != nil {
  88. if quitMsg == "" {
  89. quitMsg = client.t("Bad or unauthorized PROXY command")
  90. }
  91. client.Quit(quitMsg, session)
  92. }
  93. }()
  94. params := strings.Fields(line)
  95. if len(params) != 6 {
  96. return errBadProxyLine
  97. }
  98. if utils.IPInNets(client.realIP, server.Config().Server.proxyAllowedFromNets) {
  99. // assume PROXY connections are always secure
  100. err, quitMsg = client.ApplyProxiedIP(session, params[2], true)
  101. return
  102. } else {
  103. // real source IP is not authorized to issue PROXY:
  104. return errBadGatewayAddress
  105. }
  106. }
  107. // read a PROXY line one byte at a time, to ensure we don't read anything beyond
  108. // that into a buffer, which would break the TLS handshake
  109. func readRawProxyLine(conn net.Conn) (result string) {
  110. // normally this is covered by ping timeouts, but we're doing this outside
  111. // of the normal client goroutine:
  112. conn.SetDeadline(time.Now().Add(time.Minute))
  113. defer conn.SetDeadline(time.Time{})
  114. var buf [maxProxyLineLen]byte
  115. oneByte := make([]byte, 1)
  116. i := 0
  117. for i < maxProxyLineLen {
  118. n, err := conn.Read(oneByte)
  119. if err != nil {
  120. return
  121. } else if n == 1 {
  122. buf[i] = oneByte[0]
  123. if buf[i] == '\n' {
  124. candidate := string(buf[0 : i+1])
  125. if strings.HasPrefix(candidate, "PROXY") {
  126. return candidate
  127. } else {
  128. return
  129. }
  130. }
  131. i += 1
  132. }
  133. }
  134. // no \r\n, fail out
  135. return
  136. }