Skip to content
This repository has been archived by the owner on Aug 29, 2024. It is now read-only.

Commit

Permalink
Added examples for an SSL client and server. (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
postmodern authored Sep 30, 2023
1 parent f8936cc commit 56fcfe8
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
29 changes: 29 additions & 0 deletions examples/ssl/cert.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-----BEGIN CERTIFICATE-----
MIIE7TCCAtWgAwIBAgIBADANBgkqhkiG9w0BAQsFADA6MRIwEAYDVQQDDAlsb2Nh
bGhvc3QxETAPBgNVBAsMCGFzeW5jLWlvMREwDwYDVQQKDAhTb2NrZXRyeTAeFw0y
MzA5MjkyMzAyMTRaFw0yNDA5MjgyMzAyMTRaMDoxEjAQBgNVBAMMCWxvY2FsaG9z
dDERMA8GA1UECwwIYXN5bmMtaW8xETAPBgNVBAoMCFNvY2tldHJ5MIICIjANBgkq
hkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAtXIHml2E3yyVVHauPWDTrKWZZcT+fBua
GHZF58qyiJ7niP6ilmtxGil8ubPTb2wGUrQpNc6Pn8YY1o0XZbrg9oyQs7X5BlRu
GBBbxdmJTZi7/PpWHvfKsCZc2/ntS/NsMW8ig6PWMrzZn+VWvlQj7aP/BbD+dugg
7sSUUdlYGJdOddDfVpXbMqWswgvK9jYd/KywghmWBRvl8C+bp0yBcIQtbG02ukEd
wIygDItInNvpHJ+njcv8QchUvPWOQgV1dZO6MTRaOeT45Dwf8X9ecuuKeoz6OH5Q
pau9Ri1jKadM2WwPJzRfhVUKtWttoXWiL8LyhI3kGLFkuemEcVEhbjpTQysmi4Qs
sk/Wcma6eK3/iume2euR01wE6mip0hTBDuE5l0MZZdJKSep08AXZRBX8pnvHEd82
h3GPIgyO2EO7wbgM4lfGidERRbasoJUIyTmTo1poe6UwdjNKMdBYew1LmCPmcRbT
4dTyChR4bMPrJWd0xXiPZEFT8AJmFduumJEu0AyN/1xMPtRqDkx7sytqMa1n8NsG
c0bAoesXisdS2Peoa6G5ekMOzqFKrP1yc7dtvu8hjQtVK2uO1Y+KCSyUojFMIBGy
d/v2EB/e1corqQ2BZmvzFQ4vQrZ5344fvc9B3PHswmvR0clAFLnkc1BaNY3A0p84
/ptaSjAB9qkCAwEAATANBgkqhkiG9w0BAQsFAAOCAgEAbIehU4aXrhp+uCdk8xKy
W7LxiJfPhF/v2crVvuqpv9+SKlPDcB7auXyIKc7cchO3P1XlNM/s+Uw6VA6QZxP9
eC5dqhZ0HcagTtTTqhlaF1pzpOXXA/sGacmyeHZQu0jvilifiWNtEtqDngbBi320
qjlmknsTFP1ptFDNikZPspKfn0mhzENnYDxA27K+UDMOdoqzoNxCXPYKst/1F3vS
+u2A+4Z76TYvNQmf/uTcfrFEssgH+2L0q+8CPsMflHjFuBXI8m4AUa7QYSTavSkn
siWMJv+JKpW81P/pQ9L/hDPKIEWpNm/J8llOdp8AVvl514VYgreV5mvwqxHxvwob
fARlqBDeOKFnC1F5KZkBayd6k6te9ZGXc68JqFqzskXG6FqzCAc0rpDSzM76NWuT
IhxtK8ceoekBGOMhOUIWTbFk2jqNrGbME24/j2Y5tbTuTRsuqSlKIOM2Ao3iyuzb
zdqPgqqZJjac46FEhxO820LwxcGwN4FrTh0fniMa066Aws87FnZE9j7Q6rWOJMSL
ItIUovKnB6thIo0OWYuj5CQrodnKvOaWYd8kWPE3kPbtd0nQ9OOoMCHf+CPVo9kN
QJVFmjNEQeiq/51uvVtJs66kXRIEKWDy04S3paf9YexSxHZXBTwmch0yqJeuG6nr
wUaVIUMR7tbVLe6RGgXlVcU=
-----END CERTIFICATE-----
19 changes: 19 additions & 0 deletions examples/ssl/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env ruby

require 'async'
require 'async/io'
require 'async/io/stream'

endpoint = Async::IO::Endpoint.ssl('localhost',5678)

Async do |async|
endpoint.connect do |socket|
stream = Async::IO::Stream.new(socket)

(1..).each do |i|
stream.puts "test #{i}"
puts stream.gets
sleep 1
end
end
end
51 changes: 51 additions & 0 deletions examples/ssl/key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
-----BEGIN RSA PRIVATE KEY-----
MIIJJwIBAAKCAgEAtXIHml2E3yyVVHauPWDTrKWZZcT+fBuaGHZF58qyiJ7niP6i
lmtxGil8ubPTb2wGUrQpNc6Pn8YY1o0XZbrg9oyQs7X5BlRuGBBbxdmJTZi7/PpW
HvfKsCZc2/ntS/NsMW8ig6PWMrzZn+VWvlQj7aP/BbD+dugg7sSUUdlYGJdOddDf
VpXbMqWswgvK9jYd/KywghmWBRvl8C+bp0yBcIQtbG02ukEdwIygDItInNvpHJ+n
jcv8QchUvPWOQgV1dZO6MTRaOeT45Dwf8X9ecuuKeoz6OH5Qpau9Ri1jKadM2WwP
JzRfhVUKtWttoXWiL8LyhI3kGLFkuemEcVEhbjpTQysmi4Qssk/Wcma6eK3/iume
2euR01wE6mip0hTBDuE5l0MZZdJKSep08AXZRBX8pnvHEd82h3GPIgyO2EO7wbgM
4lfGidERRbasoJUIyTmTo1poe6UwdjNKMdBYew1LmCPmcRbT4dTyChR4bMPrJWd0
xXiPZEFT8AJmFduumJEu0AyN/1xMPtRqDkx7sytqMa1n8NsGc0bAoesXisdS2Peo
a6G5ekMOzqFKrP1yc7dtvu8hjQtVK2uO1Y+KCSyUojFMIBGyd/v2EB/e1corqQ2B
ZmvzFQ4vQrZ5344fvc9B3PHswmvR0clAFLnkc1BaNY3A0p84/ptaSjAB9qkCAwEA
AQKCAgAQqSSRFFLB0lxw6cfki2pMYVVVTrImb7tl0SBooQhlOqAciHMh+EIlqpcR
DzXUNplbCT96eRnfjCdWNhTmqrMC+JPF6Kjx35lPXNssbuXoaeSjHVKAm/Sw2Yjv
ywJy1aqC3IdRCp99v7EE+WBOcDfvV35whZjMDtMNUaAj5t7rvSL/dvs2/mInA6b6
F26ezofQ3oODorlPhUkHFbwHb+M3028/VETw8885UcBVfomfm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lRTZCnx6O90yWD
WfK56YbdhwS2mycgTXf02fjbPssGM/+gbpKeEwIbj3jnHmo8qY2Pu7nT5A==
-----END RSA PRIVATE KEY-----
25 changes: 25 additions & 0 deletions examples/ssl/server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env ruby

require 'async'
require 'async/io'
require 'async/io/stream'

key_file = File.join(__dir__,'key.pem')
cert_file = File.join(__dir__,'cert.crt')

ssl_context = OpenSSL::SSL::SSLContext.new
ssl_context.key = OpenSSL::PKey::RSA.new(File.read(key_file))
ssl_context.cert = OpenSSL::X509::Certificate.new(File.read(cert_file))

endpoint = Async::IO::Endpoint.ssl('localhost',5678, ssl_context: ssl_context)

Async do |async|
endpoint.accept do |peer|
stream = Async::IO::Stream.new(peer)

while line = stream.gets
puts "received: #{line}"
stream.puts "you sent: #{line}"
end
end
end

0 comments on commit 56fcfe8

Please sign in to comment.