Skip to content

Commit 231705c

Browse files
author
cht
committed
add information pad
1 parent abb5167 commit 231705c

2 files changed

Lines changed: 190 additions & 9 deletions

File tree

src/main.rs

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ struct App {
4444
index: Option<usize>,
4545
stateoflist: bool,
4646
show_popup: bool,
47+
informations: Vec<spider::Information>,
4748
}
4849
impl App {
4950
fn next(&mut self) {
@@ -90,6 +91,7 @@ impl Default for App {
9091
index: None,
9192
stateoflist: false,
9293
show_popup: false,
94+
informations: Vec::new(),
9395
}
9496
}
9597
}
@@ -145,10 +147,15 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
145147
let input = vec![app.input.clone()];
146148
let get_list = spider::get_the_key(input.clone());
147149
if let Ok(list) = get_list {
148-
if !list.is_empty() {
150+
if !list[0].is_empty() {
149151
app.messages = list[0].clone();
150152
app.stateoflist = true;
151153
app.state.select(Some(0));
154+
app.index = Some(0);
155+
for alist in &list[0] {
156+
app.informations
157+
.push(spider::Information::new(alist.to_string()));
158+
}
152159
}
153160
}
154161
//app.messages.push(app.input.drain(..).collect());
@@ -184,13 +191,12 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
184191
app.input_mode = InputMode::Normal;
185192
}
186193
}
187-
InputMode::Popup => match key.code {
188-
KeyCode::Char('q') => {
194+
InputMode::Popup => {
195+
if let KeyCode::Char('q') = key.code {
189196
app.input_mode = InputMode::Select;
190197
app.show_popup = false;
191198
}
192-
_ => {}
193-
},
199+
}
194200
}
195201
}
196202
}
@@ -272,6 +278,12 @@ fn ui<B: Backend>(f: &mut Frame<B>, app: &mut App) {
272278
}
273279
}
274280

281+
// Bottom two inner blocks
282+
let bottom_chunks = Layout::default()
283+
.direction(Direction::Horizontal)
284+
.constraints([Constraint::Percentage(40), Constraint::Percentage(60)].as_ref())
285+
.split(chunks[2]);
286+
275287
let messages: Vec<ListItem> = app
276288
.messages
277289
.iter()
@@ -289,11 +301,30 @@ fn ui<B: Backend>(f: &mut Frame<B>, app: &mut App) {
289301
.add_modifier(Modifier::BOLD),
290302
)
291303
.highlight_symbol(">> ");
292-
//let messages =
293-
// List::new(messages).block(Block::default().borders(Borders::ALL).title("Messages"));
294-
f.render_stateful_widget(messages, chunks[2], &mut app.state);
304+
// popup wiget
305+
f.render_stateful_widget(messages, bottom_chunks[0], &mut app.state);
306+
//let block : Box<dyn Widget> = {
307+
if let Some(a) = app.index {
308+
let list = app.informations[a].information_to_list();
309+
let messages: Vec<ListItem> = list
310+
.iter()
311+
.map(|infom| {
312+
let content = vec![Spans::from(Span::raw(infom))];
313+
ListItem::new(content)
314+
})
315+
.collect();
316+
let block =
317+
List::new(messages).block(Block::default().borders(Borders::ALL).title("Messages"));
318+
f.render_widget(block, bottom_chunks[1]);
319+
} else {
320+
let block = Block::default().title("With borders").borders(Borders::ALL);
321+
f.render_widget(block, bottom_chunks[1]);
322+
}
323+
//};
324+
//f.render_widget(*block, bottom_chunks[0]);
325+
295326
if app.show_popup {
296-
let block = Block::default().title("Popup").borders(Borders::ALL);
327+
let block = Block::default().title("About port").borders(Borders::ALL);
297328
let area = centered_rect(60, 20, f.size());
298329
f.render_widget(Clear, area); //this clears out the background
299330
f.render_widget(block, area);

src/spider.rs

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//extern crate base64;
22
use reqwest::Result;
3+
use serde_json::Value;
34
pub fn ascii_to_char(code: u8) -> char {
45
std::char::from_u32(code as u32).unwrap_or('_')
56
}
@@ -22,6 +23,14 @@ fn ascii_to_string(code: Vec<u8>) -> Vec<String> {
2223
}
2324
test
2425
}
26+
27+
fn ascii_to_string2(code: Vec<u8>) -> String {
28+
let mut test: String = String::new();
29+
for cor in code.into_iter() {
30+
test.push(ascii_to_char(cor));
31+
}
32+
test
33+
}
2534
pub fn get_the_key(paths: Vec<String>) -> Result<Vec<Vec<String>>> {
2635
let mut output: Vec<Vec<String>> = vec![];
2736
for apath in paths {
@@ -38,3 +47,144 @@ pub fn get_the_key(paths: Vec<String>) -> Result<Vec<Vec<String>>> {
3847
}
3948
Ok(output)
4049
}
50+
fn remove_quotation(input: String) -> String {
51+
let length = input.len();
52+
(&input[1..length - 1]).to_string()
53+
}
54+
enum Tcp {
55+
Ss,
56+
V2,
57+
}
58+
#[derive(Clone)]
59+
pub struct Information {
60+
pub func: String,
61+
pub urls: String,
62+
pub add: String,
63+
pub aid: String,
64+
pub host: String,
65+
pub id: String,
66+
pub net: String,
67+
pub path: String,
68+
pub port: String,
69+
pub ps: String,
70+
pub tls: String,
71+
pub typpe: String,
72+
}
73+
impl Information {
74+
pub fn information_to_list(&self) -> Vec<String> {
75+
let mut output = vec![];
76+
output.push(format!("link: {}", self.get_the_link()));
77+
output.push(format!("func: {}", self.func));
78+
output.push(format!("urls: {}", self.urls));
79+
output.push(format!("add: {}", self.add));
80+
output.push(format!("aid: {}", self.aid));
81+
output.push(format!("tls: {}", self.tls));
82+
output.push(format!("type: {}", self.typpe));
83+
output
84+
}
85+
fn get_the_link(&self) -> String {
86+
let mut temp = String::new();
87+
if self.func == *"\"vmess\"" {
88+
temp.push_str(&format!(
89+
"vmess://{}:{}-{}@{}:{}/#{}",
90+
&remove_quotation(self.net.clone()),
91+
&remove_quotation(self.id.clone()),
92+
&remove_quotation(self.aid.clone()),
93+
&remove_quotation(self.add.clone()),
94+
&remove_quotation(self.port.clone()),
95+
&remove_quotation(self.ps.clone())
96+
))
97+
} else {
98+
temp = self.urls.clone();
99+
}
100+
temp
101+
}
102+
pub fn new(url: String) -> Self {
103+
let aurl: Vec<char> = url.chars().collect();
104+
let url_type = {
105+
if aurl[0] == 's' {
106+
Tcp::Ss
107+
} else {
108+
Tcp::V2
109+
}
110+
};
111+
match url_type {
112+
Tcp::Ss => {
113+
// 预处理,去除ss://
114+
let newurl = (&url[5..]).to_string();
115+
// 用@分割字符串
116+
let first: Vec<&str> = newurl.split('@').collect();
117+
// 传来的节点补全最后一位解析
118+
let header = first[0].to_string() + "=";
119+
// 解析,解析结果会返回一个function和密码,中间通过分号分割
120+
let header2 = ascii_to_string2(base64::decode(header.as_bytes()).unwrap());
121+
// 通过分号切开两个内容
122+
let header3: Vec<&str> = header2.split(':').collect();
123+
let net = format!("\"{}\"", header3[0]);
124+
let id = format!("\"{}\"", header3[1]);
125+
126+
let first_temp = first[1].to_string();
127+
let second: Vec<&str> = first_temp.split('#').collect();
128+
let ps0 = urlencoding::decode(second[1]).unwrap();
129+
let ps = format!("\"{}\"", ps0);
130+
131+
let second_temp = second[0].to_string();
132+
let third: Vec<&str> = second_temp.split(':').collect();
133+
let add = format!("\"{}\"", third[0]);
134+
let port = format!("\"{}\"", third[1]);
135+
Information {
136+
urls: url,
137+
func: "\"ss\"".to_string(),
138+
add,
139+
aid: "\"unknown\"".to_string(),
140+
host: "\"\"".to_string(),
141+
id,
142+
net,
143+
path: "\"unknown\"".to_string(),
144+
port,
145+
ps,
146+
tls: "\"unknown\"".to_string(),
147+
typpe: "\"unknown\"".to_string(),
148+
}
149+
}
150+
Tcp::V2 => {
151+
let newurl = &url[8..];
152+
let json = ascii_to_string2(base64::decode(newurl.to_string().as_bytes()).unwrap());
153+
let v: serde_json::Result<Value> = serde_json::from_str(json.as_str());
154+
match v {
155+
Ok(input) => {
156+
Information {
157+
//company : input["add"].to_string(),
158+
urls: url,
159+
func: "\"vmess\"".to_string(),
160+
add: input["add"].to_string(),
161+
aid: input["aid"].to_string(),
162+
host: input["host"].to_string(),
163+
id: input["id"].to_string(),
164+
net: input["net"].to_string(),
165+
path: input["path"].to_string(),
166+
port: input["port"].to_string(),
167+
ps: input["ps"].to_string(),
168+
tls: input["tls"].to_string(),
169+
typpe: input["type"].to_string(),
170+
}
171+
}
172+
Err(_) => Information {
173+
urls: url,
174+
func: "\"vmess\"".to_string(),
175+
add: "\"unknown\"".to_string(),
176+
aid: "\"unknown\"".to_string(),
177+
host: "\"unknown\"".to_string(),
178+
id: "\"unknown\"".to_string(),
179+
net: "\"unknown\"".to_string(),
180+
path: "\"unknown\"".to_string(),
181+
port: "\"unknown\"".to_string(),
182+
ps: "\"unknown\"".to_string(),
183+
tls: "\"unknown\"".to_string(),
184+
typpe: "\"unknown\"".to_string(),
185+
},
186+
}
187+
}
188+
}
189+
}
190+
}

0 commit comments

Comments
 (0)