This repository has been archived by the owner on Jan 4, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 924
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d4b7f5f
commit 7771636
Showing
29 changed files
with
332 additions
and
75 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
app/src/main/java/io/nekohasekai/sagernet/fmt/brook/BrookBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/****************************************************************************** | ||
* * | ||
* Copyright (C) 2021 by nekohasekai <sekai@neko.services> * | ||
* Copyright (C) 2021 by Max Lv <max.c.lv@gmail.com> * | ||
* Copyright (C) 2021 by Mygod Studio <contact-shadowsocks-android@mygod.be> * | ||
* * | ||
* This program is free software: you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation, either version 3 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
* This program is distributed in the hope that it will be useful, * | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
* GNU General Public License for more details. * | ||
* * | ||
* You should have received a copy of the GNU General Public License * | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. * | ||
* * | ||
******************************************************************************/ | ||
|
||
package io.nekohasekai.sagernet.fmt.brook; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import com.esotericsoftware.kryo.io.ByteBufferInput; | ||
import com.esotericsoftware.kryo.io.ByteBufferOutput; | ||
|
||
import io.nekohasekai.sagernet.fmt.AbstractBean; | ||
import io.nekohasekai.sagernet.fmt.KryoConverters; | ||
|
||
public class BrookBean extends AbstractBean { | ||
|
||
public String protocol; | ||
public String password; | ||
|
||
@Override | ||
public void initDefaultValues() { | ||
super.initDefaultValues(); | ||
if (protocol == null) protocol = ""; | ||
if (password == null) password = ""; | ||
} | ||
|
||
@Override | ||
public void serialize(ByteBufferOutput output) { | ||
output.writeInt(0); | ||
super.serialize(output); | ||
output.writeString(protocol); | ||
output.writeString(password); | ||
} | ||
|
||
@Override | ||
public void deserialize(ByteBufferInput input) { | ||
int version = input.readInt(); | ||
super.deserialize(input); | ||
protocol = input.readString(); | ||
password = input.readString(); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public BrookBean clone() { | ||
return KryoConverters.deserialize(new BrookBean(), KryoConverters.serialize(this)); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
app/src/main/java/io/nekohasekai/sagernet/fmt/brook/BrookFmt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/****************************************************************************** | ||
* * | ||
* Copyright (C) 2021 by nekohasekai <sekai@neko.services> * | ||
* Copyright (C) 2021 by Max Lv <max.c.lv@gmail.com> * | ||
* Copyright (C) 2021 by Mygod Studio <contact-shadowsocks-android@mygod.be> * | ||
* * | ||
* This program is free software: you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation, either version 3 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
* This program is distributed in the hope that it will be useful, * | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
* GNU General Public License for more details. * | ||
* * | ||
* You should have received a copy of the GNU General Public License * | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. * | ||
* * | ||
******************************************************************************/ | ||
|
||
package io.nekohasekai.sagernet.fmt.brook | ||
|
||
import io.nekohasekai.sagernet.fmt.AbstractBean | ||
import io.nekohasekai.sagernet.fmt.socks.SOCKSBean | ||
import io.nekohasekai.sagernet.ktx.applyDefaultValues | ||
import io.nekohasekai.sagernet.ktx.unUrlSafe | ||
import io.nekohasekai.sagernet.ktx.urlSafe | ||
|
||
// https://txthinking.github.io/brook/#/brook-link | ||
fun parseBrook(text: String): AbstractBean { | ||
var server = text.substringAfter("brook://").unUrlSafe() | ||
if (server.startsWith("socks5://")) { | ||
server = server.substringAfter("://") | ||
val bean = SOCKSBean() | ||
bean.serverAddress = server.substringBefore(":") | ||
bean.serverPort = server.substringAfter(":").substringBefore(" ").toInt() | ||
server = server.substringAfter(":").substringAfter(" ") | ||
if (server.contains(" ")) { | ||
bean.username = server.substringBefore(" ") | ||
bean.password = server.substringAfter(" ") | ||
} | ||
return bean.applyDefaultValues() | ||
} | ||
|
||
val bean = BrookBean() | ||
|
||
if (server.startsWith("ws://")) { | ||
bean.protocol = "ws" | ||
server = server.substringAfter("://") | ||
} else if (server.startsWith("wss://")) { | ||
bean.protocol = "wss" | ||
server = server.substringAfter("://") | ||
} else { | ||
bean.protocol = "" | ||
} | ||
|
||
bean.serverAddress = server.substringBefore(":") | ||
bean.serverPort = server.substringAfter(":").substringBefore(" ").toInt() | ||
server = server.substringAfter(":") | ||
if (server.contains(" ")) { | ||
bean.password = server.substringAfter(" ") | ||
} | ||
return bean.applyDefaultValues() | ||
} | ||
|
||
fun BrookBean.toUri(): String { | ||
var server = when (protocol) { | ||
"ws" -> "ws://$serverAddress:$serverPort" | ||
"wss" -> "wss://$serverAddress:$serverPort" | ||
else -> "$serverAddress:$serverPort" | ||
} | ||
if (password.isNotBlank()) { | ||
server = "$server $password" | ||
} | ||
return "brook://" + server.urlSafe() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.