golang的net模块(LookupSRV)

huangapple go评论64阅读模式
英文:

golang net module(LookupSRV)

问题

我对golang非常陌生,我对Python有一些经验,但不是在这个水平上。我正在创建一个名为"digall"的应用程序,使用户可以轻松查看域名时的活动DNS记录。

在应用程序中,我正在使用LookupSRV,但似乎遇到了一些问题:

func srvRecord(query string) {
    service := "sipfederationtls"
    protocol := "tcp"
    fmt.Printf("\n[+] SRV Record(s)\n")
    //srvMap := ["sipfederationtls", "autodiscover", "VLMCS"]
    cname, addresses, err := net.LookupSRV(service, protocol, query)

    if err != nil {
        fmt.Printf("[!] This feature is currently under development, thus not ready yet.\n")
    }

    fmt.Printf("cname : %s \n", cname)

    for i := 0; i < len(addresses); i++ {
        fmt.Printf("addrs[%d].Target : %s \n", i, addresses[i].Target)
        fmt.Printf("addrs[%d].Port : %d \n", i, addresses[i].Port)
        fmt.Printf("addrs[%d].Priority : %d \n", i, addresses[i].Priority)
        fmt.Printf("addrs[%d].Weight : %d \n", i, addresses[i].Weight)
    }
}

如你所见,变量"service"用作SRV记录的前缀。我的问题是,我想检查此记录的多个前缀,即"sipfederationtls"、"autodiscover"和"VLMCS"。

我的问题是:如何使此函数通过这些前缀迅速执行,并返回有效的前缀?(那些出错的前缀将由我的出色错误消息处理)

我知道这是一个初学者问题,就像我说的,我对golang非常陌生。我会感激你们能给我一些建议。

这是应用程序的完整源代码:http://dpaste.com/3X24ZYR

谢谢。

英文:

I'm very new to golang, I have some experience with python but not on this level per say. I am creating an application that's called "digall", making it easy for a user to see active dns-records when checking a domain name.

In the application I am using LookupSRV, which I seem to have some issues with:

    func srvRecord(query string) {
        service := &quot;sipfederationtls&quot;
        protocol:= &quot;tcp&quot;
        fmt.Printf(&quot;\n[+] SRV Record(s)\n&quot;)
        //srvMap := [&quot;sipfederationtls&quot;, &quot;autodiscover&quot;, &quot;VLMCS&quot;]
        cname, addresses, err := net.LookupSRV(service, protocol, query)

        if err != nil {
                fmt.Printf(&quot;[!] This feature is currently under development, thus not ready yet.\n&quot;)
        }

        fmt.Printf(&quot;cname : %s \n&quot;, cname)

        for i := 0; i &lt; len(addresses); i++ {
                fmt.Printf(&quot;addrs[%d].Target : %s \n&quot;, i, addresses[i].Target)
                fmt.Printf(&quot;addrs[%d].Port : %d \n&quot;, i, addresses[i].Port)
                fmt.Printf(&quot;addrs[%d].Priority : %d \n&quot;, i, addresses[i].Priority)
                fmt.Printf(&quot;addrs[%d].Weight : %d \n&quot;, i, addresses[i].Weight)
        }
}

As you can see the variable "service" serves as the prefix of the SRV record. My only problem is that i want to check multiple prefixes of this record, namely "sipfederationtls", "autodiscover" and "VLMCS".

What I am asking is; How to i make this function swift through these prefixes and return the ones that work? (the ones that error out will be handled by err by my fantastic error message)

I am aware that this is a noob question, and like I said I am very new to golang. I would appreciate any tips you guys could give me.

Here is the full source of the application: http://dpaste.com/3X24ZYR

Thank you.

答案1

得分: 1

你无法使用LookupSRV方法同时查询多个服务,就像你不能使用dig一次查询多个服务一样。

你最好创建一个服务名称的切片:

services := [...]string{"service1", "service2", "service3"}

然后遍历该切片,并为每个服务调用LookupSRV

for _, service := range services {
    cname, addrs, err := net.LookupSRV(service, "tcp", "your.domain.name")

    // 错误处理
}

此外,在遍历查找结果时,最好使用range关键字:

for _, record := range addrs {
    fmt.Printf("目标: %s:%d\n", record.Target, record.Port)
}
英文:

You can't query multiple services at once using LookupSRV method, as you can't use dig for querying several services at once.

You better create a slice of the services' names:

services := [...]string{&quot;service1&quot;, &quot;service2&quot;, &quot;service3&quot;)

And then iterate over it and call LookupSRV for each service:

for _, service := range services {
    cname , addrs, err := net.LookupSRV(service, &quot;tcp&quot;, &quot;your.domain.name&quot;)

    // error handlling
}

Also when iterating over the lookup result, it is better to use the range keyword:

for _, record := range addrs {
    fmt.Printf(&quot;Target: %s:%d\n&quot;, record.Target, record.Port)
}

huangapple
  • 本文由 发表于 2016年12月14日 04:50:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/41130307.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定