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.
 
 
 

81 lines
2.4 KiB

  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2014 DutchCoders [https://github.com/dutchcoders/]
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. package main
  21. import (
  22. "github.com/goamz/goamz/aws"
  23. "github.com/goamz/goamz/s3"
  24. "net/http"
  25. "net/mail"
  26. "strings"
  27. "time"
  28. )
  29. func getBucket() (*s3.Bucket, error) {
  30. auth, err := aws.GetAuth(config.AWS_ACCESS_KEY, config.AWS_SECRET_KEY, "", time.Time{})
  31. if err != nil {
  32. return nil, err
  33. }
  34. conn := s3.New(auth, aws.Regions["eu-west-1"])
  35. b := conn.Bucket(config.BUCKET)
  36. return b, nil
  37. }
  38. // Request.RemoteAddress contains port, which we want to remove i.e.:
  39. // "[::1]:58292" => "[::1]"
  40. func ipAddrFromRemoteAddr(s string) string {
  41. idx := strings.LastIndex(s, ":")
  42. if idx == -1 {
  43. return s
  44. }
  45. return s[:idx]
  46. }
  47. func getIpAddress(r *http.Request) string {
  48. hdr := r.Header
  49. hdrRealIp := hdr.Get("X-Real-Ip")
  50. hdrForwardedFor := hdr.Get("X-Forwarded-For")
  51. if hdrRealIp == "" && hdrForwardedFor == "" {
  52. return ipAddrFromRemoteAddr(r.RemoteAddr)
  53. }
  54. if hdrForwardedFor != "" {
  55. // X-Forwarded-For is potentially a list of addresses separated with ","
  56. parts := strings.Split(hdrForwardedFor, ",")
  57. for i, p := range parts {
  58. parts[i] = strings.TrimSpace(p)
  59. }
  60. // TODO: should return first non-local address
  61. return parts[0]
  62. }
  63. return hdrRealIp
  64. }
  65. func encodeRFC2047(String string) string {
  66. // use mail's rfc2047 to encode any string
  67. addr := mail.Address{String, ""}
  68. return strings.Trim(addr.String(), " <>")
  69. }