github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/cloudflare/cfssl/api/client/group.go (about) 1 package client 2 3 import ( 4 "errors" 5 "github.com/hellobchain/newcryptosm/http" 6 "github.com/hellobchain/newcryptosm/tls" 7 "strings" 8 "time" 9 10 "github.com/hellobchain/third_party/cloudflare/cfssl/auth" 11 "github.com/hellobchain/third_party/cloudflare/cfssl/info" 12 ) 13 14 // Strategy is the means by which the server to use as a remote should 15 // be selected. 16 type Strategy int 17 18 const ( 19 // StrategyInvalid indicates any strategy that is unsupported 20 // or returned when no strategy is applicable. 21 StrategyInvalid = iota 22 23 // StrategyOrderedList is a sequential list of servers: if the 24 // first server cannot be reached, the next is used. The 25 // client will proceed in this manner until the list of 26 // servers is exhausted, and then an error is returned. 27 StrategyOrderedList 28 ) 29 30 var strategyStrings = map[string]Strategy{ 31 "ordered_list": StrategyOrderedList, 32 } 33 34 // StrategyFromString takes a string describing a 35 func StrategyFromString(s string) Strategy { 36 s = strings.TrimSpace(strings.ToLower(s)) 37 strategy, ok := strategyStrings[s] 38 if !ok { 39 return StrategyInvalid 40 } 41 return strategy 42 } 43 44 // NewGroup will use the collection of remotes specified with the 45 // given strategy. 46 func NewGroup(remotes []string, tlsConfig *tls.Config, strategy Strategy) (Remote, error) { 47 var servers = make([]*server, len(remotes)) 48 for i := range remotes { 49 u, err := normalizeURL(remotes[i]) 50 if err != nil { 51 return nil, err 52 } 53 servers[i] = newServer(u, tlsConfig) 54 } 55 56 switch strategy { 57 case StrategyOrderedList: 58 return newOrdererdListGroup(servers) 59 default: 60 return nil, errors.New("unrecognised strategy") 61 } 62 } 63 64 type orderedListGroup struct { 65 remotes []*server 66 } 67 68 func (g *orderedListGroup) Hosts() []string { 69 var hosts = make([]string, 0, len(g.remotes)) 70 for _, srv := range g.remotes { 71 srvHosts := srv.Hosts() 72 hosts = append(hosts, srvHosts[0]) 73 } 74 return hosts 75 } 76 77 func (g *orderedListGroup) SetRequestTimeout(timeout time.Duration) { 78 for _, srv := range g.remotes { 79 srv.SetRequestTimeout(timeout) 80 } 81 } 82 83 func newOrdererdListGroup(remotes []*server) (Remote, error) { 84 return &orderedListGroup{ 85 remotes: remotes, 86 }, nil 87 } 88 89 func (g *orderedListGroup) AuthSign(req, id []byte, provider auth.Provider) (resp []byte, err error) { 90 for i := range g.remotes { 91 resp, err = g.remotes[i].AuthSign(req, id, provider) 92 if err == nil { 93 return resp, nil 94 } 95 } 96 97 return nil, err 98 } 99 100 func (g *orderedListGroup) Sign(jsonData []byte) (resp []byte, err error) { 101 for i := range g.remotes { 102 resp, err = g.remotes[i].Sign(jsonData) 103 if err == nil { 104 return resp, nil 105 } 106 } 107 108 return nil, err 109 } 110 111 func (g *orderedListGroup) Info(jsonData []byte) (resp *info.Resp, err error) { 112 for i := range g.remotes { 113 resp, err = g.remotes[i].Info(jsonData) 114 if err == nil { 115 return resp, nil 116 } 117 } 118 119 return nil, err 120 } 121 122 // SetReqModifier does nothing because there is no request modifier for group 123 func (g *orderedListGroup) SetReqModifier(mod func(*http.Request, []byte)) { 124 // noop 125 }