use crate::llm::actions::{
client_trait::{Client, ClientActionResult},
protocol_trait::Protocol,
ActionDefinition, Parameter,
};
use crate::protocol::EventType;
use crate::state::app_state::AppState;
use anyhow::{Context, Result};
use serde_json::json;
use std::sync::LazyLock;
fn send_command_action() -> ActionDefinition {
ActionDefinition {
name: "send_command".to_string(),
description: "Send a text command to the Telnet server (a newline is appended)".to_string(),
parameters: vec![Parameter {
name: "command".to_string(),
type_hint: "string".to_string(),
description: "The command text to send (newline will be appended)".to_string(),
required: true,
}],
example: json!({
"type": "send_command",
"command": "ls -la"
}),
log_template: None,
}
}
fn send_text_action() -> ActionDefinition {
ActionDefinition {
name: "send_text".to_string(),
description: "Send raw text to the Telnet server, exactly as given and with no newline \
added. Use it where the server is waiting mid-line."
.to_string(),
parameters: vec![Parameter {
name: "text".to_string(),
type_hint: "string".to_string(),
description: "The text to send".to_string(),
required: true,
}],
example: json!({
"type": "send_text",
"text": "yes"
}),
log_template: None,
}
}
fn wait_for_more_action() -> ActionDefinition {
ActionDefinition {
name: "wait_for_more".to_string(),
description: "Send nothing and wait for the server's next write. Use it when what \
arrived is only part of a prompt or a banner."
.to_string(),
parameters: vec![],
example: json!({
"type": "wait_for_more"
}),
log_template: None,
}
}
fn disconnect_action() -> ActionDefinition {
ActionDefinition {
name: "disconnect".to_string(),
description: "Disconnect from the Telnet server".to_string(),
parameters: vec![],
example: json!({
"type": "disconnect"
}),
log_template: None,
}
}
pub static TELNET_CLIENT_CONNECTED_EVENT: LazyLock<EventType> = LazyLock::new(|| {
EventType::new(
"telnet_connected",
"Telnet client successfully connected to server. Nothing has arrived yet - many \
servers send a banner or a login prompt unprompted, so wait_for_more is often the \
right first answer.",
json!({
"type": "wait_for_more"
}),
)
.with_parameters(vec![Parameter {
name: "remote_addr".to_string(),
type_hint: "string".to_string(),
description: "Remote server address".to_string(),
required: true,
}])
.with_actions(vec![
send_command_action(),
send_text_action(),
wait_for_more_action(),
disconnect_action(),
])
});
pub static TELNET_CLIENT_DATA_RECEIVED_EVENT: LazyLock<EventType> = LazyLock::new(|| {
EventType::new(
"telnet_data_received",
"Data received from Telnet server, with option negotiation already stripped and \
answered. Reply with send_command or send_text, or wait_for_more if this looks like \
part of a prompt you have not finished reading.",
json!({
"type": "send_command",
"command": "whoami"
}),
)
.with_parameters(vec![Parameter {
name: "data".to_string(),
type_hint: "string".to_string(),
description: "The text the server sent, with Telnet IAC sequences removed".to_string(),
required: true,
}])
.with_actions(vec![
send_command_action(),
send_text_action(),
wait_for_more_action(),
disconnect_action(),
])
});
pub struct TelnetClientProtocol;
impl TelnetClientProtocol {
pub fn new() -> Self {
Self
}
}
impl Protocol for TelnetClientProtocol {
fn get_async_actions(&self, _state: &AppState) -> Vec<ActionDefinition> {
vec![
send_command_action(),
send_text_action(),
wait_for_more_action(),
disconnect_action(),
]
}
fn get_sync_actions(&self) -> Vec<ActionDefinition> {
vec![
send_command_action(),
send_text_action(),
wait_for_more_action(),
disconnect_action(),
]
}
fn protocol_name(&self) -> &'static str {
"Telnet"
}
fn get_event_types(&self) -> Vec<EventType> {
vec![
TELNET_CLIENT_CONNECTED_EVENT.clone(),
TELNET_CLIENT_DATA_RECEIVED_EVENT.clone(),
]
}
fn stack_name(&self) -> &'static str {
"ETH>IP>TCP>Telnet"
}
fn keywords(&self) -> Vec<&'static str> {
vec![
"telnet",
"telnet client",
"connect to telnet",
"remote shell",
]
}
fn metadata(&self) -> crate::protocol::metadata::ProtocolMetadataV2 {
use crate::protocol::metadata::{DevelopmentState, ProtocolMetadataV2};
ProtocolMetadataV2::builder()
.state(DevelopmentState::Experimental)
.implementation("Raw TCP with Telnet option negotiation")
.llm_control("Send commands and respond to server output, automatic option negotiation")
.e2e_testing("telnetd or netcat as test server")
.build()
}
fn description(&self) -> &'static str {
"Telnet client for connecting to Telnet servers and executing commands"
}
fn example_prompt(&self) -> &'static str {
"Connect to Telnet at localhost:23 and run 'whoami' command"
}
fn group_name(&self) -> &'static str {
"Infrastructure"
}
fn get_startup_examples(&self) -> crate::llm::actions::StartupExamples {
use crate::llm::actions::StartupExamples;
use serde_json::json;
StartupExamples::new(
json!({
"type": "open_client",
"remote_addr": "localhost:23",
"base_stack": "telnet",
"instruction": "Login and execute 'whoami' command"
}),
json!({
"type": "open_client",
"remote_addr": "localhost:23",
"base_stack": "telnet",
"event_handlers": [{
"event_pattern": "telnet_data_received",
"handler": {
"type": "script",
"language": "python",
"code": "<telnet_client_handler>"
}
}]
}),
json!({
"type": "open_client",
"remote_addr": "localhost:23",
"base_stack": "telnet",
"event_handlers": [
{
"event_pattern": "telnet_connected",
"handler": {
"type": "static",
"actions": [{
"type": "send_command",
"command": "whoami"
}]
}
},
{
"event_pattern": "telnet_data_received",
"handler": {
"type": "static",
"actions": [{
"type": "disconnect"
}]
}
}
]
}),
)
}
}
impl Client for TelnetClientProtocol {
fn connect(
&self,
ctx: crate::protocol::ConnectContext,
) -> std::pin::Pin<
Box<dyn std::future::Future<Output = anyhow::Result<std::net::SocketAddr>> + Send>,
> {
Box::pin(async move {
use crate::client::telnet::TelnetClient;
TelnetClient::connect_with_llm_actions(
ctx.remote_addr,
ctx.llm_client,
ctx.state,
ctx.status_tx,
ctx.client_id,
)
.await
})
}
fn execute_action(&self, action: serde_json::Value) -> Result<ClientActionResult> {
let action_type = action
.get("type")
.and_then(|v| v.as_str())
.context("Missing 'type' field in action")?;
match action_type {
"send_command" => {
let command = action
.get("command")
.and_then(|v| v.as_str())
.context("Missing 'command' field")?;
let data = format!("{}\r\n", command).into_bytes();
Ok(ClientActionResult::SendData(data))
}
"send_text" => {
let text = action
.get("text")
.and_then(|v| v.as_str())
.context("Missing 'text' field")?;
let data = text.as_bytes().to_vec();
Ok(ClientActionResult::SendData(data))
}
"disconnect" => Ok(ClientActionResult::Disconnect),
"wait_for_more" => Ok(ClientActionResult::WaitForMore),
_ => Err(anyhow::anyhow!(
"Unknown Telnet client action: {}",
action_type
)),
}
}
}