Added User Agent setting that can be used to set custom SIP request/response

User-Agent header value
This commit is contained in:
Juha Heinanen
2024-09-29 12:13:59 +03:00
parent b148833ef0
commit 74fa0c97e6
7 changed files with 107 additions and 27 deletions

View File

@ -441,12 +441,16 @@ class BaresipService: Service() {
activeNetwork = cm.activeNetwork
Log.i(TAG, "Active network: $activeNetwork")
val userAgent = Config.variable("user_agent")
Thread {
baresipStart(
filesPath,
addresses.removePrefix(";"),
logLevel,
"baresip v${BuildConfig.VERSION_NAME} " +
if (userAgent != "")
userAgent
else
"baresip v${BuildConfig.VERSION_NAME} " +
"(Android ${VERSION.RELEASE}/${System.getProperty("os.arch") ?: "?"})"
)
}.start()

View File

@ -107,6 +107,10 @@ object Config {
BaresipService.dynDns = true
}
val userAgent = previousVariable("user_agent")
if (userAgent != "")
config = "${config}user_agent $userAgent\n"
val darkTheme = previousVariable("dark_theme")
Preferences(ctx).displayTheme = if (darkTheme == "yes") {
config = "${config}dark_theme yes\n"
@ -185,7 +189,7 @@ object Config {
private fun previousVariable(name: String): String {
for (line in previousLines) {
val nameValue = line.split(" ")
val nameValue = line.split(" ", limit = 2)
if (nameValue.size == 2 && nameValue[0] == name)
return nameValue[1].trim()
}
@ -195,7 +199,7 @@ object Config {
private fun previousVariables(name: String): ArrayList<String> {
val result = ArrayList<String>()
for (line in previousLines) {
val nameValue = line.split(" ")
val nameValue = line.split(" ", limit = 2)
if (nameValue.size == 2 && nameValue[0] == name)
result.add(nameValue[1].trim())
}
@ -204,7 +208,7 @@ object Config {
fun variable(name: String): String {
for (line in config.split("\n")) {
val nameValue = line.split(" ")
val nameValue = line.split(" ", limit = 2)
if (nameValue.size == 2 && nameValue[0] == name)
return nameValue[1].trim()
}
@ -214,7 +218,7 @@ object Config {
fun variables(name: String): ArrayList<String> {
val result = ArrayList<String>()
for (line in config.split("\n")) {
val nameValue = line.split(" ")
val nameValue = line.split(" ", limit = 2)
if (nameValue.size == 2 && nameValue[0] == name)
result.add(nameValue[1].trim())
}

View File

@ -52,6 +52,7 @@ class ConfigActivity : AppCompatActivity() {
private lateinit var certificateFile: CheckBox
private lateinit var verifyServer: CheckBox
private lateinit var caFile: CheckBox
private lateinit var userAgent: EditText
private lateinit var darkTheme: CheckBox
private lateinit var contactsSpinner: Spinner
private lateinit var contactsMode: String
@ -70,6 +71,7 @@ class ConfigActivity : AppCompatActivity() {
private var oldDnsServers = ""
private var oldCertificateFile = false
private var oldVerifyServer = false
private var oldUserAgent = ""
private var oldLogLevel = ""
private var oldDisplayTheme = -1
private var oldContactsMode = ""
@ -390,6 +392,10 @@ class ConfigActivity : AppCompatActivity() {
}
}
userAgent = binding.UserAgent
oldUserAgent = Config.variable("user_agent")
userAgent.setText(oldUserAgent)
darkTheme = binding.DarkTheme
oldDisplayTheme = Preferences(applicationContext).displayTheme
darkTheme.isChecked = oldDisplayTheme == AppCompatDelegate.MODE_NIGHT_YES
@ -615,6 +621,21 @@ class ConfigActivity : AppCompatActivity() {
save = true
}
val userAgent = userAgent.text.toString().trim()
if (userAgent != oldUserAgent) {
if ((userAgent != "") && !Utils.checkServerVal(userAgent)) {
Utils.alertView(this, getString(R.string.notice),
"${getString(R.string.invalid_user_agent)}: $userAgent")
return false
}
if (userAgent != "")
Config.replaceVariable("user_agent", userAgent)
else
Config.removeVariable("user_agent")
save = true
restart = true
}
val newDisplayTheme = if (darkTheme.isChecked)
AppCompatDelegate.MODE_NIGHT_YES
else
@ -738,6 +759,10 @@ class ConfigActivity : AppCompatActivity() {
Utils.alertView(this, getString(R.string.tls_ca_file),
getString(R.string.tls_ca_file_help))
}
binding.UserAgentTitle.setOnClickListener {
Utils.alertView(this, getString(R.string.user_agent),
getString(R.string.user_agent_help))
}
binding.AudioSettingsTitle.setOnClickListener {
audioRequest.launch(Intent(this, AudioActivity::class.java))
}

View File

@ -363,6 +363,27 @@ object Utils {
cc.substring(1).isDigitsOnly() && cc[1] != '0'
}
fun checkServerVal(server: String): Boolean {
val parts = server.replace(Regex("[(][^()\\\\]+[)]"), "")
.trim().split("\\s+".toRegex())
for (part in parts)
if (!checkProduct(part))
return false
return true
}
private fun checkProduct(product: String): Boolean {
val parts = product.split("/", limit = 2)
return if (parts.count() == 2)
checkToken(parts[0]) && checkToken(parts[1])
else
checkToken(parts[0])
}
private fun checkToken(token: String): Boolean {
return Regex("^[-a-zA-Z0-9.!%*_+`'~]+\$").matches(token)
}
fun setAvatar(ctx: Context, imageView: ImageView, textView: TextView, uri: String) {
when (val contact = Contact.findContact(uri)) {

View File

@ -175,31 +175,25 @@
</CheckBox>
</RelativeLayout>
<RelativeLayout
<TextView
android:id="@+id/UserAgentTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/user_agent"
android:textSize="18sp" >
</TextView>
<EditText
android:id="@+id/UserAgent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-10dp"
android:layout_marginBottom="12dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/DarkThemeTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_toStartOf="@id/DarkTheme"
android:textSize="18sp"
android:text="@string/dark_theme" >
</TextView>
<CheckBox
android:id="@+id/DarkTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_gravity="end"
android:checked="false" >
</CheckBox>
</RelativeLayout>
android:textSize="18sp"
android:inputType="text"
android:importantForAutofill="no" >
</EditText>
<TextView
android:id="@+id/AudioSettingsTitle"
@ -227,6 +221,32 @@
android:paddingTop="8dp" >
</Spinner>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/DarkThemeTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_toStartOf="@id/DarkTheme"
android:textSize="18sp"
android:text="@string/dark_theme" >
</TextView>
<CheckBox
android:id="@+id/DarkTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_gravity="end"
android:checked="false" >
</CheckBox>
</RelativeLayout>
<RelativeLayout
android:id="@+id/Battery"
android:layout_width="wrap_content"

View File

@ -398,6 +398,9 @@
<string name="video_fps_help">Videokehysten lähetystaajuus per sekuntti, jota tarjotaan
SDP-neuvottelun aikana. Arvon on oltava välillä 10-30.</string>
<string name="invalid_fps">Virheellinen videokehysten lähetystaajuus \'%1$d\'</string>
<string name="user_agent">User Agent</string>
<string name="user_agent_help">Räätälöity SIP sanoman User-Agent otsikkokentän arvo</string>
<string name="invalid_user_agent">Virheellinen User-Agent otsikkokentän arvo</string>
<string name="contacts_help">Valitsee käytetäänkö baresip-sovelluksen yhteystietoja, Androidin
yhteystietoja vai molempia. Jos molempia ja sama yhteystieto on molemmissa, käytetään
baresip-sovelluksen yhteystietoa.</string>

View File

@ -371,6 +371,9 @@
<string name="video_fps_help">Video frame rate that will be offered during the SDP handshake.
Valid values are from 10 to 30.</string>
<string name="invalid_fps">Invalid Frames Per Second \'%1$d\'</string>
<string name="user_agent">User Agent</string>
<string name="user_agent_help">Custom SIP request/response User-Agent header field value</string>
<string name="invalid_user_agent">Invalid User-Agent header field value</string>
<string name="contacts_help">Chooses if baresip contacts, Android contacts, or both are used.
If both are used and a contact with the same name exists in both contacts,
the baresip contact will be chosen.</string>