tcp转发
Proxy.java
package com.dc.tcp.proxy;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Proxy {
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(8081);
while (true) {
Socket socket = server.accept();
Pool.getInstance().getExecutor().execute(new ProxyWorker(socket));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
ProxyWorker.java
package com.dc.tcp.proxy; import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException; public class ProxyWorker implements Runnable { private Socket server = null;
private Socket client = null; public ProxyWorker(Socket client) {
try {
server = new Socket("10.126.3.163", 3306);
this.client = client;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} @Override
public void run() {
Pool.getInstance().getExecutor().execute(new ProxySender(client, server));
Pool.getInstance().getExecutor().execute(new ProxyReceiver(client, server));
}
}
ProxySender.java
package com.dc.tcp.proxy; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; public class ProxySender implements Runnable { private Socket client = null;
private Socket server = null;
private Logger log = LoggerFactory.getLogger(ProxySender.class); public ProxySender(Socket client, Socket server) {
super();
this.server = server;
this.client = client;
} @Override
public void run() {
try {
InputStream is = server.getInputStream();
OutputStream os = client.getOutputStream();
// log.info("send:");
while (true) {
int flag = 0;
if ((flag = is.read()) == -1) {
break;
} else {
int count = is.available();
if (count > 0) {
byte[] b = new byte[count];
is.read(b);
byte[] temp = new byte[count + 1];
temp[0] = (byte) flag;
for (int i = 0; i < b.length; i++) {
temp[i + 1] = b[i];
}
// log.info(new String(temp));
String tempStr = "";
for (int i = 0; i < temp.length; i++) {
tempStr += " " + temp[i];
}
// log.info(tempStr);
os.write(temp);
}
}
}
} catch (IOException e) {
e.printStackTrace();
if (client != null) {
try {
client.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (server != null) {
try {
server.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
log.info("sender close socket");
}
if (client != null) {
try {
client.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (server != null) {
try {
server.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
log.info("sender close socket");
} }
ProxyReceiver.java
package com.dc.tcp.proxy; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; public class ProxyReceiver implements Runnable { private Socket server = null;
private Socket client = null;
private Logger log = LoggerFactory.getLogger(ProxyReceiver.class); public ProxyReceiver(Socket client, Socket server) {
super();
this.server = server;
this.client = client;
} @Override
public void run() {
try {
InputStream is = client.getInputStream();
OutputStream os = server.getOutputStream();
log.info("receive:");
while (true) {
int flag = 0;
if ((flag = is.read()) == -1) {
break;
} else {
int count = is.available();
if (count > 0) {
byte[] b = new byte[count];
is.read(b);
byte[] temp = new byte[count + 1];
temp[0] = (byte) flag;
for (int i = 0; i < b.length; i++) {
temp[i + 1] = b[i];
}
log.info(new String(temp));
String tempStr = "";
for (int i = 0; i < temp.length; i++) {
tempStr += " " + temp[i];
}
log.info(tempStr);
os.write(temp);
}
}
}
} catch (IOException e) {
e.printStackTrace();
if (client != null) {
try {
client.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (server != null) {
try {
server.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
log.info("receiver close socket");
}
if (client != null) {
try {
client.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (server != null) {
try {
server.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
log.info("receiver close socket");
} }
Pool.java
package com.dc.tcp.proxy; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; public class Pool { private static Pool instance = null;
private ExecutorService executor = null; private Pool() {
super();
executor = Executors.newFixedThreadPool(6);
} public static Pool getInstance() {
if (instance == null) {
synchronized (Pool.class) {
if (instance == null) {
instance = new Pool();
}
}
}
return instance;
} public ExecutorService getExecutor() {
return executor;
} }
tcp转发的更多相关文章
- centos7 编译安装nginx+tcp转发
一.依赖 1. gcc 安装安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装: yum install gcc-c++ 2. PCRE pc ...
- 手头没证书,如何给https做代理?Nginx TCP转发
线上的一个海外充值接口(https)经常因我朝网络问题中断,想借助hk的机器做个https反向代理又没证书. 一开始 一开始想到的办法是借助Nginx的tcp转发进行代理: 编译NGINX时加入 -- ...
- Golang TCP转发到指定地址
Golang TCP转发到指定地址 第二个版本,设置指定ip地址 代码 // tcpForward package main import ( "fmt" "net&qu ...
- Golang tcp转发 remoteAddr错误
Golang tcp 转发 第一版本 accept获取的Conn里的localAddr做为源地址,remoteAddr来做为目的地址 // tcpForward package main import ...
- [转帖]【rinetd】CentOS7.x上轻量级TCP转发工具rinetd的安装配置
[rinetd]CentOS7.x上轻量级TCP转发工具rinetd的安装配置 https://www.jianshu.com/p/2605d247b944 这一个写的更加全面了. 2019.07.0 ...
- nginx编译安装以及配置tcp转发
依赖包安装 yum -y install gcc gcc-c++ make automake autoconf pcre pcre-devel zlib zlib-devel openssl open ...
- FreeBSD NGINX TCP转发
前几天搞转发,研究了下TCP转发,现在记录下来 首先加载模块 注意:这是FreeBSD的位置.并且需要NGINX支持 load_module /usr/local/libexec/nginx/ngx_ ...
- p2p-tunnel 打洞内网穿透系列(三)TCP转发访问内网web服务
系列文章 p2p-tunnel 打洞内网穿透系列(一)客户端配置及打洞 p2p-tunnel 打洞内网穿透系列(二)TCP转发访问远程共享文件夹 p2p-tunnel 打洞内网穿透系列(三)TCP转发 ...
- p2p-tunnel 打洞内网穿透系列(二)TCP转发访问内网共享文件夹
系列文章 p2p-tunnel 打洞内网穿透系列(一)客户端配置及打洞 p2p-tunnel 打洞内网穿透系列(二)TCP转发访问远程共享文件夹 p2p-tunnel 打洞内网穿透系列(三)TCP转发 ...
- CentOS7.x上轻量级TCP转发工具rinetd的安装配置
一.实验背景 Linux下端口转发一般都使用iptables来实现,使用iptables可以很容易将TCP和UDP端口从防火墙转发到内部主机上. 如果需要将流量从专用地址转发到不在您当前网络上的机器上 ...
随机推荐
- PSP(11.24~11.30)
27号 类别c 内容c 开始时间s 结束e 中断I 净时间T 作业 构建执法.写博客 14:00 14:40 0m 40m 28号 类别c 内容c 开始时间s 结束e 中断I 净时间T java 编码 ...
- Business Unit Lookup in Form
Just add the below code in lookup() of StringEdit control in Form to get the Business Unit Lookup: p ...
- Oracle 11.2.4.0 ACTIVE DATAGUARD 单实例安装(COPY创建备库)
Oracle 11.2.4.0 ADG 单实例安装(COPY创建备库) 规划: 主: OS: Linux Centos 6.5 X64 hostname:ORA11G-DG1 ipaddress:19 ...
- js代码生成form,解决mvc的url参数过长问题
在MVC项目中,通常下载的文件的简单方式是直接采用 location.href+查询参数方式. var searchParams = { studentName: $("#StudentNa ...
- 使用localResizeIMG微信压缩上传图片安卓报错 weixin://preInjectJSBridge/fail
微信上传图片是经常使用的功能,首先说一下使用的是:localResizeIMG进行图片压缩上传.感觉还是很好用,基本上功能都能满足. 但是最近在开发这个功能时遇到一个奇怪的问题,就是iphone|ip ...
- C# 访问MongoDB 通用方法类
using MongoDB.Driver; using System; namespace MongoDBDemo { public class MongoDb { public MongoDb(st ...
- Samba日志分析
Samba日志分析 随着我们文件共享安全级别的提高,越来越多的情况下需要对日志进行记录并审计.Linux平台下的Samba服务的配置文件是smb.conf,有不少图形化配置工具例如Webmin.smb ...
- DataTable转List<dynamic>
DataTable转List<dynamic> 最近做的一个项目,MVC+Ado.net.没有ORM很不习惯.找到一个办法,DataTable转List<dynamic>,这样 ...
- 关于MAC
以太LAN网中发送的分组称做帧,包含7个字段 MAC帧格式 前同步码 SFD DA SA 长度或类型 数据域 CRC 7字节,物理层加入 帧首定界符,1字节 目的地址,6字节 源地址,6字节 2字节 ...
- 拒绝访问 temp 目录。用来运行 XmlSerializer 的标识“IIS APPPOOL\UGAS”没有访问 temp 目录的足够权限
在部署IIS时候会出现下图错误,拒绝访问 temp 目录.用来运行 XmlSerializer 的标识“IIS APPPOOL\UGAS”没有访问 temp 目录的足够权限 解决方法: 在IIS信息管 ...