Allow connecting to servers

This commit is contained in:
Thinkofname 2016-03-21 10:55:31 +00:00
parent 9550bd5a27
commit 1fc161c53c
6 changed files with 74 additions and 21 deletions

View File

@ -74,6 +74,8 @@ pub struct Console {
position: f64, position: f64,
} }
unsafe impl Send for Console {}
impl Console { impl Console {
pub fn new() -> Console { pub fn new() -> Console {
Console { Console {

View File

@ -86,8 +86,9 @@ impl Game {
self.connect_reply = Some(rx); self.connect_reply = Some(rx);
let address = address.to_owned(); let address = address.to_owned();
let resources = self.resource_manager.clone(); let resources = self.resource_manager.clone();
let console = self.console.clone();
thread::spawn(move || { thread::spawn(move || {
tx.send(server::Server::connect(resources, &address)).unwrap(); tx.send(server::Server::connect(resources, console, &address)).unwrap();
}); });
} }
@ -99,6 +100,7 @@ impl Game {
match server { match server {
Ok(val) => { Ok(val) => {
self.screen_sys.pop_screen(); self.screen_sys.pop_screen();
self.renderer.clear_chunks();
self.server = val; self.server = val;
}, },
Err(err) => { Err(err) => {
@ -176,7 +178,7 @@ fn main() {
let textures = renderer.get_textures(); let textures = renderer.get_textures();
let mut game = Game { let mut game = Game {
server: server::Server::dummy_server(resource_manager.clone()), server: server::Server::dummy_server(resource_manager.clone(), con.clone()),
renderer: renderer, renderer: renderer,
screen_sys: screen_sys, screen_sys: screen_sys,
resource_manager: resource_manager, resource_manager: resource_manager,

View File

@ -362,7 +362,7 @@ pub struct LenPrefixed<L: Lengthable, V> {
} }
impl <L: Lengthable, V: Default> LenPrefixed<L, V> { impl <L: Lengthable, V: Default> LenPrefixed<L, V> {
fn new(data: Vec<V>) -> LenPrefixed<L, V> { pub fn new(data: Vec<V>) -> LenPrefixed<L, V> {
LenPrefixed { LenPrefixed {
len: Default::default(), len: Default::default(),
data: data, data: data,
@ -418,7 +418,7 @@ pub struct LenPrefixedBytes<L: Lengthable> {
} }
impl <L: Lengthable> LenPrefixedBytes<L> { impl <L: Lengthable> LenPrefixedBytes<L> {
fn new(data: Vec<u8>) -> LenPrefixedBytes<L> { pub fn new(data: Vec<u8>) -> LenPrefixedBytes<L> {
LenPrefixedBytes { LenPrefixedBytes {
len: Default::default(), len: Default::default(),
data: data, data: data,

View File

@ -100,8 +100,7 @@ impl Profile {
Ok(self) Ok(self)
} }
// TODO pub fn join_server(&self, server_id: &str, shared_key: &Vec<u8>, public_key: &Vec<u8>) -> Result<(), super::Error> {
pub fn join_server(&self, server_id: &str, shared_key: &Vec<u8>, public_key: &Vec<u8>) {
let mut sha1 = openssl::SHA1::new(); let mut sha1 = openssl::SHA1::new();
sha1.update(server_id.as_bytes()); sha1.update(server_id.as_bytes());
sha1.update(&shared_key[..]); sha1.update(&shared_key[..]);
@ -130,17 +129,16 @@ impl Profile {
let join = serde_json::to_string(&join_msg).unwrap(); let join = serde_json::to_string(&join_msg).unwrap();
let client = hyper::Client::new(); let client = hyper::Client::new();
let res = client.post(JOIN_URL) let res = try!(client.post(JOIN_URL)
.body(&join) .body(&join)
.header(hyper::header::ContentType("application/json".parse().unwrap())) .header(hyper::header::ContentType("application/json".parse().unwrap()))
.send() .send());
.unwrap();
let ret: serde_json::Value = match serde_json::from_reader(res) { if res.status == hyper::status::StatusCode::NoContent {
Result::Ok(val) => val, Ok(())
Result::Err(_) => return, } else {
}; Err(super::Error::Err("Failed to auth with server".to_owned()))
panic!("{:?}", ret); }
} }
pub fn is_complete(&self) -> bool { pub fn is_complete(&self) -> bool {

View File

@ -12,24 +12,28 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use protocol; use protocol::{self, mojang};
use world; use world;
use world::block::{self, BlockSet}; use world::block::{self, BlockSet};
use rand::{self, Rng}; use rand::{self, Rng};
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock, Mutex};
use resources; use resources;
use openssl;
use console;
use auth;
pub struct Server { pub struct Server {
conn: Option<protocol::Conn>, conn: Option<protocol::Conn>,
pub world: world::World, pub world: world::World,
resources: Arc<RwLock<resources::Manager>>, resources: Arc<RwLock<resources::Manager>>,
console: Arc<Mutex<console::Console>>,
version: usize, version: usize,
} }
impl Server { impl Server {
pub fn connect(resources: Arc<RwLock<resources::Manager>>, address: &str) -> Result<Server, protocol::Error> { pub fn connect(resources: Arc<RwLock<resources::Manager>>, console: Arc<Mutex<console::Console>>, address: &str) -> Result<Server, protocol::Error> {
let mut conn = try!(protocol::Conn::new(address)); let mut conn = try!(protocol::Conn::new(address));
let host = conn.host.clone(); let host = conn.host.clone();
@ -45,24 +49,67 @@ impl Server {
username: "Thinkofdeath".to_owned() username: "Thinkofdeath".to_owned()
})); }));
let packet = match conn.read_packet().unwrap() { let packet = match try!(conn.read_packet()) {
protocol::packet::Packet::EncryptionRequest(val) => val, protocol::packet::Packet::EncryptionRequest(val) => val,
protocol::packet::Packet::LoginDisconnect(val) => return Err(protocol::Error::Disconnect(val.reason)), protocol::packet::Packet::LoginDisconnect(val) => return Err(protocol::Error::Disconnect(val.reason)),
val => return Err(protocol::Error::Err(format!("Wrong packet: {:?}", val))), val => return Err(protocol::Error::Err(format!("Wrong packet: {:?}", val))),
}; };
unimplemented!(); let mut key = openssl::PublicKey::new(&packet.public_key.data);
let shared = openssl::gen_random(16);
let shared_e = key.encrypt(&shared);
let token_e = key.encrypt(&packet.verify_token.data);
let profile = {
let console = console.lock().unwrap();
mojang::Profile {
username: console.get(auth::CL_USERNAME).clone(),
id: console.get(auth::CL_UUID).clone(),
access_token: console.get(auth::AUTH_TOKEN).clone(),
}
};
try!(profile.join_server(&packet.server_id, &shared, &packet.public_key.data));
try!(conn.write_packet(protocol::packet::login::serverbound::EncryptionResponse {
shared_secret: protocol::LenPrefixedBytes::new(shared_e),
verify_token: protocol::LenPrefixedBytes::new(token_e),
}));
let mut read = conn.clone(); // TODO
let mut write = conn.clone();
read.enable_encyption(&shared, true);
write.enable_encyption(&shared, false);
loop {
match try!(read.read_packet()) {
protocol::packet::Packet::SetInitialCompression(val) => {
read.set_compresssion(val.threshold.0, true);
write.set_compresssion(val.threshold.0, false);
}
protocol::packet::Packet::LoginSuccess(val) => {
debug!("Login: {} {}", val.username, val.uuid);
read.state = protocol::State::Play;
write.state = protocol::State::Play;
break;
}
protocol::packet::Packet::LoginDisconnect(val) => return Err(protocol::Error::Disconnect(val.reason)),
val => return Err(protocol::Error::Err(format!("Wrong packet: {:?}", val))),
}
}
let version = resources.read().unwrap().version(); let version = resources.read().unwrap().version();
Ok(Server { Ok(Server {
conn: Some(conn), conn: Some(write),
world: world::World::new(), world: world::World::new(),
resources: resources, resources: resources,
console: console,
version: version, version: version,
}) })
} }
pub fn dummy_server(resources: Arc<RwLock<resources::Manager>>) -> Server { pub fn dummy_server(resources: Arc<RwLock<resources::Manager>>, console: Arc<Mutex<console::Console>>) -> Server {
let mut world = world::World::new(); let mut world = world::World::new();
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
for x in -7*16 .. 7*16 { for x in -7*16 .. 7*16 {
@ -80,6 +127,7 @@ impl Server {
version: version, version: version,
resources: resources, resources: resources,
console: console,
} }
} }

View File

@ -536,6 +536,9 @@ impl Container {
} }
pub fn cycle_focus(&mut self) { pub fn cycle_focus(&mut self) {
if self.elements_list.is_empty() {
return;
}
// Find the last focused element // Find the last focused element
let i = self.elements_list.iter() let i = self.elements_list.iter()
.map(|v| self.elements.get(v).unwrap()) .map(|v| self.elements.get(v).unwrap())