Skip to content

Commit

Permalink
Add IPv6 togglability
Browse files Browse the repository at this point in the history
  • Loading branch information
GreaterFire committed Apr 28, 2019
1 parent 34c4eb9 commit 83f7724
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
18 changes: 11 additions & 7 deletions app/src/main/java/io/github/trojan_gfw/igniter/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ public class MainActivity extends AppCompatActivity {
private EditText remoteAddrText;
private EditText remotePortText;
private EditText passwordText;
private Switch ipv6Switch;
private Switch verifySwitch;
private Button startStopButton;

private String getConfig(String remoteAddr, int remotePort, String password, boolean verify) {
private String getConfig(String remoteAddr, int remotePort, String password, boolean enableIpv6, boolean verify) {
try {
return new JSONObject()
.put("local_addr", "127.0.0.1")
Expand All @@ -42,6 +43,7 @@ private String getConfig(String remoteAddr, int remotePort, String password, boo
.put("cert", getCacheDir() + "/cacert.pem")
.put("cipher", "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305-SHA256:ECDHE-RSA-CHACHA20-POLY1305-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-RSA-AES256-SHA:RSA-AES128-GCM-SHA256:RSA-AES256-GCM-SHA384:RSA-AES128-SHA:RSA-AES256-SHA:RSA-3DES-EDE-SHA")
.put("alpn", new JSONArray().put("h2").put("http/1.1")))
.put("enable_ipv6", enableIpv6)
.toString();
} catch (Exception e) {
e.printStackTrace();
Expand All @@ -56,6 +58,7 @@ protected void onCreate(Bundle savedInstanceState) {
remoteAddrText = findViewById(R.id.remoteAddrText);
remotePortText = findViewById(R.id.remotePortText);
passwordText = findViewById(R.id.passwordText);
ipv6Switch = findViewById(R.id.ipv6Switch);
verifySwitch = findViewById(R.id.verifySwitch);
startStopButton = findViewById(R.id.startStopButton);
startStopButton.setOnClickListener(new View.OnClickListener() {
Expand All @@ -65,9 +68,9 @@ public void onClick(View v) {
String config = getConfig(remoteAddrText.getText().toString(),
Integer.parseInt(remotePortText.getText().toString()),
passwordText.getText().toString(),
ipv6Switch.isChecked(),
verifySwitch.isChecked());
File file = new File(getFilesDir(), "config.json");

try {
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write(config.getBytes());
Expand Down Expand Up @@ -96,6 +99,7 @@ public void onClick(View v) {
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"));
}
} catch (Exception e) {
Expand All @@ -107,11 +111,11 @@ public void onClick(View v) {
try {
try (InputStream is = getResources().openRawResource(R.raw.cacert);
FileOutputStream fos = new FileOutputStream(file)) {
byte[] buf = new byte[1024];
int len;
while ((len = is.read(buf)) > 0) {
fos.write(buf, 0, len);
}
byte[] buf = new byte[1024];
int len;
while ((len = is.read(buf)) > 0) {
fos.write(buf, 0, len);
}
}
} catch (Exception e) {
e.printStackTrace();
Expand Down
33 changes: 28 additions & 5 deletions app/src/main/java/io/github/trojan_gfw/igniter/TrojanService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
import android.net.VpnService;
import android.os.ParcelFileDescriptor;

import org.json.JSONObject;

import java.io.File;
import java.io.FileInputStream;

public class TrojanService extends VpnService {
private static final int VPN_MTU = 1500;
private static final String PRIVATE_VLAN4_CLIENT = "172.19.0.1";
Expand Down Expand Up @@ -38,21 +43,39 @@ public int onStartCommand(Intent intent, int flags, int startId) {
} catch (Exception e) {
e.printStackTrace();
}
boolean enable_ipv6 = false;
File file = new File(getFilesDir(), "config.json");
if (file.exists()) {
try {
try (FileInputStream fis = new FileInputStream(file)) {
byte[] content = new byte[(int) file.length()];
fis.read(content);
JSONObject json = new JSONObject(new String(content));
enable_ipv6 = json.getBoolean("enable_ipv6");
}
} catch (Exception e) {
e.printStackTrace();
}
}
b.setSession(getString(R.string.app_name));
b.setMtu(VPN_MTU);
b.addAddress(PRIVATE_VLAN4_CLIENT, 30);
b.addRoute("0.0.0.0", 0);
b.addAddress(PRIVATE_VLAN6_CLIENT, 126);
b.addRoute("::", 0);
if (enable_ipv6) {
b.addAddress(PRIVATE_VLAN6_CLIENT, 126);
b.addRoute("::", 0);
}
b.addDnsServer("8.8.8.8");
b.addDnsServer("8.8.4.4");
b.addDnsServer("1.1.1.1");
b.addDnsServer("1.0.0.1");
b.addDnsServer("2001:4860:4860::8888");
b.addDnsServer("2001:4860:4860::8844");
if (enable_ipv6) {
b.addDnsServer("2001:4860:4860::8888");
b.addDnsServer("2001:4860:4860::8844");
}
pfd = b.establish();
JNIHelper.trojan(getFilesDir() + "/config.json");
JNIHelper.n2s(pfd.getFd(), PRIVATE_VLAN4_ROUTER, "255.255.255.0", PRIVATE_VLAN6_ROUTER, VPN_MTU, "127.0.0.1", 1080);
JNIHelper.n2s(pfd.getFd(), PRIVATE_VLAN4_ROUTER, "255.255.255.253", PRIVATE_VLAN6_ROUTER, VPN_MTU, "127.0.0.1", 1080);
return START_STICKY;
}

Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@
android:hint="@string/password"
android:inputType="textPassword" />

<Switch
android:id="@+id/ipv6Switch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/enable_ipv6" />

<Switch
android:id="@+id/verifySwitch"
android:layout_width="match_parent"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
<string name="password">Password</string>
<string name="start_stop">Start/Stop</string>
<string name="verify_certificate">Verify Certificate</string>
<string name="enable_ipv6">Enable IPv6</string>
</resources>

0 comments on commit 83f7724

Please sign in to comment.