完成功能
All checks were successful
Gitea Actions Build / Build (push) Successful in 53s

This commit is contained in:
Armamem0t 2025-07-05 23:32:11 +08:00
parent eb290714b4
commit 6056f6fa90
Signed by: minglipro
GPG Key ID: 5F355A77B22AA93B
10 changed files with 303 additions and 43 deletions

View File

@ -38,6 +38,11 @@ dependencies {
testRuntimeOnly("org.junit.platform:junit-platform-launcher") testRuntimeOnly("org.junit.platform:junit-platform-launcher")
implementation("org.jetbrains:annotations:24.0.0") implementation("org.jetbrains:annotations:24.0.0")
annotationProcessor("org.jetbrains:annotations:24.0.0") annotationProcessor("org.jetbrains:annotations:24.0.0")
implementation("com.mingliqiye:network-endpoint:1.0.2")
implementation("com.mingliqiye:string-utilts:1.0.4")
implementation("com.mingliqiye:socket-utilts:1.0.6")
implementation("org.apache.logging.log4j:log4j-core:2.12.4")
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.19.1")
} }
tasks.test { tasks.test {

View File

@ -1,27 +0,0 @@
name: Gitea Actions Build
run-name: ${{ gitea.actor }} is testing out Gitea Actions 🚀
on:
push:
branches:
- master
jobs:
Build:
runs-on: ubuntu-dev
steps:
- name: Check out repository code
uses: https://git.mingliqiye.com/Actions/checkout@v4
- name: build-test
run: |
source gradle.properties
gradle
gradle build-jar
- name: Releases
run: |
source gradle.properties
SHA=${{gitea.sha}}
curl -o- https://git.mingliqiye.com/Actions/com.mingliqiye.gitea.releases/raw/branch/master/install.sh | bash
FILENAME="${GROUPSID}-${VERSIONS}.jar"
java -jar com.mingliqiye.gitea.releases.jar -s "${{gitea.server_url}}" -o "${{gitea.repository_owner}}" -r ${{gitea.event.repository.name}} -t "${{gitea.token}}" -ti "Auto releases ${{gitea.sha}} ${VERSIONS}" -b "# Auto releases wtih ${{gitea.event.head_commit.message}} - [${{gitea.sha}}](${{gitea.event.head_commit.url}})" -tn "Auto-Releases-${VERSIONS}-${SHA:0:10}" -a "build/libs"

View File

@ -1,5 +1,5 @@
GROUPSID=com.mingliqiye GROUPSID=com.mingliqiye
ARTIFACTID=socket-utilts ARTIFACTID=tcp-proxy
VERSIONS=0.1 VERSIONS=0.1
MAINCLASS=com.mingliqiye.Main MAINCLASS=com.mingliqiye.tcp.proxy.Main
JDKVERSIONS=1.8 JDKVERSIONS=1.8

View File

@ -1 +1 @@
rootProject.name = "socket-utilts" rootProject.name = "tcp-proxy"

View File

@ -1,13 +0,0 @@
package com.mingliqiye;
public class Main {
/**
* @param args []
*/
public static void main(String[] args) {
System.out.print("Hello and welcome!");
for (int i = 1; i <= 5; i++) {
System.out.println("i = " + i);
}
}
}

View File

@ -0,0 +1,32 @@
package com.mingliqiye.tcp.proxy;
import com.mingliqiye.network.endpoint.NetworkEndpoint;
import com.mingliqiye.network.endpoint.NetworkException;
import java.lang.reflect.Array;
import java.util.Arrays;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Main {
private static final Logger log = LogManager.getLogger(Main.class);
public static void main(String[] args) {
log.info("启动中...");
log.info("命令行输入 {}", Arrays.toString(args));
if (args.length != 2) {
NetworkException exception = new NetworkException(
"请输入正确的地址代理地址"
);
log.error(exception.getMessage(), exception);
log.warn("格式 *** 0:23335 127.0.0.1:25566");
log.warn("格式 *** 本地打开 代理到远端");
throw exception;
}
Proxy proxy = new Proxy(
NetworkEndpoint.of(args[0]),
NetworkEndpoint.of(args[1])
);
proxy.start();
}
}

View File

@ -0,0 +1,32 @@
package com.mingliqiye.tcp.proxy;
import com.mingliqiye.network.endpoint.NetworkEndpoint;
import com.mingliqiye.utils.TcpSocketServer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Proxy {
private static Logger log = LogManager.getLogger(Proxy.class);
private final TcpSocketServer tcpSocketServer;
Proxy(NetworkEndpoint endpoint, NetworkEndpoint remoteEndpoint) {
tcpSocketServer = new TcpSocketServer(
new ServerEvent(endpoint, remoteEndpoint)
);
tcpSocketServer.bing(endpoint);
log.info(
"从 {} 代理到 {} 正在启动",
endpoint.toHostPortString(),
remoteEndpoint.toHostPortString()
);
}
public void start() {
tcpSocketServer.start();
}
public void close() {
tcpSocketServer.close();
}
}

View File

@ -0,0 +1,108 @@
package com.mingliqiye.tcp.proxy;
import com.mingliqiye.network.endpoint.NetworkEndpoint;
import com.mingliqiye.utils.BufferBytesEntity;
import com.mingliqiye.utils.TcpSocketClient;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class ProxyClient extends Thread implements Closeable {
private static Logger log = LogManager.getLogger(ProxyClient.class);
private final String id;
private final Socket socket = new Socket();
private OutputStream outputStream;
private InputStream inputStream;
private final ServerEvent serverEvent;
private final TcpSocketClient client;
public ProxyClient(
String id,
TcpSocketClient client,
ServerEvent serverEvent
) {
this.id = id;
this.client = client;
this.serverEvent = serverEvent;
}
public String getIds() {
return id;
}
public boolean send(String id, BufferBytesEntity bufferBytesEntity) {
try {
this.outputStream.write(
bufferBytesEntity.getBytes(),
0,
bufferBytesEntity.getLen()
);
log.info(
"({})->({}) [{}bit]",
client.getEndpoint().toHostPortString(),
NetworkEndpoint.of(
(InetSocketAddress) socket.getRemoteSocketAddress()
).toHostPortString(),
bufferBytesEntity.getLen()
);
} catch (IOException e) {
return false;
}
return true;
}
public boolean init(NetworkEndpoint endpoint) {
try {
socket.connect(endpoint.toInetSocketAddress());
outputStream = socket.getOutputStream();
inputStream = socket.getInputStream();
} catch (IOException e) {
return false;
}
return true;
}
public void run() {
byte[] bytes = new byte[1024];
int readlen;
while (!Thread.currentThread().isInterrupted()) {
try {
readlen = this.inputStream.read(bytes);
if (readlen == -1) break;
client.send(bytes, 0, readlen);
log.info(
"({})->({}) [{}bit]",
NetworkEndpoint.of(
(InetSocketAddress) socket.getRemoteSocketAddress()
).toHostPortString(),
client.getEndpoint().toHostPortString(),
readlen
);
} catch (IOException e) {
break;
}
}
client.getFather().closeItem(client.getIds());
}
@Override
public void close() {
try {
socket.close();
} catch (IOException ignored) {}
try {
outputStream.close();
} catch (IOException ignored) {}
try {
inputStream.close();
} catch (IOException ignored) {}
}
}

View File

@ -0,0 +1,94 @@
package com.mingliqiye.tcp.proxy;
import com.mingliqiye.network.endpoint.NetworkEndpoint;
import com.mingliqiye.utils.BufferBytesEntity;
import com.mingliqiye.utils.TcpServerImplementation;
import com.mingliqiye.utils.TcpSocketClient;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class ServerEvent extends TcpServerImplementation {
private final NetworkEndpoint endpoint;
private final NetworkEndpoint remoteEndpoint;
/**
* 客户端字典(线程安全的)
*/
private final Map<String, ProxyClient> clientMap =
new ConcurrentHashMap<>();
private static Logger log = LogManager.getLogger(Proxy.class);
public ServerEvent(
NetworkEndpoint endpoint,
NetworkEndpoint remoteEndpoint
) {
this.endpoint = endpoint;
this.remoteEndpoint = remoteEndpoint;
}
public boolean connect(TcpSocketClient client, String id) {
ProxyClient proxyClient = new ProxyClient(id, client, this);
if (!proxyClient.init(remoteEndpoint)) {
log.error(
"远端 {} 连接失败 已使客户端 {} 断开连接 当前连接数 {}",
remoteEndpoint.toHostPortString(),
client.getEndpoint().toHostPortString(),
clientMap.size()
);
client.getFather().closeItem(id);
return false;
}
proxyClient.start();
clientMap.put(id, proxyClient);
log.info(
"客户端 {} 连接成功 当前连接数 {}",
client.getEndpoint().toHostPortString(),
clientMap.size()
);
return true;
}
public void disconnect(TcpSocketClient client, String id) {
ProxyClient proxyClient = clientMap.get(client.getIds());
if (proxyClient == null) {
return;
}
proxyClient.close();
clientMap.remove(client.getIds());
log.info(
"客户端 {} 断开连接 当前连接数 {}",
client.getEndpoint().toHostPortString(),
clientMap.size()
);
}
public void reception(
TcpSocketClient client,
BufferBytesEntity bufferBytesEntity
) {
ProxyClient proxyClient = clientMap.get(client.getIds());
if (proxyClient == null) {
return;
}
if (!proxyClient.send(client.getIds(), bufferBytesEntity)) {
client.getFather().closeItem(client.getIds());
}
}
public void started(ServerSocket serverSocket) {
log.info(
"服务器启动 监听 {}",
NetworkEndpoint.of(
(InetSocketAddress) serverSocket.getLocalSocketAddress()
).toHostPortString()
);
}
public void closed() {
log.info("服务器已经关闭");
}
}

View File

@ -0,0 +1,29 @@
Appenders:
Console: #输出到控制台
name: CONSOLE #Appender命名
target: SYSTEM_OUT
PatternLayout:
pattern: "%style{%d{yyyy-MM-dd HH:mm:ss,SSS}}{bright,magenta} [%highlight{%p}{FATAL=white, ERROR=red, WARN=yellow, INFO=green, DEBUG=cyan, TRACE=blue}] [%style{%t}{bright,yellow}/%style{%c{1}}{bright,cyan}] -- %style{%m}{#EEDFCC}%n" #输出日志的格式
disableAnsi: "${env:DISABLECOLOR:-false}"
RollingFile:
- name: FILE
fileName: 'log/info.log'
filePattern: "log/$${date:yyyy-MM-dd}/%d{yyyy-MM-dd}-%i.log"
PatternLayout:
pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS} [%p] [%t/%F:%L/%M/%c] -- %m%n" #输出日志的格式
Policies:
SizeBasedTriggeringPolicy:
size: "10 KB"
TimeBasedTriggeringPolicy:
modulate: true
interval: 1
DefaultRolloverStrategy: # 单目录下文件最多20个超过会删除最早之前的
max: 1000
Loggers:
Root:
level: INFO
additivity: true
AppenderRef:
- ref: CONSOLE
- ref: FILE