Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

gateways.go 3.9KB

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