-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Large collection of changes to make android work
- Previous android java and jni code updated to work, but with much love still needed to make it work nicely, e.g. handling when the VPN is turned off. - DNS handling refactored to allow android to intercept and handle DNS requests as we can't set the system DNS to use a high port (and apparently Chrome ignores system DNS settings anyway) - add packet router structure to allow separate handling of specific intercepted traffic, e.g. UDP traffic to port 53 gets handled by our DNS handler rather than being naively forwarded as exit traffic. - For now, android lokinet is exit-only and hard-coded to use exit.loki as its exit. The exit will be configurable before release, but allowing to not use exit-only mode is more of a challenge. - some old gitignore remnants which were matching to things we don't want them to (and are no longer relevant) removed - some minor changes to CI configuration
- Loading branch information
Showing
43 changed files
with
1,033 additions
and
402 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,9 +40,6 @@ vsproject/ | |
.vs | ||
|
||
daemon.ini | ||
lokinet-win32.exe | ||
lokinet | ||
lokinet.exe | ||
|
||
|
||
.gradle/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package network.loki.lokinet; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
public class LokinetConfig | ||
{ | ||
static { | ||
System.loadLibrary("lokinet-android"); | ||
} | ||
|
||
private static native ByteBuffer Obtain(String dataDir); | ||
private static native void Free(ByteBuffer buf); | ||
|
||
/*** load config file from disk */ | ||
public native boolean Load(); | ||
/*** save chages to disk */ | ||
public native boolean Save(); | ||
|
||
|
||
/** override default config value before loading from config file */ | ||
public native void AddDefaultValue(String section, String key, String value); | ||
|
||
private final ByteBuffer impl; | ||
|
||
public LokinetConfig(String dataDir) | ||
{ | ||
impl = Obtain(dataDir); | ||
if(impl == null) | ||
throw new RuntimeException("cannot obtain config from "+dataDir); | ||
} | ||
|
||
public void finalize() | ||
{ | ||
if (impl != null) | ||
{ | ||
Free(impl); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
package network.loki.lokinet; | ||
|
||
import java.lang.Thread; | ||
import java.nio.ByteBuffer; | ||
import java.io.File; | ||
|
||
import android.net.VpnService; | ||
import android.util.Log; | ||
import android.content.Intent; | ||
import android.os.ParcelFileDescriptor; | ||
|
||
public class LokinetDaemon extends VpnService | ||
{ | ||
static { | ||
System.loadLibrary("lokinet-android"); | ||
} | ||
|
||
private static native ByteBuffer Obtain(); | ||
private static native void Free(ByteBuffer buf); | ||
public native boolean Configure(LokinetConfig config); | ||
public native int Mainloop(); | ||
public native boolean IsRunning(); | ||
public native boolean Stop(); | ||
public native void InjectVPNFD(); | ||
public native int GetUDPSocket(); | ||
|
||
private static native String DetectFreeRange(); | ||
|
||
public static final String LOG_TAG = "LokinetDaemon"; | ||
|
||
ByteBuffer impl = null; | ||
ParcelFileDescriptor iface; | ||
int m_FD = -1; | ||
int m_UDPSocket = -1; | ||
|
||
@Override | ||
public void onCreate() | ||
{ | ||
super.onCreate(); | ||
} | ||
|
||
@Override | ||
public void onDestroy() | ||
{ | ||
super.onDestroy(); | ||
|
||
if (IsRunning()) | ||
{ | ||
Stop(); | ||
} | ||
if (impl != null) | ||
{ | ||
Free(impl); | ||
impl = null; | ||
} | ||
} | ||
|
||
public int onStartCommand(Intent intent, int flags, int startID) | ||
{ | ||
Log.d(LOG_TAG, "onStartCommand()"); | ||
|
||
if (!IsRunning()) | ||
{ | ||
if (impl != null) | ||
{ | ||
Free(impl); | ||
impl = null; | ||
} | ||
impl = Obtain(); | ||
if (impl == null) | ||
{ | ||
Log.e(LOG_TAG, "got nullptr when creating llarp::Context in jni"); | ||
return START_NOT_STICKY; | ||
} | ||
|
||
String dataDir = getFilesDir().toString(); | ||
LokinetConfig config; | ||
try | ||
{ | ||
config = new LokinetConfig(dataDir); | ||
} | ||
catch(RuntimeException ex) | ||
{ | ||
Log.e(LOG_TAG, ex.toString()); | ||
return START_NOT_STICKY; | ||
} | ||
|
||
// FIXME: make these configurable | ||
String exitNode = "exit.loki"; | ||
String upstreamDNS = "1.1.1.1"; | ||
String ourRange = DetectFreeRange(); | ||
|
||
if(ourRange.isEmpty()) | ||
{ | ||
Log.e(LOG_TAG, "cannot detect free range"); | ||
return START_NOT_STICKY; | ||
} | ||
|
||
|
||
// set up config values | ||
config.AddDefaultValue("network", "exit-node", exitNode); | ||
config.AddDefaultValue("network", "ifaddr", ourRange); | ||
config.AddDefaultValue("dns", "upstream", upstreamDNS); | ||
|
||
|
||
if (!config.Load()) | ||
{ | ||
Log.e(LOG_TAG, "failed to load (or create) config file at: " + dataDir + "/lokinet.ini"); | ||
return START_NOT_STICKY; | ||
} | ||
|
||
VpnService.Builder builder = new VpnService.Builder(); | ||
|
||
builder.setMtu(1500); | ||
|
||
String[] parts = ourRange.split("/"); | ||
String ourIP = parts[0]; | ||
int ourMask = Integer.parseInt(parts[1]); | ||
|
||
builder.addAddress(ourIP, ourMask); | ||
builder.addRoute("0.0.0.0", 0); | ||
builder.addDnsServer(upstreamDNS); | ||
builder.setSession("Lokinet"); | ||
builder.setConfigureIntent(null); | ||
|
||
iface = builder.establish(); | ||
if (iface == null) | ||
{ | ||
Log.e(LOG_TAG, "VPN Interface from builder.establish() came back null"); | ||
return START_NOT_STICKY; | ||
} | ||
|
||
m_FD = iface.detachFd(); | ||
|
||
InjectVPNFD(); | ||
|
||
if (!Configure(config)) | ||
{ | ||
//TODO: close vpn FD if this fails, either on native side, or here if possible | ||
Log.e(LOG_TAG, "failed to configure daemon"); | ||
return START_NOT_STICKY; | ||
} | ||
|
||
m_UDPSocket = GetUDPSocket(); | ||
|
||
if (m_UDPSocket <= 0) | ||
{ | ||
Log.e(LOG_TAG, "failed to get proper UDP handle from daemon, aborting."); | ||
return START_NOT_STICKY; | ||
} | ||
|
||
protect(m_UDPSocket); | ||
|
||
new Thread(() -> { | ||
Mainloop(); | ||
}).start(); | ||
|
||
Log.d(LOG_TAG, "started successfully!"); | ||
} | ||
else | ||
{ | ||
Log.d(LOG_TAG, "already running"); | ||
} | ||
|
||
return START_STICKY; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.