endpoints.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.EqualFold(header.Key, 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.EqualFold(header.Key, key) {
  33. newHeaderList = append(newHeaderList, header)
  34. }
  35. }
  36. ep.UserDefinedHeaders = newHeaderList
  37. return nil
  38. }
  39. // Add a user defined header to the list, duplicates will be automatically removed
  40. func (ep *ProxyEndpoint) AddUserDefinedHeader(key string, value string) error {
  41. if ep.UserDefinedHeaderExists(key) {
  42. ep.RemoveUserDefinedHeader(key)
  43. }
  44. ep.UserDefinedHeaders = append(ep.UserDefinedHeaders, &UserDefinedHeader{
  45. Key: cases.Title(language.Und, cases.NoLower).String(key), //e.g. x-proxy-by -> X-Proxy-By
  46. Value: value,
  47. })
  48. return nil
  49. }
  50. /*
  51. Virtual Directory Functions
  52. */
  53. // Get virtual directory handler from given URI
  54. func (ep *ProxyEndpoint) GetVirtualDirectoryHandlerFromRequestURI(requestURI string) *VirtualDirectoryEndpoint {
  55. for _, vdir := range ep.VirtualDirectories {
  56. if strings.HasPrefix(requestURI, vdir.MatchingPath) {
  57. thisVdir := vdir
  58. return thisVdir
  59. }
  60. }
  61. return nil
  62. }
  63. // Get virtual directory handler by matching path (exact match required)
  64. func (ep *ProxyEndpoint) GetVirtualDirectoryRuleByMatchingPath(matchingPath string) *VirtualDirectoryEndpoint {
  65. for _, vdir := range ep.VirtualDirectories {
  66. if vdir.MatchingPath == matchingPath {
  67. thisVdir := vdir
  68. return thisVdir
  69. }
  70. }
  71. return nil
  72. }
  73. // Delete a vdir rule by its matching path
  74. func (ep *ProxyEndpoint) RemoveVirtualDirectoryRuleByMatchingPath(matchingPath string) error {
  75. entryFound := false
  76. newVirtualDirectoryList := []*VirtualDirectoryEndpoint{}
  77. for _, vdir := range ep.VirtualDirectories {
  78. if vdir.MatchingPath == matchingPath {
  79. entryFound = true
  80. } else {
  81. newVirtualDirectoryList = append(newVirtualDirectoryList, vdir)
  82. }
  83. }
  84. if entryFound {
  85. //Update the list of vdirs
  86. ep.VirtualDirectories = newVirtualDirectoryList
  87. return nil
  88. }
  89. return errors.New("target virtual directory routing rule not found")
  90. }
  91. // Delete a vdir rule by its matching path
  92. func (ep *ProxyEndpoint) AddVirtualDirectoryRule(vdir *VirtualDirectoryEndpoint) (*ProxyEndpoint, error) {
  93. //Check for matching path duplicate
  94. if ep.GetVirtualDirectoryRuleByMatchingPath(vdir.MatchingPath) != nil {
  95. return nil, errors.New("rule with same matching path already exists")
  96. }
  97. //Append it to the list of virtual directory
  98. ep.VirtualDirectories = append(ep.VirtualDirectories, vdir)
  99. //Prepare to replace the current routing rule
  100. parentRouter := ep.parent
  101. readyRoutingRule, err := parentRouter.PrepareProxyRoute(ep)
  102. if err != nil {
  103. return nil, err
  104. }
  105. if ep.ProxyType == ProxyType_Root {
  106. parentRouter.Root = readyRoutingRule
  107. } else if ep.ProxyType == ProxyType_Host {
  108. ep.Remove()
  109. parentRouter.AddProxyRouteToRuntime(readyRoutingRule)
  110. } else {
  111. return nil, errors.New("unsupported proxy type")
  112. }
  113. return readyRoutingRule, nil
  114. }
  115. // Create a deep clone object of the proxy endpoint
  116. // Note the returned object is not activated. Call to prepare function before pushing into runtime
  117. func (ep *ProxyEndpoint) Clone() *ProxyEndpoint {
  118. clonedProxyEndpoint := ProxyEndpoint{}
  119. js, _ := json.Marshal(ep)
  120. json.Unmarshal(js, &clonedProxyEndpoint)
  121. return &clonedProxyEndpoint
  122. }
  123. // Remove this proxy endpoint from running proxy endpoint list
  124. func (ep *ProxyEndpoint) Remove() error {
  125. ep.parent.ProxyEndpoints.Delete(ep.RootOrMatchingDomain)
  126. return nil
  127. }
  128. // Write changes to runtime without respawning the proxy handler
  129. // use prepare -> remove -> add if you change anything in the endpoint
  130. // that effects the proxy routing src / dest
  131. func (ep *ProxyEndpoint) UpdateToRuntime() {
  132. ep.parent.ProxyEndpoints.Store(ep.RootOrMatchingDomain, ep)
  133. }