server.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. "Include it in subdomains? (default=y) y/n: ",
  39. }
  40. /*
  41. Open an interactive prompt for a user to save cookies to
  42. */
  43. func AddCookiePrompt(path string, cookies []*http.Cookie) {
  44. var answers []string
  45. var https bool
  46. var subd bool
  47. var name string
  48. var value string
  49. var domain string
  50. maxAge := 0
  51. path := "/"
  52. reader := bufio.NewReader(os.Stdin)
  53. for idx := range prompts {
  54. fmt.Print(prompts[idx])
  55. ans, err := reader.ReadString('\n')
  56. if err != nil {
  57. log.Fatal("Fatal error when reading cookie creation input: ", err)
  58. }
  59. ans = strings.TrimSuffix(ans, "\n")
  60. if prompts[idx] == "HTTPS only? (default yes) y/n: " {
  61. if ans == "y" {
  62. https = true
  63. }
  64. if ans == "n" {
  65. https = false
  66. }
  67. }
  68. if prompts[idx] == "Max age of the cookie (default=0, for no expiry): " {
  69. if ans != "" {
  70. maxAge, err = strconv.Atoi(answers[idx])
  71. if err != nil {
  72. log.Fatal("Not an integer!")
  73. }
  74. }
  75. }
  76. if prompts[idx] == "Include it in subdomains? (default=y) y/n: " {
  77. if ans == "y" {
  78. subd = true
  79. }
  80. if ans == "n" {
  81. subd = false
  82. }
  83. }
  84. if prompts[idx] == "URI path to include it for (default=/): " {
  85. if ans != "" {
  86. path = prompts[idx]
  87. }
  88. }
  89. if prompts[idx] == "Name of the cookie: " {
  90. name = ans
  91. }
  92. if prompts[idx] == "Value to supply it: " {
  93. value = ans
  94. }
  95. if prompts[idx] == "Domain that the cookie belongs to: " {
  96. domain = ans
  97. }
  98. }
  99. ck := &http.Cookie{
  100. Name: name,
  101. Value: value,
  102. MaxAge: maxAge,
  103. Path: path,
  104. Domain: domain,
  105. Secure: https,
  106. }
  107. cookies = append(cookies, ck)
  108. b, err := json.Marshal(cookies)
  109. if err != nil {
  110. log.Fatal("couldnt marshal the cookie: ", err)
  111. }
  112. os.WriteFile(path, b, os.ModePerm)
  113. }