提交 d8542d7b authored 作者: Thomas Mueller's avatar Thomas Mueller

allow remote connections but only allow to open this database

上级 f9b48da1
......@@ -511,14 +511,14 @@ public class ConnectionInfo implements Cloneable {
}
/**
* Switch to server mode.
* Switch to server mode, and set the server name and database key.
*
* @param server the server
* @param serverKey the server name, '/', and the security key
*/
public void setServer(String server) {
public void setServerKey(String serverKey) {
remote = true;
persistent = false;
name = server + "/" + name;
this.name = serverKey;
}
}
......@@ -117,8 +117,13 @@ public class Constants {
/**
* The minor version of this product.
*/
public static final int VERSION_MINOR = 0;
public static final int VERSION_MINOR = 1;
/**
* The version number (major.minor) of this product.
*/
public static final double VERSION = VERSION_MAJOR + VERSION_MINOR / 10.;
/**
* If empty b-tree pages are allowed. This is supported for backward
* compatibility.
......
......@@ -527,7 +527,7 @@ public class Database implements DataHandler {
lock = new FileLock(traceSystem, Constants.LOCK_SLEEP);
lock.lock(databaseName + Constants.SUFFIX_LOCK_FILE, fileLockMethod == FileLock.LOCK_SOCKET);
if (autoServerMode) {
startServer();
startServer(lock.getUniqueId());
}
}
deleteOldTempFiles();
......@@ -622,8 +622,11 @@ public class Database implements DataHandler {
traceSystem.getTrace(Trace.DATABASE).info("opened " + databaseName);
}
private void startServer() throws SQLException {
server = Server.createTcpServer(new String[]{"-tcpPort", "0"});
private void startServer(String key) throws SQLException {
server = Server.createTcpServer(new String[]{
"-tcpPort", "0",
"-tcpAllowOthers", "true",
"-key", key, databaseName});
server.start();
String address = NetUtils.getLocalAddress() + ":" + server.getPort();
lock.addProperty("server", address);
......
......@@ -117,9 +117,9 @@ public class JdbcConnection extends TraceObject implements Connection {
int errorCode = e.getErrorCode();
if (errorCode == ErrorCode.DATABASE_ALREADY_OPEN_1) {
if (autoServerMode) {
String server = (String) ((JdbcSQLException) e).getPayload();
if (server != null) {
backup.setServer(server);
String serverKey = (String) ((JdbcSQLException) e).getPayload();
if (serverKey != null) {
backup.setServerKey(serverKey);
session = new SessionRemote().createSession(backup);
}
}
......
......@@ -23,6 +23,7 @@ import java.util.Properties;
import java.util.Set;
import org.h2.Driver;
import org.h2.constant.ErrorCode;
import org.h2.constant.SysProperties;
import org.h2.engine.Constants;
import org.h2.message.Message;
......@@ -72,6 +73,7 @@ public class TcpServer implements Service {
private String managementPassword = "";
private Thread listenerThread;
private int nextThreadId;
private String key, keyDatabase;
/**
* Get the database name of the management database.
......@@ -169,6 +171,9 @@ public class TcpServer implements Service {
managementPassword = args[++i];
} else if ("-baseDir".equals(a)) {
baseDir = args[++i];
} else if ("-key".equals(a)) {
key = args[++i];
keyDatabase = args[++i];
} else if ("-tcpAllowOthers".equals(a)) {
if (Tool.readArgBoolean(args, i) != 0) {
allowOthers = Tool.readArgBoolean(args, i) == 1;
......@@ -448,4 +453,23 @@ public class TcpServer implements Service {
}
}
/**
* If no key is set, return the original database name. If a key is set,
* check if the key matches. If yes, return the correct database name. If
* not, throw an exception.
*
* @param db the key to test (or database name if no key is used)
* @return the database name
* @throws SQLException if a key is set but doesn't match
*/
public String checkKeyAndGetDatabaseName(String db) throws SQLException {
if (key == null) {
return db;
}
if (key.equals(db)) {
return keyDatabase;
}
throw Message.getSQLException(ErrorCode.WRONG_USER_OR_PASSWORD);
}
}
......@@ -62,8 +62,8 @@ public class TcpServerThread implements Runnable {
try {
transfer.init();
trace("Connect");
// TODO server: should support a list of allowed databases and a
// list of allowed clients
// TODO server: should support a list of allowed databases
// and a list of allowed clients
try {
clientVersion = transfer.readInt();
if (!server.allow(transfer.getSocket())) {
......@@ -91,6 +91,7 @@ public class TcpServerThread implements Runnable {
if (baseDir == null) {
baseDir = SysProperties.getBaseDir();
}
db = server.checkKeyAndGetDatabaseName(db);
ConnectionInfo ci = new ConnectionInfo(db);
if (baseDir != null) {
ci.setBaseDir(baseDir);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论