summaryrefslogtreecommitdiffstats
path: root/src/bin/client/tls.rs
diff options
context:
space:
mode:
authorsalmonik <salmonik@tuta.io>2026-08-24 14:48:12 +0200
committersalmonik <salmonik@tuta.io>2026-08-24 14:48:12 +0200
commit5d6b5049c2a34a3f8abe725d4a871ba84a0dd714 (patch)
tree57cb03bf59b3358a96742f455d38c21f4f645396 /src/bin/client/tls.rs
parentstable and working, both platforms (diff)
downloadzagan-bet-5d6b5049c2a34a3f8abe725d4a871ba84a0dd714.tar.gz
zagan-bet-5d6b5049c2a34a3f8abe725d4a871ba84a0dd714.zip
TLS transport and games framework on client and server
Diffstat (limited to 'src/bin/client/tls.rs')
-rw-r--r--src/bin/client/tls.rs150
1 files changed, 150 insertions, 0 deletions
diff --git a/src/bin/client/tls.rs b/src/bin/client/tls.rs
new file mode 100644
index 0000000..8ab5c7c
--- /dev/null
+++ b/src/bin/client/tls.rs
@@ -0,0 +1,150 @@
1use std::io::{Read, Write};
2use std::net::TcpStream;
3use std::sync::mpsc;
4use std::thread;
5use std::time::{Duration, Instant};
6
7pub struct TlsConnector {
8 connector: rustls::ClientConnection,
9 server_name: String,
10}
11
12impl TlsConnector {
13 pub fn new(server_name: &str) -> Result<Self, Box<dyn std::error::Error>> {
14 let config = rustls::ClientConfig::builder()
15 .dangerous()
16 .with_custom_certificate_verifier(std::sync::Arc::new(InsecureVerifier))
17 .with_no_client_auth();
18
19 let connector =
20 rustls::ClientConnection::new(std::sync::Arc::new(config), server_name.try_into()?);
21
22 Ok(Self {
23 connector,
24 server_name: server_name.to_string(),
25 })
26 }
27
28 pub fn connect(
29 &self,
30 stream: TcpStream,
31 ) -> Result<TlsClientStream, Box<dyn std::error::Error>> {
32 let mut conn = rustls::ClientConnection::new(
33 std::sync::Arc::new(
34 rustls::ClientConfig::builder()
35 .dangerous()
36 .with_custom_verifier(rustls::client::danger::ServerCertVerifier::DEFAULT)
37 .with_no_client_auth(),
38 ),
39 self.server_name.clone().try_into()?,
40 )?;
41
42 conn.complete_io(&mut std::io::BufReader::new(&stream))?;
43
44 Ok(TlsClientStream {
45 stream,
46 conn: std::sync::Arc::new(conn),
47 })
48 }
49}
50
51struct InsecureVerifier;
52
53impl rustls::client::danger::ServerCertVerifier for InsecureVerifier {
54 fn verify_server_cert(
55 &self,
56 _end_entity: &rustls::pki_types::CertificateDer<'_>,
57 _intermediates: &[rustls::pki_types::CertificateDer<'_>],
58 _server_name: &rustls::pki_types::ServerName<'_>,
59 _ocsp_response: &[u8],
60 _now: std::time::SystemTime,
61 ) -> Result<rustls::client::danger::ServerCertVerified, rustls::Error> {
62 Ok(rustls::client::danger::ServerCertVerified::assertion())
63 }
64
65 fn verify_tls12_signature(
66 &self,
67 _message: &[u8],
68 _cert: &rustls::pki_types::CertificateDer<'_>,
69 _dss: &rustls::DigitallySignedStruct,
70 ) -> Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
71 Ok(rustls::client::danger::HandshakeSignatureValid::assertion())
72 }
73
74 fn verify_tls13_signature(
75 &self,
76 _message: &[u8],
77 _cert: &rustls::pki_types::CertificateDer<'_>,
78 _dss: &rustls::DigitallySignedStruct,
79 ) -> Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
80 Ok(rustls::client::danger::HandshakeSignatureValid::assertion())
81 }
82
83 fn supported_verify_schemes(&self) -> Vec<rustls::SignatureScheme> {
84 vec![
85 rustls::SignatureScheme::RSA_PKCS1_SHA256,
86 rustls::SignatureScheme::RSA_PKCS1_SHA384,
87 rustls::SignatureScheme::RSA_PKCS1_SHA512,
88 rustls::SignatureScheme::RSA_PSS_SHA256,
89 rustls::SignatureScheme::RSA_PSS_SHA384,
90 rustls::SignatureScheme::RSA_PSS_SHA512,
91 rustls::SignatureScheme::ECDSA_NISTP256_SHA256,
92 rustls::SignatureScheme::ECDSA_NISTP384_SHA384,
93 rustls::SignatureScheme::ECDSA_NISTP521_SHA512,
94 rustls::SignatureScheme::RSA_PSS_SHA256_LEGACY,
95 rustls::SignatureScheme::RSA_PSS_SHA384_LEGACY,
96 rustls::SignatureScheme::RSA_PSS_SHA512_LEGACY,
97 ]
98 }
99}
100
101pub struct TlsClientStream {
102 stream: TcpStream,
103 conn: std::sync::Arc<rustls::ClientConnection>,
104}
105
106impl TlsClientStream {
107 pub fn read_line(&mut self, buf: &mut String) -> std::io::Result<usize> {
108 let mut byte_buf = [0u8; 1024];
109 loop {
110 let mut tls_reader = std::io::BufReader::new(self.conn.clone());
111 let n = tls_reader.read(&mut byte_buf)?;
112 if n == 0 {
113 return Ok(0);
114 }
115 let chunk = std::str::from_utf8(&byte_buf[..n]).unwrap_or("");
116 buf.push_str(chunk);
117 if buf.contains('\n') {
118 return Ok(n);
119 }
120 }
121 }
122
123 pub fn write_line(&mut self, msg: &str) -> std::io::Result<()> {
124 use std::io::Write;
125 self.conn.writer().write_all(msg.as_bytes())?;
126 self.conn.writer().write_all(b"\n")?;
127 self.conn.writer().flush()?;
128 Ok(())
129 }
130
131 pub fn try_clone(&self) -> std::io::Result<TlsClientStream> {
132 let new_stream = self.stream.try_clone()?;
133 let mut conn = rustls::ClientConnection::new(
134 std::sync::Arc::new(
135 rustls::ClientConfig::builder()
136 .dangerous()
137 .with_custom_verifier(rustls::client::danger::ServerCertVerifier::DEFAULT)
138 .with_no_client_auth(),
139 ),
140 self.server_name.clone().try_into().unwrap(),
141 )?;
142
143 conn.complete_io(&mut std::io::BufReader::new(&new_stream))?;
144
145 Ok(TlsClientStream {
146 stream: new_stream,
147 conn: std::sync::Arc::new(conn),
148 })
149 }
150}