start reading future conf conexion
This commit is contained in:
parent
038faae0e4
commit
bb6c062be4
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -12,7 +12,6 @@ checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f"
|
|||||||
name = "minecraft_proxy"
|
name = "minecraft_proxy"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"linked-hash-map",
|
|
||||||
"yaml-rust",
|
"yaml-rust",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -3,27 +3,48 @@ use std::fs::File;
|
|||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
pub mod server_conf;
|
||||||
|
|
||||||
static FILE: &str = "mrprox.conf";
|
static FILE: &str = "mrprox.conf";
|
||||||
|
|
||||||
pub struct Servers{
|
pub struct Config{
|
||||||
l_servers : HashMap<String, String>,
|
l_servers : HashMap<String, String>,
|
||||||
|
file: Vec<yaml::Yaml>,
|
||||||
|
port: String,
|
||||||
|
port_conf: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Servers {
|
impl Config {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
|
let mut file = File::open(&FILE).unwrap();
|
||||||
|
let mut s = String::new();
|
||||||
|
file.read_to_string(&mut s).unwrap();
|
||||||
|
let yam = yaml::YamlLoader::load_from_str(&s).unwrap();
|
||||||
Self{
|
Self{
|
||||||
l_servers: Self::get_servers(),
|
l_servers: Self::get_servers(&yam),
|
||||||
|
port: Self::get_port_f(&yam),
|
||||||
|
port_conf: Self::get_conf_port_f(&yam),
|
||||||
|
file: yam,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_servers() -> HashMap<String, String> {
|
fn get_port_f(file: &Vec<yaml::Yaml>) -> String {
|
||||||
let mut f = File::open(&FILE).unwrap();
|
match file[0]["port"].as_str() {
|
||||||
let mut s = String::new();
|
Some(h) => String::from(h),
|
||||||
|
_ => String::from("25565"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_conf_port_f(file: &Vec<yaml::Yaml>) -> String {
|
||||||
|
match file[0]["port_conf"].as_str() {
|
||||||
|
Some(h) => String::from(h),
|
||||||
|
_ => String::from("25565"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_servers(file: &Vec<yaml::Yaml>) -> HashMap<String, String> {
|
||||||
let mut ret = HashMap::new();
|
let mut ret = HashMap::new();
|
||||||
f.read_to_string(&mut s).unwrap();
|
let docs2 = match file[0]["servers"] {
|
||||||
let docs = yaml::YamlLoader::load_from_str(&s).unwrap();
|
|
||||||
let docs2 = match &docs[0]["servers"] {
|
|
||||||
yaml::Yaml::Hash(ref h) => h,
|
yaml::Yaml::Hash(ref h) => h,
|
||||||
_ => return ret,
|
_ => return ret,
|
||||||
};
|
};
|
||||||
@ -57,4 +78,12 @@ impl Servers {
|
|||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_port(&self) -> &String{
|
||||||
|
&self.port
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_port_conf(&self) -> &String{
|
||||||
|
&self.port_conf
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
36
src/conf/server_conf.rs
Normal file
36
src/conf/server_conf.rs
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
use std::net::{TcpListener, TcpStream};
|
||||||
|
use std::io::prelude::*;
|
||||||
|
use crate::conf;
|
||||||
|
use std::sync::{Arc, RwLock};
|
||||||
|
|
||||||
|
pub fn server_conf(conf: Arc<RwLock<conf::Config>>){
|
||||||
|
let listener = TcpListener::bind(String::from("0.0.0.0:") +
|
||||||
|
conf.read().unwrap().get_port_conf()).unwrap();
|
||||||
|
|
||||||
|
|
||||||
|
for stream in listener.incoming() {
|
||||||
|
match stream{
|
||||||
|
Ok(s) => process_reques(s),
|
||||||
|
Err(_e) => println!("{}",_e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fn process_reques(mut stream: TcpStream) {
|
||||||
|
let mut buf: [u8; 256] = [1; 256];
|
||||||
|
match stream.read(&mut buf){
|
||||||
|
Ok(len) => check_state(&mut buf, len),
|
||||||
|
Err(e) => println!("pos no {}", e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_state(buf: &mut [u8; 256], len: usize) {
|
||||||
|
let var = String::from_utf8(buf[0 .. len-2].to_vec()).unwrap();
|
||||||
|
//let var = String::from("some");
|
||||||
|
match var.as_str(){
|
||||||
|
"some" => println!("entra"),
|
||||||
|
_=> println!("pos no {}", var),
|
||||||
|
}
|
||||||
|
println!("len {}", var.len());
|
||||||
|
}
|
66
src/main.rs
66
src/main.rs
@ -1,64 +1,18 @@
|
|||||||
use std::net::{TcpListener, TcpStream};
|
|
||||||
use std::sync::{Arc, RwLock};
|
|
||||||
use std::io::prelude::*;
|
|
||||||
use crate::client::guard;
|
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
use conf::server_conf;
|
||||||
|
use std::sync::{Arc, RwLock};
|
||||||
|
|
||||||
mod client;
|
mod client;
|
||||||
mod conf;
|
mod conf;
|
||||||
mod protocol;
|
mod protocol;
|
||||||
|
mod server_proxy;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let listener = TcpListener::bind("0.0.0.0:25565").unwrap();
|
let servers = Arc::new(RwLock::new(conf::Config::new()));
|
||||||
let servers = Arc::new(RwLock::new(conf::Servers::new()));
|
let s1 = servers.clone();
|
||||||
let guard = Arc::new(RwLock::new(guard::Guard::new()));
|
let s2 = servers.clone();
|
||||||
for stream in listener.incoming() {
|
let stop1 = thread::spawn(|| server_proxy::start_proxy(s1));
|
||||||
if guard.read().unwrap().can_add(){
|
let stop2 = thread::spawn(|| conf::server_conf::server_conf(s2));
|
||||||
match stream {
|
stop1.join();
|
||||||
Ok(stream) => {
|
stop2.join();
|
||||||
let g = guard.clone();
|
|
||||||
let s = servers.clone();
|
|
||||||
thread::spawn(|| read_connection(stream, s , g));
|
|
||||||
},
|
|
||||||
|
|
||||||
Err(_e) => println!("{}",_e),
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_connection(mut stream: TcpStream,
|
|
||||||
servers: Arc<RwLock<conf::Servers>>,
|
|
||||||
guard: Arc<RwLock<guard::Guard>> ) {
|
|
||||||
let mut buf: [u8; 256] = [1; 256];
|
|
||||||
stream.set_read_timeout(Some(Duration::from_millis(5000)));
|
|
||||||
let leng = match stream.read(&mut buf) {
|
|
||||||
Ok(l) => l,
|
|
||||||
Err(_e) => return,
|
|
||||||
};
|
|
||||||
let hs = protocol::HandShake::new(&mut buf[.. leng]);
|
|
||||||
if hs.is_handshake() { //Filtra los ping, solo controlamos los handshakes
|
|
||||||
conect_server(servers, hs, stream, guard);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
fn conect_server(servers: Arc<RwLock<conf::Servers>>,
|
|
||||||
mut hs: protocol::HandShake,
|
|
||||||
stream: TcpStream,
|
|
||||||
guard: Arc<RwLock<guard::Guard>>){
|
|
||||||
|
|
||||||
match servers.read().unwrap().get_server(&hs.get_host_name()) {
|
|
||||||
Some(s) => {
|
|
||||||
hs.replace_port(s.1);
|
|
||||||
let mut sstream = TcpStream::connect(s.0 + ":" + &s.1.to_string()).unwrap();
|
|
||||||
sstream.write(hs.get_raw());
|
|
||||||
let c1 = client::Client::new(stream,sstream, hs);
|
|
||||||
guard.write().unwrap().add_thread(c1.start_proxy());
|
|
||||||
},
|
|
||||||
None => println!("No server found for {}", hs.get_host_name())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@ -5,6 +5,8 @@ minecraft_proxy_sources = [
|
|||||||
'client/guard.rs',
|
'client/guard.rs',
|
||||||
'protocol/mod.rs',
|
'protocol/mod.rs',
|
||||||
'conf/mod.rs',
|
'conf/mod.rs',
|
||||||
|
'conf/server_conf.rs',
|
||||||
|
'server_proxy.rs',
|
||||||
]
|
]
|
||||||
|
|
||||||
minecraft_proxy_deps = [
|
minecraft_proxy_deps = [
|
||||||
|
61
src/server_proxy.rs
Normal file
61
src/server_proxy.rs
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
use std::net::{TcpListener, TcpStream};
|
||||||
|
use std::sync::{Arc, RwLock};
|
||||||
|
use std::io::prelude::*;
|
||||||
|
use crate::client::guard;
|
||||||
|
use crate::client;
|
||||||
|
use std::thread;
|
||||||
|
use std::time::Duration;
|
||||||
|
use crate::protocol;
|
||||||
|
use crate::conf;
|
||||||
|
|
||||||
|
pub fn start_proxy(servers: Arc<RwLock<conf::Config>>){
|
||||||
|
let listener = TcpListener::bind(String::from("0.0.0.0:") + servers.read().unwrap().get_port()).unwrap();
|
||||||
|
let guard = Arc::new(RwLock::new(guard::Guard::new()));
|
||||||
|
for stream in listener.incoming() {
|
||||||
|
if guard.read().unwrap().can_add(){
|
||||||
|
match stream {
|
||||||
|
Ok(stream) => {
|
||||||
|
let g = guard.clone();
|
||||||
|
let s = servers.clone();
|
||||||
|
thread::spawn(|| read_connection(stream, s , g));
|
||||||
|
},
|
||||||
|
|
||||||
|
Err(_e) => println!("{}",_e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_connection(mut stream: TcpStream,
|
||||||
|
servers: Arc<RwLock<conf::Config>>,
|
||||||
|
guard: Arc<RwLock<guard::Guard>> ) {
|
||||||
|
let mut buf: [u8; 256] = [1; 256];
|
||||||
|
stream.set_read_timeout(Some(Duration::from_millis(5000)));
|
||||||
|
let leng = match stream.read(&mut buf) {
|
||||||
|
Ok(l) => l,
|
||||||
|
Err(_e) => return,
|
||||||
|
};
|
||||||
|
let hs = protocol::HandShake::new(&mut buf[.. leng]);
|
||||||
|
if hs.is_handshake() { //Filtra los ping, solo controlamos los handshakes
|
||||||
|
conect_server(servers, hs, stream, guard);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fn conect_server(servers: Arc<RwLock<conf::Config>>,
|
||||||
|
mut hs: protocol::HandShake,
|
||||||
|
stream: TcpStream,
|
||||||
|
guard: Arc<RwLock<guard::Guard>>){
|
||||||
|
|
||||||
|
match servers.read().unwrap().get_server(&hs.get_host_name()) {
|
||||||
|
Some(s) => {
|
||||||
|
hs.replace_port(s.1);
|
||||||
|
let mut sstream = TcpStream::connect(s.0 + ":" + &s.1.to_string()).unwrap();
|
||||||
|
sstream.write(hs.get_raw());
|
||||||
|
let c1 = client::Client::new(stream,sstream, hs);
|
||||||
|
guard.write().unwrap().add_thread(c1.start_proxy());
|
||||||
|
},
|
||||||
|
None => println!("No server found for {}", hs.get_host_name())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user