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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. return ErrNoFingerprintOrPassword
  36. }
  37. if wc.PasswordString != "" {
  38. wc.Password, err = decodeLegacyPasswordHash(wc.PasswordString)
  39. }
  40. if err == nil {
  41. wc.allowedNets, err = utils.ParseNetList(wc.Hosts)
  42. }
  43. return err
  44. }
  45. // ApplyProxiedIP applies the given IP to the client.
  46. func (client *Client) ApplyProxiedIP(session *Session, proxiedIP string, tls bool) (err error, quitMsg string) {
  47. // PROXY and WEBIRC are never accepted from a Tor listener, even if the address itself
  48. // is whitelisted:
  49. if client.isTor {
  50. return errBadProxyLine, ""
  51. }
  52. // ensure IP is sane
  53. parsedProxiedIP := net.ParseIP(proxiedIP).To16()
  54. if parsedProxiedIP == nil {
  55. return errBadProxyLine, fmt.Sprintf(client.t("Proxied IP address is not valid: [%s]"), proxiedIP)
  56. }
  57. isBanned, banMsg := client.server.checkBans(parsedProxiedIP)
  58. if isBanned {
  59. return errBanned, banMsg
  60. }
  61. // successfully added a limiter entry for the proxied IP;
  62. // remove the entry for the real IP if applicable (#197)
  63. client.server.connectionLimiter.RemoveClient(session.realIP)
  64. // given IP is sane! override the client's current IP
  65. ipstring := parsedProxiedIP.String()
  66. client.server.logger.Info("localconnect-ip", "Accepted proxy IP for client", ipstring)
  67. rawHostname := utils.LookupHostname(ipstring)
  68. cloakedHostname := client.server.Config().Server.Cloaks.ComputeCloak(parsedProxiedIP)
  69. client.stateMutex.Lock()
  70. defer client.stateMutex.Unlock()
  71. client.proxiedIP = parsedProxiedIP
  72. client.rawHostname = rawHostname
  73. session.proxiedIP = parsedProxiedIP
  74. session.rawHostname = rawHostname
  75. client.cloakedHostname = cloakedHostname
  76. // nickmask will be updated when the client completes registration
  77. // set tls info
  78. client.certfp = ""
  79. client.SetMode(modes.TLS, tls)
  80. return nil, ""
  81. }
  82. // handle the PROXY command: http://www.haproxy.org/download/1.8/doc/proxy-protocol.txt
  83. // PROXY must be sent as the first message in the session and has the syntax:
  84. // PROXY TCP[46] SOURCEIP DESTIP SOURCEPORT DESTPORT\r\n
  85. // unfortunately, an ipv6 SOURCEIP can start with a double colon; in this case,
  86. // the message is invalid IRC and can't be parsed normally, hence the special handling.
  87. func handleProxyCommand(server *Server, client *Client, session *Session, line string) (err error) {
  88. var quitMsg string
  89. defer func() {
  90. if err != nil {
  91. if quitMsg == "" {
  92. quitMsg = client.t("Bad or unauthorized PROXY command")
  93. }
  94. client.Quit(quitMsg, session)
  95. }
  96. }()
  97. params := strings.Fields(line)
  98. if len(params) != 6 {
  99. return errBadProxyLine
  100. }
  101. if utils.IPInNets(client.realIP, server.Config().Server.proxyAllowedFromNets) {
  102. // assume PROXY connections are always secure
  103. err, quitMsg = client.ApplyProxiedIP(session, params[2], true)
  104. return
  105. } else {
  106. // real source IP is not authorized to issue PROXY:
  107. return errBadGatewayAddress
  108. }
  109. }
  110. // read a PROXY line one byte at a time, to ensure we don't read anything beyond
  111. // that into a buffer, which would break the TLS handshake
  112. func readRawProxyLine(conn net.Conn) (result string) {
  113. // normally this is covered by ping timeouts, but we're doing this outside
  114. // of the normal client goroutine:
  115. conn.SetDeadline(time.Now().Add(time.Minute))
  116. defer conn.SetDeadline(time.Time{})
  117. var buf [maxProxyLineLen]byte
  118. oneByte := make([]byte, 1)
  119. i := 0
  120. for i < maxProxyLineLen {
  121. n, err := conn.Read(oneByte)
  122. if err != nil {
  123. return
  124. } else if n == 1 {
  125. buf[i] = oneByte[0]
  126. if buf[i] == '\n' {
  127. candidate := string(buf[0 : i+1])
  128. if strings.HasPrefix(candidate, "PROXY") {
  129. return candidate
  130. } else {
  131. return
  132. }
  133. }
  134. i += 1
  135. }
  136. }
  137. // no \r\n, fail out
  138. return
  139. }