cache.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. "log"
  25. "net/http"
  26. "os"
  27. "strings"
  28. "github.com/patrickmn/go-cache"
  29. )
  30. type CachedResource struct {
  31. Data []byte
  32. Headers *http.Header
  33. Ctype string
  34. Rcode int
  35. }
  36. func NewCachedResource(data []byte, headers *http.Header, rcode int) *CachedResource {
  37. return &CachedResource{
  38. Data: data,
  39. Headers: headers,
  40. Rcode: rcode,
  41. }
  42. }
  43. func (c *Controller) CacheResource(key string, resource *CachedResource) {
  44. fmt.Printf("Cached resource for: %s\n", key)
  45. c.cache.Set(key, resource, cache.DefaultExpiration)
  46. }
  47. func (c *Controller) GetResource(key string) *CachedResource {
  48. resource, found := c.cache.Get(key)
  49. if found {
  50. fmt.Printf("Cache Hit! Found resource for URI: %s\n", key)
  51. return resource.(*CachedResource)
  52. }
  53. return nil
  54. }
  55. type RouteMapping struct {
  56. DomainName string `json:"domain_name"`
  57. UriPaths []string `json:"uri_paths"`
  58. RouteSet map[string]struct{}
  59. }
  60. type RouteMap struct {
  61. Mappings map[string]string `json:"mappings"`
  62. Shotgun map[string]string `json:"shotgun"`
  63. MapCache *cache.Cache
  64. }
  65. type RouteMapper interface {
  66. mapUriToDomain(string, string)
  67. GetMappedDomain(string) (string, bool)
  68. ExportRouteMaps(string)
  69. }
  70. /*
  71. Set a route to exist for the URI to the specific domain
  72. :param uri: the URI to set the route for
  73. :param domain: the domain name to resolve the uri to
  74. */
  75. func (r *RouteMap) MapUriToDomain(uri string, domain string) {
  76. r.MapCache.Set(uri, domain, cache.DefaultExpiration)
  77. }
  78. // returns the domain/url that the uri belongs to as defined in the routemap
  79. func (r *RouteMap) GetMappedDomain(uri string) (string, bool) {
  80. dname, ok := r.MapCache.Get(uri)
  81. if ok {
  82. return fmt.Sprint(dname), true
  83. }
  84. for k, v := range r.Shotgun {
  85. if strings.Contains(uri, k) {
  86. return v, true
  87. }
  88. }
  89. return "", false
  90. }
  91. // This populates the cache in a RouteMap with the data from the config file
  92. func (r *RouteMap) populateRouteMaps() {
  93. for k, v := range r.Mappings {
  94. r.MapUriToDomain(k, v)
  95. }
  96. }
  97. // Exports the cache into a JSON-friendly data structure (so that it can be written to the file system)
  98. func (r *RouteMap) ExportRouteMap(loc string) {
  99. routeMapOut := &RouteMap{
  100. Mappings: map[string]string{},
  101. Shotgun: map[string]string{},
  102. }
  103. cachedRoutes := r.MapCache.Items()
  104. for k, v := range cachedRoutes {
  105. routeMapOut.Mappings[k] = fmt.Sprint(v.Object)
  106. }
  107. for k, v := range r.Shotgun {
  108. routeMapOut.Shotgun[k] = v
  109. }
  110. b, err := json.Marshal(routeMapOut)
  111. if err != nil {
  112. log.Fatal("failed to marshal struct: ", err)
  113. }
  114. os.WriteFile(loc, b, os.ModePerm)
  115. }