golang简单tls协议用法完整示例
程序员文章站
2022-08-29 08:30:14
本文实例讲述了golang简单tls协议用法。分享给大家供大家参考,具体如下:
生成私钥:
openssl genrsa -out key.pem 2048...
本文实例讲述了golang简单tls协议用法。分享给大家供大家参考,具体如下:
生成私钥:
openssl genrsa -out key.pem 2048
生成证书:
openssl req -new -x509 -key key.pem -out cert.pem -days 3650
https:
复制代码 代码如下:
package main
import (
"io"
"net/http"
"log"
)
func helloserver(w http.responsewriter, req *http.request) {
io.writestring(w, "hello, world!\n")
}
func main() {
http.handlefunc("/hello", helloserver)
err := http.listenandservetls(":8080", "cert.pem", "key.pem", nil)
if err != nil {
log.fatal("listenandserve: ", err)
}
}
import (
"io"
"net/http"
"log"
)
func helloserver(w http.responsewriter, req *http.request) {
io.writestring(w, "hello, world!\n")
}
func main() {
http.handlefunc("/hello", helloserver)
err := http.listenandservetls(":8080", "cert.pem", "key.pem", nil)
if err != nil {
log.fatal("listenandserve: ", err)
}
}
提示: 访问请勿忘记使用https开头,否则chrome会下载一个文件如下:
dotcoo-air:tls dotcoo$ cat /users/dotcoo/downloads/hello | xxd
0000000: 1503 0100 0202 0a .......
tls server:
复制代码 代码如下:
package main
import (
"log"
"crypto/tls"
"net"
"bufio"
)
func main() {
log.setflags(log.lshortfile)
cer, err := tls.loadx509keypair("cert.pem", "key.pem")
if err != nil {
log.println(err)
return
}
config := &tls.config{certificates: []tls.certificate{cer}}
ln, err := tls.listen("tcp", ":8000", config)
if err != nil {
log.println(err)
return
}
defer ln.close()
for {
conn, err := ln.accept()
if err != nil {
log.println(err)
continue
}
go handleconnection(conn)
}
}
func handleconnection(conn net.conn) {
defer conn.close()
r := bufio.newreader(conn)
for {
msg, err := r.readstring('\n')
if err != nil {
log.println(err)
return
}
println(msg)
n, err := conn.write([]byte("world\n"))
if err != nil {
log.println(n, err)
return
}
}
}
import (
"log"
"crypto/tls"
"net"
"bufio"
)
func main() {
log.setflags(log.lshortfile)
cer, err := tls.loadx509keypair("cert.pem", "key.pem")
if err != nil {
log.println(err)
return
}
config := &tls.config{certificates: []tls.certificate{cer}}
ln, err := tls.listen("tcp", ":8000", config)
if err != nil {
log.println(err)
return
}
defer ln.close()
for {
conn, err := ln.accept()
if err != nil {
log.println(err)
continue
}
go handleconnection(conn)
}
}
func handleconnection(conn net.conn) {
defer conn.close()
r := bufio.newreader(conn)
for {
msg, err := r.readstring('\n')
if err != nil {
log.println(err)
return
}
println(msg)
n, err := conn.write([]byte("world\n"))
if err != nil {
log.println(n, err)
return
}
}
}
tls client:
复制代码 代码如下:
package main
import (
"log"
"crypto/tls"
)
func main() {
log.setflags(log.lshortfile)
conf := &tls.config{
insecureskipverify: true,
}
conn, err := tls.dial("tcp", "127.0.0.1:8000", conf)
if err != nil {
log.println(err)
return
}
defer conn.close()
n, err := conn.write([]byte("hello\n"))
if err != nil {
log.println(n, err)
return
}
buf := make([]byte, 100)
n, err = conn.read(buf)
if err != nil {
log.println(n, err)
return
}
println(string(buf[:n]))
}
import (
"log"
"crypto/tls"
)
func main() {
log.setflags(log.lshortfile)
conf := &tls.config{
insecureskipverify: true,
}
conn, err := tls.dial("tcp", "127.0.0.1:8000", conf)
if err != nil {
log.println(err)
return
}
defer conn.close()
n, err := conn.write([]byte("hello\n"))
if err != nil {
log.println(n, err)
return
}
buf := make([]byte, 100)
n, err = conn.read(buf)
if err != nil {
log.println(n, err)
return
}
println(string(buf[:n]))
}
希望本文所述对大家go语言程序设计有所帮助。