include.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. "encoding/json"
  23. "fmt"
  24. "io/ioutil"
  25. "log"
  26. "net/http"
  27. "os"
  28. "path"
  29. )
  30. type HttpServerConfig struct {
  31. HttpPort int `json:"http_port"`
  32. HttpsPort int `json:"https_port"`
  33. AllowedDomain string `json:"allowed_domain"`
  34. FullDomain string // The domain name with the protocol before it
  35. AltAllowedDomain string `json:"alt_allowed_domain"` // alternate domain that resources are sourced from
  36. FullAltAllowedDomain string // the alt domain with the protocol
  37. Proto string `json:"proto"` // http/https
  38. UserAgent string `json:"user_agent"`
  39. UseSsl bool `json:"use_ssl"`
  40. ProxyAddr string `json:"proxy_addr"`
  41. RouteMapDir string `json:"routemap_dir"`
  42. PageModPath string `json:"page_mod_path"`
  43. FullProxyDomain string // the domain name of the proxied site with the protocol
  44. CookieJar []*http.Cookie
  45. PhpSession *http.Cookie
  46. SsoToken *http.Cookie
  47. }
  48. type Cookie struct {
  49. Name string `json:"name"`
  50. Value string `json:"value"`
  51. MaxAge int `json:"max_age"`
  52. Path string `json:"path"`
  53. Domain string `json:"domain"`
  54. Secure bool `json:"secure"`
  55. IncludeSub bool `json:"include_sub"`
  56. }
  57. type RouteMapping struct {
  58. DomainName string `json:"domain_name"`
  59. UriPaths []string `json:"uri_paths"`
  60. RouteSet map[string]struct{}
  61. }
  62. /*
  63. Reads the server configuration file, along with the cookie file so that the correlated account can be
  64. accessed through the proxy
  65. :param loc: the location of the config file
  66. */
  67. func ReadConfig(loc string) (*HttpServerConfig, error) {
  68. f, err := os.ReadFile(loc)
  69. if err != nil {
  70. return nil, err
  71. }
  72. var cfg HttpServerConfig
  73. err = json.Unmarshal(f, &cfg)
  74. if err != nil {
  75. return nil, err
  76. }
  77. cf, err := os.ReadFile("./cookies.json")
  78. if err != nil {
  79. return nil, err
  80. }
  81. cfg.FullDomain = fmt.Sprintf("%s://%s", cfg.Proto, cfg.AllowedDomain)
  82. cfg.FullProxyDomain = fmt.Sprintf("%s://%s", cfg.Proto, cfg.ProxyAddr)
  83. cfg.FullAltAllowedDomain = fmt.Sprintf("%s://%s", cfg.Proto, cfg.AltAllowedDomain)
  84. var cookies []Cookie
  85. err = json.Unmarshal(cf, &cookies)
  86. if err != nil {
  87. return nil, err
  88. }
  89. for idx := range cookies {
  90. httpCookie := &http.Cookie{
  91. Domain: cookies[idx].Domain,
  92. MaxAge: cookies[idx].MaxAge,
  93. Name: cookies[idx].Name,
  94. Value: cookies[idx].Value,
  95. Path: cookies[idx].Path,
  96. Secure: cookies[idx].Secure,
  97. }
  98. cfg.CookieJar = append(cfg.CookieJar, httpCookie)
  99. if httpCookie.Name == "PHPSESSID" {
  100. cfg.PhpSession = httpCookie
  101. }
  102. if httpCookie.Name == "sso_token" {
  103. cfg.SsoToken = httpCookie
  104. }
  105. }
  106. fmt.Printf("%+v\n", cfg)
  107. return &cfg, err
  108. }
  109. func ReadRouteMap(loc string) *RouteMapping {
  110. f, err := os.ReadFile(loc)
  111. if err != nil {
  112. log.Fatal(err)
  113. }
  114. var mapfile RouteMapping
  115. err = json.Unmarshal(f, &mapfile)
  116. if err != nil {
  117. log.Fatal(err)
  118. }
  119. rmap := map[string]struct{}{}
  120. for idx := range mapfile.UriPaths {
  121. rmap[mapfile.UriPaths[idx]] = struct{}{}
  122. }
  123. mapfile.RouteSet = rmap
  124. return &mapfile
  125. }
  126. func PopulateRouteMaps(dir string) map[string]*RouteMapping {
  127. routeMaps := map[string]*RouteMapping{}
  128. fnames, err := ioutil.ReadDir(dir)
  129. if err != nil {
  130. log.Fatal("Failed to read the routemap directory: ", err)
  131. }
  132. for _, f := range fnames {
  133. mapping := ReadRouteMap(path.Join(dir, f.Name()))
  134. routeMaps[f.Name()] = mapping
  135. }
  136. return routeMaps
  137. }