Skip to content

Commit

Permalink
Fix http and socks5 outbound
Browse files Browse the repository at this point in the history
  • Loading branch information
selcarpa committed Mar 14, 2024
1 parent ec5074c commit d5a4ff5
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 25 deletions.
44 changes: 43 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,51 @@ java -jar surfer.jar -c=config.json

```json5
{
protocol: "galaxy"
protocol: "galaxy",
//optional
tag: ""
}
```

##### direct outbound by http proxy

```json5
{
protocol: "http",
httpSetting: {
host: "",
port: 20809,
//optional
auth: {
//optional, if not set, no authentication
username: "username",
password: "password"
},
},
//optional
tag: ""
}
```
##### direct outbound by socks5 proxy

```json5
{
protocol: "socks5",
socks5Setting: {
host: "",
port: 20808,
//optional
auth: {
//optional, if not set, no authentication
username: "username",
password: "password"
},
},
//optional
tag: ""
}
```


#### Rule

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "one.tain"
version = "1.15-SNAPSHOT"
version = "1.16-SNAPSHOT"

repositories {
mavenCentral()
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/model/config/Configuration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ data class Auth(val password: String, val username: String)
data class Outbound(
val protocol: String,
val trojanSetting: TrojanSetting?,
val sock5Setting: Sock5OutboundSetting?,
val socks5Setting: Socks5OutboundSetting?,
val httpSetting: HttpOutboundSetting?,
val outboundStreamBy: OutboundStreamBy?,
val tag: String?,
Expand All @@ -103,7 +103,7 @@ data class OutboundStreamBy(
)

@Serializable
data class Sock5OutboundSetting(val auth: Auth?, val port: Int, val host: String)
data class Socks5OutboundSetting(val auth: Auth?, val port: Int, val host: String)

@Serializable
data class HttpOutboundSetting(val auth: Auth?, val port: Int, val host: String)
Expand Down
16 changes: 10 additions & 6 deletions src/main/kotlin/protocol/Trojan.kt
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,16 @@ class TrojanProxy(
private fun addPreHandledWs(ctx: ChannelHandlerContext) {
val uri = URI(
"${
if (streamBy == Protocol.WS) {
"ws"
} else if (streamBy == Protocol.WSS) {
"wss"
} else {
throw IllegalArgumentException("unsupported stream")
when (streamBy) {
Protocol.WS -> {
"ws"
}
Protocol.WSS -> {
"wss"
}
else -> {
throw IllegalArgumentException("unsupported stream")
}
}
}://${outboundStreamBy.wsOutboundSetting!!.host}:${outboundStreamBy.wsOutboundSetting!!.port}/${
outboundStreamBy.wsOutboundSetting.path.removePrefix(
Expand Down
36 changes: 21 additions & 15 deletions src/main/kotlin/stream/Surfer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import io.netty.util.concurrent.Promise
import model.*
import model.config.HttpOutboundSetting
import model.config.Outbound
import model.config.Sock5OutboundSetting
import model.config.Socks5OutboundSetting
import model.protocol.Odor
import model.protocol.Protocol
import mu.KotlinLogging
Expand Down Expand Up @@ -80,12 +80,19 @@ private fun outbound(
if (Protocol.valueOfOrNull(outbound.protocol) == Protocol.GALAXY) {
return galaxy(connectListener, odor, eventLoopGroup)
}
val desProtocol = Protocol.valueOfOrNull(
if (outbound.outboundStreamBy != null) {
outbound.outboundStreamBy.type
} else {
outbound.protocol
}
)

setOdorRedirect(outbound, odor)
setOdorRedirect(desProtocol, outbound, odor)

return when (Protocol.valueOfOrNull(outbound.protocol)) {
return when (desProtocol) {
Protocol.SOCKS5 -> socks5Stream(
connectListener, outbound.sock5Setting!!, eventLoopGroup, odor
connectListener, outbound.socks5Setting!!, eventLoopGroup, odor
)

Protocol.HTTP -> httpStream(
Expand All @@ -103,31 +110,30 @@ private fun outbound(
}

else -> {
logger.error { "stream type ${outbound.outboundStreamBy!!.type} not supported" }

logger.error { "stream type ${desProtocol.name} not supported" }
}
}
}

fun setOdorRedirect(outbound: Outbound, odor: Odor) {
fun setOdorRedirect(protocol: Protocol, outbound: Outbound, odor: Odor) {
if (outbound.serverDns) {
odor.notDns = true
}
when (Protocol.valueOfOrNull(outbound.outboundStreamBy!!.type)) {
when (protocol) {

Protocol.WSS, Protocol.WS -> {
odor.redirectPort = outbound.outboundStreamBy.wsOutboundSetting!!.port
odor.redirectHost = outbound.outboundStreamBy.wsOutboundSetting.host
odor.redirectPort = outbound.outboundStreamBy!!.wsOutboundSetting!!.port
odor.redirectHost = outbound.outboundStreamBy.wsOutboundSetting!!.host
}

Protocol.TCP, Protocol.TLS -> {
odor.redirectPort = outbound.outboundStreamBy.tcpOutboundSetting!!.port
odor.redirectHost = outbound.outboundStreamBy.tcpOutboundSetting.host
odor.redirectPort = outbound.outboundStreamBy!!.tcpOutboundSetting!!.port
odor.redirectHost = outbound.outboundStreamBy.tcpOutboundSetting!!.host
}

Protocol.SOCKS5 -> {
odor.redirectPort = outbound.sock5Setting!!.port
odor.redirectHost = outbound.sock5Setting.host
odor.redirectPort = outbound.socks5Setting!!.port
odor.redirectHost = outbound.socks5Setting.host
}

Protocol.HTTP -> {
Expand Down Expand Up @@ -200,7 +206,7 @@ private fun httpStream(

private fun socks5Stream(
connectListener: FutureListener<Channel>,
socks5OutboundSetting: Sock5OutboundSetting,
socks5OutboundSetting: Socks5OutboundSetting,
eventLoopGroup: EventLoopGroup,
odor: Odor
) {
Expand Down

0 comments on commit d5a4ff5

Please sign in to comment.