Skip to content

Commit

Permalink
refine trojan config parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
wongsyrone committed Feb 16, 2020
1 parent 42d6ceb commit f132f7c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
15 changes: 9 additions & 6 deletions app/src/main/java/io/github/trojan_gfw/igniter/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,15 @@ protected void onResume() {
try (FileInputStream fis = new FileInputStream(file)) {
byte[] content = new byte[(int) file.length()];
fis.read(content);
JSONObject json = new JSONObject(new String(content));
remoteAddrText.setText(json.getString("remote_addr"));
remotePortText.setText(String.valueOf(json.getInt("remote_port")));
passwordText.setText(json.getJSONArray("password").getString(0));
ipv6Switch.setChecked(json.getBoolean("enable_ipv6"));
verifySwitch.setChecked(json.getJSONObject("ssl").getBoolean("verify"));
String contentStr = new String(content);
TrojanConfig ins = Globals.getTrojanConfigInstance();
ins.fromJSON(contentStr);

remoteAddrText.setText(ins.getRemoteAddr());
remotePortText.setText(String.valueOf(ins.getRemotePort()));
passwordText.setText(ins.getPassword());
ipv6Switch.setChecked(ins.getEnableIpv6());
verifySwitch.setChecked(ins.getVerifyCert());
}
} catch (Exception e) {
e.printStackTrace();
Expand Down
20 changes: 18 additions & 2 deletions app/src/main/java/io/github/trojan_gfw/igniter/TrojanConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ public String generateTrojanConfigJSON() {
}
}

public void fromJSON(String jsonStr) {
try {
JSONObject json = new JSONObject(jsonStr);
this.setLocalAddr(json.getString("local_addr"))
.setLocalPort(json.getInt("local_port"))
.setRemoteAddr(json.getString("remote_addr"))
.setRemotePort(json.getInt("remote_port"))
.setPassword(json.getJSONArray("password").getString(0))
.setEnableIpv6(json.getBoolean("enable_ipv6"))
.setVerifyCert(json.getJSONObject("ssl").getBoolean("verify"));

} catch (Exception e) {
e.printStackTrace();
}
}

public boolean isValidRunningConfig() {
return !TextUtils.isEmpty(this.caCertPath)
&& !TextUtils.isEmpty(this.remoteAddr)
Expand Down Expand Up @@ -119,7 +135,7 @@ public TrojanConfig setPassword(String password) {
return this;
}

public boolean isVerifyCert() {
public boolean getVerifyCert() {
return verifyCert;
}

Expand All @@ -137,7 +153,7 @@ public TrojanConfig setCaCertPath(String caCertPath) {
return this;
}

public boolean isEnableIpv6() {
public boolean getEnableIpv6() {
return enableIpv6;
}

Expand Down

0 comments on commit f132f7c

Please sign in to comment.