Line data Source code
1 : // Copyright (C) 2013 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.ssh; 16 : 17 : import com.google.common.collect.Lists; 18 : import com.google.common.flogger.FluentLogger; 19 : import com.google.gerrit.server.config.GerritServerConfig; 20 : import com.google.gerrit.server.util.SocketUtil; 21 : import com.google.inject.AbstractModule; 22 : import com.google.inject.Provides; 23 : import com.google.inject.Singleton; 24 : import java.net.InetSocketAddress; 25 : import java.net.SocketAddress; 26 : import java.util.ArrayList; 27 : import java.util.Arrays; 28 : import java.util.List; 29 : import org.eclipse.jgit.lib.Config; 30 : 31 152 : public class SshAddressesModule extends AbstractModule { 32 152 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 33 : 34 : public static final int DEFAULT_PORT = 29418; 35 : public static final int IANA_SSH_PORT = 22; 36 : 37 : @Override 38 152 : protected void configure() {} 39 : 40 : @Provides 41 : @Singleton 42 : @SshListenAddresses 43 : public List<SocketAddress> provideListenAddresses(@GerritServerConfig Config cfg) { 44 146 : List<SocketAddress> listen = Lists.newArrayListWithExpectedSize(2); 45 146 : String[] want = cfg.getStringList("sshd", null, "listenaddress"); 46 146 : if (want == null || want.length == 0) { 47 8 : listen.add(new InetSocketAddress(DEFAULT_PORT)); 48 8 : return listen; 49 : } 50 : 51 138 : if (want.length == 1 && isOff(want[0])) { 52 125 : return listen; 53 : } 54 : 55 20 : for (String desc : want) { 56 : try { 57 20 : listen.add(SocketUtil.resolve(desc, DEFAULT_PORT)); 58 0 : } catch (IllegalArgumentException e) { 59 0 : logger.atSevere().log("Bad sshd.listenaddress: %s: %s", desc, e.getMessage()); 60 20 : } 61 : } 62 20 : return listen; 63 : } 64 : 65 : private static boolean isOff(String listenHostname) { 66 138 : return "off".equalsIgnoreCase(listenHostname) 67 20 : || "none".equalsIgnoreCase(listenHostname) 68 138 : || "no".equalsIgnoreCase(listenHostname); 69 : } 70 : 71 : @Provides 72 : @Singleton 73 : @SshAdvertisedAddresses 74 : List<String> provideAdvertisedAddresses( 75 : @GerritServerConfig Config cfg, @SshListenAddresses List<SocketAddress> listen) { 76 146 : String[] want = cfg.getStringList("sshd", null, "advertisedaddress"); 77 146 : if (want.length > 0) { 78 0 : return Arrays.asList(want); 79 : } 80 146 : List<InetSocketAddress> pub = new ArrayList<>(); 81 146 : List<InetSocketAddress> local = new ArrayList<>(); 82 : 83 146 : for (SocketAddress addr : listen) { 84 28 : if (addr instanceof InetSocketAddress) { 85 28 : InetSocketAddress inetAddr = (InetSocketAddress) addr; 86 28 : if (inetAddr.getAddress().isLoopbackAddress()) { 87 20 : local.add(inetAddr); 88 : } else { 89 8 : pub.add(inetAddr); 90 : } 91 : } 92 28 : } 93 146 : if (pub.isEmpty()) { 94 138 : pub = local; 95 : } 96 146 : List<String> adv = Lists.newArrayListWithCapacity(pub.size()); 97 146 : for (InetSocketAddress addr : pub) { 98 28 : adv.add(SocketUtil.format(addr, IANA_SSH_PORT)); 99 28 : } 100 146 : return adv; 101 : } 102 : }