pagemod.go 3.0 KB

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