Line data Source code
1 : // Copyright (C) 2008 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.httpd.raw; 16 : 17 : import static java.nio.charset.StandardCharsets.UTF_8; 18 : 19 : import com.google.gerrit.server.ssh.HostKey; 20 : import com.google.gerrit.server.ssh.SshInfo; 21 : import com.google.gerrit.util.http.CacheHeaders; 22 : import com.google.inject.Inject; 23 : import com.google.inject.Singleton; 24 : import java.io.IOException; 25 : import java.io.PrintWriter; 26 : import java.util.List; 27 : import javax.servlet.http.HttpServlet; 28 : import javax.servlet.http.HttpServletRequest; 29 : import javax.servlet.http.HttpServletResponse; 30 : 31 : /** 32 : * Servlet hosting an SSH daemon on another port. During a standard HTTP GET request the servlet 33 : * returns the hostname and port number back to the client in the form <code>${host} ${port}</code>. 34 : * 35 : * <p>Use a Git URL such as <code>ssh://${email}@${host}:${port}/${path}</code>, e.g. {@code 36 : * ssh://sop@google.com@gerrit.com:8010/tools/gerrit.git} to access the SSH daemon itself. 37 : * 38 : * <p>Versions of Git before 1.5.3 may require setting the username and port properties in the 39 : * user's {@code ~/.ssh/config} file, and using a host alias through a URL such as {@code 40 : * gerrit-alias:/tools/gerrit.git}: 41 : * 42 : * <pre>{@code 43 : * Host gerrit-alias 44 : * User sop@google.com 45 : * Hostname gerrit.com 46 : * Port 8010 47 : * }</pre> 48 : */ 49 : @Singleton 50 : public class SshInfoServlet extends HttpServlet { 51 : private static final long serialVersionUID = 1L; 52 : 53 : private final SshInfo sshd; 54 : 55 : @Inject 56 99 : SshInfoServlet(SshInfo daemon) { 57 99 : sshd = daemon; 58 99 : } 59 : 60 : @Override 61 : protected void doGet(HttpServletRequest req, HttpServletResponse rsp) throws IOException { 62 0 : List<HostKey> hostKeys = sshd.getHostKeys(); 63 : String out; 64 0 : if (!hostKeys.isEmpty()) { 65 0 : String host = hostKeys.get(0).getHost(); 66 0 : String port = "22"; 67 : 68 0 : if (host.contains(":")) { 69 0 : int p = host.lastIndexOf(':'); 70 0 : port = host.substring(p + 1); 71 0 : host = host.substring(0, p); 72 : } 73 : 74 0 : if (host.equals("*")) { 75 0 : host = req.getServerName(); 76 : 77 0 : } else if (host.startsWith("[") && host.endsWith("]")) { 78 0 : host = host.substring(1, host.length() - 1); 79 : } 80 : 81 0 : out = host + " " + port; 82 0 : } else { 83 0 : out = "NOT_AVAILABLE"; 84 : } 85 : 86 0 : CacheHeaders.setNotCacheable(rsp); 87 0 : rsp.setCharacterEncoding(UTF_8.name()); 88 0 : rsp.setContentType("text/plain"); 89 0 : try (PrintWriter w = rsp.getWriter()) { 90 0 : w.write(out); 91 : } 92 0 : } 93 : }