-
Notifications
You must be signed in to change notification settings - Fork 1
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
alikemal.ocalan
committed
Jan 30, 2021
1 parent
2c7ea43
commit f5439a9
Showing
14 changed files
with
148 additions
and
198 deletions.
There are no files selected for viewing
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
90 changes: 0 additions & 90 deletions
90
src/main/kotlin/com/github/alikemalocalan/greentunnel4jvm/HttpProxyClientHandler.kt
This file was deleted.
Oops, something went wrong.
40 changes: 0 additions & 40 deletions
40
src/main/kotlin/com/github/alikemalocalan/greentunnel4jvm/HttpProxyRemoteHandler.kt
This file was deleted.
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
4 changes: 2 additions & 2 deletions
4
src/main/kotlin/com/github/alikemalocalan/greentunnel4jvm/gui/ServerThread.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
84 changes: 84 additions & 0 deletions
84
src/main/kotlin/com/github/alikemalocalan/greentunnel4jvm/handler/ProxyClientHandler.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,84 @@ | ||
package com.github.alikemalocalan.greentunnel4jvm.handler | ||
|
||
|
||
import com.github.alikemalocalan.greentunnel4jvm.models.HttpRequest | ||
import com.github.alikemalocalan.greentunnel4jvm.utils.HttpServiceUtils | ||
import com.github.alikemalocalan.greentunnel4jvm.utils.HttpServiceUtils.firstHttpsResponse | ||
import io.netty.bootstrap.Bootstrap | ||
import io.netty.buffer.ByteBuf | ||
import io.netty.channel.Channel | ||
import io.netty.channel.ChannelHandlerContext | ||
import io.netty.channel.ChannelInboundHandlerAdapter | ||
import io.netty.channel.ChannelOption | ||
import io.netty.channel.socket.nio.NioSocketChannel | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import java.util.* | ||
|
||
|
||
class ProxyClientHandler : ChannelInboundHandlerAdapter() { | ||
private val logger: Logger = LoggerFactory.getLogger(this::class.java) | ||
private var remoteChannelOpt: Optional<Channel> = Optional.empty() | ||
|
||
private val bootstrap: Bootstrap = Bootstrap() | ||
.channel(NioSocketChannel::class.java) | ||
.option(ChannelOption.SINGLE_EVENTEXECUTOR_PER_GROUP, false) | ||
.option(ChannelOption.TCP_NODELAY, true) | ||
.option(ChannelOption.SO_KEEPALIVE, true) | ||
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000) | ||
|
||
override fun channelActive(ctx: ChannelHandlerContext) { | ||
ctx.writeAndFlush(firstHttpsResponse()) // if https,return respond 200 | ||
} | ||
|
||
override fun channelRead(ctx: ChannelHandlerContext, msg: Any) { | ||
val buf: ByteBuf = msg as ByteBuf | ||
|
||
if (remoteChannelOpt.isPresent) { // request take second time from the client | ||
remoteChannelOpt.map { remoteChannel -> | ||
HttpServiceUtils.splitAndWriteByteBuf(buf, remoteChannel) | ||
} | ||
} else // request take first time from the client | ||
HttpServiceUtils.httpRequestFromByteBuf(buf).map { request -> | ||
if (request.isHttps) { | ||
remoteChannelOpt = sendRequestToRemoteChannel(ctx, request) | ||
} else { //if http,force to https without any remote connection | ||
val response = HttpServiceUtils.redirectHttpToHttps(request.host()) | ||
ctx.writeAndFlush(response) | ||
ctx.close() | ||
} | ||
|
||
} | ||
} | ||
|
||
private fun sendRequestToRemoteChannel( | ||
ctx: ChannelHandlerContext, | ||
request: HttpRequest | ||
): Optional<Channel> = | ||
kotlin.runCatching { request.toInetSocketAddress() }.fold(onSuccess = { remoteAddress -> | ||
val remoteFuture = bootstrap | ||
.group(ctx.channel().eventLoop()) // use the same EventLoop | ||
.handler(ProxyRemoteHandler(ctx, request)) | ||
.connect(remoteAddress) | ||
|
||
ctx.channel().config().isAutoRead = false // if remote connection has done, stop reading | ||
remoteFuture.addListener { | ||
ctx.channel().config().isAutoRead = true // connection is ready, enable AutoRead | ||
} | ||
|
||
return Optional.of(remoteFuture.channel()) | ||
}, onFailure = { return Optional.empty<Channel>() }) | ||
|
||
|
||
override fun exceptionCaught(ctx: ChannelHandlerContext, cause: Throwable) { | ||
ctx.close() | ||
remoteChannelOpt = Optional.empty() | ||
logger.error("Proxy Client Connection lost !!") | ||
} | ||
|
||
override fun channelInactive(ctx: ChannelHandlerContext?) { | ||
if (remoteChannelOpt.isPresent) | ||
remoteChannelOpt.get().close() | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
src/main/kotlin/com/github/alikemalocalan/greentunnel4jvm/handler/ProxyRemoteHandler.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,28 @@ | ||
package com.github.alikemalocalan.greentunnel4jvm.handler | ||
|
||
import com.github.alikemalocalan.greentunnel4jvm.models.HttpRequest | ||
import com.github.alikemalocalan.greentunnel4jvm.utils.HttpServiceUtils | ||
import io.netty.channel.ChannelHandlerContext | ||
import io.netty.channel.ChannelInboundHandlerAdapter | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
|
||
|
||
class ProxyRemoteHandler(private val clientChannel: ChannelHandlerContext, private val request: HttpRequest) : | ||
ChannelInboundHandlerAdapter() { | ||
private val logger: Logger = LoggerFactory.getLogger(this::class.java) | ||
|
||
override fun channelRead(ctx: ChannelHandlerContext, msg: Any) { | ||
clientChannel.writeAndFlush(msg) | ||
} | ||
|
||
override fun channelActive(ctx: ChannelHandlerContext) { | ||
HttpServiceUtils.splitAndWriteByteBuf(request.toByteBuf(), ctx.channel()) | ||
} | ||
|
||
override fun exceptionCaught(ctx: ChannelHandlerContext, cause: Throwable) { | ||
ctx.close() | ||
logger.error("Website Connection error") | ||
} | ||
|
||
} |
Oops, something went wrong.