/
plugin.go
64 lines (54 loc) · 1.4 KB
/
plugin.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
54
55
56
57
58
59
60
61
62
63
64
package mupstream
import (
"github.com/Sirupsen/logrus"
plugin "github.com/oif/apex/pkg/plugin/v1"
)
// PluginName for plugin
const PluginName = "Multi-upstream Plugin"
// Plugin implements pkg/plugin/v1
type Plugin struct {
upstreams []*upstream
}
// Name return the name of this plugin
func (p *Plugin) Name() string {
return PluginName
}
// Initialize Google DNS Plugin
func (p *Plugin) Initialize() error {
p.upstreams = append(p.upstreams, newUpstream("119.29.29.29:53"))
p.upstreams = append(p.upstreams, newUpstream("223.5.5.5:53"))
p.upstreams = append(p.upstreams, newUpstream("114.114.114.114:53"))
return nil
}
func (p *Plugin) Warmup(c *plugin.Context) {}
func (p *Plugin) AfterResponse(c *plugin.Context, err error) {}
func (p *Plugin) Patch(c *plugin.Context) {
up := p.bestUpstream()
resp, rtt, err := up.forward(c.Msg)
if err != nil {
// c.Error(err)
return
}
resp.CopyTo(c.Msg)
c.Logger().WithFields(logrus.Fields{
"rtt": rtt,
"upstream": up,
}).Debug("Exchange message")
c.Abort() // stop other patch steps
}
func (p *Plugin) bestUpstream() *upstream {
best := 0
for i := 0; i < len(p.upstreams); i++ {
if p.upstreams[i].srtt < p.upstreams[0].srtt {
best = i
}
}
go func(selected int) { // lost decay
for i := 0; i < len(p.upstreams); i++ {
if i != selected {
p.upstreams[i].srttAttenuation()
}
}
}(best)
return p.upstreams[best]
}