using git with apache sshd

2019-06-05 05:05发布

问题:

I'm trying to use msysgit over an SSH server I wrote in Java using sshd and I have made great progress in that i got past some git errors, I am able to connect using putty and get a shell, I have git in my windows path, but I still can't actually use git to connect to my repository over my ssh daemon. I get the following error from msysgit:

fatal: ''/C/gitrepo'' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

I used the following to add the remote to the repo I'm trying to connect from:

git remote set-url origin "ssh://test@localhost:22/C/gitrepo".

I tried a lot of other variations as well of the path with no luck. I have two git repos set up on localhost, the one i'm running git push from and the one at c:\gitrepo.

What am I missing?

Also I had to add the path to mysysgit/bin to my windows 7 environment variable, but would also like a way to get this to work without adding it to my windows environment variables but specifying it programmatically in my ssh server, but more importantly I would like to be able to run git over this ssh server.

The code for my server is below.

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import org.apache.sshd.SshServer;
import org.apache.sshd.server.UserAuth;
import org.apache.sshd.common.NamedFactory;
import org.apache.sshd.common.util.OsUtils;
import org.apache.sshd.server.Command;
import org.apache.sshd.server.CommandFactory;
import org.apache.sshd.server.ForwardingFilter;
import org.apache.sshd.server.PasswordAuthenticator;
import org.apache.sshd.server.auth.UserAuthPassword;
import org.apache.sshd.server.command.ScpCommandFactory;
import org.apache.sshd.server.filesystem.NativeFileSystemFactory;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.server.session.ServerSession;
import org.apache.sshd.server.shell.ProcessShellFactory;

public class SSHD {

   public SSHD() {
      init();
   }

   public void start() throws IOException {
      sshServer.start();
   }

   private void init() {
      sshServer = SshServer.setUpDefaultServer();

      sshServer.setPort(22);
      sshServer.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser"));

      setupAuthentication();
      setupCommandHandling();
   }

   private void setupAuthentication() {
      sshServer.setPasswordAuthenticator(new SSHD.MyPasswordAuthenticator());

      List<NamedFactory<UserAuth>> userAuthFactories = new ArrayList<NamedFactory<UserAuth>>();
      userAuthFactories.add(new UserAuthPassword.Factory());
      sshServer.setUserAuthFactories(userAuthFactories);
   }

   private void setupCommandHandling() {
      CommandFactory myCommandFactory = new CommandFactory() {
         @Override
         public Command createCommand(String command) {
            System.out.println("command = \"" + command + "\"");
            return new ProcessShellFactory(command.split(" ")).create();
         }

      };

      sshServer.setCommandFactory(new ScpCommandFactory(myCommandFactory));

      sshServer.setFileSystemFactory(new NativeFileSystemFactory());

      sshServer.setForwardingFilter(new ForwardingFilter() {
         public boolean canForwardAgent(ServerSession session) {
            return true;
         }

         public boolean canForwardX11(ServerSession session) {
            return true;
         }

         public boolean canListen(InetSocketAddress address, ServerSession session) {
            return true;
         }

         public boolean canConnect(InetSocketAddress address, ServerSession session) {
            return true;
         }

      });

      ProcessShellFactory shellFactory = null;
      if (OsUtils.isUNIX()) {
         shellFactory = new ProcessShellFactory(new String[]{"/bin/sh", "-i", "-l"},
                 EnumSet.of(ProcessShellFactory.TtyOptions.ONlCr));
      } else {
         shellFactory = new ProcessShellFactory(new String[]{"cmd.exe "},
                 EnumSet.of(ProcessShellFactory.TtyOptions.Echo, ProcessShellFactory.TtyOptions.ICrNl, ProcessShellFactory.TtyOptions.ONlCr));
      }

      sshServer.setShellFactory(shellFactory);
   }

   private static class MyPasswordAuthenticator implements PasswordAuthenticator {

      @Override
      public boolean authenticate(String username, String password, ServerSession session) {
         return true;
      }

   }

   public static void main(String[] args) {
      SSHD sshd = new SSHD();

      try {
         sshd.start();
      } catch (IOException ex) {
         System.out.println("EXCEPTION: " + ex);
         ex.printStackTrace();
      }
   }

   private SshServer sshServer;
}

Does anybody know how to fix this manual parsing on the server side. Is there a different command processor I can use or something like that?