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 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了