Skip to content

Commit

Permalink
fix: support for proxy and custom-certs for Android 6
Browse files Browse the repository at this point in the history
  • Loading branch information
inkeliz committed Apr 15, 2021
1 parent c68e5e2 commit 57d35d5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
15 changes: 12 additions & 3 deletions gowebview_android.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package gowebview
import (
"crypto/x509"
"encoding/base64"
"errors"
"git.wow.st/gmp/jni"
"sync"
"unsafe"
Expand Down Expand Up @@ -174,17 +175,21 @@ func (w *webview) setProxy(proxy *HTTPProxy) error {
return nil
}

b, err := w.callBooleanArgs("webview_proxy", "(Ljava/lang/String;Ljava/lang/String;)Z", func(env jni.Env) []jni.Value {
ok, err := w.callBooleanArgs("webview_proxy", "(Ljava/lang/String;Ljava/lang/String;)Z", func(env jni.Env) []jni.Value {
return []jni.Value{
jni.Value(jni.JavaString(env, proxy.IP)),
jni.Value(jni.JavaString(env, proxy.Port)),
}
})

if b != true || err != nil {
if err != nil {
return err
}

if !ok {
return errors.New("impossible to set proxy")
}

return nil
}

Expand All @@ -198,7 +203,7 @@ func (w *webview) setCerts(certs []x509.Certificate) error {
jcerts += base64.StdEncoding.EncodeToString(c.Raw) + ";"
}

err := w.callArgs("webview_certs", "(Ljava/lang/String;)V", func(env jni.Env) []jni.Value {
ok, err := w.callBooleanArgs("webview_certs", "(Ljava/lang/String;)Z", func(env jni.Env) []jni.Value {
return []jni.Value{
jni.Value(jni.JavaString(env, jcerts)),
}
Expand All @@ -208,6 +213,10 @@ func (w *webview) setCerts(certs []x509.Certificate) error {
return err
}

if !ok {
return errors.New("impossible to set certs")
}

return nil
}

Expand Down
Binary file modified gowebview_android.jar
Binary file not shown.
42 changes: 31 additions & 11 deletions gowebview_android.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.webkit.WebView;
import android.util.Log;
import android.os.Build;
import android.os.Parcelable;
import android.net.Proxy;
import java.lang.reflect.*;
import android.util.ArrayMap;
Expand All @@ -24,13 +25,13 @@
import android.net.http.SslCertificate;
import java.security.PublicKey;
import java.security.cert.X509Certificate;
import java.util.Base64;
import java.security.MessageDigest;
import java.security.cert.CertificateFactory;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.util.Base64;

public class gowebview_android {
private View primaryView;
Expand All @@ -50,7 +51,7 @@ public class gowebview_webbrowser extends WebViewClient {
return;
}

X509Certificate certificate = null;
Certificate certificate = null;
try{
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.Q) {
certificate = err.getCertificate().getX509Certificate();
Expand All @@ -60,8 +61,7 @@ public class gowebview_webbrowser extends WebViewClient {
byte[] certificateBytes = bundle.getByteArray("x509-certificate");
if (certificateBytes != null) {
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
Certificate cert = certFactory.generateCertificate(new ByteArrayInputStream(certificateBytes));
certificate = (X509Certificate) cert;
certificate = certFactory.generateCertificate(new ByteArrayInputStream(certificateBytes));
}
}
} catch (Exception e) {
Expand Down Expand Up @@ -98,7 +98,9 @@ public void run() {
webBrowser = new WebView(primaryView.getContext());
WebSettings webSettings = webBrowser.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSafeBrowsingEnabled(false);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O_MR1) {
webSettings.setSafeBrowsingEnabled(false);
}
webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
Expand Down Expand Up @@ -166,9 +168,9 @@ public void run() {
Context app = webBrowser.getContext().getApplicationContext();

System.setProperty("http.proxyHost", host);
System.setProperty("http.proxyPort", port + "");
System.setProperty("http.proxyPort", port);
System.setProperty("https.proxyHost", host);
System.setProperty("https.proxyPort", port + "");
System.setProperty("https.proxyPort", port);

try {
Field apk = app.getClass().getDeclaredField("mLoadedApk");
Expand All @@ -183,7 +185,22 @@ public void run() {

Class<?> cls = receiver.getClass();
if (cls.getName().contains("ProxyChangeListener")) {
cls.getDeclaredMethod("onReceive", Context.class, Intent.class).invoke(receiver, app, new Intent(Proxy.PROXY_CHANGE_ACTION));

String proxyInfoName = "android.net.ProxyInfo";
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
proxyInfoName = "android.net.ProxyProperties";
}

Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);

Class proxyInfoClass = Class.forName(proxyInfoName);
if (proxyInfoClass != null) {
Constructor proxyInfo = proxyInfoClass.getConstructor(String.class, Integer.TYPE, String.class);
proxyInfo.setAccessible(true);
intent.putExtra("proxy", (Parcelable) ((Object) proxyInfo.newInstance(host, Integer.parseInt(port), null)));
}

cls.getDeclaredMethod("onReceive", Context.class, Intent.class).invoke(receiver, app, intent);
}
}

Expand All @@ -208,22 +225,25 @@ public void run() {
return result.Get();
}

public void webview_certs(String der) {
public boolean webview_certs(String der) {
String[] sCerts = der.split(";");


additionalCerts = new PublicKey[sCerts.length];

for (int i = 0; i < sCerts.length; i++) {
InputStream streamCert = new ByteArrayInputStream(Base64.getDecoder().decode(sCerts[i]));
InputStream streamCert = new ByteArrayInputStream(Base64.decode(sCerts[i], android.util.Base64.DEFAULT));

try {
CertificateFactory factory = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate)factory.generateCertificate(streamCert);

additionalCerts[i] = cert.getPublicKey();
} catch(Exception e) {
e.printStackTrace();
return false;
}
}

return true;
}
}
2 changes: 2 additions & 0 deletions lib.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build windows

package gowebview

import (
Expand Down

0 comments on commit 57d35d5

Please sign in to comment.