本文整理匯總了Golang中github.com/miekg/dns.HandleFunc函數的典型用法代碼示例。如果您正苦於以下問題:Golang HandleFunc函數的具體用法?Golang HandleFunc怎麽用?Golang HandleFunc使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了HandleFunc函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestParseMXNegativeTests
func TestParseMXNegativeTests(t *testing.T) {
/* helper functions */
dns.HandleFunc(".", rootZone)
defer dns.HandleRemove(".")
hosts := make(map[uint16][]string)
hosts[dns.TypeMX] = []string{
"mail.matching.com. 0 IN MX 5 mail.matching.com.",
"mail.matching.com. 0 IN MX 10 mail2.matching.com.",
"mail.matching.com. 0 IN MX 15 mail3.matching.com.",
}
hosts[dns.TypeAAAA] = []string{
"mail.matching.com. 0 IN AAAA 2001:4860:1:2001::80",
}
hosts[dns.TypeA] = []string{
"mail.matching.com. 0 IN A 172.18.0.2",
"mail2.matching.com. 0 IN A 172.20.20.20",
"mail3.matching.com. 0 IN A 172.100.0.1",
}
mxMatchingCom := generateZone(hosts)
dns.HandleFunc("matching.com.", mxMatchingCom)
defer dns.HandleRemove("matching.com.")
s, addr, err := runLocalUDPServer(dnsServer)
if err != nil {
t.Fatalf("unable to run test server: %v", err)
}
defer s.Shutdown()
Nameserver = addr
/* ***************** */
ip := net.IP{127, 0, 0, 1}
domain := "matching.com"
p := NewParser(domain, domain, ip, stub)
testcases := []TokenTestCase{
TokenTestCase{&Token{tMX, qPlus, "matching.com"}, Pass, false},
TokenTestCase{&Token{tMX, qPlus, ""}, Pass, false},
//TokenTestCase{&Token{tMX, qPlus, "google.com"}, Pass, false},
TokenTestCase{&Token{tMX, qPlus, "idontexist"}, Pass, false},
TokenTestCase{&Token{tMX, qMinus, "matching.com"}, Fail, false},
}
var match bool
var result SPFResult
for _, testcase := range testcases {
match, result = p.parseMX(testcase.Input)
if testcase.Match != match {
t.Error("Match mismatch, expected ", testcase.Match, " got ", match)
}
if testcase.Result != result {
t.Error("Result mismatch, expected ", testcase.Result, " got ", result)
}
}
}
示例2: TestParseExists
// TestParseExists executes tests for exists term.
func TestParseExists(t *testing.T) {
dns.HandleFunc(".", rootZone)
defer dns.HandleRemove(".")
hosts := make(map[uint16][]string)
hosts[dns.TypeA] = []string{
"postitive.matching.net. 0 IN A 172.20.20.20",
"postitive.matching.net. 0 IN A 172.18.0.1",
"postitive.matching.net. 0 IN A 172.18.0.2",
}
dns.HandleFunc("positive.matching.net.", generateZone(hosts))
defer dns.HandleRemove("positive.matching.net.")
hosts = make(map[uint16][]string)
hosts[dns.TypeA] = []string{
"postitive.matching.com. 0 IN A 172.20.20.20",
"postitive.matching.com. 0 IN A 172.18.0.1",
"postitive.matching.com. 0 IN A 172.18.0.2",
}
dns.HandleFunc("positive.matching.com.", generateZone(hosts))
defer dns.HandleRemove("positive.matching.com.")
s, addr, err := runLocalUDPServer(dnsServer)
if err != nil {
t.Fatalf("unable to run test server: %v", err)
}
defer s.Shutdown()
Nameserver = addr
domain := "matching.com"
p := NewParser(domain, domain, ip, stub)
testcases := []TokenTestCase{
TokenTestCase{&Token{tExists, qPlus, "positive.matching.net"}, Pass, true},
TokenTestCase{&Token{tExists, qMinus, "positive.matching.net"}, Fail, true},
TokenTestCase{&Token{tExists, qMinus, "idontexist.matching.net"}, Fail, false},
TokenTestCase{&Token{tExists, qMinus, "idontexist.%{d}"}, Fail, false},
TokenTestCase{&Token{tExists, qTilde, "positive.%{d}"}, Softfail, true},
TokenTestCase{&Token{tExists, qTilde, "positive.%{d}"}, Softfail, true},
TokenTestCase{&Token{tExists, qTilde, ""}, Permerror, true},
TokenTestCase{&Token{tExists, qTilde, "invalidsyntax%{}"}, Permerror, true},
}
for _, testcase := range testcases {
match, result := p.parseExists(testcase.Input)
if testcase.Match != match {
t.Error("Match mismatch, expected ", testcase.Match, " got ", match)
}
if testcase.Result != result {
t.Error("Result mismatch, expected ", testcase.Result, " got ", result)
}
}
}
示例3: TestHandleExplanation
func TestHandleExplanation(t *testing.T) {
const domain = "matching.com"
// static.exp.matching.com. IN TXT "Invalid SPF record"
// ip.exp.matching.com. IN TXT "%{i} is not one of %{d}'s designated mail servers."
// redirect.exp.matching.com. IN TXT "See http://%{d}/why.html?s=%{s}&i=%{i}"
dns.HandleFunc(".", rootZone)
defer dns.HandleRemove(".")
hosts := make(map[uint16][]string)
hosts[dns.TypeTXT] = []string{
"static.exp.matching.com. 0 IN TXT \"Invalid SPF record\"",
}
dns.HandleFunc("static.exp.matching.com.", generateZone(hosts))
defer dns.HandleRemove("static.exp.matching.com.")
hosts = make(map[uint16][]string)
hosts[dns.TypeTXT] = []string{
"ip.exp.matching.com. 0 in TXT \"%{i} is not one of %{d}'s designated mail servers.\"",
}
dns.HandleFunc("ip.exp.matching.com.", generateZone(hosts))
defer dns.HandleRemove("ip.exp.matching.com.")
s, addr, err := runLocalUDPServer(dnsServer)
if err != nil {
t.Fatalf("unable to run test server: %v", err)
}
defer s.Shutdown()
Nameserver = addr
ExpTestCases := []ExpTestCase{
ExpTestCase{"v=spf1 -all exp=static.exp.matching.com",
"Invalid SPF record"},
ExpTestCase{"v=spf1 -all exp=ip.exp.matching.com",
"127.0.0.1 is not one of matching.com's designated mail servers."},
// TODO(zaccone): Cover this testcase
//ExpTestCase{"v=spf1 -all exp=redirect.exp.matching.com",
//ExpT"See http://matching.com/why.html?s=&i="},
}
for _, testcase := range ExpTestCases {
p := NewParser(domain, domain, ip, testcase.Query)
_, exp, err := p.Parse()
if err != nil {
t.Error("Unexpected error while parsing: ", err)
} else if exp != testcase.Explanation {
t.Errorf("Explanation mismatch, expected %s, got %s\n",
testcase.Explanation, exp)
}
}
}
示例4: LaunchDNS
// LaunchDNS starts a (TCP and UDP) DNS server for the Resolver,
// returning a error channel to which errors are asynchronously sent.
func (res *Resolver) LaunchDNS() <-chan error {
// Handers for Mesos requests
dns.HandleFunc(res.config.Domain+".", panicRecover(res.HandleMesos))
// Handler for nonMesos requests
dns.HandleFunc(".", panicRecover(res.HandleNonMesos))
errCh := make(chan error, 2)
_, e1 := res.Serve("tcp")
go func() { errCh <- <-e1 }()
_, e2 := res.Serve("udp")
go func() { errCh <- <-e2 }()
return errCh
}
示例5: Setup
func (manager *DnsManager) Setup() error {
dns.HandleFunc(manager.BaseName, manager.dnsHandler)
go func() {
manager.udpServer = &dns.Server{
Addr: fmt.Sprintf("%v:%v", manager.ServiceAddr, manager.ServicePort),
Net: "udp",
TsigSecret: nil,
}
err := manager.udpServer.ListenAndServe()
if err != nil {
log.Fatal(err)
}
}()
go func() {
manager.tcpServer = &dns.Server{
Addr: fmt.Sprintf("%v:%v", manager.ServiceAddr, manager.ServicePort),
Net: "tcp",
TsigSecret: nil,
}
err := manager.tcpServer.ListenAndServe()
if err != nil {
log.Fatal(err)
}
}()
return nil
}
示例6: main
func main() {
runtime.GOMAXPROCS(runtime.NumCPU() * 4)
flag.Parse()
configuration := nasello.ReadConfig(*configFile)
for _, filter := range configuration.Filters {
// Ensure that each pattern is a FQDN name
pattern := dns.Fqdn(filter.Pattern)
log.Printf("Proxing %s on %v\n", pattern, strings.Join(filter.Addresses, ", "))
dns.HandleFunc(pattern, nasello.ServerHandler(filter.Addresses))
}
go serve("tcp", *listenAddr)
go serve("udp", *listenAddr)
log.Printf("Started DNS server on: %s\n", *listenAddr)
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
forever:
for {
select {
case s := <-sig:
log.Printf("Signal (%s) received, stopping\n", s.String())
break forever
}
}
}
示例7: serveTestResolver
func (ts *testSrv) serveTestResolver() {
dns.HandleFunc(".", ts.dnsHandler)
dnsServer := &dns.Server{
Addr: "0.0.0.0:8053",
Net: "tcp",
ReadTimeout: time.Millisecond,
WriteTimeout: time.Millisecond,
}
go func() {
err := dnsServer.ListenAndServe()
if err != nil {
fmt.Println(err)
return
}
}()
webServer := &http.Server{
Addr: "0.0.0.0:8055",
Handler: http.HandlerFunc(ts.setTXT),
}
go func() {
err := webServer.ListenAndServe()
if err != nil {
fmt.Println(err)
return
}
}()
}
示例8: updateDns
func updateDns(ethereum *eth.Ethereum) {
stateObject := ethereum.StateManager().CurrentState().GetStateObject(dnsreg)
if stateObject != nil {
ethutil.Config.Log.Debugln("Updating DNS")
stateObject.State().EachStorage(func(name string, value *ethutil.Value) {
val := value.Bytes()[1:]
name = sanitizeString(name) + ".eth."
dns.HandleRemove(name)
zoneString := fmt.Sprintf("%s 2044 IN A %s", name, val)
zone := NewRR(zoneString)
if zone != nil {
ethutil.Config.Log.Debugln("[DNS] Updated zone:", zone)
dns.HandleFunc(name, func(w dns.ResponseWriter, r *dns.Msg) {
switch r.Question[0].Qtype {
case dns.TypeA:
m := new(dns.Msg)
m.SetReply(r)
m.Answer = []dns.RR{zone}
m.RecursionAvailable = true
w.WriteMsg(m)
default:
ethutil.Config.Log.Debugln("[DNS] Type not supported yet")
}
})
} else {
ethutil.Config.Log.Debugln("Invalid zone", zoneString)
}
})
}
}
示例9: main
func main() {
parse_flags()
var err error
if enable_cache {
// create cache
dns_cache, err = lru.New(1000)
if err != nil {
log.Fatal(err)
}
}
dns.HandleFunc(".", handleRoot)
logger = NewLogger(logfile, debug)
logger.Info("Listen on %s\n", bind_addr)
go func() {
/* listen tcp */
err := dns.ListenAndServe(bind_addr, "tcp", nil)
if err != nil {
log.Fatal(err)
}
}()
/* listen udp */
err = dns.ListenAndServe(bind_addr, "udp", nil)
if err != nil {
log.Fatal(err)
}
}
示例10: Serve
func (d *DNSResponder) Serve(domains []string) error {
for _, domain := range domains {
dns.HandleFunc(domain+".", d.handleDNS)
}
addr := d.Address
if addr == "" {
addr = DefaultAddress
}
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
server := &dns.Server{Addr: addr, Net: "udp", TsigSecret: nil}
server.ListenAndServe()
}()
go func() {
defer wg.Done()
server := &dns.Server{Addr: addr, Net: "tcp", TsigSecret: nil}
server.ListenAndServe()
}()
wg.Wait()
return nil
}
示例11: main
func main() {
port := flag.Int("port", 5353, "port to run on")
flag.Parse()
dns.HandleFunc(".", handleFirstHost)
serve := func(net string, port int) {
server := &dns.Server{Addr: ":" + strconv.Itoa(port), Net: net}
err := server.ListenAndServe()
if err != nil {
glog.Infof("Failed to setup the "+net+" server: %s\n", err.Error())
}
}
go serve("tcp", *port)
go serve("udp", *port)
sig := make(chan os.Signal)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
forever:
for {
select {
case s := <-sig:
log.Fatalf("Signal (%d) received, stopping\n", s)
break forever
}
}
}
示例12: main
func main() {
runtime.GOMAXPROCS(runtime.NumCPU() * 4)
for z, rr := range zones {
rrx := rr.(*dns.SOA) // Needed to create the actual RR, and not an reference.
dns.HandleFunc(z, func(w dns.ResponseWriter, r *dns.Msg) {
m := new(dns.Msg)
m.SetReply(r)
m.Authoritative = true
m.Ns = []dns.RR{rrx}
w.WriteMsg(m)
})
}
go func() {
err := dns.ListenAndServe(":8053", "tcp", nil)
if err != nil {
log.Fatal("Failed to set tcp listener %s\n", err.Error())
}
}()
go func() {
err := dns.ListenAndServe(":8053", "udp", nil)
if err != nil {
log.Fatal("Failed to set udp listener %s\n", err.Error())
}
}()
sig := make(chan os.Signal)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
for {
select {
case s := <-sig:
log.Fatalf("Signal (%d) received, stopping\n", s)
}
}
}
示例13: TestTransferIn
func TestTransferIn(t *testing.T) {
soa := soa{250}
dns.HandleFunc(testZone, soa.Handler)
defer dns.HandleRemove(testZone)
s, addrstr, err := test.TCPServer("127.0.0.1:0")
if err != nil {
t.Fatalf("unable to run test server: %v", err)
}
defer s.Shutdown()
z := new(Zone)
z.Expired = new(bool)
z.origin = testZone
z.TransferFrom = []string{addrstr}
err = z.TransferIn()
if err != nil {
t.Fatalf("unable to run TransferIn: %v", err)
}
if z.Apex.SOA.String() != fmt.Sprintf("%s 3600 IN SOA bla. bla. 250 0 0 0 0", testZone) {
t.Fatalf("unknown SOA transferred")
}
}
示例14: addZoneHandles
func addZoneHandles() {
client := connectToRedis()
zones := client.Keys("zone:*").Val()
for i := 0; i < len(zones); i++ {
dns.HandleFunc(zones[i][5:], handleRequest)
}
}
示例15: main
func main() {
flag.Parse()
if *defaultServer == "" {
log.Fatal("-default is required")
}
transferIPs = strings.Split(*allowTransfer, ",")
routes = make(map[string]string)
if *routeList != "" {
for _, s := range strings.Split(*routeList, ",") {
s := strings.SplitN(s, "=", 2)
if len(s) != 2 {
log.Fatal("invalid -routes format")
}
if !strings.HasSuffix(s[0], ".") {
s[0] += "."
}
routes[s[0]] = s[1]
}
}
udpServer := &dns.Server{Addr: *address, Net: "udp"}
tcpServer := &dns.Server{Addr: *address, Net: "tcp"}
dns.HandleFunc(".", route)
go func() {
log.Fatal(udpServer.ListenAndServe())
}()
log.Fatal(tcpServer.ListenAndServe())
}