本文整理匯總了Golang中github.com/miekg/dns.ClientConfigFromFile函數的典型用法代碼示例。如果您正苦於以下問題:Golang ClientConfigFromFile函數的具體用法?Golang ClientConfigFromFile怎麽用?Golang ClientConfigFromFile使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了ClientConfigFromFile函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: handlerToAnswer
func handlerToAnswer(w dns.ResponseWriter, r *dns.Msg) {
db, err := sql.Open("mysql", "inserter:[email protected]/dnsServerCache") //Handler for database
if err != nil {
panic(err.Error()) // Just for example purpose. You should use proper error handling instead of panic
}
answer := new(dns.Msg) //Set answer to client
answer.SetReply(r) //Set reply flag and start collecting data for reply
var answersAll []dns.RR
answersAll = dbRespond(db, r.Question[0].String())
if len(answersAll) != 0 {
for _, answers := range answersAll {
answer.Answer = append(answer.Answer, answers)
}
} else {
client := new(dns.Client)
var err1 error
config, _ := dns.ClientConfigFromFile("/etc/resolv.conf")
answer, _, err1 = client.Exchange(r, config.Servers[0]+":"+config.Port)
if err1 != nil {
panic(err1.Error())
}
if r.Rcode != dns.RcodeSuccess {
return
}
go dbWriter(answer.Answer, db, r.Question[0].String())
}
w.WriteMsg(answer)
}
示例2: main
func main() {
if len(os.Args) <= 1 {
log.Printf("Usage: %s domain", os.Args[0])
os.Exit(1)
}
config, _ := dns.ClientConfigFromFile("/etc/resolv.conf")
c := new(dns.Client)
m := new(dns.Msg)
m.SetQuestion(dns.Fqdn(os.Args[1]), dns.TypeMX)
m.RecursionDesired = true
r, _, err := c.Exchange(m, net.JoinHostPort(config.Servers[0], config.Port))
if r == nil {
log.Fatalf("*** error: %s\n", err.Error())
}
if r.Rcode != dns.RcodeSuccess {
log.Fatalf(" *** invalid answer name %s after MX query for %s\n", os.Args[1], os.Args[1])
}
// Stuff must be in the answer section
for _, a := range r.Answer {
fmt.Printf("%v\n", a)
}
}
示例3: lookupSRV
func lookupSRV(name string) (*dns.Msg, error) {
conf, err := dns.ClientConfigFromFile(resolvConf)
if err != nil {
return nil, fmt.Errorf("Couldn't load resolv.conf: %s", err)
}
client := &dns.Client{}
response := &dns.Msg{}
for _, server := range conf.Servers {
servAddr := net.JoinHostPort(server, conf.Port)
for _, suffix := range conf.Search {
response, err = lookup(name, dns.TypeSRV, client, servAddr, suffix, false)
if err == nil {
if len(response.Answer) > 0 {
return response, nil
}
} else {
glog.Warningf("Resolving %s.%s failed: %s", name, suffix, err)
}
}
response, err = lookup(name, dns.TypeSRV, client, servAddr, "", false)
if err == nil {
return response, nil
}
}
return response, fmt.Errorf("Couldn't resolve %s: No server responded", name)
}
示例4: Config
func (u *upstream) Config() (*dns.ClientConfig, error) {
u.Lock()
defer u.Unlock()
// If we checked less than five seconds ago return what we already have
now := time.Now()
if now.Sub(u.lastStat) < 5*time.Second {
return u.cachedConfig, nil
}
u.lastStat = now
// Reread file if it has changed
if fi, err := os.Stat(u.resolvConf); err != nil {
return u.cachedConfig, err
} else if fi.ModTime() != u.lastModified {
if config, err := dns.ClientConfigFromFile(u.resolvConf); err == nil {
config.Servers = filter(config.Servers, u.filterAddress)
u.lastModified = fi.ModTime()
u.cachedConfig = config
} else {
return u.cachedConfig, err
}
}
return u.cachedConfig, nil
}
示例5: init
func init() {
conf, err := dns.ClientConfigFromFile("/etc/resolv.conf")
if err != nil || len(conf.Servers) == 0 {
return
}
DefaultResolve = strings.Join(conf.Servers, ",")
}
示例6: loadRootConfig
func loadRootConfig() (err error) {
rootConfig, err = dns.ClientConfigFromFile("/etc/resolv.conf")
if err != nil {
return err
}
return nil
}
示例7: NewDefaultLookupLib
func NewDefaultLookupLib() *LookupLib {
config, _ := dns.ClientConfigFromFile("/etc/resolv.conf")
serverString := config.Servers[0] + ":" + config.Port
l := new(LookupLib)
l.serverString = serverString
return l
}
示例8: localNameservers
// localNameservers returns list of local nameservers.
func localNameservers() ([]string, error) {
c, err := dns.ClientConfigFromFile("/etc/resolv.conf")
if err != nil {
return nil, err
}
return c.Servers, nil
}
示例9: ResolvConf
func ResolvConf(config *Config, ctx *cli.Context) error {
// Get host resolv config
resolvConf, err := dns.ClientConfigFromFile("/etc/resolv.conf")
if err != nil {
return err
}
if len(config.Nameservers) == 0 {
for _, s := range resolvConf.Servers {
config.Nameservers = append(config.Nameservers, net.JoinHostPort(s, resolvConf.Port))
}
}
if !ctx.IsSet("ndots") && resolvConf.Ndots != 1 {
log.Debugf("Setting ndots from resolv.conf: %d", resolvConf.Ndots)
config.Ndots = resolvConf.Ndots
}
if config.EnableSearch && len(config.SearchDomains) == 0 {
for _, s := range resolvConf.Search {
s = dns.Fqdn(strings.ToLower(s))
config.SearchDomains = append(config.SearchDomains, s)
}
}
return nil
}
示例10: NewDefaultResolver
func NewDefaultResolver() *Resolver {
config, _ := dns.ClientConfigFromFile("/etc/resolv.conf")
serverString := config.Servers[0] + ":" + config.Port
l := new(Resolver)
l.serverString = serverString
return l
}
示例11: Init
func Init() {
var el error
cf, el = dns.ClientConfigFromFile(DEFAULT_RESOLV_FILE)
if el != nil {
fmt.Println("DEFAULT_RESOLV_FILE ERR")
}
}
示例12: lookupAll
func lookupAll(name string, qtype uint16) (*dns.Msg, error) {
conf, err := dns.ClientConfigFromFile(resolvConf)
if err != nil {
return nil, fmt.Errorf("could not load resolv.conf: %s", err)
}
client := &dns.Client{}
response := &dns.Msg{}
for _, server := range conf.Servers {
servAddr := net.JoinHostPort(server, conf.Port)
for _, suffix := range conf.Search {
response, err = lookup(name, qtype, client, servAddr, suffix, false)
if err != nil {
log.Warnf("resolving %s.%s failed: %s", name, suffix, err)
continue
}
if len(response.Answer) > 0 {
return response, nil
}
}
response, err = lookup(name, qtype, client, servAddr, "", false)
if err == nil {
return response, nil
}
}
return response, fmt.Errorf("could not resolve %s: No server responded", name)
}
示例13: main
func main() {
if len(os.Args) != 2 {
fmt.Printf("%s NAMESERVER\n", os.Args[0])
os.Exit(1)
}
conf, _ := dns.ClientConfigFromFile("/etc/resolv.conf")
m := new(dns.Msg)
m.Question = make([]dns.Question, 1)
c := new(dns.Client)
addr := addresses(conf, c, os.Args[1])
if len(addr) == 0 {
fmt.Printf("No address found for %s\n", os.Args[1])
os.Exit(1)
}
for _, a := range addr {
m.Question[0] = dns.Question{"version.bind.", dns.TypeTXT, dns.ClassCHAOS}
in, rtt, _ := c.ExchangeRtt(m, a)
if in != nil && len(in.Answer) > 0 {
fmt.Printf("(time %.3d µs) %v\n", rtt/1e3, in.Answer[0])
}
m.Question[0] = dns.Question{"hostname.bind.", dns.TypeTXT, dns.ClassCHAOS}
in, rtt, _ = c.ExchangeRtt(m, a)
if in != nil && len(in.Answer) > 0 {
fmt.Printf("(time %.3d µs) %v\n", rtt/1e3, in.Answer[0])
}
}
}
示例14: loadConfig
func loadConfig() error {
loadConfigOnce.Do(func() {
clientConfig, configLoadError = dns.ClientConfigFromFile("/etc/resolv.conf")
})
return configLoadError
}
示例15: getDNSServerAddress
func getDNSServerAddress() (string, error) {
conf, err := dns.ClientConfigFromFile("/etc/resolv.conf")
if err != nil {
return "", fmt.Errorf("failed to load DNS config from /etc/resolv.conf: %v", err)
}
return net.JoinHostPort(conf.Servers[0], conf.Port), nil
}