@@ -5,6 +5,29 @@ import org.json.JSONArray
55import org.json.JSONObject
66import java.time.Instant
77
8+ data class AppTrafficEntry (
9+ val uid : Int ,
10+ val rxBytes : Long ,
11+ val txBytes : Long ,
12+ val label : String ,
13+ ) {
14+ fun toJson (): JSONObject = JSONObject ().apply {
15+ put(" uid" , uid)
16+ put(" rx" , rxBytes)
17+ put(" tx" , txBytes)
18+ put(" label" , label)
19+ }
20+
21+ companion object {
22+ fun fromJson (o : JSONObject ): AppTrafficEntry = AppTrafficEntry (
23+ uid = o.optInt(" uid" ),
24+ rxBytes = o.optLong(" rx" , 0L ),
25+ txBytes = o.optLong(" tx" , 0L ),
26+ label = o.optString(" label" , " ?" ),
27+ )
28+ }
29+ }
30+
831data class SessionRecord (
932 val start : Instant ,
1033 val end : Instant ? = null ,
@@ -19,6 +42,12 @@ data class SessionRecord(
1942 val dnsOkBefore : Boolean ,
2043 val dnsOkAfter : Boolean? = null ,
2144 val probeOk : Boolean ,
45+ val rxBytes : Long? = null ,
46+ val txBytes : Long? = null ,
47+ val byApp : List <AppTrafficEntry > = emptyList(),
48+ val trafficCollectError : String? = null ,
49+ val routePrefixes : List <String > = emptyList(),
50+ val excludePrefixes : List <String > = emptyList(),
2251) {
2352 fun toJson (): JSONObject {
2453 val j = JSONObject ()
@@ -35,6 +64,18 @@ data class SessionRecord(
3564 j.put(" dnsOKBefore" , dnsOkBefore)
3665 dnsOkAfter?.let { j.put(" dnsOKAfter" , it) }
3766 j.put(" probeOK" , probeOk)
67+ rxBytes?.let { j.put(" rxBytes" , it) }
68+ txBytes?.let { j.put(" txBytes" , it) }
69+ trafficCollectError?.let { j.put(" trafficCollectError" , it) }
70+ if (byApp.isNotEmpty()) {
71+ j.put(" byApp" , JSONArray ().apply { byApp.forEach { put(it.toJson()) } })
72+ }
73+ if (routePrefixes.isNotEmpty()) {
74+ j.put(" routePrefixes" , JSONArray ().apply { routePrefixes.forEach { put(it) } })
75+ }
76+ if (excludePrefixes.isNotEmpty()) {
77+ j.put(" excludePrefixes" , JSONArray ().apply { excludePrefixes.forEach { put(it) } })
78+ }
3879 return j
3980 }
4081
@@ -53,9 +94,25 @@ data class SessionRecord(
5394 dnsOkBefore = j.getBoolean(" dnsOKBefore" ),
5495 dnsOkAfter = if (j.has(" dnsOKAfter" ) && ! j.isNull(" dnsOKAfter" )) j.getBoolean(" dnsOKAfter" ) else null ,
5596 probeOk = j.getBoolean(" probeOK" ),
97+ rxBytes = if (j.has(" rxBytes" ) && ! j.isNull(" rxBytes" )) j.getLong(" rxBytes" ) else null ,
98+ txBytes = if (j.has(" txBytes" ) && ! j.isNull(" txBytes" )) j.getLong(" txBytes" ) else null ,
99+ byApp = parseAppList(j.optJSONArray(" byApp" )),
100+ trafficCollectError = j.optNullableString(" trafficCollectError" ),
101+ routePrefixes = parseStringList(j.optJSONArray(" routePrefixes" )),
102+ excludePrefixes = parseStringList(j.optJSONArray(" excludePrefixes" )),
56103 )
57104
58105 fun listFromJson (arr : JSONArray ): List <SessionRecord > = List (arr.length()) { i -> fromJson(arr.getJSONObject(i)) }
106+
107+ private fun parseAppList (arr : JSONArray ? ): List <AppTrafficEntry > {
108+ if (arr == null ) return emptyList()
109+ return List (arr.length()) { i -> AppTrafficEntry .fromJson(arr.getJSONObject(i)) }
110+ }
111+
112+ private fun parseStringList (arr : JSONArray ? ): List <String > {
113+ if (arr == null ) return emptyList()
114+ return List (arr.length()) { i -> arr.getString(i) }
115+ }
59116 }
60117}
61118
0 commit comments