-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathProvider.kt
More file actions
211 lines (171 loc) · 7.36 KB
/
Copy pathProvider.kt
File metadata and controls
211 lines (171 loc) · 7.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package net.portswigger.mcp.providers
import burp.api.montoya.logging.Logging
import kotlinx.serialization.json.*
import net.portswigger.mcp.config.McpConfig
import java.io.File
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.StandardCopyOption
import javax.swing.JFileChooser
import kotlin.io.path.createDirectories
import kotlin.io.path.exists
import kotlin.io.path.readText
import kotlin.io.path.writeText
interface Provider {
val name: String
val installButtonText: String
val confirmationText: String?
fun install(config: McpConfig): String?
}
class GitHubCopilotCliProvider(
private val logging: Logging,
private val proxyJarManager: ProxyJarManager,
private val userHome: Path = Path.of(System.getProperty("user.home"))
) : Provider {
private val configFileName = "mcp-config.json"
private val serverName = "burp"
override val name = "GitHub Copilot CLI"
override val installButtonText = "Install to $name"
override val confirmationText =
"Install to $name?\nThis will create or update $name's MCP configuration ($configFileName)."
override fun install(config: McpConfig): String {
val proxyJarFile = proxyJarManager.getProxyJar()
val path = configFilePath()
path.parent.createDirectories()
val content: MutableMap<String, JsonElement> = if (path.exists()) {
Json.parseToJsonElement(path.readText()).jsonObject.toMutableMap<String, JsonElement>()
} else {
mutableMapOf("mcpServers" to buildJsonObject {})
}
val burpServerConfig = buildJsonObject {
put("type", JsonPrimitive("local"))
put("command", JsonPrimitive("java"))
put("args", buildJsonArray {
add(JsonPrimitive("-jar"))
add(JsonPrimitive(proxyJarFile.toString()))
add(JsonPrimitive("--sse-url"))
add(JsonPrimitive("http://${config.host}:${config.port}"))
})
put("tools", buildJsonArray {
add(JsonPrimitive("*"))
})
}
val mcpServers = mutableMapOf<String, JsonElement>().apply {
content["mcpServers"]?.jsonObject?.let { putAll(it) }
}
mcpServers[serverName] = burpServerConfig
content["mcpServers"] = JsonObject(mcpServers)
val json = Json {
prettyPrint = true
encodeDefaults = true
}
path.writeText(json.encodeToString(JsonObject.serializer(), JsonObject(content)))
logging.logToOutput("Installed Burp MCP Server to GitHub Copilot CLI config")
return "Installation successful. Please restart $name if it is currently running."
}
private fun configFilePath(): Path {
return userHome.resolve(".copilot").resolve(configFileName)
}
}
class ClaudeDesktopProvider(private val logging: Logging, private val proxyJarManager: ProxyJarManager) : Provider {
private val claudeConfigFileName = "claude_desktop_config.json"
private val serverName = "burp"
override val name = "Claude Desktop"
override val installButtonText = "Install to $name"
override val confirmationText =
"Install to $name?\nThis will create an entry within $name's MCP configuration file ($claudeConfigFileName)"
override fun install(config: McpConfig): String {
val proxyJarFile = proxyJarManager.getProxyJar()
val path = configFilePath() ?: error("Could not find Claude config path")
val content = Json.parseToJsonElement(path.readText()).jsonObject.toMutableMap()
val javaPath = javaPath()
logging.logToOutput("Using Java from: $javaPath")
val sseUrl = "http://${config.host}:${config.port}"
val burpServerConfig = buildJsonObject {
put("command", JsonPrimitive(javaPath))
put("args", buildJsonArray {
add(JsonPrimitive("-jar"))
add(JsonPrimitive(proxyJarFile.toString()))
add(JsonPrimitive("--sse-url"))
add(JsonPrimitive(sseUrl))
})
}
val mcpServers = content["mcpServers"]?.jsonObject?.toMutableMap() ?: mutableMapOf()
mcpServers[serverName] = burpServerConfig
content["mcpServers"] = JsonObject(mcpServers)
val json = Json {
prettyPrint = true
encodeDefaults = true
}
path.writeText(json.encodeToString(JsonObject.serializer(), JsonObject(content)))
logging.logToOutput("Installed Burp MCP Server to Claude Desktop config")
return "Installation successful. Please restart $name if it is currently running."
}
private fun configFilePath(): Path? {
val os = System.getProperty("os.name").lowercase()
val home = System.getProperty("user.home")
val basePath = when {
os.contains("win") -> Path.of(home, "AppData", "Roaming", "Claude")
os.contains("mac") || os.contains("darwin") -> Path.of(home, "Library", "Application Support", "Claude")
os.contains("linux") -> Path.of(home, ".config", "Claude")
else -> return null
}
if (!basePath.exists()) return null
val configFile = basePath.resolve(claudeConfigFileName)
if (!configFile.exists()) {
createDefaultConfig(configFile)
}
return configFile
}
private fun createDefaultConfig(path: Path): Boolean {
try {
val defaultConfig = buildJsonObject {
put("mcpServers", buildJsonObject {})
}
val json = Json {
prettyPrint = true
encodeDefaults = true
}
path.writeText(json.encodeToString(JsonObject.serializer(), defaultConfig))
logging.logToOutput("Created default Claude Desktop config at $path")
return true
} catch (e: Exception) {
logging.logToError("Failed to create default Claude Desktop config: ${e.message}")
return false
}
}
private fun javaPath(): String {
val javaHome = System.getProperty("java.home")
val os = System.getProperty("os.name").lowercase()
return if (os.contains("win")) {
"$javaHome\\bin\\java.exe"
} else {
"$javaHome/bin/java"
}
}
}
class ManualProxyInstallerProvider(private val logging: Logging, private val proxyJarManager: ProxyJarManager) :
Provider {
override val name = "Proxy jar"
override val installButtonText = "Extract server proxy jar"
override val confirmationText = null
override fun install(config: McpConfig): String? {
val proxyJarFile = proxyJarManager.getProxyJar()
val fileChooser = JFileChooser().apply {
dialogTitle = "Save proxy jar"
selectedFile = File("mcp-proxy.jar")
}
if (fileChooser.showSaveDialog(null) != JFileChooser.APPROVE_OPTION) {
return null
}
val destinationFile = fileChooser.selectedFile
try {
Files.copy(proxyJarFile, destinationFile.toPath(), StandardCopyOption.REPLACE_EXISTING)
logging.logToOutput("MCP proxy jar saved successfully to ${destinationFile.absolutePath}")
} catch (ex: Exception) {
logging.logToError("Failed to save installer: ${ex.message}")
throw ex
}
return "Extracted proxy jar to $destinationFile"
}
}