Go使用https

作者 : admin 本文共943个字,预计阅读时间需要3分钟 发布时间: 2024-06-11 共1人阅读

一、服务端

1. 生成私钥和证书

  • 安装OpenSSL
    windows安装OpenSSL
  • 生成CA证书
  • 创建证书

以上两个步骤,参考:Go http2 和 h2c

2. 代码

package main

import (
    "log"
    "net/http"
    "time"
    "golang.org/x/net/http2"
)

const idleTimeout = 5 * time.Minute
const activeTimeout = 10 * time.Minute

func main() {
    var srv http.Server
    //http2.VerboseLogs = true
    srv.Addr = ":8080"
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("hello http2"))
    })
    http2.ConfigureServer(&srv, &http2.Server{})
    go func() {
        log.Fatal(srv.ListenAndServeTLS("server.crt", "server.key"))
    }()
    select {}
}

二、客户端

三、浏览器

1、开启了h2的网站

为了方便介绍后面的检测h2的插件和检测有没有开h2的方法,这边给出了几个已经开启了h2的一些网站地址

  • https://www.bilibili.com/
  • https://v.qq.com/
  • https://www.iqiyi.com/
  • https://www.taobao.com/
  • https://www.aliyun.com/
  • https://myssl.com/http2_check.html

2、检测插件

  • HTTP/2 and SPDY indicator

当访问开启h2的网站

Go使用https插图

3、检测方法

方法一、HTTP/2 and SPDY indicator插件

方法二、谷歌浏览器:

  • Console
    • window.chrome.loadTimes()
    • window.chrome.loadTimes().connectionInfo
    • window.chrome.loadTimes().npnNegotiatedProtocol

开启了http1.1的网站

Go使用https插图(1)

开启了http2.0的网站

Go使用https插图(2)

四、参考资料

  • Go http2 和 h2c
本站无任何商业行为
个人在线分享 » Go使用https
E-->