Skip to content

Instantly share code, notes, and snippets.

@Na0ki
Last active June 25, 2025 23:18
Show Gist options
  • Save Na0ki/100d3e37e01848de0c87a1af463b8f7f to your computer and use it in GitHub Desktop.
Save Na0ki/100d3e37e01848de0c87a1af463b8f7f to your computer and use it in GitHub Desktop.
接続検証サンプル(再現)
require 'net/http'
require 'openssl'
uri = URI('https://host.docker.internal:8443/')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
# 自己署名許可
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
# OpenSSL::SSL::SSLContextに直接オプションを設定
# if OpenSSL::SSL.const_defined?(:OP_IGNORE_UNEXPECTED_EOF)
# OpenSSL::SSL::SSLContext::DEFAULT_PARAMS[:options] |= OpenSSL::SSL::OP_IGNORE_UNEXPECTED_EOF
# end
begin
response = http.get('/')
puts "Response: #{response.code} #{response.body}"
rescue => e
puts "Exception: #{e.class} - #{e.message}"
end
require 'socket'
require 'openssl'
PORT_NUM = 8443
tcp_server = TCPServer.new('127.0.0.1', PORT_NUM)
# openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 1 -nodes -subj "/CN=localhost"
context = OpenSSL::SSL::SSLContext.new
context.cert = OpenSSL::X509::Certificate.new(File.read('cert.pem'))
context.key = OpenSSL::PKey::RSA.new(File.read('key.pem'))
ssl_server = OpenSSL::SSL::SSLServer.new(tcp_server, context)
puts "server is listening at port #{PORT_NUM}"
loop do
ssl_socket = ssl_server.accept
begin
response_body = "Hello World"
response = <<~HTTP
HTTP/1.1 200 OK\r
Content-Type: text/html\r
Connection: close\r
\r
#{response_body}
HTTP
ssl_socket.write(response)
ssl_socket.flush
ssl_socket.io.close # TLSの終了処理を踏まずに切断
rescue => e
puts "サーバーエラー: #{e.message}"
ensure
ssl_socket.close rescue nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment