feat: add docker registry api support

This commit is contained in:
椰子 2023-11-11 02:32:43 +08:00
parent 67eca5685b
commit 2b50fdcab7
6 changed files with 196 additions and 100 deletions

22
Cargo.lock generated
View File

@ -201,7 +201,6 @@ dependencies = [
"reqwest",
"serde",
"serde_json",
"tokio",
]
[[package]]
@ -274,6 +273,12 @@ version = "0.3.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb1d22c66e66d9d72e1758f0bd7d4fd0bee04cad842ee34587d68c07e45d088c"
[[package]]
name = "futures-io"
version = "0.3.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8bf34a163b5c4c52d0478a4d757da8fb65cabef42ba90515efee0f6f9fa45aaa"
[[package]]
name = "futures-sink"
version = "0.3.29"
@ -293,9 +298,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a19526d624e703a3179b3d322efec918b6246ea0fa51d41124525f00f1cc8104"
dependencies = [
"futures-core",
"futures-io",
"futures-task",
"memchr",
"pin-project-lite",
"pin-utils",
"slab",
]
[[package]]
@ -893,21 +901,9 @@ dependencies = [
"num_cpus",
"pin-project-lite",
"socket2 0.5.5",
"tokio-macros",
"windows-sys",
]
[[package]]
name = "tokio-macros"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tokio-native-tls"
version = "0.3.1"

View File

@ -6,8 +6,7 @@ authors = ["Yezzi Hsueh"]
license = "MIT"
[dependencies]
reqwest = "0.11.22"
tokio = { version = "1.34.0", features = ["macros", "rt-multi-thread", "rt"] }
reqwest = { version = "0.11.22", features = ["blocking"] }
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
clap = { version = "4.4.7", features = ["derive"] }

View File

@ -1,14 +1,14 @@
use std::error::Error;
use reqwest;
use clap::{arg, Parser};
use docker_tags::{DockerResponse, DockerResult};
//use docker_tags::docker::DockerHubTagsFetcher;
use docker_tags::{DockerTags, QueryArgs};
use docker_tags::registry::RegistryTagsFetcher;
#[derive(Parser, Debug)]
#[command(name = "dockertags", author, version, about = "List all tags for a Docker image on a remote registry", long_about = "List all tags for a Docker image on a remote registry")]
struct Args {
/// docker image name
#[arg(value_name = "REPOSITORY")]
image: Option<String>,
repository: Option<String>,
/// docker image architecture
#[arg(short, long, value_name = "ARCHITECTURE")]
arch: Option<String>,
@ -17,51 +17,19 @@ struct Args {
name: Option<String>,
}
async fn get_images(namespace: &str, repository: &str) -> Result<Vec<DockerResult>, Box<dyn Error>> {
let page = 0;
let page_size = 1000;
let mut url = format!("https://hub.docker.com/v2/namespaces/{namespace}/repositories/{repository}/tags?page={page}&page_size={page_size}");
let mut results: Vec<DockerResult> = Vec::new();
loop {
let text = reqwest::get(url).await?
.text().await?;
let mut response: DockerResponse = serde_json::from_str(&text).unwrap();
results.append(&mut response.results);
url = match response.next {
None => { break; }
Some(next) => { next }
};
}
// println!("{:?}", results);
Ok(results)
}
#[tokio::main]
async fn main() {
fn main() {
let args = Args::parse();
let mut namespace = String::from("library");
let mut repository = args.image.unwrap();
let split = repository.split('/').collect::<Vec<&str>>();
if split.len() > 1 {
namespace = split.get(0).unwrap().to_string();
repository = split.get(1).unwrap().to_string();
}
let results = get_images(&namespace, &repository).await.unwrap();
let mut filtered = results.into_iter()
.filter(|x| !x.images.is_empty())
.collect::<Vec<DockerResult>>();
if args.name.is_some() {
let pat = args.name.unwrap();
filtered = filtered.into_iter().filter(|x| x.name.clone().unwrap().contains(&pat))
.collect::<Vec<DockerResult>>();
let query_args = QueryArgs {
repository: args.repository,
name: args.name,
arch: args.arch,
};
if args.arch.is_some() {
let pat = args.arch.unwrap();
filtered = filtered.into_iter().filter(|x| x.images.get(0).as_ref().unwrap().architecture.clone().unwrap().contains(&pat))
.collect::<Vec<DockerResult>>();
};
for result in filtered {
println!("{}", result.name.unwrap())
// let docker_tags_fetcher = DockerHubTagsFetcher {};
let docker_tags_fetcher = RegistryTagsFetcher {};
let tags = docker_tags_fetcher.get_tags(&query_args);
for tag in tags {
println!("{}", &tag)
}
}

95
src/docker.rs Normal file
View File

@ -0,0 +1,95 @@
use serde::{Deserialize, Serialize};
use crate::{DockerTags, QueryArgs};
#[derive(Serialize, Deserialize)]
struct DockerResult {
content_type: Option<String>,
creator: u64,
full_size: u64,
id: u64,
last_updated: Option<String>,
last_updater: u64,
last_updater_username: Option<String>,
media_type: Option<String>,
name: Option<String>,
repository: u32,
tag_last_pulled: Option<String>,
tag_last_pushed: Option<String>,
tag_status: Option<String>,
v2: bool,
images: Vec<DockerImage>,
}
#[derive(Serialize, Deserialize)]
struct DockerImage {
architecture: Option<String>,
digest: Option<String>,
features: Option<String>,
last_pulled: Option<String>,
last_pushed: Option<String>,
os: Option<String>,
os_features: Option<String>,
os_version: Option<String>,
size: u64,
status: Option<String>,
variant: Option<String>,
}
#[derive(Serialize, Deserialize)]
struct DockerResponse {
count: u32,
next: Option<String>,
previous: Option<String>,
results: Vec<DockerResult>,
}
pub struct DockerHubTagsFetcher {}
impl DockerTags for DockerHubTagsFetcher {
fn get_tags(&self, args: &QueryArgs) -> Vec<String> {
let page = 0;
let page_size = 1000;
let mut namespace = "library";
// let mut repository = args.repository.clone().unwrap();
let repository = match &args.repository {
Some(s) => {
let vec = s.split('/')
.into_iter()
.collect::<Vec<&str>>();
namespace = vec[0];
vec[1]
}
None => { "" }
};
let mut url = format!("https://hub.docker.com/v2/namespaces/{namespace}/repositories/{repository}/tags?page={page}&page_size={page_size}");
let mut results: Vec<DockerResult> = Vec::new();
loop {
let text = reqwest::blocking::get(url).unwrap().text().unwrap();
let mut response: DockerResponse = serde_json::from_str(&text).unwrap();
results.append(&mut response.results);
url = match response.next {
None => { break; }
Some(next) => { next }
};
}
let mut filtered = results.into_iter()
.filter(|x| !x.images.is_empty())
.collect::<Vec<DockerResult>>();
if args.name.is_some() {
let pat = args.name.clone().unwrap();
filtered = filtered.into_iter().filter(|x| x.name.clone().unwrap().contains(&pat))
.collect::<Vec<DockerResult>>();
};
if args.arch.is_some() {
let pat = args.arch.clone().unwrap();
filtered = filtered.into_iter().filter(|x| x.images.get(0).as_ref().unwrap().architecture.clone().unwrap().contains(&pat))
.collect::<Vec<DockerResult>>();
};
let mut results = Vec::<String>::new();
for x in &filtered {
results.push(x.name.clone().unwrap());
}
results
}
}

View File

@ -1,43 +1,14 @@
use serde::{Deserialize, Serialize};
pub mod registry;
pub mod docker;
#[derive(Serialize, Deserialize, Debug)]
pub struct DockerResult {
pub content_type: Option<String>,
pub creator: u64,
pub full_size: u64,
pub id: u64,
pub last_updated: Option<String>,
pub last_updater: u64,
pub last_updater_username: Option<String>,
pub media_type: Option<String>,
/// query args
pub struct QueryArgs {
pub repository: Option<String>,
pub name: Option<String>,
pub repository: u32,
pub tag_last_pulled: Option<String>,
pub tag_last_pushed: Option<String>,
pub tag_status: Option<String>,
pub v2: bool,
pub images: Vec<DockerImage>,
pub arch: Option<String>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct DockerImage {
pub architecture: Option<String>,
pub digest: Option<String>,
pub features: Option<String>,
pub last_pulled: Option<String>,
pub last_pushed: Option<String>,
pub os: Option<String>,
pub os_features: Option<String>,
pub os_version: Option<String>,
pub size: u64,
pub status: Option<String>,
pub variant: Option<String>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct DockerResponse {
pub count: u32,
pub next: Option<String>,
pub previous: Option<String>,
pub results: Vec<DockerResult>,
pub trait DockerTags {
/// query tags
fn get_tags(&self, args: &QueryArgs) -> Vec<String>;
}

67
src/registry.rs Normal file
View File

@ -0,0 +1,67 @@
use serde::{Deserialize, Serialize};
use crate::{DockerTags, QueryArgs};
/// registry api response struct
#[derive(Serialize, Deserialize)]
struct RegistryTagsResponse {
name: Option<String>,
tags: Option<Vec<String>>,
}
#[derive(Serialize, Deserialize)]
struct RegistryTokenResponse {
token: Option<String>,
access_token: Option<String>,
expires_in: u32,
issued_at: Option<String>,
}
pub struct RegistryTagsFetcher {}
impl DockerTags for RegistryTagsFetcher {
fn get_tags(&self, args: &QueryArgs) -> Vec<String> {
let mut namespace = "library";
let repository = match &args.repository {
Some(s) => {
let vec = s.split('/')
.into_iter()
.collect::<Vec<&str>>();
namespace = vec[0];
vec[1]
}
None => { "" }
};
let token = get_token(namespace, repository).unwrap();
let url = format!("https://registry-1.docker.io/v2/{namespace}/{repository}/tags/list");
let client = reqwest::blocking::Client::new();
let response = client.get(url)
.header("Authorization", format!("Bearer {token}"))
.send();
let mut results = Vec::<String>::new();
if let Ok(r) = response {
let json: RegistryTagsResponse = serde_json::from_str(r.text().unwrap().as_str()).unwrap();
results.append(&mut json.tags.unwrap());
};
results
}
}
fn get_token(namespace: &str, repository: &str) -> reqwest::Result<String> {
let username = "simaek";
let password = "snow@PX1314";
let service = "registry.docker.io";
let scope = format!("repository:{namespace}/{repository}:pull");
let auth = format!("https://auth.docker.io/token?service={service}&scope={scope}");
let client = reqwest::blocking::Client::new();
let response = client.get(auth)
.basic_auth(username, Some(password))
.send();
match response {
Ok(r) => {
let json: RegistryTokenResponse = serde_json::from_str(r.text().unwrap().as_str()).unwrap();
Ok(json.token.unwrap())
}
Err(e) => { Err(e) }
}
}