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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. "net"
  9. "github.com/ergochat/ergo/irc/flatip"
  10. "github.com/ergochat/ergo/irc/modes"
  11. "github.com/ergochat/ergo/irc/utils"
  12. )
  13. var (
  14. errBadGatewayAddress = errors.New("PROXY/WEBIRC commands are not accepted from this IP address")
  15. errBadProxyLine = errors.New("Invalid PROXY/WEBIRC command")
  16. )
  17. const (
  18. // https://www.haproxy.org/download/1.8/doc/proxy-protocol.txt
  19. // "a 108-byte buffer is always enough to store all the line and a trailing zero
  20. // for string processing."
  21. maxProxyLineLen = 107
  22. )
  23. type webircConfig struct {
  24. PasswordString string `yaml:"password"`
  25. Password []byte `yaml:"password-bytes"`
  26. Fingerprint *string // legacy name for certfp, #1050
  27. Certfp string
  28. Hosts []string
  29. AcceptHostname bool `yaml:"accept-hostname"`
  30. allowedNets []net.IPNet
  31. }
  32. // Populate fills out our password or fingerprint.
  33. func (wc *webircConfig) Populate() (err error) {
  34. if wc.PasswordString != "" {
  35. wc.Password, err = decodeLegacyPasswordHash(wc.PasswordString)
  36. if err != nil {
  37. return
  38. }
  39. }
  40. certfp := wc.Certfp
  41. if certfp == "" && wc.Fingerprint != nil {
  42. certfp = *wc.Fingerprint
  43. }
  44. if certfp != "" {
  45. wc.Certfp, err = utils.NormalizeCertfp(certfp)
  46. }
  47. if err != nil {
  48. return
  49. }
  50. if wc.Certfp == "" && wc.PasswordString == "" {
  51. return errors.New("webirc block has no certfp or password specified")
  52. }
  53. wc.allowedNets, err = utils.ParseNetList(wc.Hosts)
  54. return err
  55. }
  56. // ApplyProxiedIP applies the given IP to the client.
  57. func (client *Client) ApplyProxiedIP(session *Session, proxiedIP net.IP, tls bool) (err error, quitMsg string) {
  58. // PROXY and WEBIRC are never accepted from a Tor listener, even if the address itself
  59. // is whitelisted. Furthermore, don't accept PROXY or WEBIRC if we already accepted
  60. // a proxied IP from any source (PROXY, WEBIRC, or X-Forwarded-For):
  61. if session.isTor || session.proxiedIP != nil {
  62. return errBadProxyLine, ""
  63. }
  64. // ensure IP is sane
  65. if proxiedIP == nil {
  66. return errBadProxyLine, "proxied IP is not valid"
  67. }
  68. proxiedIP = proxiedIP.To16()
  69. isBanned, requireSASL, banMsg := client.server.checkBans(client.server.Config(), proxiedIP, true)
  70. if isBanned {
  71. return errBanned, banMsg
  72. }
  73. client.requireSASL = requireSASL
  74. if requireSASL {
  75. client.requireSASLMessage = banMsg
  76. }
  77. // successfully added a limiter entry for the proxied IP;
  78. // remove the entry for the real IP if applicable (#197)
  79. client.server.connectionLimiter.RemoveClient(flatip.FromNetIP(session.realIP))
  80. // given IP is sane! override the client's current IP
  81. client.server.logger.Info("connect-ip", "Accepted proxy IP for client", proxiedIP.String())
  82. client.stateMutex.Lock()
  83. defer client.stateMutex.Unlock()
  84. client.proxiedIP = proxiedIP
  85. session.proxiedIP = proxiedIP
  86. // nickmask will be updated when the client completes registration
  87. // set tls info
  88. session.certfp = ""
  89. session.peerCerts = nil
  90. client.SetMode(modes.TLS, tls)
  91. return nil, ""
  92. }
  93. // handle the PROXY command: http://www.haproxy.org/download/1.8/doc/proxy-protocol.txt
  94. // PROXY must be sent as the first message in the session and has the syntax:
  95. // PROXY TCP[46] SOURCEIP DESTIP SOURCEPORT DESTPORT\r\n
  96. // unfortunately, an ipv6 SOURCEIP can start with a double colon; in this case,
  97. // the message is invalid IRC and can't be parsed normally, hence the special handling.
  98. func handleProxyCommand(server *Server, client *Client, session *Session, line string) (err error) {
  99. var quitMsg string
  100. defer func() {
  101. if err != nil {
  102. if quitMsg == "" {
  103. quitMsg = client.t("Bad or unauthorized PROXY command")
  104. }
  105. client.Quit(quitMsg, session)
  106. }
  107. }()
  108. ip, err := utils.ParseProxyLineV1(line)
  109. if err != nil {
  110. return err
  111. } else if ip == nil {
  112. return nil
  113. }
  114. if utils.IPInNets(client.realIP, server.Config().Server.proxyAllowedFromNets) {
  115. // assume PROXY connections are always secure
  116. err, quitMsg = client.ApplyProxiedIP(session, ip, true)
  117. return
  118. } else {
  119. // real source IP is not authorized to issue PROXY:
  120. return errBadGatewayAddress
  121. }
  122. }