Stabilized Consul integration with HTTP api

This commit is contained in:
Ara Sadoyan
2025-04-08 10:20:02 +02:00
parent b2823508b7
commit 788c618a3d
6 changed files with 94 additions and 35 deletions

View File

@@ -1,18 +1,19 @@
use crate::utils::parceyaml::load_configuration;
use crate::utils::parceyaml::{load_configuration, ServiceMapping};
use crate::utils::tools::{clone_dashmap_into, compare_dashmaps, Headers, UpstreamsDashMap};
use dashmap::DashMap;
use futures::channel::mpsc::Sender;
use futures::SinkExt;
use hickory_client::client::{Client, ClientHandle};
use hickory_client::proto::rr::{DNSClass, Name, RecordType};
use hickory_client::proto::runtime::TokioRuntimeProvider;
use hickory_client::proto::tcp::TcpClientStream;
use log::info;
// use hickory_client::client::{Client, ClientHandle};
// use hickory_client::proto::rr::{DNSClass, Name, RecordType};
// use hickory_client::proto::runtime::TokioRuntimeProvider;
// use hickory_client::proto::tcp::TcpClientStream;
use log::{info, warn};
use pingora::prelude::sleep;
use rand::Rng;
// use std::str::FromStr;
use reqwest::header::{HeaderMap, HeaderValue};
use serde::Deserialize;
use std::collections::HashMap;
use std::str::FromStr;
use std::sync::atomic::AtomicUsize;
use std::time::Duration;
@@ -51,14 +52,17 @@ pub async fn start(fp: String, mut toreturn: Sender<(UpstreamsDashMap, Headers)>
let servers = consul.servers.unwrap();
info!("Consul Servers => {:?}", servers);
let end = servers.len();
loop {
// println!(" ==> {:?}", consul.services);
let num = rand::thread_rng().gen_range(1..end);
headers.clear();
for (k, v) in config.headers.clone() {
headers.insert(k.to_string(), v);
}
let consul_data = servers.get(num).unwrap().to_string();
let upstreams = http_request(consul_data, consul.whitelist.clone());
// let upstreams = http_request(consul_data, consul.whitelist.clone());
let upstreams = consul_request(consul_data, consul.services.clone(), consul.token.clone());
match upstreams.await {
Some(upstreams) => {
if !compare_dashmaps(&upstreams, &prev_upstreams) {
@@ -78,6 +82,7 @@ pub async fn start(fp: String, mut toreturn: Sender<(UpstreamsDashMap, Headers)>
}
}
/*
async fn http_request(url: String, whitelist: Option<Vec<String>>) -> Option<UpstreamsDashMap> {
let client = reqwest::Client::new();
let to = Duration::from_secs(1);
@@ -132,11 +137,38 @@ async fn http_request(url: String, whitelist: Option<Vec<String>>) -> Option<Ups
}
}
}
*/
async fn consul_request(url: String, whitelist: Option<Vec<ServiceMapping>>, token: Option<String>) -> Option<UpstreamsDashMap> {
let upstreams = UpstreamsDashMap::new();
let ss = url.clone() + "/v1/catalog/service/";
match whitelist {
Some(whitelist) => {
for k in whitelist.iter() {
let pref: String = ss.clone() + &k.real;
let list = get_by_http(pref.clone(), token.clone()).await;
match list {
Some(list) => {
upstreams.insert(k.proxy.clone(), list);
}
None => {
warn!("Whitelist not found for {}", k.proxy);
}
}
}
}
None => {}
}
Some(upstreams)
}
async fn get_by_http(url: String) -> Option<DashMap<String, (Vec<(String, u16, bool)>, AtomicUsize)>> {
async fn get_by_http(url: String, token: Option<String>) -> Option<DashMap<String, (Vec<(String, u16, bool)>, AtomicUsize)>> {
let client = reqwest::Client::new();
let mut headers = HeaderMap::new();
if let Some(token) = token {
headers.insert("X-Consul-Token", HeaderValue::from_str(&token).unwrap());
}
let to = Duration::from_secs(1);
let u = client.get(url.clone()).timeout(to).send();
let u = client.get(url).timeout(to).send();
let mut values = Vec::new();
let upstreams: DashMap<String, (Vec<(String, u16, bool)>, AtomicUsize)> = DashMap::new();
match u.await {
@@ -160,19 +192,19 @@ async fn get_by_http(url: String) -> Option<DashMap<String, (Vec<(String, u16, b
Some(upstreams)
}
#[allow(dead_code)]
async fn get_by_dns() {
let (stream, sender) = TcpClientStream::new(([192, 168, 22, 1], 53).into(), None, None, TokioRuntimeProvider::new());
let client = Client::new(stream, sender, None);
let (mut client, bg) = client.await.expect("connection failed");
tokio::spawn(bg);
let query = client.query(Name::from_str("_frontend-dev-frontend-srv._tcp.service.consul.").unwrap(), DNSClass::IN, RecordType::SRV);
// let query = client.query(Name::from_str("matyan.org.").unwrap(), DNSClass::IN, RecordType::A);
let response = query.await.unwrap();
for t in response.answers().iter() {
for y in t.data().as_srv().iter() {
println!(" DNS ==> {:?} : {:?}", y.target().to_utf8(), y.port());
}
}
}
// #[allow(dead_code)]
// async fn get_by_dns() {
// let (stream, sender) = TcpClientStream::new(([192, 168, 22, 1], 53).into(), None, None, TokioRuntimeProvider::new());
// let client = Client::new(stream, sender, None);
// let (mut client, bg) = client.await.expect("connection failed");
// tokio::spawn(bg);
// let query = client.query(Name::from_str("_frontend-dev-frontend-srv._tcp.service.consul.").unwrap(), DNSClass::IN, RecordType::SRV);
// // let query = client.query(Name::from_str("matyan.org.").unwrap(), DNSClass::IN, RecordType::A);
// let response = query.await.unwrap();
//
// for t in response.answers().iter() {
// for y in t.data().as_srv().iter() {
// println!(" DNS ==> {:?} : {:?}", y.target().to_utf8(), y.port());
// }
// }
// }