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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. // given IP is sane! override the client's current IP
  55. ipstring := parsedProxiedIP.String()
  56. client.server.logger.Info("localconnect-ip", "Accepted proxy IP for client", ipstring)
  57. rawHostname := utils.LookupHostname(ipstring)
  58. cloakedHostname := client.server.Config().Server.Cloaks.ComputeCloak(parsedProxiedIP)
  59. client.stateMutex.Lock()
  60. defer client.stateMutex.Unlock()
  61. session.proxiedIP = parsedProxiedIP
  62. client.proxiedIP = parsedProxiedIP
  63. session.rawHostname = rawHostname
  64. client.rawHostname = rawHostname
  65. client.cloakedHostname = cloakedHostname
  66. // nickmask will be updated when the client completes registration
  67. // set tls info
  68. client.certfp = ""
  69. client.SetMode(modes.TLS, tls)
  70. return nil, ""
  71. }
  72. // handle the PROXY command: http://www.haproxy.org/download/1.8/doc/proxy-protocol.txt
  73. // PROXY must be sent as the first message in the session and has the syntax:
  74. // PROXY TCP[46] SOURCEIP DESTIP SOURCEPORT DESTPORT\r\n
  75. // unfortunately, an ipv6 SOURCEIP can start with a double colon; in this case,
  76. // the message is invalid IRC and can't be parsed normally, hence the special handling.
  77. func handleProxyCommand(server *Server, client *Client, session *Session, line string) (err error) {
  78. var quitMsg string
  79. defer func() {
  80. if err != nil {
  81. if quitMsg == "" {
  82. quitMsg = client.t("Bad or unauthorized PROXY command")
  83. }
  84. client.Quit(quitMsg, session)
  85. }
  86. }()
  87. params := strings.Fields(line)
  88. if len(params) != 6 {
  89. return errBadProxyLine
  90. }
  91. if utils.IPInNets(client.realIP, server.Config().Server.proxyAllowedFromNets) {
  92. // assume PROXY connections are always secure
  93. err, quitMsg = client.ApplyProxiedIP(session, params[2], true)
  94. return
  95. } else {
  96. // real source IP is not authorized to issue PROXY:
  97. return errBadGatewayAddress
  98. }
  99. }