duanmaifu3428 2016-12-13 20:50
浏览 139
已采纳

golang网络模块(LookupSRV)

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 := "sipfederationtls"
        protocol:= "tcp"
        fmt.Printf("
[+] SRV Record(s)
")
        //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.
")
        }

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

        for i := 0; i < len(addresses); i++ {
                fmt.Printf("addrs[%d].Target : %s 
", i, addresses[i].Target)
                fmt.Printf("addrs[%d].Port : %d 
", i, addresses[i].Port)
                fmt.Printf("addrs[%d].Priority : %d 
", i, addresses[i].Priority)
                fmt.Printf("addrs[%d].Weight : %d 
", 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条回答 默认 最新

  • 普通网友 2016-12-13 20:59
    关注

    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{"service1", "service2", "service3")
    

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

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

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

    for _, record := range addrs {
        fmt.Printf("Target: %s:%d
    ", record.Target, record.Port)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题