pagemod.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. "regexp"
  29. )
  30. type PageMod struct {
  31. Target string `json:"target"`
  32. Search string `json:"search"`
  33. Sub string `json:"sub"`
  34. }
  35. /*
  36. operate on a PageMod pointer to set the 'sub' field
  37. :param content: the content to set as the 'sub' field
  38. */
  39. func (p *PageMod) SetFieldSub(content string) {
  40. p.Sub = content
  41. }
  42. /*
  43. operate on a PageMod pointer and set the 'target' field
  44. :param content: the content to set as the 'target' field
  45. */
  46. func (p *PageMod) SetFieldTarget(content string) {
  47. p.Target = content
  48. }
  49. type AllPageMods struct {
  50. Content []*PageMod `json:"modifications"`
  51. Bypass map[string]struct{} `json:"bypass"`
  52. }
  53. /*
  54. Replace all of the upstreamDomains in the target data byte array with the proxyDomain
  55. :param data: the page content to modify
  56. :param upstreamDomains: a list of strings containing the target domains to replace
  57. :param proxyDomain: the hostname of your proxy
  58. */
  59. func DomainRewrite(data []byte, upstreamDomains []string, proxyDomain string) []byte { // TODO: replace this with a domain name detected regex
  60. for i := range upstreamDomains {
  61. data = bytes.ReplaceAll(data, []byte(upstreamDomains[i]), []byte(proxyDomain))
  62. }
  63. return data
  64. }
  65. /*
  66. Load page modifications from the config/pagemod directory
  67. :param loc: the path to the pagemods directory
  68. */
  69. func LoadPageMods(loc string) *AllPageMods {
  70. b, err := os.ReadFile(loc)
  71. if err != nil {
  72. log.Fatal("Couldnt load the page modification config from: ", loc, ". Error: ", err)
  73. }
  74. var pgMod AllPageMods
  75. err = json.Unmarshal(b, &pgMod)
  76. if err != nil {
  77. log.Fatal("There was an error loading the configuration: ", err)
  78. }
  79. for idx := range pgMod.Content {
  80. _, err = os.Stat(pgMod.Content[idx].Sub)
  81. if err != nil {
  82. if os.IsNotExist(err) {
  83. continue
  84. }
  85. }
  86. b, err = os.ReadFile(pgMod.Content[idx].Sub)
  87. if err != nil {
  88. log.Fatal("Could not read the target file: ", pgMod.Content[idx].Sub, " error: ", err)
  89. }
  90. pgMod.Content[idx].SetFieldTarget(string(b))
  91. }
  92. for idx := range pgMod.Content {
  93. _, err = os.Stat(pgMod.Content[idx].Target)
  94. if err != nil {
  95. if os.IsNotExist(err) {
  96. continue
  97. }
  98. }
  99. b, err = os.ReadFile(pgMod.Content[idx].Target)
  100. if err != nil {
  101. log.Fatal("Could not read the target file: ", pgMod.Content[idx].Target, " error: ", err)
  102. }
  103. pgMod.Content[idx].SetFieldSub(string(b))
  104. fmt.Print(string(b))
  105. }
  106. return &pgMod
  107. }
  108. func replaceDomains(data string, targetdomain string) string {
  109. reg := regexp.MustCompile(`^(?=.{1,253}\.?$)(?:(?!-|[^.]+_)[A-Za-z0-9-_]{1,63}(?<!-)(?:\.|$)){2,}$`)
  110. return reg.ReplaceAllString(string(data), targetdomain)
  111. }
  112. /*
  113. Rewrite all occurences of these values into the response body
  114. */
  115. func (c *Controller) pageMod(data []byte) []byte {
  116. data = DomainRewrite(data, c.Config.KnownHosts, c.Config.ProxyAddr)
  117. for idx := range c.PageMods.Content {
  118. if c.PageMods.Content[idx].Target == "content" {
  119. data = bytes.ReplaceAll(data, []byte(c.PageMods.Content[idx].Search), []byte(c.PageMods.Content[idx].Sub))
  120. }
  121. }
  122. return data
  123. }
  124. /*
  125. perform any request body rewrites as per described in the pagemod config
  126. :param data: a byte array to modify
  127. */
  128. func (c *Controller) requestBodyRewrites(data io.Reader) io.Reader {
  129. b, err := io.ReadAll(data)
  130. if err != nil {
  131. log.Fatal("couldnt read POST body data: ", err)
  132. }
  133. b = DomainRewrite(b, c.Config.KnownHosts, c.Config.ProxyAddr)
  134. for idx := range c.PageMods.Content {
  135. if c.PageMods.Content[idx].Target == "body" {
  136. b = bytes.ReplaceAll(b, []byte(c.PageMods.Content[idx].Search), []byte(c.PageMods.Content[idx].Sub))
  137. }
  138. }
  139. return bytes.NewReader(b)
  140. }