Skip to content

Commit 60f994f

Browse files
committed
fix clap bugs
1 parent 199938c commit 60f994f

File tree

4 files changed

+25
-28
lines changed

4 files changed

+25
-28
lines changed

src/config.rs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use std::collections::HashMap;
2-
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
3-
use std::time::{Duration, Instant};
4-
51
use clap::Clap;
62
use crypto::digest::Digest;
73
use crypto::sha2::Sha224;
4+
use std::collections::HashMap;
5+
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
6+
use std::time::{Duration, Instant};
87
use trust_dns_resolver::Resolver;
98

109
pub struct DnsEntry {
@@ -21,8 +20,8 @@ pub struct Opts {
2120
pub log_file: Option<String>,
2221
#[clap(short = "a", long, help = "listen address for server")]
2322
pub local_addr: String,
24-
#[clap(required = true, short, long, help = "passwords for negotiation")]
25-
password: Vec<String>,
23+
#[clap(short, long, help = "passwords for negotiation")]
24+
password: String,
2625
#[clap(short = "L", long, default_value = "2", help = "log level, 0 for trace, 1 for debug, 2 for info, 3 for warning, 4 for error, 5 for off")]
2726
pub log_level: u8,
2827
#[clap(short, long, default_value = "255", help = "set marker used by tproxy")]
@@ -32,7 +31,7 @@ pub struct Opts {
3231
#[clap(skip)]
3332
dns_cache_duration: Duration,
3433
#[clap(skip)]
35-
sha_pass: Vec<String>,
34+
sha_pass: String,
3635
#[clap(skip)]
3736
pub pass_len: usize,
3837
#[clap(skip)]
@@ -129,28 +128,24 @@ impl Opts {
129128

130129
fn digest_pass(&mut self) {
131130
let mut encoder = Sha224::new();
132-
self.sha_pass.clear();
133-
for pass in &self.password {
134-
encoder.reset();
135-
encoder.input(pass.as_bytes());
136-
let result = encoder.result_str();
137-
self.pass_len = result.len();
138-
log::info!("sha224({}) = {}, length = {}", pass, result, self.pass_len);
139-
self.sha_pass.push(result);
140-
}
131+
encoder.reset();
132+
encoder.input(self.password.as_bytes());
133+
let result = encoder.result_str();
134+
self.pass_len = result.len();
135+
log::info!("sha224({}) = {}, length = {}", self.password, result, self.pass_len);
136+
self.sha_pass = result;
141137
}
142138

143139
pub fn check_pass(&self, pass: &str) -> Option<&String> {
144-
for i in 0..self.sha_pass.len() {
145-
if self.sha_pass[i].eq(pass) {
146-
return Some(&self.password[i]);
147-
}
140+
if self.sha_pass.eq(pass) {
141+
Some(&self.password)
142+
} else {
143+
None
148144
}
149-
None
150145
}
151146

152147
pub fn get_pass(&self) -> &String {
153-
self.sha_pass.get(0).unwrap()
148+
&self.sha_pass
154149
}
155150

156151
pub fn update_dns(&mut self, domain: String, address: IpAddr) {

src/main.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use clap::Clap;
1+
use clap::{App, AppSettings, FromArgMatches};
2+
use clap::derive::IntoApp;
23

34
use crate::config::{Mode, Opts};
45

@@ -10,7 +11,10 @@ mod proxy;
1011
mod session;
1112

1213
fn main() {
13-
let mut opts = Opts::parse();
14+
let mut app: App = <Opts as IntoApp>::into_app();
15+
app.set(AppSettings::AllowExternalSubcommands);
16+
let mut opts = <Opts as FromArgMatches>::from_arg_matches(&app.get_matches());
17+
1418
config::setup_logger(&opts.log_file, opts.log_level);
1519
opts.setup();
1620
match opts.mode {

src/proto.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
2-
31
use bytes::{BufMut, BytesMut};
2+
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
43

54
use crate::config::Opts;
65

src/session.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use std::io::{Error, ErrorKind, Read, Result, Write};
2-
31
use bytes::{Buf, BytesMut};
2+
use std::io::{Error, ErrorKind, Read, Result, Write};
43

54
pub struct TcpSession {
65
pub recv_buf: BytesMut,

0 commit comments

Comments
 (0)