cache.go 3.5 KB

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