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 3.6KB

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