Skip to content

Commit

Permalink
g3proxy: allow SMTP EHLO at later stage
Browse files Browse the repository at this point in the history
  • Loading branch information
zh-jq-b committed Jun 4, 2024
1 parent baf708c commit 006016e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
18 changes: 16 additions & 2 deletions g3proxy/src/inspect/smtp/forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ use g3_io_ext::{LimitedWriteExt, LineRecvBuf};
use g3_smtp_proto::command::{Command, MailParam};
use g3_smtp_proto::response::{ReplyCode, ResponseEncoder, ResponseParser};

use super::{CommandLineRecvExt, ResponseLineRecvExt, ResponseParseExt, SmtpRelayBuf};
use super::{
CommandLineRecvExt, InitializedExtensions, Initiation, ResponseLineRecvExt, ResponseParseExt,
SmtpRelayBuf,
};
use crate::serve::{ServerTaskError, ServerTaskResult};

pub(super) enum ForwardNextAction {
Quit,
StartTls,
ReverseConnection,
SetExtensions(InitializedExtensions),
MailTransport(MailParam),
}

Expand Down Expand Up @@ -83,7 +87,6 @@ impl<'a> Forward<'a> {
|cmd| {
match &cmd {
Command::Hello(_)
| Command::ExtendHello(_)
| Command::Recipient(_)
| Command::Data
| Command::BinaryData(_)
Expand Down Expand Up @@ -144,6 +147,17 @@ impl<'a> Forward<'a> {
return Ok(ForwardNextAction::ReverseConnection);
}
}
Command::ExtendHello(_host) => {
let mut initialization = Initiation::new(self.config, self.local_ip, true);
if initialization
.recv_relay_check_rsp(&mut buf.rsp_recv_buf, ups_r, clt_w)
.await?
.is_some()
{
let (_, extensions) = initialization.into_parts();
return Ok(ForwardNextAction::SetExtensions(extensions));
}
}
Command::Mail(param) => {
let rsp = self.recv_relay_rsp(buf, ups_r, clt_w).await?;
if rsp == ReplyCode::OK {
Expand Down
2 changes: 1 addition & 1 deletion g3proxy/src/inspect/smtp/initiation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl<'a> Initiation<'a> {
}
}

async fn recv_relay_check_rsp<CW, UR>(
pub(super) async fn recv_relay_check_rsp<CW, UR>(
&mut self,
rsp_recv_buf: &mut LineRecvBuf<{ ResponseParser::MAX_LINE_SIZE }>,
ups_r: &mut UR,
Expand Down
15 changes: 8 additions & 7 deletions g3proxy/src/inspect/smtp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ mod ending;
use ending::{EndQuitServer, EndWaitClient};

mod initiation;
use initiation::Initiation;
use initiation::{InitializedExtensions, Initiation};

mod forward;
use forward::{Forward, ForwardNextAction};
Expand Down Expand Up @@ -242,17 +242,14 @@ where
initiation
.relay(&mut clt_r, &mut clt_w, &mut ups_r, &mut ups_w)
.await?;
let (client_host, server_ext) = initiation.into_parts();
let (client_host, mut server_ext) = initiation.into_parts();
self.client_host = Some(client_host);

let allow_odmr = server_ext.allow_odmr(interception_config);
let allow_starttls = server_ext.allow_starttls(self.from_starttls);
let allow_chunking = server_ext.allow_chunking();
let allow_burl = server_ext.allow_burl(interception_config);

let mut relay_buf = SmtpRelayBuf::default();

loop {
let allow_odmr = server_ext.allow_odmr(interception_config);
let allow_starttls = server_ext.allow_starttls(self.from_starttls);
let mut forward =
Forward::new(interception_config, local_ip, allow_odmr, allow_starttls);
let next_action = forward
Expand Down Expand Up @@ -304,7 +301,11 @@ where
.await
.map(|_| None);
}
ForwardNextAction::SetExtensions(ext) => server_ext = ext,
ForwardNextAction::MailTransport(param) => {
let allow_chunking = server_ext.allow_chunking();
let allow_burl = server_ext.allow_burl(interception_config);

let mut transaction =
Transaction::new(&self.ctx, local_ip, allow_chunking, allow_burl, param);
transaction
Expand Down

0 comments on commit 006016e

Please sign in to comment.