1 /* 2 * Copyright (C) 2004-2008 Jive Software. All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.jivesoftware.openfire.container; 18 19 import org.jivesoftware.openfire.XMPPServer; 20 import org.jivesoftware.util.cache.ClusterTask; 21 import org.jivesoftware.util.cache.ExternalizableUtil; 22 23 import java.io.IOException; 24 import java.io.ObjectInput; 25 import java.io.ObjectOutput; 26 import java.net.*; 27 import java.util.Collections; 28 import java.util.Enumeration; 29 30 /** 31 * Task that will return the bind interface and ports being used by the admin 32 * console of the node where the task will be executed. When the admin console 33 * is binded to all network interfaces this task will try to find a valid IP 34 * address that will work for the remote node.<p> 35 * 36 * A {@code null} bindInterface in the result of this task means that the task 37 * failed to find a valid IP address where the admin console is listening. 38 * 39 * @author Gaston Dombiak 40 */ 41 public class GetAdminConsoleInfoTask implements ClusterTask<GetAdminConsoleInfoTask> { 42 private String bindInterface; 43 private int adminPort; 44 private int adminSecurePort; 45 private String adminSecret; 46 47 48 @Override getResult()49 public GetAdminConsoleInfoTask getResult() { 50 return this; 51 } 52 53 @Override run()54 public void run() { 55 PluginManager pluginManager = XMPPServer.getInstance().getPluginManager(); 56 AdminConsolePlugin adminConsolePlugin = ((AdminConsolePlugin) pluginManager.getPlugin("admin")); 57 58 bindInterface = adminConsolePlugin.getBindInterface(); 59 adminPort = adminConsolePlugin.getAdminUnsecurePort(); 60 adminSecurePort = adminConsolePlugin.getAdminSecurePort(); 61 adminSecret = AdminConsolePlugin.secret; 62 63 if (bindInterface == null) { 64 Enumeration<NetworkInterface> nets = null; 65 try { 66 nets = NetworkInterface.getNetworkInterfaces(); 67 } catch (SocketException e) { 68 // We failed to discover a valid IP address where the admin console is running 69 return; 70 } 71 for (NetworkInterface netInterface : Collections.list(nets)) { 72 boolean found = false; 73 Enumeration<InetAddress> addresses = netInterface.getInetAddresses(); 74 for (InetAddress address : Collections.list(addresses)) { 75 if ("127.0.0.1".equals(address.getHostAddress()) || "0:0:0:0:0:0:0:1".equals(address.getHostAddress())) { 76 continue; 77 } 78 Socket socket = new Socket(); 79 InetSocketAddress remoteAddress = new InetSocketAddress(address, adminPort > 0 ? adminPort : adminSecurePort); 80 try { 81 socket.connect(remoteAddress); 82 bindInterface = address.getHostAddress(); 83 found = true; 84 break; 85 } catch (IOException e) { 86 // Ignore this address. Let's hope there is more addresses to validate 87 } 88 } 89 if (found) { 90 break; 91 } 92 } 93 } 94 } 95 getBindInterface()96 public String getBindInterface() { 97 return bindInterface; 98 } 99 getAdminPort()100 public int getAdminPort() { 101 return adminPort; 102 } 103 getAdminSecurePort()104 public int getAdminSecurePort() { 105 return adminSecurePort; 106 } 107 getAdminSecret()108 public String getAdminSecret() { 109 return adminSecret; 110 } 111 112 @Override writeExternal(ObjectOutput out)113 public void writeExternal(ObjectOutput out) throws IOException { 114 ExternalizableUtil.getInstance().writeInt(out, adminPort); 115 ExternalizableUtil.getInstance().writeInt(out, adminSecurePort); 116 ExternalizableUtil.getInstance().writeBoolean(out, bindInterface != null); 117 if (bindInterface != null) { 118 ExternalizableUtil.getInstance().writeSafeUTF(out, bindInterface); 119 } 120 ExternalizableUtil.getInstance().writeSafeUTF(out, adminSecret); 121 } 122 123 @Override readExternal(ObjectInput in)124 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { 125 adminPort = ExternalizableUtil.getInstance().readInt(in); 126 adminSecurePort = ExternalizableUtil.getInstance().readInt(in); 127 if (ExternalizableUtil.getInstance().readBoolean(in)) { 128 bindInterface = ExternalizableUtil.getInstance().readSafeUTF(in); 129 } 130 adminSecret = ExternalizableUtil.getInstance().readSafeUTF(in); 131 } 132 133 } 134