/
state.go
53 lines (46 loc) · 1.32 KB
/
state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package commands
import (
"errors"
odoh "github.com/cloudflare/odoh-go"
"net/http"
"sync"
"time"
)
type state struct {
sync.RWMutex
configContents map[string]odoh.ObliviousDoHConfigContents
client []*http.Client
}
var instance state
func GetInstance(N uint64) *state {
instance.client = make([]*http.Client, N)
for index := 0; index < int(N); index++ {
tr := &http.Transport{
MaxIdleConnsPerHost: 1024,
TLSHandshakeTimeout: 0 * time.Second,
// Uncomment the line below to explicitly disable http/2 in the clients.
//TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
}
instance.client[index] = &http.Client{Transport: tr}
}
instance.configContents = make(map[string]odoh.ObliviousDoHConfigContents)
return &instance
}
func (s *state) InsertKey(targethost string, key odoh.ObliviousDoHConfigContents) {
s.Lock()
defer s.Unlock()
s.configContents[targethost] = key
}
func (s *state) GetTargetConfigContents(targethost string) (odoh.ObliviousDoHConfigContents, error) {
s.RLock()
defer s.RUnlock()
if key, ok := s.configContents[targethost]; ok {
return key, nil
}
return odoh.ObliviousDoHConfigContents{}, errors.New("public key for target not available")
}
func (s *state) TotalNumberOfTargets() int {
s.RLock()
defer s.RUnlock()
return len(s.configContents)
}