Debian Codegraph @

pkg/server/proxystrategies/proxystrategies.go

raw
1 package proxystrategies
2
3 import (
4 "fmt"
5 "strings"
6 )
7
8 type ProxyStrategy int
9
10 const (
11 // With this strategy the Proxy Server will randomly pick a backend from
12 // the current healthy backends to establish the tunnel over which to
13 // forward requests.
14 ProxyStrategyDefault ProxyStrategy = iota + 1
15 // With this strategy the Proxy Server will pick a backend that has the same
16 // associated host as the request.Host to establish the tunnel.
17 ProxyStrategyDestHost
18 // ProxyStrategyDefaultRoute will only forward traffic to agents that have explicity advertised
19 // they serve the default route through an agent identifier. Typically used in combination with destHost
20 ProxyStrategyDefaultRoute
21 )
22
23 func (ps ProxyStrategy) String() string {
24 switch ps {
25 case ProxyStrategyDefault:
26 return "default"
27 case ProxyStrategyDestHost:
28 return "destHost"
29 case ProxyStrategyDefaultRoute:
30 return "defaultRoute"
31 }
32 panic(fmt.Sprintf("unhandled ProxyStrategy: %d", ps))
33 }
34
35 func ParseProxyStrategy(s string) (ProxyStrategy, error) {
36 switch s {
37 case ProxyStrategyDefault.String():
38 return ProxyStrategyDefault, nil
39 case ProxyStrategyDestHost.String():
40 return ProxyStrategyDestHost, nil
41 case ProxyStrategyDefaultRoute.String():
42 return ProxyStrategyDefaultRoute, nil
43 default:
44 return 0, fmt.Errorf("unknown proxy strategy: %s", s)
45 }
46 }
47
48 // GenProxyStrategiesFromStr generates the list of proxy strategies from the
49 // comma-seperated string, i.e., destHost.
50 func ParseProxyStrategies(proxyStrategies string) ([]ProxyStrategy, error) {
51 var result []ProxyStrategy
52
53 strs := strings.Split(proxyStrategies, ",")
54 for _, s := range strs {
55 if len(s) == 0 {
56 continue
57 }
58 ps, err := ParseProxyStrategy(s)
59 if err != nil {
60 return nil, err
61 }
62 result = append(result, ps)
63 }
64 if len(result) == 0 {
65 return nil, fmt.Errorf("proxy strategies cannot be empty")
66 }
67 return result, nil
68 }