include.go 4.3 KB

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