15
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

テスト用SSLサーバーをGoで作る

Last updated at Posted at 2015-04-14

経緯

  • SSLでRest APIを叩くクライアントを作りたい
  • 実装毎にサーバーから返る結果が違う(Go:NG/Python:OK/curl:OK)。なぜ
  • アプリ内でリクエストをdumpしてheader/Bodyを見たが差異があるように見えない
  • テスト用のSSLサーバーをローカルに立てて、wiresharkでパケットを見よう

実装

以下にやり方がまんま載っている。感謝。

Golang ListenAndServeTLS returns data when not using https in the browser
http://stackoverflow.com/questions/23494082/golang-listenandservetls-returns-data-when-not-using-https-in-the-browser

鍵の作成

# openssl genrsa -out private_key 2048
# openssl req -new -x509 -key private_key -out public_key -days 365

サーバーの作成

server.go
package main

import (
        "fmt"
        "net/http"
)

func rootHander(w http.ResponseWriter, r *http.Request) {
        fmt.Fprint(w, "Hello.")
}

func main() {
        http.HandleFunc("/", rootHander)
        err := http.ListenAndServeTLS(":9444", "./public_key", "private_key", nil)
        if err != nil {
                fmt.Println(err)
        }
}

実行

# go build server.go && ./server &
# curl -k https://localhost:9444/
hello.
15
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?