use of com.biglybt.core.logging.LogEvent in project BiglyBT by BiglySoftware.
the class ClientRestarterImpl method runUpdateProcess.
private boolean runUpdateProcess(boolean update_only, boolean no_wait) throws CoreException {
PluginInterface pi = core.getPluginManager().getPluginInterfaceByID("azupdater");
if (pi == null) {
Logger.log(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_ERROR, "Can't update/restart, mandatory plugin 'azupdater' not found"));
throw (new CoreException("mandatory plugin 'azupdater' not found"));
}
String updater_dir = pi.getPluginDirectoryName();
classpath_prefix = updater_dir + File.separator + UPDATER_JAR;
String app_path = SystemProperties.getApplicationPath();
while (app_path.endsWith(File.separator)) {
app_path = app_path.substring(0, app_path.length() - 1);
}
String user_path = SystemProperties.getUserPath();
while (user_path.endsWith(File.separator)) {
user_path = user_path.substring(0, user_path.length() - 1);
}
String config_override = System.getProperty(SystemProperties.SYS_PROP_CONFIG_OVERRIDE);
if (config_override == null) {
config_override = "";
}
String[] parameters = { update_only ? "updateonly" : "restart", app_path, user_path, config_override };
FileOutputStream fos = null;
try {
Properties update_properties = new Properties();
long max_mem = Runtime.getRuntime().maxMemory();
update_properties.put("max_mem", "" + max_mem);
update_properties.put("app_name", SystemProperties.getApplicationName());
update_properties.put("app_entry", SystemProperties.getApplicationEntryPoint());
if (System.getProperty(SystemProperties.SYSPROP_NATIVELAUNCHER) != null || Constants.isOSX) {
try {
String cmd = PlatformManagerFactory.getPlatformManager().getApplicationCommandLine();
if (cmd != null) {
update_properties.put("app_cmd", cmd);
}
} catch (Throwable e) {
Debug.printStackTrace(e);
}
}
if (no_wait) {
update_properties.put("no_wait", "1");
}
update_properties.put("instance_port", String.valueOf(Constants.INSTANCE_PORT));
fos = new FileOutputStream(new File(user_path, UPDATE_PROPERTIES));
// this handles unicode chars by writing \\u escapes
update_properties.store(fos, "BiglyBT restart properties");
} catch (Throwable e) {
Debug.printStackTrace(e);
} finally {
if (fos != null) {
try {
fos.close();
} catch (Throwable e) {
Debug.printStackTrace(e);
}
}
}
String[] properties = { "-Duser.dir=\"" + app_path + "\"" };
ByteArrayOutputStream os = new ByteArrayOutputStream();
boolean res = restartApp(new PrintWriter(os) {
@Override
public void println(String str) {
// we intercept these logs and log immediately
Logger.log(new LogEvent(LOGID, str));
}
}, MAIN_CLASS, properties, parameters, update_only);
// just check if any non-logged data exists
byte[] bytes = os.toByteArray();
if (bytes.length > 0) {
Logger.log(new LogEvent(LOGID, "BiglyBTUpdater: extra log - " + new String(bytes)));
}
return (res);
}
use of com.biglybt.core.logging.LogEvent in project BiglyBT by BiglySoftware.
the class VersionCheckClient method executeTCP.
private Map executeTCP(Map data_to_send, InetAddress bind_ip, int bind_port, boolean v6) throws Exception {
if (COConfigurationManager.getBooleanParameter("update.anonymous")) {
throw (new Exception("TCP disabled for anonymous updates"));
}
if (v6 && !enable_v6) {
throw (new Exception("IPv6 is disabled"));
}
String host = getHost(v6, TCP_SERVER_ADDRESS_V6, TCP_SERVER_ADDRESS_V4);
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, "VersionCheckClient retrieving " + "version information from " + host + ":" + TCP_SERVER_PORT + " via TCP"));
String get_str = getHTTPGetString(data_to_send, false, v6);
Socket socket = null;
try {
socket = new Socket();
if (bind_ip != null) {
socket.bind(new InetSocketAddress(bind_ip, bind_port));
} else if (bind_port != 0) {
socket.bind(new InetSocketAddress(bind_port));
}
socket.setSoTimeout(10000);
socket.connect(new InetSocketAddress(host, TCP_SERVER_PORT), 10000);
OutputStream os = socket.getOutputStream();
os.write(get_str.getBytes("ISO-8859-1"));
os.flush();
InputStream is = socket.getInputStream();
byte[] buffer = new byte[1];
String header = "";
int content_length = -1;
while (true) {
int len = is.read(buffer);
if (len <= 0) {
break;
}
header += (char) buffer[0];
if (header.endsWith("\r\n\r\n")) {
header = header.toLowerCase(MessageText.LOCALE_ENGLISH);
int pos = header.indexOf("content-length:");
if (pos == -1) {
throw (new IOException("content length missing"));
}
header = header.substring(pos + 15);
pos = header.indexOf('\r');
header = header.substring(0, pos).trim();
content_length = Integer.parseInt(header);
if (content_length > 10000) {
throw (new IOException("content length too large"));
}
break;
}
if (header.length() > 2048) {
throw (new IOException("header too large"));
}
}
ByteArrayOutputStream baos = new ByteArrayOutputStream(content_length);
buffer = new byte[content_length];
while (content_length > 0) {
int len = is.read(buffer);
if (len <= 0) {
break;
}
baos.write(buffer, 0, len);
content_length -= len;
}
if (content_length != 0) {
throw (new IOException("error reading reply"));
}
byte[] reply_bytes = baos.toByteArray();
Map reply = BDecoder.decode(new BufferedInputStream(new ByteArrayInputStream(reply_bytes)));
preProcessReply(reply, v6);
return (reply);
} finally {
if (socket != null) {
try {
socket.close();
} catch (Throwable e) {
}
}
}
}
use of com.biglybt.core.logging.LogEvent in project BiglyBT by BiglySoftware.
the class VersionCheckClient method performVersionCheck.
/**
* Perform the actual version check by connecting to the version server.
* @param data_to_send version message
* @return version reply
* @throws Exception if the server check connection fails
*/
private Map performVersionCheck(Map data_to_send, boolean use_az_message, boolean use_http, boolean v6) throws Exception {
Exception error = null;
Map reply = null;
if (use_http) {
try {
reply = executeHTTP(data_to_send, v6);
reply.put("protocol_used", "HTTP");
error = null;
} catch (IOException e) {
error = e;
} catch (Exception e) {
Debug.printStackTrace(e);
error = e;
}
}
if (error != null) {
throw (error);
}
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, "VersionCheckClient server " + "version check successful. Received " + (reply == null ? "null" : reply.size()) + " reply keys."));
if (v6) {
last_check_time_v6 = SystemTime.getCurrentTime();
} else {
last_check_time_v4 = SystemTime.getCurrentTime();
}
return reply;
}
use of com.biglybt.core.logging.LogEvent in project BiglyBT by BiglySoftware.
the class VersionCheckClient method executeHTTP.
private Map executeHTTP(Map data_to_send, boolean v6) throws Exception {
if (v6 && !enable_v6) {
throw (new Exception("IPv6 is disabled"));
}
String host = getHost(v6, HTTP_SERVER_ADDRESS_V6, HTTP_SERVER_ADDRESS_V4);
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, "VersionCheckClient retrieving " + "version information from " + host + ":" + HTTP_SERVER_PORT + " via HTTP"));
String url_str = "http://" + (v6 ? UrlUtils.convertIPV6Host(host) : host) + (HTTP_SERVER_PORT == 80 ? "" : (":" + HTTP_SERVER_PORT)) + "/version?";
url_str += URLEncoder.encode(new String(BEncoder.encode(data_to_send), "ISO-8859-1"), "ISO-8859-1");
URL url = new URL(url_str);
try {
if (COConfigurationManager.getBooleanParameter("update.anonymous")) {
throw (new Exception("Direct HTTP disabled for anonymous updates"));
}
Properties http_properties = new Properties();
http_properties.put(ClientIDGenerator.PR_URL, url);
try {
ClientIDManagerImpl cman = ClientIDManagerImpl.getSingleton();
if (cman != null && cman.getGenerator() != null) {
cman.generateHTTPProperties(null, http_properties);
}
} catch (Throwable e) {
Debug.out(e);
throw (new IOException(e.getMessage()));
}
url = (URL) http_properties.get(ClientIDGenerator.PR_URL);
HttpURLConnection url_connection = (HttpURLConnection) url.openConnection();
url_connection.setConnectTimeout(10 * 1000);
url_connection.setReadTimeout(10 * 1000);
url_connection.connect();
try {
InputStream is = url_connection.getInputStream();
Map reply = BDecoder.decode(new BufferedInputStream(is));
preProcessReply(reply, v6);
return (reply);
} finally {
url_connection.disconnect();
}
} catch (Exception e) {
if (!v6) {
PluginProxy proxy = AEProxyFactory.getPluginProxy("Vuze version check", url);
if (proxy != null) {
boolean worked = false;
try {
HttpURLConnection url_connection = (HttpURLConnection) proxy.getURL().openConnection(proxy.getProxy());
url_connection.setConnectTimeout(30 * 1000);
url_connection.setReadTimeout(30 * 1000);
url_connection.connect();
try {
InputStream is = url_connection.getInputStream();
Map reply = BDecoder.decode(new BufferedInputStream(is));
preProcessReply(reply, v6);
worked = true;
return (reply);
} finally {
url_connection.disconnect();
}
} finally {
proxy.setOK(worked);
}
}
}
throw (e);
}
}
use of com.biglybt.core.logging.LogEvent in project BiglyBT by BiglySoftware.
the class PRUDPPacketHandlerImpl method sendAndReceive.
public PRUDPPacketHandlerRequestImpl sendAndReceive(PasswordAuthentication auth, PRUDPPacket request_packet, InetSocketAddress destination_address, PRUDPPacketReceiver receiver, long timeout, int priority) throws PRUDPPacketHandlerException {
if (socket == null) {
if (init_error != null) {
throw (new PRUDPPacketHandlerException("Transport unavailable", init_error));
}
throw (new PRUDPPacketHandlerException("Transport unavailable"));
}
checkTargetAddress(destination_address);
PRUDPPacketHandlerImpl delegate = altProtocolDelegate;
if (delegate != null && destination_address.getAddress().getClass().isInstance(delegate.explicit_bind_ip)) {
return delegate.sendAndReceive(auth, request_packet, destination_address, receiver, timeout, priority);
}
try {
MyByteArrayOutputStream baos = new MyByteArrayOutputStream(MAX_PACKET_SIZE);
DataOutputStream os = new DataOutputStream(baos);
request_packet.serialise(os);
byte[] _buffer = baos.getBuffer();
int _length = baos.size();
request_packet.setSerialisedSize(_length);
if (auth != null) {
// <parg_home> so <new_packet> = <old_packet> + <user_padded_to_8_bytes> + <hash>
// <parg_home> where <hash> = first 8 bytes of sha1(<old_packet> + <user_padded_to_8> + sha1(pass))
// <XTF> Yes
SHA1Hasher hasher = new SHA1Hasher();
String user_name = auth.getUserName();
String password = new String(auth.getPassword());
byte[] sha1_password;
if (user_name.equals("<internal>")) {
sha1_password = Base64.decode(password);
} else {
sha1_password = hasher.calculateHash(password.getBytes());
}
byte[] user_bytes = new byte[8];
Arrays.fill(user_bytes, (byte) 0);
for (int i = 0; i < user_bytes.length && i < user_name.length(); i++) {
user_bytes[i] = (byte) user_name.charAt(i);
}
hasher = new SHA1Hasher();
hasher.update(_buffer, 0, _length);
hasher.update(user_bytes);
hasher.update(sha1_password);
byte[] overall_hash = hasher.getDigest();
// System.out.println("PRUDPHandler - auth = " + auth.getUserName() + "/" + new String(auth.getPassword()));
baos.write(user_bytes);
baos.write(overall_hash, 0, 8);
_buffer = baos.getBuffer();
_length = baos.size();
}
DatagramPacket dg_packet = new DatagramPacket(_buffer, _length, destination_address);
PRUDPPacketHandlerRequestImpl request = new PRUDPPacketHandlerRequestImpl(receiver, timeout);
try {
requests_mon.enter();
requests.put(new Integer(request_packet.getTransactionId()), request);
} finally {
requests_mon.exit();
}
try {
if (send_delay > 0 && priority != PRUDPPacketHandler.PRIORITY_IMMEDIATE) {
try {
send_queue_mon.enter();
if (send_queue_data_size > MAX_SEND_QUEUE_DATA_SIZE) {
request.sent();
// synchronous write holding lock to block senders
sendToSocket(dg_packet);
stats.packetSent(_length);
if (TRACE_REQUESTS) {
Logger.log(new LogEvent(LOGID, "PRUDPPacketHandler: request packet sent to " + destination_address + ": " + request_packet.getString()));
}
Thread.sleep(send_delay * 4);
} else {
send_queue_data_size += dg_packet.getLength();
send_queues[priority].add(new Object[] { dg_packet, request });
if (TRACE_REQUESTS) {
String str = "";
for (int i = 0; i < send_queues.length; i++) {
str += (i == 0 ? "" : ",") + send_queues[i].size();
}
System.out.println("send queue sizes: " + str);
}
send_queue_sem.release();
if (send_thread == null) {
send_thread = new AEThread("PRUDPPacketHandler:sender") {
@Override
public void runSupport() {
int[] consecutive_sends = new int[send_queues.length];
while (true) {
try {
send_queue_sem.reserve();
Object[] data;
int selected_priority = 0;
try {
send_queue_mon.enter();
for (int i = 0; i < send_queues.length; i++) {
List queue = send_queues[i];
int queue_size = queue.size();
if (queue_size > 0) {
selected_priority = i;
if (consecutive_sends[i] >= 4 || (i < send_queues.length - 1 && send_queues[i + 1].size() - queue_size > 500)) {
// too many consecutive or too imbalanced, see if there are
// lower priority queues with entries
consecutive_sends[i] = 0;
} else {
consecutive_sends[i]++;
break;
}
} else {
consecutive_sends[i] = 0;
}
}
data = (Object[]) send_queues[selected_priority].remove(0);
DatagramPacket p = (DatagramPacket) data[0];
// mark as sent before sending in case send fails
// and we then rely on timeout to pick this up
send_queue_data_size -= p.getLength();
} finally {
send_queue_mon.exit();
}
DatagramPacket p = (DatagramPacket) data[0];
PRUDPPacketHandlerRequestImpl r = (PRUDPPacketHandlerRequestImpl) data[1];
r.sent();
sendToSocket(p);
stats.packetSent(p.getLength());
if (TRACE_REQUESTS) {
Logger.log(new LogEvent(LOGID, "PRUDPPacketHandler: request packet sent to " + p.getAddress()));
}
long delay = send_delay;
if (selected_priority == PRIORITY_HIGH) {
delay = delay / 2;
}
Thread.sleep(delay);
} catch (Throwable e) {
// get occasional send fails, not very interesting
Logger.log(new LogEvent(LOGID, LogEvent.LT_WARNING, "PRUDPPacketHandler: send failed: " + Debug.getNestedExceptionMessage(e)));
}
}
}
};
send_thread.setDaemon(true);
send_thread.start();
}
}
} finally {
send_queue_mon.exit();
}
} else {
request.sent();
if (dg_packet == null) {
throw new NullPointerException("dg_packet is null");
}
sendToSocket(dg_packet);
// System.out.println( "sent:" + buffer.length );
stats.packetSent(_length);
if (TRACE_REQUESTS) {
Logger.log(new LogEvent(LOGID, "PRUDPPacketHandler: " + "request packet sent to " + destination_address + ": " + request_packet.getString()));
}
}
return (request);
} catch (Throwable e) {
try {
requests_mon.enter();
requests.remove(new Integer(request_packet.getTransactionId()));
} finally {
requests_mon.exit();
}
throw (e);
}
} catch (Throwable e) {
if (e instanceof NullPointerException) {
Debug.out(e);
}
String msg = Debug.getNestedExceptionMessage(e);
Logger.log(new LogEvent(LOGID, LogEvent.LT_ERROR, "PRUDPPacketHandler: sendAndReceive to " + destination_address + " failed: " + msg));
if (msg.contains("Invalid data length")) {
Debug.out("packet=" + request_packet.getString() + ",auth=" + auth);
Debug.out(e);
}
throw (new PRUDPPacketHandlerException("PRUDPPacketHandler:sendAndReceive failed", e));
}
}
Aggregations