Various fixes

This commit is contained in:
pokamest 2022-02-09 15:23:20 +03:00
parent cb21991efa
commit 505c9c6218
5 changed files with 306 additions and 295 deletions

View file

@ -37,12 +37,13 @@ class VPNService : android.net.VpnService() {
SharedLibraryLoader.loadSharedLibrary(this, "ovpn3") SharedLibraryLoader.loadSharedLibrary(this, "ovpn3")
Log.i(tag, "Loaded libs") Log.i(tag, "Loaded libs")
Log.e(tag, "Wireguard Version ${wgVersion()}") Log.e(tag, "Wireguard Version ${wgVersion()}")
mOpenVPNThreadv3 = OpenVPNThreadv3 (this) mOpenVPNThreadv3 = OpenVPNThreadv3(this)
mAlreadyInitialised = true mAlreadyInitialised = true
} }
override fun onUnbind(intent: Intent?): Boolean { override fun onUnbind(intent: Intent?): Boolean {
Log.v(tag, "Got Unbind request")
if (!isUp) { if (!isUp) {
// If the Qt Client got closed while we were not connected // If the Qt Client got closed while we were not connected
// we do not need to stay as a foreground service. // we do not need to stay as a foreground service.
@ -128,6 +129,7 @@ class VPNService : android.net.VpnService() {
putOpt("deviceIpv4", mConfig?.getJSONObject("device")?.getString("ipv4Address")) putOpt("deviceIpv4", mConfig?.getJSONObject("device")?.getString("ipv4Address"))
} }
} }
/* /*
* Checks if the VPN Permission is given. * Checks if the VPN Permission is given.
* If the permission is given, returns true * If the permission is given, returns true
@ -180,30 +182,30 @@ class VPNService : android.net.VpnService() {
mbuilder.setMtu(mtu) mbuilder.setMtu(mtu)
} }
fun addAddress(ip: String, len: Int){ fun addAddress(ip: String, len: Int) {
Log.v(tag, "mbuilder.addAddress($ip, $len)") Log.v(tag, "mbuilder.addAddress($ip, $len)")
mbuilder.addAddress(ip, len) mbuilder.addAddress(ip, len)
} }
fun addRoute(ip: String, len: Int){ fun addRoute(ip: String, len: Int) {
Log.v(tag, "mbuilder.addRoute($ip, $len)") Log.v(tag, "mbuilder.addRoute($ip, $len)")
mbuilder.addRoute(ip, len) mbuilder.addRoute(ip, len)
} }
fun addDNS(ip: String){ fun addDNS(ip: String) {
Log.v(tag, "mbuilder.addDnsServer($ip)") Log.v(tag, "mbuilder.addDnsServer($ip)")
mbuilder.addDnsServer(ip) mbuilder.addDnsServer(ip)
if ("samsung".equals(Build.BRAND) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){ if ("samsung".equals(Build.BRAND) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mbuilder.addRoute(ip, 32) mbuilder.addRoute(ip, 32)
} }
} }
fun setSessionName(name: String){ fun setSessionName(name: String) {
Log.v(tag, "mbuilder.setSession($name)") Log.v(tag, "mbuilder.setSession($name)")
mbuilder.setSession(name) mbuilder.setSession(name)
} }
fun addHttpProxy(host: String, port: Int): Boolean{ fun addHttpProxy(host: String, port: Int): Boolean {
val proxyInfo = ProxyInfo.buildDirectProxy(host, port) val proxyInfo = ProxyInfo.buildDirectProxy(host, port)
Log.v(tag, "mbuilder.addHttpProxy($host, $port)") Log.v(tag, "mbuilder.addHttpProxy($host, $port)")
mbuilder.setHttpProxy(proxyInfo) mbuilder.setHttpProxy(proxyInfo)
@ -217,7 +219,7 @@ class VPNService : android.net.VpnService() {
fun turnOff() { fun turnOff() {
Log.v(tag, "Try to disable tunnel") Log.v(tag, "Try to disable tunnel")
when(mProtocol){ when (mProtocol) {
"wireguard" -> wgTurnOff(currentTunnelHandle) "wireguard" -> wgTurnOff(currentTunnelHandle)
"openvpn" -> ovpnTurnOff() "openvpn" -> ovpnTurnOff()
else -> { else -> {
@ -237,6 +239,7 @@ class VPNService : android.net.VpnService() {
mOpenVPNThreadv3 = null mOpenVPNThreadv3 = null
Log.e(tag, "mOpenVPNThreadv3 stop!") Log.e(tag, "mOpenVPNThreadv3 stop!")
} }
/** /**
* Configures an Android VPN Service Tunnel * Configures an Android VPN Service Tunnel
* with a given Wireguard Config * with a given Wireguard Config
@ -296,7 +299,8 @@ class VPNService : android.net.VpnService() {
currentSection?.let { currentSection?.let {
parseData.put(it.first, it.second) parseData.put(it.first, it.second)
} }
currentSection = line.substring(1, line.indexOfLast { it == ']' }) to mutableMapOf() currentSection =
line.substring(1, line.indexOfLast { it == ']' }) to mutableMapOf()
} else { } else {
val parameter = line.split("=", limit = 2) val parameter = line.split("=", limit = 2)
currentSection!!.second.put(parameter.first().trim(), parameter.last().trim()) currentSection!!.second.put(parameter.first().trim(), parameter.last().trim())
@ -361,13 +365,13 @@ class VPNService : android.net.VpnService() {
} }
private fun startOpenVpn() { private fun startOpenVpn() {
mOpenVPNThreadv3 = OpenVPNThreadv3 (this) mOpenVPNThreadv3 = OpenVPNThreadv3(this)
Thread ({ Thread({
mOpenVPNThreadv3?.run() mOpenVPNThreadv3?.run()
}).start() }).start()
} }
private fun startWireGuard(){ private fun startWireGuard() {
val wireguard_conf = buildWireugardConfig(mConfig!!) val wireguard_conf = buildWireugardConfig(mConfig!!)
if (currentTunnelHandle != -1) { if (currentTunnelHandle != -1) {
Log.e(tag, "Tunnel already up") Log.e(tag, "Tunnel already up")
@ -379,7 +383,7 @@ class VPNService : android.net.VpnService() {
setupBuilder(wireguard_conf, builder) setupBuilder(wireguard_conf, builder)
builder.setSession("avpn0") builder.setSession("avpn0")
builder.establish().use { tun -> builder.establish().use { tun ->
if (tun == null)return if (tun == null) return
Log.i(tag, "Go backend " + wgVersion()) Log.i(tag, "Go backend " + wgVersion())
currentTunnelHandle = wgTurnOn("avpn0", tun.detachFd(), wgConfig) currentTunnelHandle = wgTurnOn("avpn0", tun.detachFd(), wgConfig)
} }
@ -401,6 +405,7 @@ class VPNService : android.net.VpnService() {
NotificationUtil.show(this) // Go foreground NotificationUtil.show(this) // Go foreground
} }
companion object { companion object {
@JvmStatic @JvmStatic
fun startService(c: Context) { fun startService(c: Context) {
@ -412,15 +417,20 @@ class VPNService : android.net.VpnService() {
@JvmStatic @JvmStatic
private external fun wgGetConfig(handle: Int): String? private external fun wgGetConfig(handle: Int): String?
@JvmStatic @JvmStatic
private external fun wgGetSocketV4(handle: Int): Int private external fun wgGetSocketV4(handle: Int): Int
@JvmStatic @JvmStatic
private external fun wgGetSocketV6(handle: Int): Int private external fun wgGetSocketV6(handle: Int): Int
@JvmStatic @JvmStatic
private external fun wgTurnOff(handle: Int) private external fun wgTurnOff(handle: Int)
@JvmStatic @JvmStatic
private external fun wgTurnOn(ifName: String, tunFd: Int, settings: String): Int private external fun wgTurnOn(ifName: String, tunFd: Int, settings: String): Int
@JvmStatic @JvmStatic
private external fun wgVersion(): String? private external fun wgVersion(): String?
} }
} }

View file

@ -1,4 +1,4 @@
QT += widgets core gui network xml remoteobjects quick QT += widgets core gui network xml remoteobjects quick svg
TARGET = AmneziaVPN TARGET = AmneziaVPN
TEMPLATE = app TEMPLATE = app

View file

@ -38,8 +38,6 @@ void QrDecoderLogic::onDetectedQrCode(const QString &code)
if (magic == amnezia::qrMagicCode) { if (magic == amnezia::qrMagicCode) {
qDebug() << "QrDecoderLogic::onDetectedQrCode magic code detected" << magic << ba.size();
quint8 chunksCount; s >> chunksCount; quint8 chunksCount; s >> chunksCount;
if (totalChunksCount() != chunksCount) { if (totalChunksCount() != chunksCount) {
m_chunks.clear(); m_chunks.clear();

View file

@ -12,6 +12,7 @@ BasicButtonType {
anchors.left: parent.left anchors.left: parent.left
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
svg.source: root.icon.source svg.source: root.icon.source
color: "#100A44"
width: 25 width: 25
height: 25 height: 25
} }

View file

@ -112,6 +112,7 @@ UiLogic::~UiLogic()
{ {
emit hide(); emit hide();
#ifdef AMNEZIA_DESKTOP
if (m_vpnConnection->connectionState() != VpnProtocol::VpnConnectionState::Disconnected) { if (m_vpnConnection->connectionState() != VpnProtocol::VpnConnectionState::Disconnected) {
m_vpnConnection->disconnectFromVpn(); m_vpnConnection->disconnectFromVpn();
for (int i = 0; i < 50; i++) { for (int i = 0; i < 50; i++) {
@ -122,6 +123,7 @@ UiLogic::~UiLogic()
} }
} }
} }
#endif
m_vpnConnection->deleteLater(); m_vpnConnection->deleteLater();
m_vpnConnectionThread.quit(); m_vpnConnectionThread.quit();