VersionCheckClientpublic class VersionCheckClient extends Object Client for checking version information from a remote server. |
Fields Summary |
---|
private static final LogIDs | LOGID | public static final String | REASON_UPDATE_CHECK_START | public static final String | REASON_UPDATE_CHECK_PERIODIC | public static final String | REASON_CHECK_SWT | public static final String | REASON_DHT_EXTENDED_ALLOWED | public static final String | REASON_DHT_ENABLE_ALLOWED | public static final String | REASON_EXTERNAL_IP | public static final String | REASON_RECOMMENDED_PLUGINS | public static final String | REASON_SECONDARY_CHECK | private static final String | AZ_MSG_SERVER_ADDRESS_V4 | private static final int | AZ_MSG_SERVER_PORT | private static final String | MESSAGE_TYPE_ID | public static final String | HTTP_SERVER_ADDRESS_V4 | public static final int | HTTP_SERVER_PORT | public static final String | TCP_SERVER_ADDRESS_V4 | public static final int | TCP_SERVER_PORT | public static final String | UDP_SERVER_ADDRESS_V4 | public static final int | UDP_SERVER_PORT | public static final String | AZ_MSG_SERVER_ADDRESS_V6 | public static final String | HTTP_SERVER_ADDRESS_V6 | public static final String | TCP_SERVER_ADDRESS_V6 | public static final String | UDP_SERVER_ADDRESS_V6 | private static final long | CACHE_PERIOD | private static boolean | secondary_check_done | private static boolean | prefer_v6 | private static final int | AT_V4 | private static final int | AT_V6 | private static final int | AT_EITHER | private static final VersionCheckClient | instance | private Map | last_check_data_v4 | private Map | last_check_data_v6 | private final AEMonitor | check_mon | private long | last_check_time_v4 | private long | last_check_time_v6 |
Constructors Summary |
---|
private VersionCheckClient()
/* blank */
|
Methods Summary |
---|
public boolean | DHTEnableAllowed()Is the DHT plugin allowed to be enabled.
Map reply = getVersionCheckInfo( REASON_DHT_ENABLE_ALLOWED, AT_EITHER );
boolean res = false;
byte[] value = (byte[])reply.get( "enable_dht" );
if( value != null ) {
res = new String( value ).equalsIgnoreCase( "true" );
}
// we take the view that if the version check failed then we go ahead
// and enable the DHT (i.e. we're being optimistic)
if ( !res ){
res = !isVersionCheckDataValid( AT_EITHER );
}
return res;
| public boolean | DHTExtendedUseAllowed()Is the DHT allowed to be used by external plugins.
Map reply = getVersionCheckInfo( REASON_DHT_EXTENDED_ALLOWED, AT_EITHER );
boolean res = false;
byte[] value = (byte[])reply.get( "enable_dht_extended_use" );
if( value != null ) {
res = new String( value ).equalsIgnoreCase( "true" );
}
// be generous and enable extended use if check failed
if ( !res ){
res = !isVersionCheckDataValid( AT_EITHER );
}
return res;
| private java.util.Map | constructVersionCheckMessage(java.lang.String reason)Construct the default version check message.
Map message = new HashMap();
message.put( "appid", SystemProperties.getApplicationIdentifier());
message.put( "version", Constants.AZUREUS_VERSION );
String id = COConfigurationManager.getStringParameter( "ID", null );
boolean send_info = COConfigurationManager.getBooleanParameter( "Send Version Info" );
int last_send_time = COConfigurationManager.getIntParameter( "Send Version Info Last Time", -1 );
int current_send_time = (int)(SystemTime.getCurrentTime()/1000);
COConfigurationManager.setParameter( "Send Version Info Last Time", current_send_time );
if( id != null && send_info ) {
message.put( "id", id );
message.put( "os", Constants.OSName );
message.put( "os_version", System.getProperty( "os.version" ) );
message.put( "os_arch", System.getProperty( "os.arch" ) ); //see http://lopica.sourceforge.net/os.html
if ( last_send_time != -1 && last_send_time < current_send_time ){
// tims since last
message.put( "tsl", new Long(current_send_time-last_send_time));
}
message.put( "reason", reason );
String java_version = System.getProperty( "java.version" );
if ( java_version == null ){ java_version = "unknown"; }
message.put( "java", java_version );
String java_vendor = System.getProperty( "java.vm.vendor" );
if ( java_vendor == null ){ java_vendor = "unknown"; }
message.put( "javavendor", java_vendor );
long max_mem = Runtime.getRuntime().maxMemory()/(1024*1024);
message.put( "javamx", new Long( max_mem ) );
String java_rt_name = System.getProperty("java.runtime.name");
if (java_rt_name != null) {
message.put( "java_rt_name", java_rt_name);
}
String java_rt_version = System.getProperty("java.runtime.version");
if (java_rt_version != null) {
message.put( "java_rt_version", java_rt_version);
}
OverallStats stats = StatsFactory.getStats();
if ( stats != null ){
//long total_bytes_downloaded = stats.getDownloadedBytes();
//long total_bytes_uploaded = stats.getUploadedBytes();
long total_uptime = stats.getTotalUpTime();
//removed due to complaints about anonymous stats collection
//message.put( "total_bytes_downloaded", new Long( total_bytes_downloaded ) );
//message.put( "total_bytes_uploaded", new Long( total_bytes_uploaded ) );
message.put( "total_uptime", new Long( total_uptime ) );
//message.put( "dlstats", stats.getDownloadStats());
}
String as = COConfigurationManager.getStringParameter( "ASN AS", null );
if ( as != null ){
// there was borkage with DNS based queries that left leading " on AS
if ( as.startsWith( "\"" )){
as = as.substring( 1 ).trim();
COConfigurationManager.setParameter( "ASN AS", as );
}
message.put( "ip_as", as );
}
String asn = COConfigurationManager.getStringParameter( "ASN ASN", null );
if ( asn != null ){
if ( asn.length() > 64 ){
asn = asn.substring( 0, 64 );
}
message.put( "ip_asn", asn );
}
String ui = COConfigurationManager.getStringParameter("ui");
if (ui.length() > 0) {
message.put("ui", ui);
}
// send locale, so we can determine which languages need attention
message.put("locale", Locale.getDefault().toString());
String originalLocale = System.getProperty("user.language") + "_"
+ System.getProperty("user.country");
String variant = System.getProperty("user.variant");
if (variant != null && variant.length() > 0) {
originalLocale += "_" + variant;
}
message.put("orig_locale", originalLocale);
if ( AzureusCoreFactory.isCoreAvailable()){
//installed plugin IDs
PluginInterface[] plugins = AzureusCoreFactory.getSingleton().getPluginManager().getPluginInterfaces();
List pids = new ArrayList();
for (int i=0;i<plugins.length;i++){
String pid = plugins[i].getPluginID();
String info = (String)plugins[i].getPluginconfig().getPluginStringParameter( "plugin.info" );
// filter out built-in and core ones
if ( ( info != null && info.length() > 0 ) ||
( !pid.startsWith( "<" ) &&
!pid.startsWith( "azbp" ) &&
!pid.startsWith( "azupdater" ) &&
!pid.startsWith( "azplatform" ) &&
!pids.contains( pid ))){
if ( info != null && info.length() > 0 ){
if( info.length() < 256 ){
pid += ":" + info;
}else{
Debug.out( "Plugin '" + pid + "' reported excessive info string '" + info + "'" );
}
}
pids.add( pid );
}
}
message.put( "plugins", pids );
}
}
//swt stuff
try {
Class c = Class.forName( "org.eclipse.swt.SWT" );
String swt_platform = (String)c.getMethod( "getPlatform", new Class[]{} ).invoke( null, new Object[]{} );
message.put( "swt_platform", swt_platform );
if( send_info ) {
Integer swt_version = (Integer)c.getMethod( "getVersion", new Class[]{} ).invoke( null, new Object[]{} );
message.put( "swt_version", new Long( swt_version.longValue() ) );
c = Class.forName("org.gudy.azureus2.ui.swt.mainwindow.MainWindow");
if (c != null) {
c.getMethod("addToVersionCheckMessage", new Class[] { Map.class }).invoke(
null, new Object[] { message });
}
}
}
catch( ClassNotFoundException e ) { /* ignore */ }
catch( NoClassDefFoundError er ) { /* ignore */ }
catch( InvocationTargetException err ) { /* ignore */ }
catch( Throwable t ) { t.printStackTrace(); }
boolean using_phe = COConfigurationManager.getBooleanParameter( "network.transport.encrypted.require" );
message.put( "using_phe", using_phe ? new Long(1) : new Long(0) );
return message;
| private java.util.Map | executeAZMessage(java.util.Map data_to_send, boolean v6)
String host = v6?AZ_MSG_SERVER_ADDRESS_V6:AZ_MSG_SERVER_ADDRESS_V4;
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, "VersionCheckClient retrieving "
+ "version information from " + host + ":" + AZ_MSG_SERVER_PORT));
ClientMessageService msg_service = null;
Map reply = null;
try{
msg_service = ClientMessageServiceClient.getServerService( host, AZ_MSG_SERVER_PORT, MESSAGE_TYPE_ID );
msg_service.sendMessage( data_to_send ); //send our version message
reply = msg_service.receiveMessage(); //get the server reply
preProcessReply( reply, v6 );
}finally{
if ( msg_service != null ){
msg_service.close();
}
}
return( reply );
| private java.util.Map | executeHTTP(java.util.Map data_to_send, boolean v6)
String host = 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://" + 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 );
HttpURLConnection url_connection = (HttpURLConnection)url.openConnection();
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();
}
| private java.util.Map | executeTCP(java.util.Map data_to_send, java.net.InetAddress bind_ip, int bind_port, boolean v6)
String host = 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();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int total_len = 0;
while( true ){
int len = is.read( buffer );
if ( len <= 0 ){
break;
}
total_len += len;
if ( total_len > 16000 ){
throw( new IOException( "reply too large" ));
}
baos.write( buffer, 0, len );
}
byte[] reply_bytes = baos.toByteArray();
for (int i=3;i<reply_bytes.length;i++){
if ( reply_bytes[i-3]== (byte)'\015" &&
reply_bytes[i-2]== (byte)'\012" &&
reply_bytes[i-1]== (byte)'\015" &&
reply_bytes[i-0]== (byte)'\012" ){
Map reply = BDecoder.decode( new BufferedInputStream( new ByteArrayInputStream( reply_bytes, i+1, reply_bytes.length - (i+1 ))));
preProcessReply( reply, v6 );
return( reply );
}
}
throw( new Exception( "Invalid reply: " + new String( reply_bytes )));
}finally{
if ( socket != null ){
try{
socket.close();
}catch( Throwable e ){
}
}
}
| private java.util.Map | executeUDP(java.util.Map data_to_send, java.net.InetAddress bind_ip, int bind_port, boolean v6)
String host = v6?UDP_SERVER_ADDRESS_V6:UDP_SERVER_ADDRESS_V4;
PRUDPReleasablePacketHandler handler = PRUDPPacketHandlerFactory.getReleasableHandler( bind_port );
PRUDPPacketHandler packet_handler = handler.getHandler();
long timeout = 5;
Random random = new Random();
try{
Exception last_error = null;
packet_handler.setExplicitBindAddress( bind_ip );
for (int i=0;i<3;i++){
try{
// connection ids for requests must always have their msb set...
// apart from the original darn udp tracker spec....
long connection_id = 0x8000000000000000L | random.nextLong();
VersionCheckClientUDPRequest request_packet = new VersionCheckClientUDPRequest( connection_id );
request_packet.setPayload( data_to_send );
VersionCheckClientUDPReply reply_packet = (VersionCheckClientUDPReply)packet_handler.sendAndReceive( null, request_packet, new InetSocketAddress( host, UDP_SERVER_PORT ), timeout );
Map reply = reply_packet.getPayload();
preProcessReply( reply, v6 );
return( reply );
}catch( Exception e){
last_error = e;
timeout = timeout * 2;
}
}
if ( last_error != null ){
throw( last_error );
}
throw( new Exception( "Timeout" ));
}finally{
packet_handler.setExplicitBindAddress( null );
handler.release();
}
| public long | getCacheTime(boolean v6)
return( v6?last_check_time_v6:last_check_time_v4);
| public java.lang.String | getExternalIpAddress(boolean only_if_cached, boolean v6)Get the ip address seen by the version check server.
NOTE: This information may be cached, see getVersionCheckInfo().
Map reply = getVersionCheckInfoSupport( REASON_EXTERNAL_IP, only_if_cached, false, v6 );
byte[] address = (byte[])reply.get( "source_ip_address" );
if( address != null ) {
return new String( address );
}
return( null );
| public java.net.InetAddress | getExternalIpAddressHTTP(boolean v6)
Map reply = executeHTTP( new HashMap(), v6 );
byte[] address = (byte[])reply.get( "source_ip_address" );
return( InetAddress.getByName( new String( address )));
| public java.net.InetAddress | getExternalIpAddressTCP(java.net.InetAddress bind_ip, int bind_port, boolean v6)
Map reply = executeTCP( new HashMap(), bind_ip, bind_port, v6 );
byte[] address = (byte[])reply.get( "source_ip_address" );
return( InetAddress.getByName( new String( address )));
| public java.net.InetAddress | getExternalIpAddressUDP(java.net.InetAddress bind_ip, int bind_port, boolean v6)
Map reply = executeUDP( new HashMap(), bind_ip, bind_port, v6 );
byte[] address = (byte[])reply.get( "source_ip_address" );
return( InetAddress.getByName( new String( address )));
| public java.lang.String | getHTTPGetString(boolean for_proxy, boolean v6)
return( getHTTPGetString( new HashMap(), for_proxy, v6 ));
| private java.lang.String | getHTTPGetString(java.util.Map content, boolean for_proxy, boolean v6)
String host = v6?HTTP_SERVER_ADDRESS_V6:HTTP_SERVER_ADDRESS_V4;
String get_str = "GET " + (for_proxy?("http://" + host + ":" + HTTP_SERVER_PORT ):"") +"/version?";
try{
get_str += URLEncoder.encode( new String( BEncoder.encode( content ), "ISO-8859-1" ), "ISO-8859-1" );
}catch( Throwable e ){
}
get_str +=" HTTP/1.1" + "\015\012" + "\015\012";
return( get_str );
| public java.lang.String[] | getRecommendedPlugins()
Map reply = getVersionCheckInfo( REASON_RECOMMENDED_PLUGINS, AT_EITHER );
List l = (List)reply.get( "recommended_plugins" );
if ( l == null ){
return( new String[0] );
}
String[] res = new String[l.size()];
for (int i=0;i<l.size();i++){
res[i] = new String((byte[])l.get(i));
}
return( res );
| public static com.aelitis.azureus.core.versioncheck.VersionCheckClient | getSingleton()Get the singleton instance of the version check client. return instance;
| public java.util.Map | getVersionCheckInfo(java.lang.String reason)Get the version check reply info.
return( getVersionCheckInfo( reason, AT_EITHER ));
| public java.util.Map | getVersionCheckInfo(java.lang.String reason, int address_type)
if ( address_type == AT_V4 ){
return( getVersionCheckInfoSupport( reason, false, false, false ));
}else if ( address_type == AT_V6 ){
return( getVersionCheckInfoSupport( reason, false, false, true ));
}else{
Map reply = getVersionCheckInfoSupport( reason, false, false, prefer_v6 );
if ( reply == null || reply.size() == 0 ){
reply = getVersionCheckInfoSupport( reason, false, false, !prefer_v6 );
}
return( reply );
}
| protected java.util.Map | getVersionCheckInfoSupport(java.lang.String reason, boolean only_if_cached, boolean force, boolean v6)
if ( v6 ){
try { check_mon.enter();
long time_diff = SystemTime.getCurrentTime() - last_check_time_v6;
force = force || time_diff > CACHE_PERIOD || time_diff < 0;
if( last_check_data_v6 == null || last_check_data_v6.size() == 0 || force ) {
// if we've never checked before then we go ahead even if the "only_if_cached"
// flag is set as its had not chance of being cached yet!
if ( only_if_cached && last_check_data_v6 != null ){
return( new HashMap() );
}
try {
last_check_data_v6 = performVersionCheck( constructVersionCheckMessage( reason ), true, true, true );
}
catch( UnknownHostException t ) {
// no internet
Debug.out(t.getClass().getName() + ": " + t.getMessage());
}
catch( Throwable t ) {
Debug.out(t);
last_check_data_v6 = new HashMap();
}
}
else {
Logger.log(new LogEvent(LOGID, "VersionCheckClient is using "
+ "cached version check info. Using " + last_check_data_v6.size()
+ " reply keys."));
}
}
finally { check_mon.exit(); }
if( last_check_data_v6 == null ) last_check_data_v6 = new HashMap();
return last_check_data_v6;
}else{
try { check_mon.enter();
long time_diff = SystemTime.getCurrentTime() - last_check_time_v4;
force = force || time_diff > CACHE_PERIOD || time_diff < 0;
if( last_check_data_v4 == null || last_check_data_v4.size() == 0 || force ) {
// if we've never checked before then we go ahead even if the "only_if_cached"
// flag is set as its had not chance of being cached yet!
if ( only_if_cached && last_check_data_v4 != null ){
return( new HashMap() );
}
try {
last_check_data_v4 = performVersionCheck( constructVersionCheckMessage( reason ), true, true, false );
}
catch( UnknownHostException t ) {
// no internet
Debug.outNoStack("VersionCheckClient - " + t.getClass().getName() + ": " + t.getMessage());
}
catch (IOException t) {
// General connection problem.
Debug.outNoStack("VersionCheckClient - " + t.getClass().getName() + ": " + t.getMessage());
}
catch( Throwable t ) {
Debug.out(t);
last_check_data_v4 = new HashMap();
}
}
else {
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, "VersionCheckClient is using "
+ "cached version check info. Using " + last_check_data_v4.size()
+ " reply keys."));
}
}
finally { check_mon.exit(); }
if( last_check_data_v4 == null ) last_check_data_v4 = new HashMap();
return last_check_data_v4;
}
| private boolean | isVersionCheckDataValid(int address_type)
boolean v6_ok = last_check_data_v6 != null && last_check_data_v6.size() > 0;
boolean v4_ok = last_check_data_v4 != null && last_check_data_v4.size() > 0;
if ( address_type == AT_V4 ){
return( v4_ok );
}else if ( address_type == AT_V6 ){
return( v6_ok );
}else{
return( v4_ok | v6_ok );
}
| public static void | main(java.lang.String[] args)
try{
COConfigurationManager.initialise();
boolean v6= false;
System.out.println( "UDP: " + getSingleton().getExternalIpAddressUDP(null,0,v6));
System.out.println( "TCP: " + getSingleton().getExternalIpAddressTCP(null,0,v6));
System.out.println( "HTTP: " + getSingleton().getExternalIpAddressHTTP(v6));
}catch( Throwable e){
e.printStackTrace();
}
| private java.util.Map | performVersionCheck(java.util.Map data_to_send, boolean use_az_message, boolean use_http, boolean v6)Perform the actual version check by connecting to the version server.
Exception error = null;
Map reply = null;
if ( use_az_message ){
try{
reply = executeAZMessage( data_to_send, v6 );
reply.put( "protocol_used", "AZMSG" );
}
catch (IOException e) {
error = e;
}
catch (Exception e) {
Debug.printStackTrace( e );
error = e;
}
}
if ( reply == null && 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.size()
+ " reply keys."));
if ( v6 ){
last_check_time_v6 = SystemTime.getCurrentTime();
}else{
last_check_time_v4 = SystemTime.getCurrentTime();
}
return reply;
| protected void | preProcessReply(java.util.Map reply, boolean v6)
NetworkAdmin admin = NetworkAdmin.getSingleton();
try{
byte[] address = (byte[])reply.get( "source_ip_address" );
InetAddress my_ip = InetAddress.getByName( new String( address ));
NetworkAdminASN old_asn = admin.getCurrentASN();
NetworkAdminASN new_asn = admin.lookupASN( my_ip );
if ( !new_asn.sameAs( old_asn )){
// kick off a secondary version check to communicate the new information
if ( !secondary_check_done ){
secondary_check_done = true;
new AEThread( "Secondary version check", true )
{
public void
runSupport()
{
getVersionCheckInfoSupport( REASON_SECONDARY_CHECK, false, true, v6 );
}
}.start();
}
}
}catch( Throwable e ){
Debug.printStackTrace(e);
}
Long as_advice = (Long)reply.get( "as_advice" );
if ( as_advice != null ){
NetworkAdminASN current_asn = admin.getCurrentASN();
String asn = current_asn.getASName();
if ( asn != null ){
long advice = as_advice.longValue();
if ( advice != 0 ){
// require crypto
String done_asn = COConfigurationManager.getStringParameter( "ASN Advice Followed", "" );
if ( !done_asn.equals( asn )){
COConfigurationManager.setParameter( "ASN Advice Followed", asn );
boolean change = advice == 1 || advice == 2;
boolean alert = advice == 1 || advice == 3;
if ( !COConfigurationManager.getBooleanParameter( "network.transport.encrypted.require" )){
if ( change ){
COConfigurationManager.setParameter( "network.transport.encrypted.require", true );
}
if ( alert ){
String msg =
MessageText.getString(
"crypto.alert.as.warning",
new String[]{ asn });
Logger.log( new LogAlert( false, LogAlert.AT_WARNING, msg ));
}
}
}
}
}
}
// set ui.toolbar.uiswitcher based on instructions from tracker
// Really shouldn't be in VersionCheck client, but instead have some
// listener and have the code elsewhere. Simply calling
//getVersionCheckInfo from "code elsewhere" (to get the cached result)
//caused a deadlock at startup.
Long lEnabledUISwitcher = (Long) reply.get("ui.toolbar.uiswitcher");
if (lEnabledUISwitcher != null) {
COConfigurationManager.setBooleanDefault("ui.toolbar.uiswitcher",
lEnabledUISwitcher.longValue() == 1);
}
|
|