Skip to content

Commit

Permalink
fix: make WIFI state worked
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Changed database, so it will crash.
  • Loading branch information
xchacha20-poly1305 committed Jan 1, 2024
1 parent 5857f39 commit f1b849c
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 25 deletions.
18 changes: 15 additions & 3 deletions app/schemas/io.nekohasekai.sagernet.database.SagerDatabase/1.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"formatVersion": 1,
"database": {
"version": 1,
"identityHash": "cff00d0142d9e53d2ca24a6a55cd213c",
"identityHash": "1ca66b3fb1a21bfd1590dc7a9c6eb46d",
"entities": [
{
"tableName": "proxy_groups",
Expand Down Expand Up @@ -260,7 +260,7 @@
},
{
"tableName": "rules",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `name` TEXT NOT NULL, `userOrder` INTEGER NOT NULL, `enabled` INTEGER NOT NULL, `domains` TEXT NOT NULL, `ip` TEXT NOT NULL, `port` TEXT NOT NULL, `sourcePort` TEXT NOT NULL, `network` TEXT NOT NULL, `source` TEXT NOT NULL, `protocol` TEXT NOT NULL, `outbound` INTEGER NOT NULL, `packages` TEXT NOT NULL)",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `name` TEXT NOT NULL, `userOrder` INTEGER NOT NULL, `enabled` INTEGER NOT NULL, `domains` TEXT NOT NULL, `ip` TEXT NOT NULL, `port` TEXT NOT NULL, `sourcePort` TEXT NOT NULL, `network` TEXT NOT NULL, `source` TEXT NOT NULL, `protocol` TEXT NOT NULL, `outbound` INTEGER NOT NULL, `packages` TEXT NOT NULL, `ssid` TEXT NOT NULL, `bssid` TEXT NOT NULL)",
"fields": [
{
"fieldPath": "id",
Expand Down Expand Up @@ -339,6 +339,18 @@
"columnName": "packages",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "ssid",
"columnName": "ssid",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "bssid",
"columnName": "bssid",
"affinity": "TEXT",
"notNull": true
}
],
"primaryKey": {
Expand All @@ -354,7 +366,7 @@
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'cff00d0142d9e53d2ca24a6a55cd213c')"
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '1ca66b3fb1a21bfd1590dc7a9c6eb46d')"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ data class RuleEntity(
var protocol: String = "",
var outbound: Long = 0,
var packages: List<String> = listOf(),
var ssid: String = "",
var bssid: String = "",
) : Parcelable {

fun displayName(): String {
Expand All @@ -41,6 +43,8 @@ data class RuleEntity(
if (packages.isNotEmpty()) summary += app.getString(
R.string.apps_message, packages.size
) + "\n"
if (ssid.isNotBlank()) summary += "ssid: $ssid"
if (bssid.isNotBlank()) summary += "bssid: $bssid"
val lines = summary.trim().split("\n")
return if (lines.size > 3) {
lines.subList(0, 3).joinToString("\n", postfix = "\n...")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,12 @@ fun buildConfig(
if (rule.protocol.isNotBlank()) {
protocol = rule.protocol.listByLineOrComma()
}
if (rule.ssid.isNotBlank()) {
wifi_ssid = rule.ssid.listByLineOrComma()
}
if (rule.bssid.isNotBlank()) {
wifi_bssid = rule.bssid.listByLineOrComma()
}

fun makeDnsRuleObj(): DNSRule_DefaultOptions {
return DNSRule_DefaultOptions().apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class RouteSettingsActivity(
DataStore.routeSource = source
DataStore.routeProtocol = protocol
DataStore.routeOutboundRule = outbound
DataStore.routeSSID = ssid
DataStore.routeBSSID = bssid
DataStore.routeOutbound = when (outbound) {
0L -> 0
-1L -> 1
Expand All @@ -83,6 +85,8 @@ class RouteSettingsActivity(
network = DataStore.routeNetwork
source = DataStore.routeSource
protocol = DataStore.routeProtocol
ssid = DataStore.routeSSID
bssid = DataStore.routeBSSID
outbound = when (DataStore.routeOutbound) {
0 -> 0L
1 -> -1L
Expand Down
23 changes: 4 additions & 19 deletions app/src/main/java/moe/matsuri/nb4a/SingBoxOptionsUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ object SingBoxOptionsUtil {
}

fun SingBoxOptions.DNSRule_DefaultOptions.makeSingBoxRule(list: List<String>) {
geosite = mutableListOf<String>()
domain = mutableListOf<String>()
domain_suffix = mutableListOf<String>()
domain_regex = mutableListOf<String>()
Expand All @@ -54,7 +53,6 @@ fun SingBoxOptions.DNSRule_DefaultOptions.makeSingBoxRule(list: List<String>) {
domain.plusAssign(it.lowercase())
}
}
geosite?.removeIf { it.isNullOrBlank() }
rule_set?.removeIf { it.isNullOrBlank() }
domain?.removeIf { it.isNullOrBlank() }
domain_suffix?.removeIf { it.isNullOrBlank() }
Expand All @@ -73,15 +71,14 @@ fun SingBoxOptions.DNSRule_DefaultOptions.makeSingBoxRule(list: List<String>) {
}

fun SingBoxOptions.DNSRule_DefaultOptions.checkEmpty(): Boolean {
if (geosite?.isNotEmpty() == true) return false
if (rule_set?.isNotEmpty() == true) return false
if (domain?.isNotEmpty() == true) return false
if (domain_suffix?.isNotEmpty() == true) return false
if (domain_regex?.isNotEmpty() == true) return false
if (domain_keyword?.isNotEmpty() == true) return false
if (user_id?.isNotEmpty() == true) return false
if (wifi_ssid?.isEmpty() == true) return false
if (wifi_bssid?.isEmpty() == true) return false
if (wifi_ssid?.isNotEmpty() == true) return false
if (wifi_bssid?.isNotEmpty() == true) return false
return true
}

Expand All @@ -95,8 +92,6 @@ fun SingBoxOptions.Rule_DefaultOptions.makeSingBoxRule(list: List<String>, isIP:
domain_suffix = mutableListOf<String>()
domain_regex = mutableListOf<String>()
domain_keyword = mutableListOf<String>()
wifi_ssid = mutableListOf<String>()
wifi_bssid = mutableListOf<String>()
}
rule_set = mutableListOf<String>()
list.forEach {
Expand Down Expand Up @@ -126,31 +121,21 @@ fun SingBoxOptions.Rule_DefaultOptions.makeSingBoxRule(list: List<String>, isIP:
}
}
ip_cidr?.removeIf { it.isNullOrBlank() }
geoip?.removeIf { it.isNullOrBlank() }
geosite?.removeIf { it.isNullOrBlank() }
rule_set?.removeIf { it.isNullOrBlank() }
domain?.removeIf { it.isNullOrBlank() }
domain_suffix?.removeIf { it.isNullOrBlank() }
domain_regex?.removeIf { it.isNullOrBlank() }
domain_keyword?.removeIf { it.isNullOrBlank() }
wifi_ssid?.removeIf() { it.isNullOrBlank() }
wifi_bssid?.removeIf() { it.isNullOrBlank() }
if (ip_cidr?.isEmpty() == true) ip_cidr = null
if (geoip?.isEmpty() == true) geoip = null
if (geosite?.isEmpty() == true) geosite = null
if (rule_set?.isEmpty() == true) rule_set = null
if (domain?.isEmpty() == true) domain = null
if (domain_suffix?.isEmpty() == true) domain_suffix = null
if (domain_regex?.isEmpty() == true) domain_regex = null
if (domain_keyword?.isEmpty() == true) domain_keyword = null
if (wifi_ssid?.isEmpty() == true) wifi_ssid = null
if (wifi_bssid?.isEmpty() == true) wifi_bssid = null
}

fun SingBoxOptions.Rule_DefaultOptions.checkEmpty(): Boolean {
if (ip_cidr?.isNotEmpty() == true) return false
if (geoip?.isNotEmpty() == true) return false
if (geosite?.isNotEmpty() == true) return false
if (rule_set?.isNotEmpty() == true) return false
if (domain?.isNotEmpty() == true) return false
if (domain_suffix?.isNotEmpty() == true) return false
Expand All @@ -161,8 +146,8 @@ fun SingBoxOptions.Rule_DefaultOptions.checkEmpty(): Boolean {
if (port?.isNotEmpty() == true) return false
if (port_range?.isNotEmpty() == true) return false
if (source_ip_cidr?.isNotEmpty() == true) return false
if (wifi_ssid?.isEmpty() == true) return false
if (wifi_bssid?.isEmpty() == true) return false
if (wifi_ssid?.isNotEmpty() == true) return false
if (wifi_bssid?.isNotEmpty() == true) return false
return true
}

Expand Down
6 changes: 5 additions & 1 deletion libcore/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,14 @@ func NewSingBoxInstance(config string) (b *BoxInstance, err error) {
// create box
ctx, cancel := context.WithCancel(context.Background())
ctx = pause.WithDefaultManager(ctx)
platformWrapper := &boxPlatformInterfaceWrapper{}
if intfBox != nil {
platformWrapper.iif = intfBox
}
instance, err := dunbox.New(dunbox.Options{
Options: options,
Context: ctx,
PlatformInterface: boxPlatformInterfaceInstance,
PlatformInterface: platformWrapper,
})
if err != nil {
cancel()
Expand Down
10 changes: 8 additions & 2 deletions libcore/platform_box.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import (

var boxPlatformInterfaceInstance platform.Interface = &boxPlatformInterfaceWrapper{}

type boxPlatformInterfaceWrapper struct{}
type boxPlatformInterfaceWrapper struct {
iif BoxPlatformInterface
}

type WIFIState struct {
SSID string
Expand All @@ -34,7 +36,11 @@ func NewWIFIState(wifiSSID string, wifiBSSID string) *WIFIState {
}

func (w *boxPlatformInterfaceWrapper) ReadWIFIState() adapter.WIFIState {
return adapter.WIFIState{}
wifiState := w.iif.ReadWIFIState()
if wifiState == nil {
return adapter.WIFIState{}
}
return (adapter.WIFIState)(*wifiState)
}

func (w *boxPlatformInterfaceWrapper) Initialize(ctx context.Context, router adapter.Router) error {
Expand Down

0 comments on commit f1b849c

Please sign in to comment.