加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

golang简单实现一个基于TLS/SSL的 TCP服务器和客户端

发布时间:2020-12-16 18:12:13 所属栏目:大数据 来源:网络整理
导读:本篇文章介绍一下使用TLS/SSL创建安全的TCP通信,首先我们要准备一个数字证书和一个密钥关于如何产生密钥,请看下面文章: Author: 岳东卫 Email: usher.yue@gmail.com 通过Openssl创建数字证书和密钥 TLS服务器端代码 TLS客户端代码 通过Openssl创建数字证书

本篇文章介绍一下使用TLS/SSL创建安全的TCP通信,首先我们要准备一个数字证书和一个密钥关于如何产生密钥,请看下面文章:

Author: 岳东卫
Email: usher.yue@gmail.com

    • 通过Openssl创建数字证书和密钥
    • TLS服务器端代码
    • TLS客户端代码

通过Openssl创建数字证书和密钥

关于如何通过Openssl创建证书和私钥

TLS服务器端代码

package main

import (
    "crypto/rand"
    "crypto/tls"
    "fmt"
    "log"
    "net"
    "time"
)

func HandleClientConnect(conn net.Conn) {
    defer conn.Close()
    fmt.Println("Receive Connect Request From ",conn.RemoteAddr().String())
    buffer := make([]byte, 1024)
    for {
        len,err := conn.Read(buffer)
        if err != nil {
            log.Println(err.Error())
            break
        }
        fmt.Printf("Receive Data: %sn",string(buffer[:len]))
        //发送给客户端
        _,err = conn.Write([]byte("服务器收到数据:" + string(buffer[:len])))
        if err != nil {
            break
        }
    }
    fmt.Println("Client " + conn.RemoteAddr().String() + " Connection Closed.....")
}

func main() {
    crt,err := tls.LoadX509KeyPair("server.crt","server.key")
    if err != nil {
        log.Fatalln(err.Error())
    }
    tlsConfig := &tls.Config{}
    tlsConfig.Certificates = []tls.Certificate{crt}
    // Time returns the current time as the number of seconds since the epoch.
    // If Time is nil,TLS uses time.Now.
    tlsConfig.Time = time.Now
    // Rand provides the source of entropy for nonces and RSA blinding.
    // If Rand is nil,TLS uses the cryptographic random reader in package
    // crypto/rand.
    // The Reader must be safe for use by multiple goroutines.
    tlsConfig.Rand = rand.Reader
    l,err := tls.Listen("tcp",":8888",tlsConfig)
    if err != nil {
        log.Fatalln(err.Error())
    }
    for {
        conn,err := l.Accept()
        if err != nil {
            fmt.Println(err.Error())
            continue
        } else {
            go HandleClientConnect(conn)
        }
    }

}

TLS客户端代码

package main

import (
    "crypto/tls"
    "fmt"
    "io"
    "time"

    "log"
)

func main() {
    //注意这里要使用证书中包含的主机名称
    conn,err := tls.Dial("tcp","abc.com:8888",nil)
    if err != nil {
        log.Fatalln(err.Error())
    }
    defer conn.Close()
    log.Println("Client Connect To ",conn.RemoteAddr())
    status := conn.ConnectionState()
    fmt.Printf("%#vn",status)
    buf := make([]byte, 1024)
    ticker := time.NewTicker(1 * time.Millisecond * 500)
    for {
        select {
        case <-ticker.C:
            {
                _,err = io.WriteString(conn,"hello")
                if err != nil {
                    log.Fatalln(err.Error())
                }
                len,err := conn.Read(buf)
                if err != nil {
                    fmt.Println(err.Error())
                } else {
                    fmt.Println("Receive From Server:",string(buf[:len]))
                }
            }
        }
    }

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读