Skip to content

Commit

Permalink
feat: Customize server and dns server
Browse files Browse the repository at this point in the history
  • Loading branch information
oldkingOK committed Jul 28, 2024
1 parent bb67eae commit 8990fc9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
16 changes: 15 additions & 1 deletion app/src/main/java/cx/myth/zjuconnect/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ protected void onCreate(Bundle savedInstanceState) {
((EditText) findViewById(R.id.usernameEditText)).setText(value);
value = sharedPreferences.getString("password", "");
((EditText) findViewById(R.id.passwordEditText)).setText(value);
value = sharedPreferences.getString("server", getString(R.string.default_server));
((EditText) findViewById(R.id.serverEditText)).setText(value);
value = sharedPreferences.getString("dns_server", getString(R.string.default_dns_server));
((EditText) findViewById(R.id.dns_serverEditText)).setText(value);

Intent explicitIntent = new Intent("cx.myth.zjuconnect.LOGIN_FAILED");
explicitIntent.setPackage("cx.myth.zjuconnect");
Expand Down Expand Up @@ -188,11 +192,18 @@ protected void onCreate(Bundle savedInstanceState) {

findViewById(R.id.advancedSettings).setVisibility(isChecked ? View.VISIBLE : View.INVISIBLE);
});

findViewById(R.id.resetServerButton).setOnClickListener(v -> {
((EditText) findViewById(R.id.serverEditText)).setText(getString(R.string.default_server));
});
findViewById(R.id.resetDnsServerButton).setOnClickListener(v -> {
((EditText) findViewById(R.id.dns_serverEditText)).setText(getString(R.string.default_dns_server));
});
}

private void startVpnService() {
new Thread(() -> {
String host = "vpn.hitsz.edu.cn";
String host = ((EditText) findViewById(R.id.serverEditText)).getText().toString();

try {
InetAddress address = InetAddress.getByName(host);
Expand All @@ -208,6 +219,7 @@ private void startVpnService() {
intent.putExtra("server", hostIp + ":443");
intent.putExtra("username", ((EditText) findViewById(R.id.usernameEditText)).getText().toString());
intent.putExtra("password", ((EditText) findViewById(R.id.passwordEditText)).getText().toString());
intent.putExtra("dns_server", ((EditText) findViewById(R.id.dns_serverEditText)).getText().toString());
startService(intent);

binding.fab.setEnabled(false);
Expand All @@ -218,6 +230,8 @@ private void startVpnService() {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("username", ((EditText) findViewById(R.id.usernameEditText)).getText().toString());
editor.putString("password", ((EditText) findViewById(R.id.passwordEditText)).getText().toString());
editor.putString("server", ((EditText) findViewById(R.id.serverEditText)).getText().toString());
editor.putString("dns_server", ((EditText) findViewById(R.id.dns_serverEditText)).getText().toString());
editor.apply();
}

Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/cx/myth/zjuconnect/MyVpnService.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {

new Thread(() -> {
String ip = Mobile.login(intent.getStringExtra("server"), intent.getStringExtra("username"), intent.getStringExtra("password"));
String dnsServer = intent.getStringExtra("dns_server");
if (ip.equals("")) {
localBroadcastManager.sendBroadcast(new Intent("cx.myth.zjuconnect.LOGIN_FAILED"));
stopSelf();
Expand All @@ -45,7 +46,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {

localBroadcastManager.sendBroadcast(new Intent("cx.myth.zjuconnect.LOGIN_SUCCEEDED"));

Builder builder = new Builder().addAddress(ip, 8).addRoute("10.0.0.0", 8).addDnsServer("10.248.98.30").setMtu(1400);
Builder builder = new Builder().addAddress(ip, 8).addRoute("10.0.0.0", 8).addDnsServer(dnsServer).setMtu(1400);
tun = builder.establish();

executors.submit(() -> {
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<resources>
<string name="app_name">HITSZ Connect</string>
<string name="default_server" translatable="false">vpn.hitsz.edu.cn</string>
<string name="default_dns_server" translatable="false">10.248.98.30</string>

<!-- Strings used for fragments for navigation -->
<string name="previous">Previous</string>

Expand Down

0 comments on commit 8990fc9

Please sign in to comment.