dr5648 2018-10-20 10:18
浏览 140
已采纳

尝试对服务器进行身份验证时由对等方重置连接

I am trying to authenticate myself via a Go client. Here is what i have done so far

func main() {
    servAddr := "192.168.7.13:443"
    tcpAddr, err := net.ResolveTCPAddr("tcp", servAddr)
    if err != nil {
        fmt.Println("ResolveTCPAddr failed:", err.Error())
        os.Exit(1)
    }

    conn, err := net.DialTCP("tcp", nil, tcpAddr)
    if err != nil {
        fmt.Println("Dial failed:", err.Error())
        os.Exit(1)
    }

    _, err = conn.Write([]byte(postReq))
    if err != nil {
        fmt.Println("Write to server failed:", err.Error())
        os.Exit(1)
    }

    fmt.Println(postReq)

    reply := make([]byte, 1024)
    _, err = conn.Read(reply)
    if err != nil {
        fmt.Println("Write to server failed:", err.Error())
        os.Exit(1)
    }

    fmt.Println("reply from server=", string(reply))

    conn.Close()
}

This is the message which i am writing to the connection:

POST /ManagementServer/ServerCommandService.svc HTTP/1.1 Host: 192.xxx.xx.xx Content-Type: text/xml; charset=utf-8 Authorization: Basic {username:password in base64} Content-Length: 405 SOAPAction:"http://videoos.net/2/XProtectCSServerCommand/IServerCommandService/Login" Connection: Keep-Alive 190fc316-6412-41c4-8a9c-d468cc7bee9d

After this, I am expecting to get an authentication token, but instead i am getting a

> Write to server failed: read tcp server-ip:port->server-ip:443:
> read: connection reset by peer

What am I doing wrong? FYI, I am using the same message in python and with sockets I am able to get the token. Can someone help me do the same in Go lang? Thanks

  • 写回答

1条回答 默认 最新

  • dongzhui9936 2018-10-20 10:41
    关注

    Judging from the fact, that your server uses the port 443, I assume the server expects a TLS connection. To get such a connection, you can use tls.Dial:

    conn, err := tls.Dial("tcp", servAddr, nil) // servAddr not tcpAddr
    

    This connection will handle the underlying steps required for the encryption of TLS, like the TLS handshake and allow you to Write the bytes you want to send, instead of having to do the encryption yourself.

    As you mentioned an error with the certificate validation, using

    conn, err := tls.Dial("tcp", servAddr, &tls.Config{InsecureSkipVerify: true}) 
    

    will disable this validation. Be aware that this removes most of the security TLS provides.

    One note, I would suggest you use net/http to send simple HTTP requests. To disable the certificate validation here, you can use

    http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
    

    but again, be aware that this removes most of the security TLS provides. You can check this answer for more details on disabling this for request send using net.http.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型