cookie.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. GNU GENERAL PUBLIC LICENSE
  3. Version 3, 29 June 2007
  4. Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
  5. Everyone is permitted to copy and distribute verbatim copies
  6. of this license document, but changing it is not allowed.
  7. http-wokou, An HTTP Proxying framework for bypassing DNS Security
  8. Copyright (C) 2024 Russell Hrubesky, ChiralWorks Software LLC
  9. This program is free software: you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation, either version 3 of the License, or
  12. (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. package httpserver
  21. import (
  22. "bufio"
  23. "encoding/json"
  24. "fmt"
  25. "log"
  26. "net/http"
  27. "os"
  28. "strconv"
  29. "strings"
  30. )
  31. var prompts = [...]string{
  32. "Name of the cookie: ",
  33. "Value to supply it: ",
  34. "Max age of the cookie (default=0, for no expiry): ",
  35. "URI path to include it for (default=/): ",
  36. "Domain that the cookie belongs to: ",
  37. "HTTPS only? (default yes) y/n: ",
  38. }
  39. /*
  40. Open an interactive prompt for a user to save cookies to
  41. */
  42. func AddCookiePrompt(loc string, cookies []*http.Cookie) {
  43. var answers []string
  44. var https bool
  45. var name string
  46. var value string
  47. var domain string
  48. maxAge := 0
  49. path := "/"
  50. reader := bufio.NewReader(os.Stdin)
  51. for idx := range prompts {
  52. fmt.Print(prompts[idx])
  53. ans, err := reader.ReadString('\n')
  54. if err != nil {
  55. log.Fatal("Fatal error when reading cookie creation input: ", err)
  56. }
  57. ans = strings.TrimSuffix(ans, "\n")
  58. if prompts[idx] == "HTTPS only? (default yes) y/n: " {
  59. if ans == "y" {
  60. https = true
  61. }
  62. if ans == "n" {
  63. https = false
  64. }
  65. }
  66. if prompts[idx] == "Max age of the cookie (default=0, for no expiry): " {
  67. if ans != "" {
  68. maxAge, err = strconv.Atoi(answers[idx])
  69. if err != nil {
  70. log.Fatal("Not an integer!")
  71. }
  72. }
  73. }
  74. if prompts[idx] == "URI path to include it for (default=/): " {
  75. if ans != "" {
  76. path = prompts[idx]
  77. }
  78. }
  79. if prompts[idx] == "Name of the cookie: " {
  80. name = ans
  81. }
  82. if prompts[idx] == "Value to supply it: " {
  83. value = ans
  84. }
  85. if prompts[idx] == "Domain that the cookie belongs to: " {
  86. domain = ans
  87. }
  88. }
  89. ck := &http.Cookie{
  90. Name: name,
  91. Value: value,
  92. MaxAge: maxAge,
  93. Path: path,
  94. Domain: domain,
  95. Secure: https,
  96. }
  97. cookies = append(cookies, ck)
  98. b, err := json.Marshal(cookies)
  99. if err != nil {
  100. log.Fatal("couldnt marshal the cookie: ", err)
  101. }
  102. os.WriteFile(loc, b, os.ModePerm)
  103. }