Skip to content

Commit

Permalink
Reformat for performance
Browse files Browse the repository at this point in the history
  • Loading branch information
alikemal.ocalan committed Jan 30, 2021
1 parent 2c7ea43 commit f5439a9
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 198 deletions.
16 changes: 1 addition & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,10 @@ Add the dependency:

```
dependencies {
implementation 'com.github.alikemalocalan:greentunnel4jvm:1.13-SNAPSHOT'
implementation 'com.github.alikemalocalan:greentunnel4jvm:2.3-SNAPSHOT'
}
```


and then:

```bash
./gradlew run
```

code:

```kotlin
com.github.alikemalocalan.greentunnel4jvm.HttpProxyServer().newProxyService()

```

## GUI ScreenShot
![screenshot](https://raw.githubusercontent.com/alikemalocalan/greentunnel4jvm/master/Screen-gui.png)

Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ application {
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
compile 'io.netty:netty-handler-proxy:4.1.58.Final'
implementation "org.jetbrains.kotlin:kotlin-stdlib"
compile 'io.netty:netty-transport:4.1.58.Final'
compile 'com.squareup.okhttp3:okhttp-dnsoverhttps:4.9.0'
implementation 'ch.qos.logback:logback-classic:1.2.3'
}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ kotlin.code.style=official
kotlin_version=1.4.21-2
projectGroup=com.github.alikemalocalan
projectName=greentunnel4jvm
projectVersion=2.3-SNAPSHOT
projectVersion=2.4-SNAPSHOT
projectMainClassName=com.github.alikemalocalan.greentunnel4jvm.gui.Gui
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.alikemalocalan</groupId>
<artifactId>greentunnel4jvm</artifactId>
<version>2.3-SNAPSHOT</version>
<version>2.4-SNAPSHOT</version>
<inceptionYear>2008</inceptionYear>
<licenses>
<license>
Expand All @@ -16,7 +16,7 @@
<dependencies>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-handler-proxy</artifactId>
<artifactId>netty-transport</artifactId>
<version>4.1.58.Final</version>
<scope>compile</scope>
</dependency>
Expand All @@ -28,7 +28,7 @@
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<artifactId>kotlin-stdlib</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.github.alikemalocalan.greentunnel4jvm

import ch.qos.logback.classic.util.ContextInitializer
import com.github.alikemalocalan.greentunnel4jvm.handler.ProxyClientHandler
import com.github.alikemalocalan.greentunnel4jvm.utils.HttpServiceUtils
import io.netty.bootstrap.ServerBootstrap
import io.netty.channel.ChannelInitializer
import io.netty.channel.ChannelOption
Expand All @@ -11,7 +13,7 @@ import org.slf4j.Logger
import org.slf4j.LoggerFactory


class HttpProxyServer {
class ProxyServer {
private val logger: Logger = LoggerFactory.getLogger(this::class.java)

private val workerGroup = NioEventLoopGroup(10)
Expand All @@ -22,6 +24,7 @@ class HttpProxyServer {
.channel(NioServerSocketChannel::class.java)
.option(ChannelOption.SO_BACKLOG, 1024)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000)
.childOption(ChannelOption.SO_KEEPALIVE, true)


fun createNettyServer(port: Int = 8080) {
Expand All @@ -30,7 +33,7 @@ class HttpProxyServer {
bootstrap.childHandler(object : ChannelInitializer<SocketChannel>() {
override fun initChannel(ch: SocketChannel) {
ch.pipeline().addLast(
HttpProxyClientHandler()
ProxyClientHandler()
)
}
})
Expand All @@ -55,9 +58,9 @@ fun main() {
System.setProperty(ContextInitializer.CONFIG_FILE_PROPERTY, "console-log-config.xml")
val port: Any? = System.getProperties()["proxy.port"]

fun getPort() = port?.toString()?.toInt() ?: 8080
fun getPort() = port?.toString()?.toInt() ?: HttpServiceUtils.defaultPort

val server = HttpProxyServer()
val server = ProxyServer()

server.createNettyServer(port = getPort())
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.github.alikemalocalan.greentunnel4jvm.gui

import com.github.alikemalocalan.greentunnel4jvm.HttpProxyServer
import com.github.alikemalocalan.greentunnel4jvm.ProxyServer
import java.util.concurrent.atomic.AtomicBoolean

class ServerThread(name: String, private val port: Int) : Thread(name) {
private val running: AtomicBoolean = AtomicBoolean(false)

private val serverBuilder = HttpProxyServer()
private val serverBuilder = ProxyServer()

override fun run() {
running.set(true)
Expand Down
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()
}

}
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")
}

}
Loading

0 comments on commit f5439a9

Please sign in to comment.