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

Load settings based on env variable #29

Merged
merged 3 commits into from
Jun 22, 2022
Merged
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
29 changes: 22 additions & 7 deletions src/main/kotlin/burp/BurpExtender.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import kotlin.concurrent.thread

const val NAME = "Piper"
const val EXTENSION_SETTINGS_KEY = "settings"
const val CONFIG_ENV_VAR = "PIPER_CONFIG"

data class MessageInfo(val content: ByteArray, val text: String, val headers: List<String>?, val url: URL?, val hrr: IHttpRequestResponse? = null) {
val asContentExtensionPair: Pair<ByteArray, String?> get() {
Expand Down Expand Up @@ -577,14 +578,28 @@ class BurpExtender : IBurpExtender, ITab, ListDataListener, IHttpListener {
}

private fun loadConfig(): Piper.Config {
val serialized = callbacks.loadExtensionSetting(EXTENSION_SETTINGS_KEY)
return if (serialized == null)
{
try {
val env = System.getenv(CONFIG_ENV_VAR)
if (env != null) {
val fmt = if (env.endsWith(".yml") || env.endsWith(".yaml")){
ConfigFormat.YAML
} else {
ConfigFormat.PROTOBUF
}
val configFile = File(env)
return fmt.parse(configFile.readBytes()).updateEnabled(true)
}

val serialized = callbacks.loadExtensionSetting(EXTENSION_SETTINGS_KEY)
if (serialized != null) {
return Piper.Config.parseFrom(decompress(unpad4(Z85.Z85Decoder(serialized))))
}

throw Exception("Fallback to default config")
} catch (e: Exception) {
val cfgMod = loadDefaultConfig()
saveConfig(cfgMod)
cfgMod
} else {
Piper.Config.parseFrom(decompress(unpad4(Z85.Z85Decoder(serialized))))
return cfgMod
}
}

Expand Down Expand Up @@ -790,4 +805,4 @@ private fun handleGUI(process: Process, tools: List<Piper.MinimalTool>) {
}
}.start()
}
}
}