Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add database definition for traffic data #878

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2024 Blocker
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.merxury.blocker.core.data.respository.traffic

import com.merxury.blocker.core.database.traffic.TrafficDataDao
import com.merxury.blocker.core.database.traffic.asExternalModel
import com.merxury.blocker.core.database.traffic.fromExternalModel
import com.merxury.blocker.core.model.data.TrafficData
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject

class LocalTrafficDataRepository @Inject constructor(
private val trafficDataDao: TrafficDataDao,
) : TrafficDataRepository {
override fun insertTrafficData(trafficData: TrafficData) {
trafficDataDao.insert(trafficData.fromExternalModel())
}

override fun getTrafficData(packageName: String, keyword: String): Flow<List<TrafficData>> = trafficDataDao.getTrafficData(packageName, keyword)
.map { trafficDataList ->
trafficDataList.map { it.asExternalModel() }
}

override fun deleteTrafficData() {
trafficDataDao.deleteAll()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2024 Blocker
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.merxury.blocker.core.data.respository.traffic

import com.merxury.blocker.core.model.data.TrafficData
import kotlinx.coroutines.flow.Flow

interface TrafficDataRepository {
fun insertTrafficData(trafficData: TrafficData)
fun getTrafficData(packageName: String, keyword: String): Flow<List<TrafficData>>
fun deleteTrafficData()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2024 Blocker
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.merxury.blocker.core.data.respository.traffic

import com.merxury.blocker.core.model.data.TrafficData
import kotlinx.coroutines.flow.Flow

interface TrafficDataSource {
fun getTrafficData(packageName: String, keyword: String): Flow<List<TrafficData>>
fun insertTrafficData(trafficData: TrafficData)
fun insertTrafficData(trafficData: List<TrafficData>)
fun deleteTrafficData()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"formatVersion": 1,
"database": {
"version": 1,
"identityHash": "4e6d693b5a2004c66c250ff874b42300",
"entities": [
{
"tableName": "traffic_data",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `timestamp` INTEGER NOT NULL, `packageName` TEXT NOT NULL, `ipAddress` TEXT NOT NULL, `domain` TEXT, `port` INTEGER NOT NULL, `path` TEXT, `blocked` INTEGER NOT NULL)",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "timestamp",
"columnName": "timestamp",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "packageName",
"columnName": "packageName",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "ipAddress",
"columnName": "ipAddress",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "domain",
"columnName": "domain",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "port",
"columnName": "port",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "path",
"columnName": "path",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "blocked",
"columnName": "blocked",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
]
},
"indices": [],
"foreignKeys": []
}
],
"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, '4e6d693b5a2004c66c250ff874b42300')"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import com.merxury.blocker.core.database.app.InstalledAppDao
import com.merxury.blocker.core.database.app.InstalledAppDatabase
import com.merxury.blocker.core.database.generalrule.GeneralRuleDao
import com.merxury.blocker.core.database.generalrule.GeneralRuleDatabase
import com.merxury.blocker.core.database.traffic.TrafficDataDao
import com.merxury.blocker.core.database.traffic.TrafficDataDatabase
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
Expand All @@ -39,4 +41,8 @@ internal object DaosModule {
@Provides
@Singleton
fun provideGeneralRuleDao(database: GeneralRuleDatabase): GeneralRuleDao = database.generalRuleDao()

@Provides
@Singleton
fun provideTrafficDataDao(database: TrafficDataDatabase): TrafficDataDao = database.trafficDataDao()
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import android.content.Context
import androidx.room.Room
import com.merxury.blocker.core.database.app.InstalledAppDatabase
import com.merxury.blocker.core.database.generalrule.GeneralRuleDatabase
import com.merxury.blocker.core.database.traffic.TrafficDataDatabase
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
Expand Down Expand Up @@ -47,4 +48,10 @@ internal object DatabaseModule {
fun provideGeneralRuleDatabase(@ApplicationContext context: Context): GeneralRuleDatabase = Room.databaseBuilder(context, GeneralRuleDatabase::class.java, "general_rule")
.fallbackToDestructiveMigration()
.build()

@Provides
@Singleton
fun provideTrafficDataDatabase(@ApplicationContext context: Context): TrafficDataDatabase = Room.databaseBuilder(context, TrafficDataDatabase::class.java, "traffic_data")
.fallbackToDestructiveMigration()
.build()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2024 Blocker
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.merxury.blocker.core.database.traffic

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import kotlinx.coroutines.flow.Flow

@Dao
interface TrafficDataDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(trafficData: TrafficDataEntity)

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAll(trafficData: List<TrafficDataEntity>)

@Query("SELECT * FROM traffic_data WHERE packageName = :packageName AND (domain LIKE '%' || :keyword || '%' OR path LIKE '%' || :keyword || '%') ORDER BY timestamp DESC")
fun getTrafficData(packageName: String, keyword: String): Flow<List<TrafficDataEntity>>

@Query("DELETE FROM traffic_data")
fun deleteAll()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2024 Blocker
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.merxury.blocker.core.database.traffic

import androidx.room.Database
import androidx.room.RoomDatabase

@Database(entities = [TrafficDataEntity::class], version = 1)
abstract class TrafficDataDatabase : RoomDatabase() {
abstract fun trafficDataDao(): TrafficDataDao
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2024 Blocker
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.merxury.blocker.core.database.traffic

import androidx.room.Entity
import androidx.room.PrimaryKey
import com.merxury.blocker.core.model.data.TrafficData

@Entity(tableName = "traffic_data")
data class TrafficDataEntity(
@PrimaryKey(autoGenerate = true) val id: Long = 0,
val timestamp: Long,
val packageName: String,
val ipAddress: String,
val domain: String? = null,
val port: Int,
val path: String? = null,
val blocked: Boolean = false,
)

fun TrafficDataEntity.asExternalModel() = TrafficData(
id = id,
timestamp = timestamp,
packageName = packageName,
ipAddress = ipAddress,
domain = domain,
port = port,
path = path,
blocked = blocked,
)

fun TrafficData.fromExternalModel() = TrafficDataEntity(
id = id,
timestamp = timestamp,
packageName = packageName,
ipAddress = ipAddress,
domain = domain,
port = port,
path = path,
blocked = blocked,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2024 Blocker
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.merxury.blocker.core.model.data

data class TrafficData(
val id: Long = 0,
val timestamp: Long,
val packageName: String,
val ipAddress: String,
val domain: String?,
val port: Int,
val path: String?,
val blocked: Boolean = false,
)
1 change: 1 addition & 0 deletions core/vpn/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
Loading
Loading