Skip to content

Commit 3da1d48

Browse files
committed
Initial commit
Let's see if the workflow works first try...
0 parents  commit 3da1d48

File tree

13 files changed

+472
-0
lines changed

13 files changed

+472
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.github/workflows/docker.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: docker
2+
3+
on:
4+
push:
5+
6+
jobs:
7+
docker:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- name: Checkout code.
11+
uses: actions/checkout@v3
12+
13+
- name: Set up JDK 21
14+
uses: actions/setup-java@v3
15+
with:
16+
java-version: "21"
17+
distribution: "temurin"
18+
cache: maven
19+
20+
- name: Compile the code.
21+
run: mvn clean package
22+
23+
- name: Set up QEMU
24+
uses: docker/setup-qemu-action@v3
25+
26+
- name: Set up Docker Buildx
27+
uses: docker/setup-buildx-action@v3
28+
29+
- name: Login to GitHub Container Registry
30+
uses: docker/login-action@v1
31+
with:
32+
registry: ghcr.io
33+
username: ${{github.actor}}
34+
password: ${{secrets.GITHUB_TOKEN}}
35+
36+
- name: Build Inventory Image
37+
run: |
38+
docker build . --tag ghcr.io/casterlabs/tcp-proxy:latest
39+
docker push ghcr.io/casterlabs/tcp-proxy:latest

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# IntelliJ
2+
*.iml
3+
.idea/
4+
5+
# Eclipse
6+
.settings/
7+
.project
8+
.classpath
9+
10+
target/
11+
config.json

Dockerfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM eclipse-temurin:21-jre-ubi9-minimal
2+
WORKDIR /home/container
3+
4+
LABEL org.opencontainers.image.source="https://github.com/casterlabs/tcp-proxy"
5+
6+
# code
7+
COPY ./launch.sh /home/container
8+
COPY ./target/tcp-proxy.jar /home/container
9+
RUN chmod +x launch.sh
10+
11+
# entrypoint
12+
CMD [ "./launch.sh" ]
13+
EXPOSE 8000/tcp

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Casterlabs
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

compose.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
services:
2+
tcpproxy:
3+
image: ghcr.io/casterlabs/tcp-proxy:latest
4+
restart: always
5+
environment:
6+
- SO_TIMEOUT=30000
7+
- TARGET_ADDR=example.com
8+
- TARGET_PORT=80
9+
ports:
10+
- 8000:8080

docker_launch.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
java \
4+
-Dfastloggingframework.defaultlevel=INFO \
5+
-Dtp.bindport=8000 \
6+
-Dtp.sotimeout=$SO_TIMEOUT \
7+
-Dtp.targetaddr=$TARGET_ADDR \
8+
-Dtp.targetport=$TARGET_PORT \
9+
-jar tcp-proxy.jar

pom.xml

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>co.casterlabs</groupId>
6+
<artifactId>tcp-proxy</artifactId>
7+
<version>1.0.0</version>
8+
9+
<properties>
10+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
12+
</properties>
13+
14+
<build>
15+
<plugins>
16+
<plugin>
17+
<groupId>org.apache.maven.plugins</groupId>
18+
<artifactId>maven-compiler-plugin</artifactId>
19+
<version>3.8.1</version>
20+
<configuration>
21+
<source>21</source>
22+
<target>21</target>
23+
</configuration>
24+
</plugin>
25+
<plugin>
26+
<groupId>org.apache.maven.plugins</groupId>
27+
<artifactId>maven-jar-plugin</artifactId>
28+
<version>2.3.2</version>
29+
<configuration>
30+
<finalName>${project.name}-original</finalName>
31+
</configuration>
32+
</plugin>
33+
<plugin>
34+
<groupId>org.apache.maven.plugins</groupId>
35+
<artifactId>maven-shade-plugin</artifactId>
36+
<version>3.2.1</version>
37+
<executions>
38+
<execution>
39+
<id>shade</id>
40+
<phase>package</phase>
41+
<goals>
42+
<goal>shade</goal>
43+
</goals>
44+
</execution>
45+
</executions>
46+
<configuration>
47+
<shadedArtifactAttached>true</shadedArtifactAttached>
48+
<finalName>${project.name}</finalName>
49+
<transformers>
50+
<transformer
51+
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
52+
<mainClass>
53+
co.casterlabs.tcp_proxy.Main</mainClass>
54+
</transformer>
55+
</transformers>
56+
</configuration>
57+
</plugin>
58+
</plugins>
59+
</build>
60+
61+
<repositories>
62+
<repository>
63+
<id>casterlabs-maven</id>
64+
<url>https://repo.casterlabs.co/maven</url>
65+
</repository>
66+
<repository>
67+
<id>jitpack.io</id>
68+
<url>https://jitpack.io</url>
69+
</repository>
70+
</repositories>
71+
72+
<dependencies>
73+
<dependency>
74+
<groupId>org.projectlombok</groupId>
75+
<artifactId>lombok</artifactId>
76+
<version>1.18.12</version>
77+
<scope>provided</scope>
78+
</dependency>
79+
<dependency>
80+
<groupId>org.jetbrains</groupId>
81+
<artifactId>annotations</artifactId>
82+
<version>16.0.1</version>
83+
<scope>provided</scope>
84+
</dependency> <!-- For Eclipse users -->
85+
86+
<dependency>
87+
<groupId>co.casterlabs.commons</groupId>
88+
<artifactId>io</artifactId>
89+
<version>1.8.0-1</version>
90+
<scope>compile</scope>
91+
</dependency>
92+
93+
<dependency>
94+
<groupId>co.casterlabs</groupId>
95+
<artifactId>rson</artifactId>
96+
<version>1.18.0</version>
97+
<scope>compile</scope>
98+
</dependency>
99+
<dependency>
100+
<groupId>com.github.e3ndr</groupId>
101+
<artifactId>FastLoggingFramework</artifactId>
102+
<version>1.12.1</version>
103+
<scope>compile</scope>
104+
</dependency>
105+
</dependencies>
106+
</project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package co.casterlabs.tcp_proxy;
2+
3+
import co.casterlabs.rakurai.json.annotating.JsonClass;
4+
import co.casterlabs.rakurai.json.validation.JsonValidate;
5+
6+
@JsonClass(exposeAll = true)
7+
public class Config {
8+
public String bindAddress = "::";
9+
public int bindPort = 8080;
10+
11+
public String targetAddress = "example.com";
12+
public int targetPort = 80;
13+
14+
public int soTimeoutSeconds = 30;
15+
16+
@JsonValidate
17+
private void $validate() {
18+
if (this.bindPort < 0 || this.bindPort > 65535) throw new IllegalArgumentException("bindPort must be between 0 and 65535 (inclusive).");
19+
if (this.targetPort < 0 || this.targetPort > 65535) throw new IllegalArgumentException("targetPort must be between 0 and 65535 (inclusive).");
20+
if (this.soTimeoutSeconds < 0) throw new IllegalArgumentException("soTimeoutSeconds must be greater than or equal to 0.");
21+
if (this.bindAddress == null) throw new IllegalArgumentException("bindAddresses item cannot be null.");
22+
if (this.targetAddress == null) throw new IllegalArgumentException("targetAddress cannot be null.");
23+
}
24+
25+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package co.casterlabs.tcp_proxy;
2+
3+
import java.io.File;
4+
import java.nio.file.FileSystems;
5+
import java.nio.file.Path;
6+
import java.nio.file.StandardWatchEventKinds;
7+
import java.nio.file.WatchEvent;
8+
import java.nio.file.WatchKey;
9+
import java.nio.file.WatchService;
10+
import java.util.concurrent.TimeUnit;
11+
12+
import lombok.AllArgsConstructor;
13+
14+
//Modified from: https://stackoverflow.com/questions/16251273/can-i-watch-for-single-file-change-with-watchservice-not-the-whole-directory
15+
@AllArgsConstructor
16+
public abstract class FileWatcher extends Thread {
17+
private File file;
18+
19+
public abstract void onChange();
20+
21+
@SuppressWarnings("unchecked")
22+
@Override
23+
public void run() {
24+
try (WatchService watcher = FileSystems.getDefault().newWatchService()) {
25+
Path path = this.file.getCanonicalFile().getParentFile().toPath();
26+
path.register(watcher, StandardWatchEventKinds.ENTRY_MODIFY);
27+
28+
while (true) {
29+
WatchKey key = watcher.poll(25, TimeUnit.MILLISECONDS);
30+
31+
if (key == null) {
32+
Thread.yield();
33+
continue;
34+
}
35+
36+
for (WatchEvent<?> event : key.pollEvents()) {
37+
WatchEvent<Path> ev = (WatchEvent<Path>) event;
38+
WatchEvent.Kind<?> kind = event.kind();
39+
Path filename = ev.context();
40+
41+
if (kind == StandardWatchEventKinds.OVERFLOW) {
42+
Thread.yield();
43+
continue;
44+
} else if (kind == java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY
45+
&& filename.toString().equals(this.file.getName())) {
46+
this.onChange();
47+
}
48+
boolean valid = key.reset();
49+
if (!valid) {
50+
break;
51+
}
52+
}
53+
}
54+
} catch (Throwable e) {
55+
e.printStackTrace();
56+
}
57+
}
58+
59+
}

0 commit comments

Comments
 (0)