61 lines
1.7 KiB
Java
61 lines
1.7 KiB
Java
package com.sshmanager.service;
|
|
|
|
import com.sshmanager.entity.Connection;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.time.Instant;
|
|
import java.util.Map;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
import java.util.concurrent.atomic.AtomicLong;
|
|
|
|
/**
|
|
* In-memory registry for quick-connect (ephemeral) SSH connections.
|
|
* Entries are NOT persisted to the database and are cleaned up
|
|
* when the WebSocket session closes.
|
|
*/
|
|
@Component
|
|
public class QuickConnectionRegistry {
|
|
|
|
private final AtomicLong idGen = new AtomicLong(10_000_000);
|
|
private final Map<Long, Entry> entries = new ConcurrentHashMap<>();
|
|
|
|
public Connection create(String host, String username, int port, Long userId) {
|
|
long id = idGen.incrementAndGet();
|
|
Connection conn = new Connection();
|
|
conn.setId(id);
|
|
conn.setUserId(userId);
|
|
conn.setName(host + "-quick");
|
|
conn.setHost(host);
|
|
conn.setPort(port);
|
|
conn.setUsername(username);
|
|
conn.setAuthType(Connection.AuthType.PASSWORD);
|
|
conn.setEncryptedPassword(null);
|
|
conn.setCreatedAt(Instant.now());
|
|
conn.setUpdatedAt(Instant.now());
|
|
entries.put(id, new Entry(conn));
|
|
return conn;
|
|
}
|
|
|
|
public Connection get(Long id) {
|
|
Entry entry = entries.get(id);
|
|
return entry != null ? entry.connection : null;
|
|
}
|
|
|
|
public void remove(Long id) {
|
|
entries.remove(id);
|
|
}
|
|
|
|
public int size() {
|
|
return entries.size();
|
|
}
|
|
|
|
private static class Entry {
|
|
final Connection connection;
|
|
final long createdAt = System.currentTimeMillis();
|
|
|
|
Entry(Connection connection) {
|
|
this.connection = connection;
|
|
}
|
|
}
|
|
}
|