2016-03-18 18:24:30 -04:00
|
|
|
// Copyright 2015 Matthew Collins
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-03-21 06:55:31 -04:00
|
|
|
use protocol::{self, mojang};
|
2016-03-18 18:24:30 -04:00
|
|
|
use world;
|
2016-03-19 20:29:35 -04:00
|
|
|
use world::block::{self, BlockSet};
|
2016-03-18 18:24:30 -04:00
|
|
|
use rand::{self, Rng};
|
2016-03-21 06:55:31 -04:00
|
|
|
use std::sync::{Arc, RwLock, Mutex};
|
2016-03-19 13:34:12 -04:00
|
|
|
use resources;
|
2016-03-21 06:55:31 -04:00
|
|
|
use openssl;
|
|
|
|
use console;
|
|
|
|
use auth;
|
2016-03-18 18:24:30 -04:00
|
|
|
|
|
|
|
pub struct Server {
|
|
|
|
conn: Option<protocol::Conn>,
|
2016-03-19 12:32:13 -04:00
|
|
|
pub world: world::World,
|
2016-03-19 13:34:12 -04:00
|
|
|
|
|
|
|
resources: Arc<RwLock<resources::Manager>>,
|
2016-03-21 06:55:31 -04:00
|
|
|
console: Arc<Mutex<console::Console>>,
|
2016-03-19 13:34:12 -04:00
|
|
|
version: usize,
|
2016-03-18 18:24:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Server {
|
2016-03-20 08:04:02 -04:00
|
|
|
|
2016-03-21 06:55:31 -04:00
|
|
|
pub fn connect(resources: Arc<RwLock<resources::Manager>>, console: Arc<Mutex<console::Console>>, address: &str) -> Result<Server, protocol::Error> {
|
2016-03-20 08:04:02 -04:00
|
|
|
let mut conn = try!(protocol::Conn::new(address));
|
|
|
|
|
|
|
|
let host = conn.host.clone();
|
|
|
|
let port = conn.port;
|
|
|
|
try!(conn.write_packet(protocol::packet::handshake::serverbound::Handshake {
|
|
|
|
protocol_version: protocol::VarInt(protocol::SUPPORTED_PROTOCOL),
|
|
|
|
host: host,
|
|
|
|
port: port,
|
|
|
|
next: protocol::VarInt(2),
|
|
|
|
}));
|
|
|
|
conn.state = protocol::State::Login;
|
|
|
|
try!(conn.write_packet(protocol::packet::login::serverbound::LoginStart {
|
|
|
|
username: "Thinkofdeath".to_owned()
|
|
|
|
}));
|
|
|
|
|
2016-03-21 06:55:31 -04:00
|
|
|
let packet = match try!(conn.read_packet()) {
|
2016-03-20 08:04:02 -04:00
|
|
|
protocol::packet::Packet::EncryptionRequest(val) => val,
|
2016-03-20 16:17:21 -04:00
|
|
|
protocol::packet::Packet::LoginDisconnect(val) => return Err(protocol::Error::Disconnect(val.reason)),
|
|
|
|
val => return Err(protocol::Error::Err(format!("Wrong packet: {:?}", val))),
|
2016-03-20 08:04:02 -04:00
|
|
|
};
|
|
|
|
|
2016-03-21 06:55:31 -04:00
|
|
|
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))),
|
|
|
|
}
|
|
|
|
}
|
2016-03-20 08:04:02 -04:00
|
|
|
|
|
|
|
let version = resources.read().unwrap().version();
|
|
|
|
Ok(Server {
|
2016-03-21 06:55:31 -04:00
|
|
|
conn: Some(write),
|
2016-03-20 08:04:02 -04:00
|
|
|
world: world::World::new(),
|
|
|
|
resources: resources,
|
2016-03-21 06:55:31 -04:00
|
|
|
console: console,
|
2016-03-20 08:04:02 -04:00
|
|
|
version: version,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-03-21 06:55:31 -04:00
|
|
|
pub fn dummy_server(resources: Arc<RwLock<resources::Manager>>, console: Arc<Mutex<console::Console>>) -> Server {
|
2016-03-18 18:24:30 -04:00
|
|
|
let mut world = world::World::new();
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
for x in -7*16 .. 7*16 {
|
|
|
|
for z in -7*16 .. 7*16 {
|
|
|
|
let h = rng.gen_range(3, 10);
|
|
|
|
for y in 0 .. h {
|
2016-03-19 20:29:35 -04:00
|
|
|
world.set_block(x, y, z, block::MISSING.base());
|
2016-03-18 18:24:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-19 13:34:12 -04:00
|
|
|
let version = resources.read().unwrap().version();
|
2016-03-18 18:24:30 -04:00
|
|
|
Server {
|
|
|
|
conn: None,
|
|
|
|
world: world,
|
2016-03-19 13:34:12 -04:00
|
|
|
|
|
|
|
version: version,
|
|
|
|
resources: resources,
|
2016-03-21 06:55:31 -04:00
|
|
|
console: console,
|
2016-03-19 13:34:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tick(&mut self) {
|
|
|
|
let version = self.resources.read().unwrap().version();
|
|
|
|
if version != self.version {
|
|
|
|
self.version = version;
|
|
|
|
self.world.flag_dirty_all();
|
2016-03-18 18:24:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|