Skip to content
Snippets Groups Projects
Commit 59942b97 authored by Léo Banno-Cloutier's avatar Léo Banno-Cloutier
Browse files

ServerParameters: use backend_address for domain name

Change-Id: I952b4bd430efa36f9e060ec1f8954703549e8fc8
parent aef8c513
No related branches found
No related tags found
No related merge requests found
......@@ -23,6 +23,7 @@
package net.jami.jams.nameserver;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import lombok.extern.slf4j.Slf4j;
......@@ -30,8 +31,6 @@ import net.jami.jams.common.jami.NameLookupResponse;
import net.jami.jams.common.jami.NameRegistrationRequest;
import net.jami.jams.common.jami.NameServer;
import net.jami.jams.common.serialization.adapters.GsonFactory;
import net.minidev.json.JSONObject;
import net.minidev.json.parser.JSONParser;
import java.net.HttpURLConnection;
import java.net.URL;
......@@ -90,20 +89,15 @@ public class PublicNameServer implements NameServer {
URL url = new URL(nameserverURI + "/addr/" + address);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
if (con.getResponseCode() == 200) {
StringBuilder responseData = new StringBuilder();
int respSize = Integer.parseInt(con.getHeaderField("Content-Length"));
int currentSize = 0;
while (currentSize < respSize) {
responseData.append((char) con.getInputStream().read());
currentSize++;
}
log.info("Response received from public nameserver {} ", responseData);
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(responseData.toString());
return json.getAsString("name");
if (con.getResponseCode() != 200) {
return null;
}
return null;
String responseData = new String(con.getInputStream().readAllBytes());
log.info("Response received from public nameserver {} ", responseData);
JsonObject json = gson.fromJson(responseData.toString(), JsonObject.class);
return json.get("name").getAsString();
} catch (Exception e) {
log.info("An error occurred while querying the public nameserver {} ", e.toString());
return null;
......
......@@ -14,7 +14,10 @@ import * as tool from "../../tools";
import axios from "axios";
import configApiCall from "../../api";
import { api_path_post_install_server } from "../../globalUrls";
import {
api_path_post_install_server,
backend_address,
} from "../../globalUrls";
import auth from "auth";
import * as Yup from "yup";
......@@ -40,15 +43,17 @@ const useStyles = makeStyles((theme) => ({
},
}));
export default function ServerParameters(props) {
export default function ServerParameters({ setError, setErrorMessage }) {
// Formik validation fields
const initialValues = { domain: window.location.origin };
const initialValues = { domain: backend_address.origin };
const validationSchema = Yup.object().shape({
domain: Yup.string().required(
i18next.t("domain_is_required", "Domain is required.")
),
});
type Settings = Yup.InferType<typeof validationSchema>;
const certificateRevocationTypes = [
{ value: 300000, label: i18next.t("5_minutes", "5 minutes") },
{ value: 600000, label: i18next.t("10_minutes", "10 minutes") },
......@@ -108,7 +113,7 @@ export default function ServerParameters(props) {
});
}
function handleSubmit(values) {
function handleSubmit(values: Settings) {
let jsonData = {};
let re = new RegExp(/^http[s]?:\/\/\w+(\.\w+)*(:[0-9]+)?\/?(\/[.\w]*)*$/);
let nohttpre = new RegExp(/^\w+(\.\w+)*(:[0-9]+)?\/?(\/[.\w]*)*$/);
......@@ -133,8 +138,8 @@ export default function ServerParameters(props) {
console.log("Error installing server parameters: " + error);
});
} else {
props.setError(true);
props.setErrorMessage(
setError(true);
setErrorMessage(
i18next.t(
"please_enter_valid_cors_domain_url",
"Please enter a valid CORS domain URL."
......@@ -173,15 +178,14 @@ export default function ServerParameters(props) {
handleSubmit(values);
}}
>
{(props) => {
const {
values,
touched,
errors,
handleSubmit,
handleChange,
handleBlur,
} = props;
{({
values,
touched,
errors,
handleSubmit,
handleChange,
handleBlur,
}) => {
return (
<form className={classes.form} noValidate onSubmit={handleSubmit}>
<Typography variant="h5" gutterBottom color="primary">
......
......@@ -104,68 +104,22 @@ public class Server {
dataStore = new DataStore("jdbc:derby:jams;create=true");
// Create image folder if it doesn't exist.
File imagesFolder = new File("." + File.separator + "images");
if (!imagesFolder.exists()) {
boolean created = imagesFolder.mkdirs();
if (created) {
log.info("Created images folder");
} else {
log.error("Failed to create images folder");
System.exit(-1);
}
}
createImageFolder();
isInstalled.set(
new File(System.getProperty("user.dir") + File.separator + "config.json").exists());
File configJsonFile =
new File(System.getProperty("user.dir") + File.separator + "config.json");
isInstalled.set(configJsonFile.exists());
log.info("Server is already installed: " + isInstalled.get());
ServerSettings serverSettings = null;
if (isInstalled.get()) {
try {
Reader reader =
new FileReader(
new File(
System.getProperty("user.dir")
+ File.separator
+ "config.json"));
serverSettings = gson.fromJson(reader, ServerSettings.class);
certificateAuthority =
CryptoEngineLoader.loadCertificateAuthority(
serverSettings.getCaConfiguration(), dataStore);
userAuthenticationModule =
AuthModuleLoader.loadAuthenticationModule(dataStore, certificateAuthority);
if (serverSettings.getLdapConfiguration() != null)
userAuthenticationModule.attachAuthSource(
AuthenticationSourceType.LDAP, serverSettings.getLdapConfiguration());
if (serverSettings.getActiveDirectoryConfiguration() != null) {
userAuthenticationModule.attachAuthSource(
AuthenticationSourceType.AD,
serverSettings.getActiveDirectoryConfiguration());
}
if (serverSettings.getLocalDirectoryConfiguration() != null) {
LocalAuthSettings settings =
gson.fromJson(
serverSettings.getLocalDirectoryConfiguration(),
LocalAuthSettings.class);
if (settings.getPublicNames())
nameServer = new PublicNameServer(settings.getPublicNameServer());
else
nameServer =
new LocalNameServer(
dataStore,
userAuthenticationModule,
serverSettings.getServerPublicURI());
} else
nameServer =
new LocalNameServer(
dataStore,
userAuthenticationModule,
serverSettings.getServerPublicURI());
licenseService.loadLicense();
Reader reader = new FileReader(configJsonFile);
loadConfig(reader);
log.info("All services are UP and ready for use...");
} catch (Exception e) {
log.error(
"Could not load configuration file or initialize some components - this is critical");
System.exit(1);
}
} else {
certificateAuthority = CryptoEngineLoader.loadCertificateAuthority(null, dataStore);
......@@ -183,6 +137,60 @@ public class Server {
}
}
private static void createImageFolder() {
File imagesFolder = new File("." + File.separator + "images");
if (!imagesFolder.exists()) {
boolean created = imagesFolder.mkdirs();
if (created) {
log.info("Created images folder");
} else {
log.error("Failed to create images folder");
System.exit(-1);
}
}
}
private static void loadConfig(Reader reader) {
ServerSettings serverSettings = gson.fromJson(reader, ServerSettings.class);
certificateAuthority =
CryptoEngineLoader.loadCertificateAuthority(
serverSettings.getCaConfiguration(), dataStore);
userAuthenticationModule =
AuthModuleLoader.loadAuthenticationModule(dataStore, certificateAuthority);
if (serverSettings.getLdapConfiguration() != null)
userAuthenticationModule.attachAuthSource(
AuthenticationSourceType.LDAP, serverSettings.getLdapConfiguration());
if (serverSettings.getActiveDirectoryConfiguration() != null) {
userAuthenticationModule.attachAuthSource(
AuthenticationSourceType.AD, serverSettings.getActiveDirectoryConfiguration());
}
if (serverSettings.getLocalDirectoryConfiguration() != null) {
LocalAuthSettings settings =
gson.fromJson(
serverSettings.getLocalDirectoryConfiguration(),
LocalAuthSettings.class);
if (settings.getPublicNames())
nameServer = new PublicNameServer(settings.getPublicNameServer());
else
nameServer =
new LocalNameServer(
dataStore,
userAuthenticationModule,
serverSettings.getServerPublicURI());
} else
nameServer =
new LocalNameServer(
dataStore,
userAuthenticationModule,
serverSettings.getServerPublicURI());
licenseService.loadLicense();
}
public static void startGUI() throws Exception {
boolean ready = false;
while (!ready) {
......@@ -190,6 +198,7 @@ public class Server {
if (userAuthenticationModule.getAuthModulePubKey() != null) {
ready = true;
}
Thread.sleep(10);
}
log.info("Authentication module is ready!");
if (Desktop.isDesktopSupported()
......
......@@ -24,6 +24,8 @@ package net.jami.jams.server.servlets.api.jaminameserver;
import static net.jami.jams.server.Server.nameServer;
import com.google.gson.Gson;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
......@@ -31,11 +33,12 @@ import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import net.jami.jams.common.annotations.JsonContent;
import net.jami.jams.common.serialization.adapters.GsonFactory;
import net.jami.jams.common.serialization.tomcat.TomcatCustomErrorHandler;
import org.json.JSONObject;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@WebServlet("/api/nameserver/addr/*")
public class AddressServlet extends HttpServlet {
......@@ -62,12 +65,14 @@ public class AddressServlet extends HttpServlet {
String[] path = req.getPathInfo().split("/");
String username = nameServer.getNameFromAddress(path[path.length - 1]);
JSONObject obj = new JSONObject();
obj.put("name", username);
resp.setContentType("application/json;charset=UTF-8");
if (username == null)
if (username == null) {
TomcatCustomErrorHandler.sendCustomError(resp, 404, "Address not found!");
else resp.getWriter().write(obj.toString());
return;
}
Gson gson = GsonFactory.createGson();
Map<String, String> response = new HashMap<>();
response.put("name", username);
resp.getWriter().write(gson.toJson(response));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment