endpoints.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package dynamicproxy
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "strings"
  6. "golang.org/x/text/cases"
  7. "golang.org/x/text/language"
  8. )
  9. /*
  10. endpoint.go
  11. author: tobychui
  12. This script handle the proxy endpoint object actions
  13. so proxyEndpoint can be handled like a proper oop object
  14. Most of the functions are implemented in dynamicproxy.go
  15. */
  16. /*
  17. User Defined Header Functions
  18. */
  19. // Check if a user define header exists in this endpoint, ignore case
  20. func (ep *ProxyEndpoint) UserDefinedHeaderExists(key string) bool {
  21. for _, header := range ep.UserDefinedHeaders {
  22. if strings.ToLower(header.Key) == strings.ToLower(key) {
  23. return true
  24. }
  25. }
  26. return false
  27. }
  28. // Remvoe a user defined header from the list
  29. func (ep *ProxyEndpoint) RemoveUserDefinedHeader(key string) error {
  30. newHeaderList := []*UserDefinedHeader{}
  31. for _, header := range ep.UserDefinedHeaders {
  32. if strings.ToLower(header.Key) != strings.ToLower(key) {
  33. newHeaderList = append(newHeaderList, header)
  34. }
  35. }
  36. return nil
  37. }
  38. func (ep *ProxyEndpoint) AddUserDefinedHeader(key string, value string) error {
  39. if ep.UserDefinedHeaderExists(key) {
  40. ep.RemoveUserDefinedHeader(key)
  41. }
  42. ep.UserDefinedHeaders = append(ep.UserDefinedHeaders, &UserDefinedHeader{
  43. Key: cases.Title(language.Und, cases.NoLower).String(key), //e.g. x-proxy-by -> X-Proxy-By
  44. Value: value,
  45. })
  46. return nil
  47. }
  48. /*
  49. Virtual Directory Functions
  50. */
  51. // Get virtual directory handler from given URI
  52. func (ep *ProxyEndpoint) GetVirtualDirectoryHandlerFromRequestURI(requestURI string) *VirtualDirectoryEndpoint {
  53. for _, vdir := range ep.VirtualDirectories {
  54. if strings.HasPrefix(requestURI, vdir.MatchingPath) {
  55. return vdir
  56. }
  57. }
  58. return nil
  59. }
  60. // Get virtual directory handler by matching path (exact match required)
  61. func (ep *ProxyEndpoint) GetVirtualDirectoryRuleByMatchingPath(matchingPath string) *VirtualDirectoryEndpoint {
  62. for _, vdir := range ep.VirtualDirectories {
  63. if vdir.MatchingPath == matchingPath {
  64. return vdir
  65. }
  66. }
  67. return nil
  68. }
  69. // Delete a vdir rule by its matching path
  70. func (ep *ProxyEndpoint) RemoveVirtualDirectoryRuleByMatchingPath(matchingPath string) error {
  71. entryFound := false
  72. newVirtualDirectoryList := []*VirtualDirectoryEndpoint{}
  73. for _, vdir := range ep.VirtualDirectories {
  74. if vdir.MatchingPath == matchingPath {
  75. entryFound = true
  76. } else {
  77. newVirtualDirectoryList = append(newVirtualDirectoryList, vdir)
  78. }
  79. }
  80. if entryFound {
  81. //Update the list of vdirs
  82. ep.VirtualDirectories = newVirtualDirectoryList
  83. return nil
  84. }
  85. return errors.New("target virtual directory routing rule not found")
  86. }
  87. // Delete a vdir rule by its matching path
  88. func (ep *ProxyEndpoint) AddVirtualDirectoryRule(vdir *VirtualDirectoryEndpoint) (*ProxyEndpoint, error) {
  89. //Check for matching path duplicate
  90. if ep.GetVirtualDirectoryRuleByMatchingPath(vdir.MatchingPath) != nil {
  91. return nil, errors.New("rule with same matching path already exists")
  92. }
  93. //Append it to the list of virtual directory
  94. ep.VirtualDirectories = append(ep.VirtualDirectories, vdir)
  95. //Prepare to replace the current routing rule
  96. parentRouter := ep.parent
  97. readyRoutingRule, err := parentRouter.PrepareProxyRoute(ep)
  98. if err != nil {
  99. return nil, err
  100. }
  101. if ep.ProxyType == ProxyType_Root {
  102. parentRouter.Root = readyRoutingRule
  103. } else if ep.ProxyType == ProxyType_Host {
  104. ep.Remove()
  105. parentRouter.AddProxyRouteToRuntime(readyRoutingRule)
  106. } else {
  107. return nil, errors.New("unsupported proxy type")
  108. }
  109. return readyRoutingRule, nil
  110. }
  111. // Create a deep clone object of the proxy endpoint
  112. // Note the returned object is not activated. Call to prepare function before pushing into runtime
  113. func (ep *ProxyEndpoint) Clone() *ProxyEndpoint {
  114. clonedProxyEndpoint := ProxyEndpoint{}
  115. js, _ := json.Marshal(ep)
  116. json.Unmarshal(js, &clonedProxyEndpoint)
  117. return &clonedProxyEndpoint
  118. }
  119. // Remove this proxy endpoint from running proxy endpoint list
  120. func (ep *ProxyEndpoint) Remove() error {
  121. ep.parent.ProxyEndpoints.Delete(ep.RootOrMatchingDomain)
  122. return nil
  123. }
  124. // Write changes to runtime without respawning the proxy handler
  125. // use prepare -> remove -> add if you change anything in the endpoint
  126. // that effects the proxy routing src / dest
  127. func (ep *ProxyEndpoint) UpdateToRuntime() {
  128. ep.parent.ProxyEndpoints.Store(ep.RootOrMatchingDomain, ep)
  129. }