Skip to content

Commit

Permalink
implement DNS customization
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Cai <[email protected]>
  • Loading branch information
Peter Cai committed Oct 24, 2015
1 parent ecb179a commit 4db4d02
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 10 deletions.
26 changes: 23 additions & 3 deletions app/src/main/java/net/typeblog/socks/ProfileFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public void onServiceDisconnected(ComponentName p1) {
private SocksVpnService.VpnBinder mBinder;

private ListPreference mPrefProfile, mPrefRoutes;
private EditTextPreference mPrefServer, mPrefPort, mPrefUsername, mPrefPassword;
private EditTextPreference mPrefServer, mPrefPort, mPrefUsername, mPrefPassword,
mPrefDns, mPrefDnsPort;
private CheckBoxPreference mPrefUserpw;

@Override
Expand Down Expand Up @@ -128,6 +129,17 @@ public boolean onPreferenceChange(Preference p, Object newValue) {
mProfile.setRoute(newValue.toString());
resetListN(mPrefRoutes, newValue);
return true;
} else if (p == mPrefDns) {
mProfile.setDns(newValue.toString());
resetTextN(mPrefDns, newValue);
return true;
} else if (p == mPrefDnsPort) {
if (TextUtils.isEmpty(newValue.toString()))
return false;

mProfile.setDnsPort(Integer.valueOf(newValue.toString()));
resetTextN(mPrefDnsPort, newValue);
return true;
} else {
return false;
}
Expand All @@ -151,7 +163,9 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
.putExtra(INTENT_NAME, mProfile.getName())
.putExtra(INTENT_SERVER, mProfile.getServer())
.putExtra(INTENT_PORT, mProfile.getPort())
.putExtra(INTENT_ROUTE, mProfile.getRoute());
.putExtra(INTENT_ROUTE, mProfile.getRoute())
.putExtra(INTENT_DNS, mProfile.getDns())
.putExtra(INTENT_DNS_PORT, mProfile.getDnsPort());

if (mProfile.isUserPw()) {
i.putExtra(INTENT_USERNAME, mProfile.getUsername())
Expand All @@ -172,6 +186,8 @@ private void initPreferences() {
mPrefUsername = (EditTextPreference) findPreference(PREF_AUTH_USERNAME);
mPrefPassword = (EditTextPreference) findPreference(PREF_AUTH_PASSWORD);
mPrefRoutes = (ListPreference) findPreference(PREF_ADV_ROUTE);
mPrefDns = (EditTextPreference) findPreference(PREF_ADV_DNS);
mPrefDnsPort = (EditTextPreference) findPreference(PREF_ADV_DNS_PORT);

mPrefProfile.setOnPreferenceChangeListener(this);
mPrefServer.setOnPreferenceChangeListener(this);
Expand All @@ -180,6 +196,8 @@ private void initPreferences() {
mPrefUsername.setOnPreferenceChangeListener(this);
mPrefPassword.setOnPreferenceChangeListener(this);
mPrefRoutes.setOnPreferenceChangeListener(this);
mPrefDns.setOnPreferenceChangeListener(this);
mPrefDnsPort.setOnPreferenceChangeListener(this);
}

private void reload() {
Expand All @@ -199,7 +217,9 @@ private void reload() {
mPrefPort.setText(String.valueOf(mProfile.getPort()));
mPrefUsername.setText(mProfile.getUsername());
mPrefPassword.setText(mProfile.getPassword());
resetText(mPrefServer, mPrefPort, mPrefUsername, mPrefPassword);
mPrefDns.setText(mProfile.getDns());
mPrefDnsPort.setText(String.valueOf(mProfile.getDnsPort()));
resetText(mPrefServer, mPrefPort, mPrefUsername, mPrefPassword, mPrefDns, mPrefDnsPort);
}

private void resetList(ListPreference... pref) {
Expand Down
8 changes: 5 additions & 3 deletions app/src/main/java/net/typeblog/socks/SocksVpnService.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public int onStartCommand(Intent intent, int flags, int startId) {
final String username = intent.getStringExtra(INTENT_USERNAME);
final String passwd = intent.getStringExtra(INTENT_PASSWORD);
final String route = intent.getStringExtra(INTENT_ROUTE);
final String dns = intent.getStringExtra(INTENT_DNS);
final int dnsPort = intent.getIntExtra(INTENT_DNS_PORT, 53);

// Create an fd.
configure(name, route);
Expand All @@ -47,7 +49,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "fd: " + mInterface.getFd());

if (mInterface != null)
start(mInterface.getFd(), server, port, username, passwd, "8.8.8.8");
start(mInterface.getFd(), server, port, username, passwd, dns, dnsPort);

return START_STICKY;
}
Expand Down Expand Up @@ -106,9 +108,9 @@ private void configure(String name, String route) {
}
}

private void start(int fd, String server, int port, String user, String passwd, String dns) {
private void start(int fd, String server, int port, String user, String passwd, String dns, int dnsPort) {
// Start DNS daemon first
Utility.makePdnsdConf(this, dns);
Utility.makePdnsdConf(this, dns, dnsPort);

Utility.exec(String.format("%s/pdnsd -c %s/pdnsd.conf", DIR, DIR));

Expand Down
8 changes: 6 additions & 2 deletions app/src/main/java/net/typeblog/socks/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public class Constants
INTENT_PORT = INTENT_PREFIX + "PORT",
INTENT_USERNAME = INTENT_PREFIX + "UNAME",
INTENT_PASSWORD = INTENT_PREFIX + "PASSWD",
INTENT_ROUTE = INTENT_PREFIX + "ROUTE";
INTENT_ROUTE = INTENT_PREFIX + "ROUTE",
INTENT_DNS = INTENT_PREFIX + "DNS",
INTENT_DNS_PORT = INTENT_PREFIX + "DNSPORT";

public static final String PREF = "profile",
PREF_PROFILE = "profile",
Expand All @@ -25,5 +27,7 @@ public class Constants
PREF_AUTH_USERPW = "auth_userpw",
PREF_AUTH_USERNAME = "auth_username",
PREF_AUTH_PASSWORD = "auth_password",
PREF_ADV_ROUTE = "adv_route";
PREF_ADV_ROUTE = "adv_route",
PREF_ADV_DNS = "adv_dns",
PREF_ADV_DNS_PORT = "adv_dns_port";
}
18 changes: 18 additions & 0 deletions app/src/main/java/net/typeblog/socks/util/Profile.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ public void setRoute(String route) {
mPref.edit().putString(key("route"), route).commit();
}

public String getDns() {
return mPref.getString(key("dns"), "8.8.8.8");
}

public void setDns(String dns) {
mPref.edit().putString(key("dns"), dns).commit();
}

public int getDnsPort() {
return mPref.getInt(key("dns_port"), 53);
}

public void setDnsPort(int port) {
mPref.edit().putInt(key("dns_port"), port).commit();
}

void delete() {
mPref.edit()
.remove(key("server"))
Expand All @@ -78,6 +94,8 @@ void delete() {
.remove(key("username"))
.remove(key("password"))
.remove(key("route"))
.remove(key("dns"))
.remove(key("dns_port"))
.commit();
}

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/net/typeblog/socks/util/Utility.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ public static String join(List<String> list, String separator) {
return ret.substring(0, ret.length() - separator.length());
}

public static void makePdnsdConf(Context context, String dns) {
String conf = String.format(context.getString(R.string.pdnsd_conf), dns);
public static void makePdnsdConf(Context context, String dns, int port) {
String conf = String.format(context.getString(R.string.pdnsd_conf), dns, port);

File f = new File(DIR + "/pdnsd.conf");

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/pdnsd.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ global {
server {
label= "upstream";
ip = %s;
port = %d;
uptest = none;
}

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
<string name="adv_route">Route</string>
<string name="adv_route_all">All (Default)</string>
<string name="adv_route_non_chn">Non-Chinese IPs</string>
<string name="adv_dns">DNS Server</string>
<string name="adv_dns_port">DNS Port (TCP)</string>

<string name="prof_add">Add profile</string>
<string name="prof_del">Delete profile</string>
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/xml/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@
android:entries="@array/adv_routes"
android:entryValues="@array/adv_routes_values"/>

<EditTextPreference
android:key="adv_dns"
android:title="@string/adv_dns"/>

<EditTextPreference
android:key="adv_dns_port"
android:title="@string/adv_dns_port"
android:inputType="number"/>

</PreferenceCategory>

</PreferenceScreen>

0 comments on commit 4db4d02

Please sign in to comment.