1
0

endpoints.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. "imuslab.com/zoraxy/mod/dynamicproxy/loadbalance"
  9. )
  10. /*
  11. endpoint.go
  12. author: tobychui
  13. This script handle the proxy endpoint object actions
  14. so proxyEndpoint can be handled like a proper oop object
  15. Most of the functions are implemented in dynamicproxy.go
  16. */
  17. /*
  18. User Defined Header Functions
  19. */
  20. // Check if a user define header exists in this endpoint, ignore case
  21. func (ep *ProxyEndpoint) UserDefinedHeaderExists(key string) bool {
  22. for _, header := range ep.UserDefinedHeaders {
  23. if strings.EqualFold(header.Key, key) {
  24. return true
  25. }
  26. }
  27. return false
  28. }
  29. // Remvoe a user defined header from the list
  30. func (ep *ProxyEndpoint) RemoveUserDefinedHeader(key string) error {
  31. newHeaderList := []*UserDefinedHeader{}
  32. for _, header := range ep.UserDefinedHeaders {
  33. if !strings.EqualFold(header.Key, key) {
  34. newHeaderList = append(newHeaderList, header)
  35. }
  36. }
  37. ep.UserDefinedHeaders = newHeaderList
  38. return nil
  39. }
  40. // Add a user defined header to the list, duplicates will be automatically removed
  41. func (ep *ProxyEndpoint) AddUserDefinedHeader(newHeaderRule *UserDefinedHeader) error {
  42. if ep.UserDefinedHeaderExists(newHeaderRule.Key) {
  43. ep.RemoveUserDefinedHeader(newHeaderRule.Key)
  44. }
  45. newHeaderRule.Key = cases.Title(language.Und, cases.NoLower).String(newHeaderRule.Key)
  46. ep.UserDefinedHeaders = append(ep.UserDefinedHeaders, newHeaderRule)
  47. return nil
  48. }
  49. /*
  50. Virtual Directory Functions
  51. */
  52. // Get virtual directory handler from given URI
  53. func (ep *ProxyEndpoint) GetVirtualDirectoryHandlerFromRequestURI(requestURI string) *VirtualDirectoryEndpoint {
  54. for _, vdir := range ep.VirtualDirectories {
  55. if strings.HasPrefix(requestURI, vdir.MatchingPath) {
  56. thisVdir := vdir
  57. return thisVdir
  58. }
  59. }
  60. return nil
  61. }
  62. // Get virtual directory handler by matching path (exact match required)
  63. func (ep *ProxyEndpoint) GetVirtualDirectoryRuleByMatchingPath(matchingPath string) *VirtualDirectoryEndpoint {
  64. for _, vdir := range ep.VirtualDirectories {
  65. if vdir.MatchingPath == matchingPath {
  66. thisVdir := vdir
  67. return thisVdir
  68. }
  69. }
  70. return nil
  71. }
  72. // Delete a vdir rule by its matching path
  73. func (ep *ProxyEndpoint) RemoveVirtualDirectoryRuleByMatchingPath(matchingPath string) error {
  74. entryFound := false
  75. newVirtualDirectoryList := []*VirtualDirectoryEndpoint{}
  76. for _, vdir := range ep.VirtualDirectories {
  77. if vdir.MatchingPath == matchingPath {
  78. entryFound = true
  79. } else {
  80. newVirtualDirectoryList = append(newVirtualDirectoryList, vdir)
  81. }
  82. }
  83. if entryFound {
  84. //Update the list of vdirs
  85. ep.VirtualDirectories = newVirtualDirectoryList
  86. return nil
  87. }
  88. return errors.New("target virtual directory routing rule not found")
  89. }
  90. // Delete a vdir rule by its matching path
  91. func (ep *ProxyEndpoint) AddVirtualDirectoryRule(vdir *VirtualDirectoryEndpoint) (*ProxyEndpoint, error) {
  92. //Check for matching path duplicate
  93. if ep.GetVirtualDirectoryRuleByMatchingPath(vdir.MatchingPath) != nil {
  94. return nil, errors.New("rule with same matching path already exists")
  95. }
  96. //Append it to the list of virtual directory
  97. ep.VirtualDirectories = append(ep.VirtualDirectories, vdir)
  98. //Prepare to replace the current routing rule
  99. parentRouter := ep.parent
  100. readyRoutingRule, err := parentRouter.PrepareProxyRoute(ep)
  101. if err != nil {
  102. return nil, err
  103. }
  104. if ep.ProxyType == ProxyType_Root {
  105. parentRouter.Root = readyRoutingRule
  106. } else if ep.ProxyType == ProxyType_Host {
  107. ep.Remove()
  108. parentRouter.AddProxyRouteToRuntime(readyRoutingRule)
  109. } else {
  110. return nil, errors.New("unsupported proxy type")
  111. }
  112. return readyRoutingRule, nil
  113. }
  114. /* Upstream related wrapper functions */
  115. //Check if there already exists another upstream with identical origin
  116. func (ep *ProxyEndpoint) UpstreamOriginExists(originURL string) bool {
  117. for _, origin := range ep.Origins {
  118. if origin.OriginIpOrDomain == originURL {
  119. return true
  120. }
  121. }
  122. return false
  123. }
  124. // Add upstream to origin and update it to runtime
  125. func (ep *ProxyEndpoint) AddUpstreamOrigin(newOrigin *loadbalance.Upstream) error {
  126. //Check if the upstream already exists
  127. if ep.UpstreamOriginExists(newOrigin.OriginIpOrDomain) {
  128. return errors.New("upstream with same origin already exists")
  129. }
  130. //Ok, add the origin to list
  131. ep.Origins = append(ep.Origins, newOrigin)
  132. ep.UpdateToRuntime()
  133. return nil
  134. }
  135. // Check if the proxy endpoint hostname or alias name contains subdomain wildcard
  136. func (ep *ProxyEndpoint) ContainsWildcardName(skipAliasCheck bool) bool {
  137. hostname := ep.RootOrMatchingDomain
  138. aliasHostnames := ep.MatchingDomainAlias
  139. wildcardCheck := func(hostname string) bool {
  140. return len(hostname) > 0 && hostname[0] == '*'
  141. }
  142. if wildcardCheck(hostname) {
  143. return true
  144. }
  145. if !skipAliasCheck {
  146. for _, aliasHostname := range aliasHostnames {
  147. if wildcardCheck(aliasHostname) {
  148. return true
  149. }
  150. }
  151. }
  152. return false
  153. }
  154. // Create a deep clone object of the proxy endpoint
  155. // Note the returned object is not activated. Call to prepare function before pushing into runtime
  156. func (ep *ProxyEndpoint) Clone() *ProxyEndpoint {
  157. clonedProxyEndpoint := ProxyEndpoint{}
  158. js, _ := json.Marshal(ep)
  159. json.Unmarshal(js, &clonedProxyEndpoint)
  160. return &clonedProxyEndpoint
  161. }
  162. // Remove this proxy endpoint from running proxy endpoint list
  163. func (ep *ProxyEndpoint) Remove() error {
  164. ep.parent.ProxyEndpoints.Delete(ep.RootOrMatchingDomain)
  165. return nil
  166. }
  167. // Write changes to runtime without respawning the proxy handler
  168. // use prepare -> remove -> add if you change anything in the endpoint
  169. // that effects the proxy routing src / dest
  170. func (ep *ProxyEndpoint) UpdateToRuntime() {
  171. ep.parent.ProxyEndpoints.Store(ep.RootOrMatchingDomain, ep)
  172. }