diff --git a/app/src/main/java/io/nekohasekai/sagernet/group/RawUpdater.kt b/app/src/main/java/io/nekohasekai/sagernet/group/RawUpdater.kt index c1a034a2..fc70e58c 100644 --- a/app/src/main/java/io/nekohasekai/sagernet/group/RawUpdater.kt +++ b/app/src/main/java/io/nekohasekai/sagernet/group/RawUpdater.kt @@ -23,6 +23,7 @@ import io.nekohasekai.sagernet.ktx.* import libcore.Libcore import moe.matsuri.nb4a.Protocols import moe.matsuri.nb4a.proxy.config.ConfigBean +import moe.matsuri.nb4a.utils.Util import org.ini4j.Ini import org.json.JSONArray import org.json.JSONObject @@ -66,10 +67,11 @@ object RawUpdater : GroupUpdater() { setURL(subscription.link) setUserAgent(subscription.customUserAgent.takeIf { it.isNotBlank() } ?: USER_AGENT) }.execute() - proxies = parseRaw(response.contentString) + proxies = parseRaw(Util.getStringBox(response.contentString)) ?: error(app.getString(R.string.no_proxies_found)) - subscription.subscriptionUserinfo = response.getHeader("Subscription-Userinfo") + subscription.subscriptionUserinfo = + Util.getStringBox(response.getHeader("Subscription-Userinfo")) } val proxiesMap = LinkedHashMap() diff --git a/app/src/main/java/io/nekohasekai/sagernet/ui/AssetsActivity.kt b/app/src/main/java/io/nekohasekai/sagernet/ui/AssetsActivity.kt index 0679a1e4..d6384828 100644 --- a/app/src/main/java/io/nekohasekai/sagernet/ui/AssetsActivity.kt +++ b/app/src/main/java/io/nekohasekai/sagernet/ui/AssetsActivity.kt @@ -18,6 +18,7 @@ import io.nekohasekai.sagernet.databinding.LayoutAssetsBinding import io.nekohasekai.sagernet.ktx.* import io.nekohasekai.sagernet.widget.UndoSnackbarManager import libcore.Libcore +import moe.matsuri.nb4a.utils.Util import org.json.JSONObject import java.io.File import java.io.FileWriter @@ -283,7 +284,7 @@ class AssetsActivity : ThemedActivity() { setURL("https://api.github.com/repos/$repo/releases/latest") }.execute() - val release = JSONObject(response.contentString) + val release = JSONObject(Util.getStringBox(response.contentString)) val tagName = release.optString("tag_name") if (tagName == localVersion) { diff --git a/app/src/main/java/io/nekohasekai/sagernet/utils/Cloudflare.kt b/app/src/main/java/io/nekohasekai/sagernet/utils/Cloudflare.kt index eee031f8..b70d854a 100644 --- a/app/src/main/java/io/nekohasekai/sagernet/utils/Cloudflare.kt +++ b/app/src/main/java/io/nekohasekai/sagernet/utils/Cloudflare.kt @@ -9,6 +9,7 @@ import io.nekohasekai.sagernet.utils.cf.RegisterRequest import io.nekohasekai.sagernet.utils.cf.UpdateDeviceRequest import libcore.Libcore import moe.matsuri.nb4a.utils.JavaUtil.gson +import moe.matsuri.nb4a.utils.Util // kang from wgcf object Cloudflare { @@ -37,8 +38,9 @@ object Cloudflare { setUserAgent("okhttp/3.12.1") }.execute() - Logs.d(response.contentString) - val device = gson.fromJson(response.contentString, DeviceResponse::class.java) + Logs.d(Util.getStringBox(response.contentString)) + val device = + gson.fromJson(Util.getStringBox(response.contentString), DeviceResponse::class.java) val accessToken = device.token client.newRequest().apply { diff --git a/app/src/main/java/moe/matsuri/nb4a/utils/Util.kt b/app/src/main/java/moe/matsuri/nb4a/utils/Util.kt index 77bb1901..35df4ee6 100644 --- a/app/src/main/java/moe/matsuri/nb4a/utils/Util.kt +++ b/app/src/main/java/moe/matsuri/nb4a/utils/Util.kt @@ -3,6 +3,7 @@ package moe.matsuri.nb4a.utils import android.annotation.SuppressLint import android.content.Context import android.util.Base64 +import libcore.StringBox import java.io.ByteArrayOutputStream import java.text.SimpleDateFormat import java.util.* @@ -181,4 +182,10 @@ object Util { } } + fun getStringBox(b: StringBox?): String { + if (b != null && b.value != null) { + return b.value + } + return "" + } } diff --git a/libcore/build.sh b/libcore/build.sh index 22b9a7b0..202ef26d 100755 --- a/libcore/build.sh +++ b/libcore/build.sh @@ -1,5 +1,6 @@ #!/bin/bash +source ../../env_go.sh || true source ../buildScript/init/env_ndk.sh BUILD=".build" diff --git a/libcore/fix.go b/libcore/fix.go new file mode 100644 index 00000000..ccc41c48 --- /dev/null +++ b/libcore/fix.go @@ -0,0 +1,12 @@ +package libcore + +// https://github.com/golang/go/issues/46893 +// TODO: remove after `bulkBarrierPreWrite: unaligned arguments` fixed + +type StringBox struct { + Value string +} + +func wrapString(value string) *StringBox { + return &StringBox{Value: value} +} diff --git a/libcore/http.go b/libcore/http.go index 609faddc..db15a9af 100644 --- a/libcore/http.go +++ b/libcore/http.go @@ -45,9 +45,9 @@ type HTTPRequest interface { } type HTTPResponse interface { - GetHeader(string) string + GetHeader(string) *StringBox GetContent() ([]byte, error) - GetContentString() (string, error) + GetContentString() (*StringBox, error) WriteTo(path string) error } @@ -204,7 +204,7 @@ type httpResponse struct { } func (h *httpResponse) errorString() string { - content, err := h.GetContentString() + content, err := h.getContentString() if err != nil { return fmt.Sprint("HTTP ", h.Status) } @@ -214,8 +214,8 @@ func (h *httpResponse) errorString() string { return fmt.Sprint("HTTP ", h.Status, ": ", content) } -func (h *httpResponse) GetHeader(key string) string { - return h.Header.Get(key) +func (h *httpResponse) GetHeader(key string) *StringBox { + return wrapString(h.Header.Get(key)) } func (h *httpResponse) GetContent() ([]byte, error) { @@ -226,7 +226,15 @@ func (h *httpResponse) GetContent() ([]byte, error) { return h.content, h.contentError } -func (h *httpResponse) GetContentString() (string, error) { +func (h *httpResponse) GetContentString() (*StringBox, error) { + content, err := h.getContentString() + if err != nil { + return nil, err + } + return wrapString(content), nil +} + +func (h *httpResponse) getContentString() (string, error) { content, err := h.GetContent() if err != nil { return "", err