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

Please add regular WireGuard configuration parsing. #3497

Closed
phenomenonRT opened this issue Aug 17, 2024 · 11 comments
Closed

Please add regular WireGuard configuration parsing. #3497

phenomenonRT opened this issue Aug 17, 2024 · 11 comments

Comments

@phenomenonRT
Copy link

Please add regular WireGuard configuration parsing.
Example:

[Interface]
PrivateKey = 
Address = 
DNS = 
MTU =

[Peer]
PublicKey =
AllowedIPs =
Endpoint = 
@2dust
Copy link
Owner

2dust commented Aug 18, 2024

Waiting for someone to pr

@CodeWithTamim
Copy link
Contributor

@2dust I have done this before, should I do it and submit a pr?

@APT-ZERO
Copy link

Please add the same thing to v2rayN too

@2dust
Copy link
Owner

2dust commented Aug 19, 2024

@2dust I have done this before, should I do it and submit a pr?

Thank you for your work.
Since there is no protocol header to recognize, consider handling it here

fun parseCustomConfigServer(server: String?, subid: String): Int {

@CodeWithTamim
Copy link
Contributor

@2dust I have done this before, should I do it and submit a pr?

Thank you for your work. Since there is no protocol header to recognize, consider handling it here

fun parseCustomConfigServer(server: String?, subid: String): Int {

Hi guys Im little busy now, but I'm giving code for parsing the wireguard config, Just use it and it will work fine.

This is the data class for wireguard config

data class WireGuardConfig(
    val interface: WireGuardInterface,
    val peer: WireGuardPeer
)

data class WireGuardInterface(
    val privateKey: String,
    val address: String
)

data class WireGuardPeer(
    val publicKey: String,
    val presharedKey: String,
    val allowedIPs: List<String>,
    val persistentKeepalive: Int,
    val endpoint: String
)

This is the function which will take string config and returns data class

fun parseWireGuardConfig(config: String): WireGuardConfig {
    val lines = config.split("\n").map { it.trim() }.filter { it.isNotEmpty() }

    var interfaceSection = true
    val interfaceMap = mutableMapOf<String, String>()
    val peerMap = mutableMapOf<String, String>()

    for (line in lines) {
        when {
            line.startsWith("[Interface]") -> {
                interfaceSection = true
            }
            line.startsWith("[Peer]") -> {
                interfaceSection = false
            }
            interfaceSection -> {
                val (key, value) = line.split("=", limit = 2).map { it.trim() }
                interfaceMap[key] = value
            }
            else -> {
                val (key, value) = line.split("=", limit = 2).map { it.trim() }
                peerMap[key] = value
            }
        }
    }

    val `interface` = WireGuardInterface(
        privateKey = interfaceMap["PrivateKey"] ?: "",
        address = interfaceMap["Address"] ?: ""
    )

    val peer = WireGuardPeer(
        publicKey = peerMap["PublicKey"] ?: "",
        presharedKey = peerMap["PresharedKey"] ?: "",
        allowedIPs = peerMap["AllowedIPs"]?.split(",")?.map { it.trim() } ?: emptyList(),
        persistentKeepalive = peerMap["PersistentKeepalive"]?.toIntOrNull() ?: 0,
        endpoint = peerMap["Endpoint"] ?: ""
    )

    return WireGuardConfig(`interface`, peer)
}

@titancomputer
Copy link

titancomputer commented Aug 31, 2024

you should add default remark with unix time or endpoint to prevent empty config names.

@CodeWithTamim
Copy link
Contributor

you should add default remark with unix time or endpoint to prevent empty config names.

yeah I saw it shows blank name

@titancomputer
Copy link

you should add default remark with unix time or endpoint to prevent empty config names.

yeah I saw it shows blank name

now, if you go to edit mode, you have to set a remark to submit that.

@CodeWithTamim
Copy link
Contributor

ok fix in new pr

@Saje95
Copy link

Saje95 commented Sep 1, 2024

data class WireGuardConfig(
val interface: WireGuardInterface,
val peer: WireGuardPeer
)

data class WireGuardInterface(
val privateKey: String,
val address: String
)

data class WireGuardPeer(
val publicKey: String,
val presharedKey: String,
val allowedIPs: List,
val persistentKeepalive: Int,
val endpoint: String
)

@m0x61h0x64i
Copy link

still when i import a wireguard config which was created using wireguard core, the wireguard config does not work #3252

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants