-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
130 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,4 @@ build : | |
clean : | ||
rm *.coverprofile | ||
|
||
.PHONY: test build clean | ||
.PHONY: build clean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package mupstream | ||
|
||
import ( | ||
"net" | ||
"time" | ||
|
||
"github.com/miekg/dns" | ||
) | ||
|
||
var checkClient = &dns.Client{ | ||
Net: "tcp", | ||
ReadTimeout: 2 * time.Second, | ||
WriteTimeout: 2 * time.Second, | ||
} | ||
|
||
type upstream struct { | ||
addr string | ||
srtt float32 | ||
fails uint32 | ||
client *dns.Client | ||
} | ||
|
||
func newUpstream(addr string) *upstream { | ||
return &upstream{ | ||
addr: addr, | ||
client: &dns.Client{ | ||
Net: "udp", | ||
Dialer: &net.Dialer{ | ||
KeepAlive: time.Minute, | ||
}, | ||
Timeout: time.Second, | ||
}, | ||
} | ||
} | ||
|
||
func (u *upstream) String() string { | ||
return u.addr | ||
} | ||
|
||
func (u *upstream) srttAttenuation() { | ||
u.srtt *= 0.98 | ||
} | ||
|
||
func (u *upstream) forward(m *dns.Msg) (*dns.Msg, time.Duration, error) { | ||
resp, rtt, err := u.client.Exchange(m, u.addr) | ||
if err != nil { // | ||
u.srtt = u.srtt + 200 | ||
} else { // success | ||
if rtt > 300 { | ||
rtt = 300 | ||
} | ||
u.srtt = u.srtt*0.7 + float32(rtt)*0.3 | ||
} | ||
return resp, rtt, err | ||
} |