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

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