Skip to content

Commit

Permalink
Push code
Browse files Browse the repository at this point in the history
  • Loading branch information
limitedeternity committed Aug 7, 2020
1 parent 80e168a commit fede3b9
Show file tree
Hide file tree
Showing 4 changed files with 350 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
276 changes: 276 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "squidclient"
version = "0.1.0"
authors = ["Marise Hayashi <vyatheslav2707@mail.ru>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = "3.0.0-beta.1"
base64 = "0.12.3"
62 changes: 62 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use std::io::{BufRead, BufReader, Write};
use std::net::TcpStream;
use std::time::Duration;
use clap::{crate_version, crate_authors, Arg, App};
use base64::{encode};

fn main() {
let matches = App::new("squidclient")
.version(crate_version!())
.author(crate_authors!())
.about("Executes Squid proxy cachemgr commands")
.arg(Arg::with_name("command")
.about("A command to execute")
.required(true))
.arg(Arg::with_name("host")
.about("Squid proxy address")
.short('h')
.long("host")
.required(true)
.takes_value(true))
.arg(Arg::with_name("port")
.about("Squid proxy port")
.short('p')
.long("port")
.default_value("3128")
.takes_value(true))
.arg(Arg::with_name("with_password")
.about("Squid proxy cachemgr password")
.short('w')
.long("with_password")
.takes_value(true))
.get_matches();

let mut stream = TcpStream::connect(format!("{}:{}", matches.value_of("host").unwrap(), matches.value_of("port").unwrap())).expect("Failed to connect to server");
stream.set_read_timeout(Some(Duration::new(1, 0))).expect("Failed to set read timeout");

let get = format!("GET cache_object://{}:{}/{} HTTP/1.1\r\n",
matches.value_of("host").unwrap(),
matches.value_of("port").unwrap(),
matches.value_of("command").unwrap());

let agent = format!("User-Agent: squidclient/{}\r\n", crate_version!());
let host = format!("Host: {}\r\n", matches.value_of("host").unwrap());
let accept = "Accept: */*\r\n";

let auth = if matches.is_present("with_password") {
format!("Authorization: Basic {}\r\n", encode([":", matches.value_of("with_password").unwrap()].concat()))
} else {
"\r\n".to_string()
};

stream.write(format!("{}{}{}{}{}\r\n", get, agent, host, accept, auth).as_bytes()).expect("Command write failed");

let reader = BufReader::new(stream);
for line in reader.lines() {
if let Ok(s) = line {
println!("{}", s);
} else {
break;
}
}
}

0 comments on commit fede3b9

Please sign in to comment.