我正在使用 golang crypto/tls
来处理自定义的面向行的消息协议(protocol)。
这种方法在 Windows 上运行良好:
var fullBuffer string
for {
// If we're not connected, attempt reconnect
if this.conn == nil {
if this.IsSecure() {
this.conn, err = tls.Dial("tcp", this.GetHostOnly(), nil)
} else {
this.conn, err = net.Dial("tcp", this.GetHostOnly())
}
if err == nil {
// log and continue
}
}
// Read from socket into our local buffer (blocking)
if this.conn != nil {
readBuff := make([]byte, 4096)
nbytes, err = this.conn.Read(readBuff)
if nbytes > 0 {
fullBuffer += string(readBuff[0:nbytes])
}
}
非常简单 - 在 win64 上运行良好。
但是当我尝试在 Linux 上运行它时(debian 8 - i386 和 amd64 - golang 1.5 native 和 1.6 从 windows 交叉编译)我得到以下 panic :
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x4ec8b4]
goroutine 8 [running]:
panic(0x8237780, 0x18522030)
C:/Go/src/runtime/panic.go:464 +0x326
crypto/tls.(*Conn).Handshake(0x0, 0x0, 0x0)
C:/Go/src/crypto/tls/conn.go:1023 +0x198
crypto/tls.(*Conn).Read(0x0, 0x18597000, 0x1000, 0x1000, 0x0, 0x0, 0x0)
C:/Go/src/crypto/tls/conn.go:922 +0x5e
mylib.(*MyConnection).worker(0x18512480)
C:/gopath/src/mylib/mylib.go:342 +0x200
Read 调用失败,因为它以某种方式将 nil 传递给 TLS 握手。
这里出了什么问题?
而且,为什么问题只出现在 linux 上?
最佳答案
好的,Linux 构建正在产生错误 x509: certificate signed by unknown authority
。
但是
证书有效
crypto/x509/root_unix.go
查找世界可读的/etc/ssl/certs
- 但是
openssl s_client
卡在-showcerts -verify
上,指出我的操作系统网络配置存在一些问题
出于某种原因,不管错误如何,下面的
if
block 都会被输入- 但我将其存储为
net.Conn
而不是指针,因此它不是nil
的。
- 但我将其存储为
调用此解决,抱歉噪音。希望这个调试故事对以后的其他人有帮助
关于linux - tls conn Read 中的 Golang panic - 仅在 linux 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36380233/