Skip to content

Commit 074bd8f

Browse files
authored
Merge pull request #79 from everpcpc/fix-init
feat(mysql): add init_before_ssl_with_config
2 parents ae91dc3 + c4c2ea3 commit 074bd8f

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ Check out the [CONTRIBUTING.md](./CONTRIBUTING.md) guide for more details on get
3131

3232
## Getting help
3333

34-
Submit [issues](https://github.com/datafuselabs/opensrv/issues/new/choose) for bug report or asking questions in [discussion](https://github.com/datafuselabs/opensrv/discussions/new?category=q-a).
34+
Submit [issues](https://github.com/datafuselabs/opensrv/issues/new/choose) for bug report or asking questions in [discussion](https://github.com/datafuselabs/opensrv/discussions/new?category=q-a).
3535

3636
## License
3737

3838
Licensed under <a href="./LICENSE">Apache License, Version 2.0</a>.
39-

mysql/src/lib.rs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,15 @@ pub struct AsyncMysqlIntermediary<B, S: AsyncRead + Unpin, W> {
238238
writer: packet_writer::PacketWriter<W>,
239239
}
240240

241+
/// Configuration used to craft the initial server handshake before a shim is available.
242+
#[derive(Clone, Debug)]
243+
pub struct ServerHandshakeConfig {
244+
pub version: String,
245+
pub connection_id: u32,
246+
pub default_auth_plugin: String,
247+
pub scramble: [u8; SCRAMBLE_SIZE],
248+
}
249+
241250
impl<B, R, W> AsyncMysqlIntermediary<B, R, W>
242251
where
243252
W: AsyncWrite + Send + Unpin,
@@ -297,16 +306,43 @@ where
297306
),
298307
B::Error,
299308
> {
309+
let config = ServerHandshakeConfig {
310+
version: shim.version(),
311+
connection_id: shim.connect_id(),
312+
default_auth_plugin: shim.default_auth_plugin().to_string(),
313+
scramble: shim.salt(),
314+
};
315+
316+
AsyncMysqlIntermediary::<B, R, W>::init_before_ssl_with_config(
317+
&config,
318+
input_stream,
319+
output_stream,
320+
#[cfg(feature = "tls")]
321+
tls_conf,
322+
)
323+
.await
324+
.map_err(B::Error::from)
325+
}
326+
327+
pub async fn init_before_ssl_with_config(
328+
config: &ServerHandshakeConfig,
329+
input_stream: R,
330+
output_stream: &mut W,
331+
#[cfg(feature = "tls")] tls_conf: &Option<std::sync::Arc<ServerConfig>>,
332+
) -> io::Result<(
333+
bool,
334+
(ClientHandshake, u8, CapabilityFlags, PacketReader<R>),
335+
)> {
300336
let mut reader = PacketReader::new(input_stream);
301337
let mut writer = PacketWriter::new(output_stream);
302338
// https://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::HandshakeV10
303339
writer.write_all(&[10])?; // protocol 10
304340

305-
writer.write_all(shim.version().as_bytes())?;
341+
writer.write_all(config.version.as_bytes())?;
306342
writer.write_all(&[0x00])?;
307343

308344
// connection_id (4 bytes)
309-
writer.write_all(&shim.connect_id().to_le_bytes())?;
345+
writer.write_all(&config.connection_id.to_le_bytes())?;
310346

311347
let server_capabilities = CapabilityFlags::CLIENT_PROTOCOL_41
312348
| CapabilityFlags::CLIENT_SECURE_CONNECTION
@@ -324,8 +360,8 @@ where
324360
};
325361

326362
let server_capabilities_vec = server_capabilities.bits().to_le_bytes();
327-
let default_auth_plugin = shim.default_auth_plugin();
328-
let scramble = shim.salt();
363+
let default_auth_plugin = config.default_auth_plugin.as_str();
364+
let scramble = &config.scramble;
329365

330366
writer.write_all(&scramble[0..AUTH_PLUGIN_DATA_PART_1_LENGTH])?; // auth-plugin-data-part-1
331367
writer.write_all(&[0x00])?;

0 commit comments

Comments
 (0)