1

我正在尝试在我用 Java 编写的 SSH 服务器上使用 msysgit sshd,我已经取得了很大进展,因为我克服了一些 git 错误,我能够使用 putty 连接并获得一个 shell,我的 Windows 路径中有 git ,但我仍然不能真正使用 git 通过我的 ssh 守护进程连接到我的存储库。我从 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.

我使用以下内容将遥控器添加到我尝试连接的存储库中:

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

我也尝试了许多其他变体,但没有运气。我在 localhost 上设置了两个 git repos,一个是我正在运行 git push 的,一个是在c:\gitrepo.

我错过了什么?

此外,我还必须将 mysysgit/bin 的路径添加到我的 windows 7 环境变量中,但也希望有一种方法可以在不将其添加到我的 windows 环境变量但在我的 ssh 服务器中以编程方式指定它的情况下使其工作,但更重要的是我希望能够在这个 ssh 服务器上运行 git。

我的服务器的代码如下。

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;
}

有谁知道如何在服务器端修复此手动解析。我可以使用不同的命令处理器或类似的东西吗?

4

0 回答 0