Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: mysql support #62

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mod auditlog;
mod config;
mod policy_evaluator;
mod postgres_driver;
mod mysql_driver;
mod sql;
use apiproto::api::*;
use apiproto::api_grpc::*;
Expand Down
56 changes: 56 additions & 0 deletions src/mysql_driver/driver.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2022 poonai
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use tokio;
use tokio::net::{TcpListener, TcpStream};
use anyhow::anyhow;
use log::*;

#[derive(Clone, Debug)]
pub struct MySqlDriver{}

impl MySqlDriver{
pub fn start(&self) {
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async move {
let listener = TcpListener::bind(format!(
"0.0.0.0:{}",
2000
))
.await
.map_err(|_| anyhow!("unable to listern on the given port"))
.unwrap();
info!(
"mysql driver listeneing at 0.0.0.0:{}",
2000
);
loop {
let (socket, _) = listener.accept().await.unwrap();
// let acceptor = acceptor.clone();
let driver = self.clone();
tokio::spawn(async move {
if let Err(e) = driver.handle_client_conn(socket).await {
error!("error while handling client connection {:?}", e);
}
()
});
}
});
}

async fn handle_client_conn(&self, conn: TcpStream) -> Result<(), anyhow::Error>{
// to initate the handshake, we must get the target mysql configuration.

todo!()
}
}
60 changes: 60 additions & 0 deletions src/mysql_driver/message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use std::fmt::Debug;

// Copyright 2022 poonai
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use anyhow;
use byteorder::{ByteOrder, LittleEndian};
use bytes::{Bytes, BytesMut};
use tokio::io::AsyncReadExt;
use tokio::net::TcpStream;

pub enum ClientMessage {
RawMessage { seq_id: u8, payload: Bytes },
}

impl ClientMessage {
pub async fn decode(stream: &mut TcpStream) -> Result<ClientMessage, anyhow::Error> {
let mut buf = BytesMut::new();
buf.resize(4, b'0');
stream.read_exact(&mut buf).await?;
// check the packet length.
let packet_length = LittleEndian::read_u24(&buf) as usize;
let seq_id = buf[3];
buf.resize(packet_length, b'0');
stream.read_exact(&mut buf).await?;
return Ok(ClientMessage::RawMessage {
seq_id,
payload: buf.freeze(),
});
}
}

impl Debug for ClientMessage {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::RawMessage { seq_id, payload } => write!(
f,
"seq_id => {}, payload => {}",
seq_id,
String::from_utf8(payload.to_vec()).expect("Found invalid UTF-8")
),
}
}
}

pub enum ServerMessage {
InitialHandShake {

},
}
16 changes: 16 additions & 0 deletions src/mysql_driver/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2022 poonai
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

mod driver;
mod message;