| 9 | ) |
| 10 |
| 11 | var ErrRouterConfigConflict = errors.New("router config conflict") |
| 12 |
| 13 | type routerByHTTPUser map[string][]*Router |
| 14 |
| 15 | type Routers struct { |
| 16 | indexByDomain map[string]routerByHTTPUser |
| 17 |
| 18 | mutex sync.RWMutex |
| 19 | } |
| 20 |
| 21 | type Router struct { |
| 22 | domain string |
| 23 | location string |
| 24 | httpUser string |
| 25 |
| 26 | // store any object here |
| 27 | payload any |
| 28 | } |
| 29 |
| 30 | func NewRouters() *Routers { |
| 31 | return &Routers{ |
| 32 | indexByDomain: make(map[string]routerByHTTPUser), |
| 33 | } |
| 34 | } |
| 35 |
| 36 | func (r *Routers) Add(domain, location, httpUser string, payload any) error { |
| 37 | domain = strings.ToLower(domain) |
| 38 |
| 39 | r.mutex.Lock() |
| 40 | defer r.mutex.Unlock() |
| 41 |
| 42 | if _, exist := r.exist(domain, location, httpUser); exist { |
| 43 | return ErrRouterConfigConflict |
| 44 | } |
| 45 |
| 46 | routersByHTTPUser, found := r.indexByDomain[domain] |
| 47 | if !found { |
| 48 | routersByHTTPUser = make(map[string][]*Router) |
| 49 | } |
| 50 | vrs, found := routersByHTTPUser[httpUser] |
| 51 | if !found { |
| 52 | vrs = make([]*Router, 0, 1) |
| 53 | } |
| 54 |
| 55 | vr := &Router{ |
| 56 | domain: domain, |
| 57 | location: location, |
| 58 | httpUser: httpUser, |
| 59 | payload: payload, |
| 60 | } |
| 61 | vrs = append(vrs, vr) |
| 62 |
| 63 | slices.SortFunc(vrs, func(a, b *Router) int { |
| 64 | return -cmp.Compare(a.location, b.location) |
| 65 | }) |
| 66 |
| 67 | routersByHTTPUser[httpUser] = vrs |
| 68 | r.indexByDomain[domain] = routersByHTTPUser |
| 69 | return nil |
| 70 | } |
| 71 |
| 72 | func (r *Routers) Del(domain, location, httpUser string) { |
| 73 | domain = strings.ToLower(domain) |
| 74 |
| 75 | r.mutex.Lock() |
| 76 | defer r.mutex.Unlock() |
| 77 |
| 78 | routersByHTTPUser, found := r.indexByDomain[domain] |
| 79 | if !found { |
| 80 | return |
| 81 | } |
| 82 |
| 83 | vrs, found := routersByHTTPUser[httpUser] |
| 84 | if !found { |
| 85 | return |
| 86 | } |
| 87 | newVrs := make([]*Router, 0) |
| 88 | for _, vr := range vrs { |
| 89 | if vr.location != location { |
| 90 | newVrs = append(newVrs, vr) |
| 91 | } |
| 92 | } |
| 93 | routersByHTTPUser[httpUser] = newVrs |
| 94 | } |
| 95 |
| 96 | // Get returns the best location match for an exact host and exact HTTP user. |
| 97 | // It does not apply all-users, wildcard-domain, or catch-all-domain fallback. |
| 98 | func (r *Routers) Get(host, path, httpUser string) (vr *Router, exist bool) { |
| 99 | host = strings.ToLower(host) |
| 100 |
| 101 | r.mutex.RLock() |
| 102 | defer r.mutex.RUnlock() |
| 103 |
| 104 | return r.getLocked(host, path, httpUser) |
| 105 | } |
| 106 |
| 107 | // getLocked performs an exact-host lookup; host must already be lower-cased. |
| 108 | func (r *Routers) getLocked(host, path, httpUser string) (vr *Router, exist bool) { |
| 109 | routersByHTTPUser, found := r.indexByDomain[host] |
| 110 | if !found { |
| 111 | return |
| 112 | } |
| 113 |
| 114 | vrs, found := routersByHTTPUser[httpUser] |
| 115 | if !found { |
| 116 | return |
| 117 | } |
| 118 |
| 119 | for _, vr = range vrs { |
| 120 | if strings.HasPrefix(path, vr.location) { |
| 121 | return vr, true |
| 122 | } |
| 123 | } |
| 124 | return |
| 125 | } |
| 126 |
| 127 | func (r *Routers) getByRoute(host, path, httpUser string) (*Router, bool) { |
| 128 | host = strings.ToLower(host) |
| 129 |
| 130 | r.mutex.RLock() |
| 131 | defer r.mutex.RUnlock() |
| 132 |
| 133 | // First we check the full hostname; if it doesn't exist, then check wildcard domains. |
| 134 | // For example, test.example.com checks *.example.com before falling back to "*". |
| 135 | vr, ok := r.getExactOrAllUsersLocked(host, path, httpUser) |
| 136 | if ok { |
| 137 | return vr, true |
| 138 | } |
| 139 |
| 140 | hostSplit := strings.Split(host, ".") |
| 141 | // Keep two-label hosts out of the wildcard walk, so example.com does not match *.com. |
| 142 | for len(hostSplit) >= 3 { |
| 143 | // Replace the leftmost remaining label with the wildcard marker. |
| 144 | hostSplit[0] = "*" |
| 145 | host = strings.Join(hostSplit, ".") |
| 146 | vr, ok = r.getExactOrAllUsersLocked(host, path, httpUser) |
| 147 | if ok { |
| 148 | return vr, true |
| 149 | } |
| 150 | hostSplit = hostSplit[1:] |
| 151 | } |
| 152 |
| 153 | // Finally, try to check if there is one proxy whose domain is "*", which means match all domains. |
| 154 | return r.getExactOrAllUsersLocked("*", path, httpUser) |
| 155 | } |
| 156 |
| 157 | func (r *Routers) getExactOrAllUsersLocked(host, path, httpUser string) (*Router, bool) { |
| 158 | vr, ok := r.getLocked(host, path, httpUser) |
| 159 | if ok { |
| 160 | return vr, true |
| 161 | } |
| 162 | // Try to check if there is one proxy that doesn't specify routeByHTTPUser, it means match all. |
| 163 | vr, ok = r.getLocked(host, path, "") |
| 164 | if ok { |
| 165 | return vr, true |
| 166 | } |
| 167 | return nil, false |
| 168 | } |
| 169 |
| 170 | func (r *Routers) exist(host, path, httpUser string) (route *Router, exist bool) { |
| 171 | routersByHTTPUser, found := r.indexByDomain[host] |
| 172 | if !found { |
| 173 | return |
| 174 | } |
| 175 | routers, found := routersByHTTPUser[httpUser] |
| 176 | if !found { |
| 177 | return |
| 178 | } |
| 179 |
| 180 | for _, route = range routers { |
| 181 | if path == route.location { |
| 182 | return route, true |
| 183 | } |
| 184 | } |
| 185 | return |
| 186 | } |