Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/main/java/core/packetproxy/common/ConfigDaoHub.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2026 DeNA Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package packetproxy.common;

import com.google.gson.annotations.SerializedName;
import java.util.List;
import packetproxy.model.*;

/** 設定エクスポート/インポート用 JSON の DTO。 */
public class ConfigDaoHub {

@SerializedName(value = "listenPorts")
List<ListenPort> listenPortList;

@SerializedName(value = "servers")
List<Server> serverList;

@SerializedName(value = "modifications")
List<Modification> modificationList;

@SerializedName(value = "sslPassThroughs")
List<SSLPassThrough> sslPassThroughList;
}
63 changes: 11 additions & 52 deletions src/main/java/core/packetproxy/common/ConfigHttpServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,24 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;
import fi.iki.elonen.NanoHTTPD;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.*;
import packetproxy.gui.GUIMain;
import packetproxy.model.*;

public class ConfigHttpServer extends NanoHTTPD {

private final String allowedAccessToken;
private final ConfigHttpUiActions uiActions;
private final ConfigSettingsWriter settingsWriter;

private static class DaoHub {

@SerializedName(value = "listenPorts")
List<ListenPort> listenPortList;

@SerializedName(value = "servers")
List<Server> serverList;

@SerializedName(value = "modifications")
List<Modification> modificationList;

@SerializedName(value = "sslPassThroughs")
List<SSLPassThrough> sslPassThroughList;
}

public ConfigHttpServer(String hostname, int port, String allowedAccessToken) {
public ConfigHttpServer(String hostname, int port, String allowedAccessToken, ConfigHttpUiActions uiActions,
ConfigSettingsWriter settingsWriter) {
super(hostname, port);
this.allowedAccessToken = allowedAccessToken;
this.uiActions = uiActions;
this.settingsWriter = settingsWriter;
}

private void fixUpServerList(Map<Integer, Integer> serverMap, List<Server> serverList) {
Expand Down Expand Up @@ -74,7 +61,7 @@ private void fixUpModificationList(Map<Integer, Integer> serverMap, List<Modific
}
}

private void fixUp(DaoHub daoHub) {
private void fixUp(ConfigDaoHub daoHub) {
Map<Integer, Integer> serverMap = new HashMap<>();
fixUpServerList(serverMap, daoHub.serverList);
fixUpListenPortList(serverMap, daoHub.listenPortList);
Expand Down Expand Up @@ -107,7 +94,7 @@ public Response serve(IHTTPSession session) {

try {

DaoHub daoHub = new DaoHub();
ConfigDaoHub daoHub = new ConfigDaoHub();

daoHub.listenPortList = ListenPorts.getInstance().queryAll();
daoHub.serverList = Servers.getInstance().queryAll();
Expand All @@ -133,18 +120,9 @@ public Response serve(IHTTPSession session) {

try {

GUIMain.getInstance().setAlwaysOnTop(true);
GUIMain.getInstance().setVisible(true);

GUIMain.getInstance().getTabbedPane().setSelectedIndex(GUIMain.Panes.OPTIONS.ordinal());

int option = JOptionPane.showConfirmDialog(GUIMain.getInstance(),
I18nString.get("Do you want to overwrite config?"), I18nString.get("Loading config"),
JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
uiActions.showOptionsTab();

GUIMain.getInstance().setAlwaysOnTop(false);

if (option == JOptionPane.NO_OPTION) {
if (!uiActions.confirmOverwriteConfig()) {

return NanoHTTPD.newFixedLengthResponse(Response.Status.UNAUTHORIZED, MIME_HTML, null);
}
Expand All @@ -153,26 +131,7 @@ public Response serve(IHTTPSession session) {
session.parseBody(map);
String json = map.get("postData");

DaoHub daoHub = new Gson().fromJson(json, DaoHub.class);

Database.getInstance().dropConfigs();

for (ListenPort listenPort : daoHub.listenPortList) {

ListenPorts.getInstance().create(listenPort);
}
for (Server server : daoHub.serverList) {

Servers.getInstance().create(server);
}
for (Modification mod : daoHub.modificationList) {

Modifications.getInstance().create(mod);
}
for (SSLPassThrough passThrough : daoHub.sslPassThroughList) {

SSLPassThroughs.getInstance().create(passThrough);
}
settingsWriter.applyFromJson(json);

Response res = NanoHTTPD.newFixedLengthResponse(Response.Status.OK, "application/json",
"{\"status\": \"ok\"}");
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/core/packetproxy/common/ConfigHttpUiActions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2026 DeNA Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package packetproxy.common;

/** リモート設定 HTTP API から必要な GUI 操作。 */
public interface ConfigHttpUiActions {

void showOptionsTab();

/**
* @return 設定上書きを許可する場合 true
*/
boolean confirmOverwriteConfig();
}
22 changes: 3 additions & 19 deletions src/main/java/core/packetproxy/common/ConfigIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -11,21 +10,6 @@

public class ConfigIO {

private static class DaoHub {

@SerializedName(value = "listenPorts")
List<ListenPort> listenPortList;

@SerializedName(value = "servers")
List<Server> serverList;

@SerializedName(value = "modifications")
List<Modification> modificationList;

@SerializedName(value = "sslPassThroughs")
List<SSLPassThrough> sslPassThroughList;
}

public ConfigIO() {
}

Expand Down Expand Up @@ -68,15 +52,15 @@ private void fixUpModificationList(Map<Integer, Integer> serverMap, List<Modific
}
}

private void fixUp(DaoHub daoHub) {
private void fixUp(ConfigDaoHub daoHub) {
Map<Integer, Integer> serverMap = new HashMap<>();
fixUpServerList(serverMap, daoHub.serverList);
fixUpListenPortList(serverMap, daoHub.listenPortList);
fixUpModificationList(serverMap, daoHub.modificationList);
}

public String getOptions() throws Exception {
DaoHub daoHub = new DaoHub();
ConfigDaoHub daoHub = new ConfigDaoHub();

daoHub.listenPortList = ListenPorts.getInstance().queryAll();
daoHub.serverList = Servers.getInstance().queryAll();
Expand All @@ -92,7 +76,7 @@ public String getOptions() throws Exception {
}

public void setOptions(String json) throws Exception {
DaoHub daoHub = new Gson().fromJson(json, DaoHub.class);
ConfigDaoHub daoHub = new Gson().fromJson(json, ConfigDaoHub.class);

Database.getInstance().dropConfigs();

Expand Down
46 changes: 46 additions & 0 deletions src/main/java/core/packetproxy/common/ConfigSettingsWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2026 DeNA Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package packetproxy.common;

import com.google.gson.Gson;
import packetproxy.model.*;

/** リモート設定 JSON を DB に反映する。 */
public class ConfigSettingsWriter {

public void applyFromJson(String json) throws Exception {
var daoHub = new Gson().fromJson(json, ConfigDaoHub.class);

Database.getInstance().dropConfigs();

for (ListenPort listenPort : daoHub.listenPortList) {

ListenPorts.getInstance().create(listenPort);
}
for (Server server : daoHub.serverList) {

Servers.getInstance().create(server);
}
for (Modification mod : daoHub.modificationList) {

Modifications.getInstance().create(mod);
}
for (SSLPassThrough passThrough : daoHub.sslPassThroughList) {

SSLPassThroughs.getInstance().create(passThrough);
}
}
}
4 changes: 3 additions & 1 deletion src/main/java/core/packetproxy/gui/GUIOptionHubServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import javax.swing.*;
import org.apache.commons.lang3.RandomStringUtils;
import packetproxy.common.ConfigHttpServer;
import packetproxy.common.ConfigSettingsWriter;
import packetproxy.common.I18nString;
import packetproxy.model.ConfigBoolean;
import packetproxy.model.ConfigString;
Expand Down Expand Up @@ -104,7 +105,8 @@ private void stopServer() {

private void startServer() throws Exception {
String accessToken = new ConfigString("SharingConfigsAccessToken").getString();
this.server = new ConfigHttpServer("localhost", 32349, accessToken);
this.server = new ConfigHttpServer("localhost", 32349, accessToken, new SwingConfigHttpUiActions(),
new ConfigSettingsWriter());
this.server.start();
}

Expand Down
59 changes: 59 additions & 0 deletions src/main/java/core/packetproxy/gui/SwingConfigHttpUiActions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2026 DeNA Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package packetproxy.gui;

import javax.swing.JOptionPane;
import packetproxy.common.ConfigHttpUiActions;
import packetproxy.common.I18nString;

public class SwingConfigHttpUiActions implements ConfigHttpUiActions {

@Override
public void showOptionsTab() {
try {
showOptionsTabInternal();
} catch (Exception e) {
throw new IllegalStateException(e);
}
}

private void showOptionsTabInternal() throws Exception {
var main = GUIMain.getInstance();
main.setAlwaysOnTop(true);
main.setVisible(true);
main.getTabbedPane().setSelectedIndex(GUIMain.Panes.OPTIONS.ordinal());
main.setAlwaysOnTop(false);
}

@Override
public boolean confirmOverwriteConfig() {
try {
return confirmOverwriteConfigInternal();
} catch (Exception e) {
throw new IllegalStateException(e);
}
}

private boolean confirmOverwriteConfigInternal() throws Exception {
var main = GUIMain.getInstance();
main.setAlwaysOnTop(true);
main.setVisible(true);
int option = JOptionPane.showConfirmDialog(main, I18nString.get("Do you want to overwrite config?"),
I18nString.get("Loading config"), JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
main.setAlwaysOnTop(false);
return option == JOptionPane.YES_OPTION;
}
}
Loading