123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- package dynamicproxy
- import (
- "encoding/json"
- "errors"
- "strings"
- "golang.org/x/text/cases"
- "golang.org/x/text/language"
- )
- /*
- endpoint.go
- author: tobychui
- This script handle the proxy endpoint object actions
- so proxyEndpoint can be handled like a proper oop object
- Most of the functions are implemented in dynamicproxy.go
- */
- /*
- User Defined Header Functions
- */
- // Check if a user define header exists in this endpoint, ignore case
- func (ep *ProxyEndpoint) UserDefinedHeaderExists(key string) bool {
- for _, header := range ep.UserDefinedHeaders {
- if strings.EqualFold(header.Key, key) {
- return true
- }
- }
- return false
- }
- // Remvoe a user defined header from the list
- func (ep *ProxyEndpoint) RemoveUserDefinedHeader(key string) error {
- newHeaderList := []*UserDefinedHeader{}
- for _, header := range ep.UserDefinedHeaders {
- if !strings.EqualFold(header.Key, key) {
- newHeaderList = append(newHeaderList, header)
- }
- }
- ep.UserDefinedHeaders = newHeaderList
- return nil
- }
- // Add a user defined header to the list, duplicates will be automatically removed
- func (ep *ProxyEndpoint) AddUserDefinedHeader(key string, value string) error {
- if ep.UserDefinedHeaderExists(key) {
- ep.RemoveUserDefinedHeader(key)
- }
- ep.UserDefinedHeaders = append(ep.UserDefinedHeaders, &UserDefinedHeader{
- Key: cases.Title(language.Und, cases.NoLower).String(key), //e.g. x-proxy-by -> X-Proxy-By
- Value: value,
- })
- return nil
- }
- /*
- Virtual Directory Functions
- */
- // Get virtual directory handler from given URI
- func (ep *ProxyEndpoint) GetVirtualDirectoryHandlerFromRequestURI(requestURI string) *VirtualDirectoryEndpoint {
- for _, vdir := range ep.VirtualDirectories {
- if strings.HasPrefix(requestURI, vdir.MatchingPath) {
- return vdir
- }
- }
- return nil
- }
- // Get virtual directory handler by matching path (exact match required)
- func (ep *ProxyEndpoint) GetVirtualDirectoryRuleByMatchingPath(matchingPath string) *VirtualDirectoryEndpoint {
- for _, vdir := range ep.VirtualDirectories {
- if vdir.MatchingPath == matchingPath {
- return vdir
- }
- }
- return nil
- }
- // Delete a vdir rule by its matching path
- func (ep *ProxyEndpoint) RemoveVirtualDirectoryRuleByMatchingPath(matchingPath string) error {
- entryFound := false
- newVirtualDirectoryList := []*VirtualDirectoryEndpoint{}
- for _, vdir := range ep.VirtualDirectories {
- if vdir.MatchingPath == matchingPath {
- entryFound = true
- } else {
- newVirtualDirectoryList = append(newVirtualDirectoryList, vdir)
- }
- }
- if entryFound {
- //Update the list of vdirs
- ep.VirtualDirectories = newVirtualDirectoryList
- return nil
- }
- return errors.New("target virtual directory routing rule not found")
- }
- // Delete a vdir rule by its matching path
- func (ep *ProxyEndpoint) AddVirtualDirectoryRule(vdir *VirtualDirectoryEndpoint) (*ProxyEndpoint, error) {
- //Check for matching path duplicate
- if ep.GetVirtualDirectoryRuleByMatchingPath(vdir.MatchingPath) != nil {
- return nil, errors.New("rule with same matching path already exists")
- }
- //Append it to the list of virtual directory
- ep.VirtualDirectories = append(ep.VirtualDirectories, vdir)
- //Prepare to replace the current routing rule
- parentRouter := ep.parent
- readyRoutingRule, err := parentRouter.PrepareProxyRoute(ep)
- if err != nil {
- return nil, err
- }
- if ep.ProxyType == ProxyType_Root {
- parentRouter.Root = readyRoutingRule
- } else if ep.ProxyType == ProxyType_Host {
- ep.Remove()
- parentRouter.AddProxyRouteToRuntime(readyRoutingRule)
- } else {
- return nil, errors.New("unsupported proxy type")
- }
- return readyRoutingRule, nil
- }
- // Create a deep clone object of the proxy endpoint
- // Note the returned object is not activated. Call to prepare function before pushing into runtime
- func (ep *ProxyEndpoint) Clone() *ProxyEndpoint {
- clonedProxyEndpoint := ProxyEndpoint{}
- js, _ := json.Marshal(ep)
- json.Unmarshal(js, &clonedProxyEndpoint)
- return &clonedProxyEndpoint
- }
- // Remove this proxy endpoint from running proxy endpoint list
- func (ep *ProxyEndpoint) Remove() error {
- ep.parent.ProxyEndpoints.Delete(ep.RootOrMatchingDomain)
- return nil
- }
- // Write changes to runtime without respawning the proxy handler
- // use prepare -> remove -> add if you change anything in the endpoint
- // that effects the proxy routing src / dest
- func (ep *ProxyEndpoint) UpdateToRuntime() {
- ep.parent.ProxyEndpoints.Store(ep.RootOrMatchingDomain, ep)
- }
|