pagemod.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. "bytes"
  23. "encoding/json"
  24. "fmt"
  25. "io"
  26. "log"
  27. "os"
  28. )
  29. type PageMod struct {
  30. Target string `json:"target"`
  31. Search string `json:"search"`
  32. Sub string `json:"sub"`
  33. }
  34. /*
  35. operate on a PageMod pointer to set the 'sub' field
  36. :param content: the content to set as the 'sub' field
  37. */
  38. func (p *PageMod) SetFieldSub(content string) {
  39. p.Sub = content
  40. }
  41. /*
  42. operate on a PageMod pointer and set the 'target' field
  43. :param content: the content to set as the 'target' field
  44. */
  45. func (p *PageMod) SetFieldTarget(content string) {
  46. p.Target = content
  47. }
  48. type AllPageMods struct {
  49. Content []*PageMod `json:"modifications"`
  50. Bypass map[string]struct{} `json:"bypass"`
  51. }
  52. /*
  53. Load page modifications from the config/pagemod directory
  54. :param loc: the path to the pagemods directory
  55. */
  56. func LoadPageMods(loc string) *AllPageMods {
  57. b, err := os.ReadFile(loc)
  58. if err != nil {
  59. log.Fatal("Couldnt load the page modification config from: ", loc, ". Error: ", err)
  60. }
  61. var pgMod AllPageMods
  62. err = json.Unmarshal(b, &pgMod)
  63. if err != nil {
  64. log.Fatal("There was an error loading the configuration: ", err)
  65. }
  66. for idx := range pgMod.Content {
  67. _, err = os.Stat(pgMod.Content[idx].Sub)
  68. if err != nil {
  69. if os.IsNotExist(err) {
  70. continue
  71. }
  72. }
  73. b, err = os.ReadFile(pgMod.Content[idx].Sub)
  74. if err != nil {
  75. log.Fatal("Could not read the target file: ", pgMod.Content[idx].Sub, " error: ", err)
  76. }
  77. pgMod.Content[idx].SetFieldTarget(string(b))
  78. }
  79. for idx := range pgMod.Content {
  80. _, err = os.Stat(pgMod.Content[idx].Target)
  81. if err != nil {
  82. if os.IsNotExist(err) {
  83. continue
  84. }
  85. }
  86. b, err = os.ReadFile(pgMod.Content[idx].Target)
  87. if err != nil {
  88. log.Fatal("Could not read the target file: ", pgMod.Content[idx].Target, " error: ", err)
  89. }
  90. pgMod.Content[idx].SetFieldSub(string(b))
  91. fmt.Print(string(b))
  92. }
  93. return &pgMod
  94. }
  95. /*
  96. Rewrite all occurences of these values into the response body
  97. */
  98. func (c *Controller) pageMod(data []byte) []byte {
  99. for idx := range c.PageMods.Content {
  100. if c.PageMods.Content[idx].Target == "content" {
  101. data = bytes.ReplaceAll(data, []byte(c.PageMods.Content[idx].Search), []byte(c.PageMods.Content[idx].Sub))
  102. }
  103. }
  104. return data
  105. }
  106. /*
  107. perform any request body rewrites as per described in the pagemod config
  108. :param data: a byte array to modify
  109. */
  110. func (c *Controller) requestBodyRewrites(data io.Reader) io.Reader {
  111. b, err := io.ReadAll(data)
  112. if err != nil {
  113. log.Fatal("couldnt read POST body data: ", err)
  114. }
  115. for idx := range c.PageMods.Content {
  116. if c.PageMods.Content[idx].Target == "body" {
  117. b = bytes.ReplaceAll(b, []byte(c.PageMods.Content[idx].Search), []byte(c.PageMods.Content[idx].Sub))
  118. }
  119. }
  120. return bytes.NewReader(b)
  121. }