下面列出了怎么用org.eclipse.jgit.transport.OpenSshConfig.Host的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
protected Session createSession(Host hc, String user, String host, int port, FS fs)
throws JSchException {
JSch jSch = getJSch(hc, fs);
// assumption: identities from agent are always unencrypted
final Collection<Identity> allUnencryptedIdentities = getIdentitiesFromAgentProxy();
@SuppressWarnings("unchecked")
Collection<Identity> identities = ((Collection<Identity>) jSch.getIdentityRepository().getIdentities());
identities.stream()
.filter(id -> !id.isEncrypted())
.forEach(allUnencryptedIdentities::add);
Session session = jSch.getSession(user, host, port);
session.setIdentityRepository(new ReadOnlyIdentityRepository(allUnencryptedIdentities));
return session;
}
@Override
protected Session createSession(Host hc, String user, String host, int port, FS fs)
throws JSchException {
if (this.sshKeysByHostname.containsKey(host)) {
JGitEnvironmentProperties sshUriProperties = this.sshKeysByHostname.get(host);
this.jSch.addIdentity(host, sshUriProperties.getPrivateKey().getBytes(), null,
null);
if (sshUriProperties.getKnownHostsFile() != null) {
this.jSch.setKnownHosts(sshUriProperties.getKnownHostsFile());
}
if (sshUriProperties.getHostKey() != null) {
HostKey hostkey = new HostKey(host,
Base64.decode(sshUriProperties.getHostKey()));
this.jSch.getHostKeyRepository().add(hostkey, null);
}
return this.jSch.getSession(user, host, port);
}
throw new JSchException("no keys configured for hostname " + host);
}
@Override
protected Session createSession (Host hc, String user, String host, int port, FS fs) throws JSchException {
Session session = super.createSession(hc, user, host, port, fs);
try {
List<Proxy> proxies = ProxySelector.getDefault().select(new URI("socket",
null,
host,
port == -1 ? 22 : port,
null, null, null));
if (proxies.size() > 0) {
Proxy p = proxies.iterator().next();
if (p.type() == Proxy.Type.DIRECT) {
session.setProxy(null);
} else {
SocketAddress addr = p.address();
if (addr instanceof InetSocketAddress) {
InetSocketAddress inetAddr = (InetSocketAddress) addr;
String proxyHost = inetAddr.getHostName();
int proxyPort = inetAddr.getPort();
session.setProxy(createProxy(proxyHost, proxyPort));
}
}
}
} catch (URISyntaxException ex) {
Logger.getLogger(JGitSshSessionFactory.class.getName()).log(Level.INFO, "Invalid URI: " + host + ":" + port, ex);
}
return session;
}
private boolean setupJSch (FS fs, String host, CredentialItem.StringType identityFile, URIish uri, boolean preferAgent) throws TransportException {
boolean agentUsed;
if (sshConfig == null) {
sshConfig = OpenSshConfig.get(fs);
}
final OpenSshConfig.Host hc = sshConfig.lookup(host);
try {
JSch jsch = getJSch(hc, fs);
agentUsed = setupJSchIdentityRepository(jsch, identityFile.getValue(), preferAgent);
} catch (JSchException ex) {
throw new TransportException(uri, ex.getMessage(), ex);
}
return agentUsed;
}
public static void gitSync() throws IOException, InvalidRemoteException, org.eclipse.jgit.api.errors.TransportException, GitAPIException {
SshSessionFactory.setInstance(new JschConfigSessionFactory() {
public void configure(Host hc, Session session) {
session.setConfig("StrictHostKeyChecking", "no");
};
}
);
if (openRepository()) {
pullRepository();
}
else cloneRepository();
}
private void initialize() {
if (!this.initialized) {
SshSessionFactory.setInstance(new JschConfigSessionFactory() {
@Override
protected void configure(Host hc, Session session) {
session.setConfig("StrictHostKeyChecking",
isStrictHostKeyChecking() ? "yes" : "no");
}
});
this.initialized = true;
}
}
@Override
protected void configure(Host hc, Session session) {
JGitEnvironmentProperties sshProperties = this.sshKeysByHostname
.get(hc.getHostName());
String hostKeyAlgorithm = sshProperties.getHostKeyAlgorithm();
if (hostKeyAlgorithm != null) {
session.setConfig(SERVER_HOST_KEY, hostKeyAlgorithm);
}
if (sshProperties.getHostKey() == null
|| !sshProperties.isStrictHostKeyChecking()) {
session.setConfig(STRICT_HOST_KEY_CHECKING, NO_OPTION);
}
else {
session.setConfig(STRICT_HOST_KEY_CHECKING, YES_OPTION);
}
String preferredAuthentications = sshProperties.getPreferredAuthentications();
if (preferredAuthentications != null) {
session.setConfig(PREFERRED_AUTHENTICATIONS, preferredAuthentications);
}
ProxyHostProperties proxyHostProperties = sshProperties.getProxy()
.get(ProxyHostProperties.ProxyForScheme.HTTP);
if (proxyHostProperties != null) {
ProxyHTTP proxy = createProxy(proxyHostProperties);
proxy.setUserPasswd(proxyHostProperties.getUsername(),
proxyHostProperties.getPassword());
session.setProxy(proxy);
}
}
@Override
protected void configure(final Host host, final Session session) {
// ignored
}
@Override
protected JSch getJSch(final Host host, final FS fs) throws JSchException {
return configureIdentity(super.getJSch(host, fs));
}
@Override
protected void configure (Host host, Session sn) {
sn.setConfig("PreferredAuthentications", "publickey,password,keyboard-interactive"); //NOI18N
}
@Override
protected void configure(Host hc, Session session) {
// nothing to do
}