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