package main

/*
ListenTLS方法一般均默认开启了h2,如果需要仅开启https,需要手动listen监听然后使用app.Serve启动服务。
*/

import (
    "crypto/tls"
    "crypto/x509"
    "io/ioutil"
    "net/http"
    "os"

    "github.com/eudore/eudore"
    "github.com/eudore/eudore/component/httptest"
)

func main() {
    createssl()
    defer os.Remove("ca.cer")
    defer os.Remove("server.key")
    defer os.Remove("server.cer")
    defer os.Remove("client.key")
    defer os.Remove("client.cer")
    app := eudore.NewApp()
    app.AnyFunc("/*", func(ctx eudore.Context) {
        ctx.Debug("istls:", ctx.Istls())
    })

    ln, err := (&eudore.ServerListenConfig{
        Addr:     ":8088",
        HTTPS:    true,
        HTTP2:    true,
        Mutual:   true,
        Keyfile:  "server.key",
        Certfile: "server.cer",
    }).Listen()
    if err == nil {
        app.Serve(ln)
    } else {
        app.Error(err)
    }

    ln, err = (&eudore.ServerListenConfig{
        Addr:      ":8088",
        HTTPS:     true,
        HTTP2:     true,
        Mutual:    true,
        Keyfile:   "server.key",
        Certfile:  "server.cer",
        Trustfile: "ca.cer",
    }).Listen()
    if err == nil {
        app.Serve(ln)
    } else {
        app.Error(err)
    }

    client := httptest.NewClient(app)
    tp, err := createtp()
    if err == nil {
        client.Client.Transport = tp
    } else {
        app.Options(err)
    }
    client.NewRequest("GET", "https://localhost:8088/").Do().CheckStatus(200).Out()

    // app.CancelFunc()
    app.Run()
}

func createtp() (*http.Transport, error) {
    pool := x509.NewCertPool()
    data, err := ioutil.ReadFile("/tmp/mca/ca.cer")
    pool.AppendCertsFromPEM(data)
    if err != nil {
        return nil, err
    }

    clientCrt, err := tls.LoadX509KeyPair("client.cer", "client.key")
    if err != nil {
        return nil, err
    }

    return &http.Transport{
        TLSClientConfig: &tls.Config{
            RootCAs:      pool,
            Certificates: []tls.Certificate{clientCrt},
        },
    }, nil
}

/*
# 成服务端私钥
openssl genrsa -out server.key 2048
#生成服务端证书请求文件
openssl req -new -key server.key -out server.csr -subj "/C=CN/ST=BJ/L=beijing/O=eudore/OU=eudore/OU=eudore/CN=localhost"

# 生成客户端私钥
openssl genrsa -out client.key 2048
# 生成客户证书请求文件
openssl req -new -key client.key -out client.csr -subj "/C=CN/ST=BJ/L=beijing/O=eudore/OU=eudore/OU=eudore/CN=localhost"

# 生成根证书私钥
openssl genrsa -out ca.key 2048
# 生成根证书请求文件
openssl req -new -key ca.key -out ca.csr -subj "/C=CN/ST=BJ/L=beijing/O=eudore/OU=eudore/OU=eudore/CN=localhost"
# 生成自签名的根证书文件
openssl x509 -req -days 3650 -sha1 -extensions v3_ca -signkey ca.key -in ca.csr -out ca.cer
# 利用已签名根证书生成服务端证书和客户端证书
# 生成服务端证书
openssl x509 -req -days 3650 -sha1 -extensions v3_req -CA ca.cer -CAkey ca.key -CAcreateserial -in server.csr -out server.cer
# 生成客户端证书
openssl x509 -req -days 3650 -sha1 -extensions v3_req -CA ca.cer -CAkey ca.key -CAcreateserial -in client.csr -out client.cer

openssl x509 -in server.cer -text -noout 2>&1| head -n 15
*/

func createssl() {
    ioutil.WriteFile("ca.cer", []byte(`-----BEGIN CERTIFICATE-----
MIIDYjCCAkoCCQDkcCR+EmTT1jANBgkqhkiG9w0BAQUFADBzMQswCQYDVQQGEwJD
TjELMAkGA1UECAwCQkoxEDAOBgNVBAcMB2JlaWppbmcxDzANBgNVBAoMBmV1ZG9y
ZTEPMA0GA1UECwwGZXVkb3JlMQ8wDQYDVQQLDAZldWRvcmUxEjAQBgNVBAMMCWxv
Y2FsaG9zdDAeFw0yMDA0MjgxNTUzMjdaFw0zMDA0MjYxNTUzMjdaMHMxCzAJBgNV
BAYTAkNOMQswCQYDVQQIDAJCSjEQMA4GA1UEBwwHYmVpamluZzEPMA0GA1UECgwG
ZXVkb3JlMQ8wDQYDVQQLDAZldWRvcmUxDzANBgNVBAsMBmV1ZG9yZTESMBAGA1UE
AwwJbG9jYWxob3N0MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvG1a
zMYrF/suaHIy+vDqFMuv8hrw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