Java使用Netty实现端口转发&Http代理&Sock5代理服务器

Java使用Netty实现端口转发&Http代理&Sock5代理服务器.md


一、简介

这里总结整理了之前使用Java写的端口转发、Http代理、Sock5代理程序,放在同一个工程中,方便使用。

开发语言:Java
开发框架:Netty

1.功能

端口转发:
HTTP代理服务器,支持账号密码认证
Sock5代理服务器,支持账号密码认证
支持连接后端时直接连接或采用代理连接,也后端代理连接认证

2.参数配置

修改config.yml

configs:
    #端口转发demo
  - enable: true
    serverType: forward
    serverPort: 13306
    remoteHost: 127.0.0.1
    remotePort: 3306
    
    #http/https代理demo
  - enable: true
    serverType: http
    serverPort: 3128
    
    #sock5代理demo,需要认证
  - enable: true
    serverType: sock5
    serverPort: 1080
    needLogin: true
    username: "test"
    password: "123456"

比如上面的配置,就是开启了一个端口转发,一个Http代理和一个Sock5代理

全量配置参考,config_full.yml:

#此文件包含所有能配置的属性,只用来查看使用,程序使用的是config.yml中的配置
configs:
    #规则是否生效,true或者false,默认为true
  - enable: true
    #类型,forward或http或sock5,表示端口转发或http代理或sock5代理,默认为forward
    serverType: forward
    #本地监听的端口号
    serverPort: 13306
    #转发的目标IP,serverType为forward时此参数才有意义
    remoteHost: 127.0.0.1
    #转发的目标端口,serverType为forward时此参数才有意义
    remotePort: 3306
    #是否需要认证,serverType为http和sock5时此参数才有意义
    needLogin: true
    #认证账号,serverType为http和sock5时此参数才有意义
    username: "user"
    #认证密码,serverType为http和sock5时此参数才有意义
    password: "pwd"
    #是否需要通过后端代理连接远程服务器,会覆盖全局的配置
    proxyNeed: false
    #如果需要后端口代理,代理连接类型,http或socks5,会覆盖全局的配置
    proxyType: http
    #如果需要后端口代理,代理连接IP,会覆盖全局的配置
    proxyIp: 127.0.0.1
    #如果需要后端口代理,代理连接Port,会覆盖全局的配置
    proxyPort: 1080
    #如果需要后端口代理,代理连接用户名,通过是否为空来决定需不需要认证,会覆盖全局的配置
    proxyUsername: ""
    #如果需要后端口代理,代理连接密码,通过是否为空来决定需不需要认证,会覆盖全局的配置
    proxyPassword: ""

#===后端代理全局配置,会对所有的configs有效,以下配置都有默认值,如果没配置,则采用默认===#
global:
  #是否需要通过后端代理连接远程服务器
  proxyNeed: false
  #代理连接类型,http或socks5
  proxyType: http
  #代理连接IP
  proxyIp: 127.0.0.1
  #代理连接Port
  proxyPort: 1080
  #代理连接用户名,通过是否为空来决定需不需要认证
  proxyUsername: ""
  #代理连接密码,通过是否为空来决定需不需要认证
  proxyPassword: ""

3.程序下载

程度可直接下载已编绎好的文件(要求JDK1.8环境下使用)

https://gitee.com/jxlhljh/nettyProxyServer/raw/master/release/nettyProxyServer.zip
https://github.com/jxlhljh/nettyProxyServer/blob/master/release/nettyProxyServer.zip

也可以采用源码编绎

git clone https://github.com/jxlhljh/nettyProxyServer.git
或
git clone https://gitee.com/jxlhljh/nettyProxyServer.git

mvn clean package

4.程序启动

解压程序

unzip nettyProxyServer.zip

$ ls -hl
total 99K
-rw-r--r-- 1 liujh 197121 342 Jan 20 17:06 config.yml
drwxr-xr-x 1 liujh 197121   0 Jan 20 18:25 lib/
-rw-r--r-- 1 liujh 197121 71K Jan 20 14:50 nettyProxyServer.jar
-rw-r--r-- 1 liujh 197121 137 Jan 20 20:59 start.bat
-rwxr-xr-x 1 liujh 197121 226 Jan 20 17:03 start.sh

启动程序

#window
./start.bat

#Linux
./start.sh

5.源码

git clone https://github.com/jxlhljh/nettyProxyServer.git
git clone https://gitee.com/jxlhljh/nettyProxyServer.git
  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java使用Netty生成代理需要使用动态代理技术。下面是一个简单的示例代码,演示了如何使用Netty生成代理: ```java import io.netty.bootstrap.Bootstrap; import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; interface MyService { String hello(String name); } class MyServiceHandler extends ChannelInboundHandlerAdapter { private Object result; public Object getResult() { return result; } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { result = msg; ctx.close(); } } class MyServiceProxy implements InvocationHandler { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { EventLoopGroup group = new NioEventLoopGroup(); try { MyServiceHandler handler = new MyServiceHandler(); Bootstrap bootstrap = new Bootstrap(); bootstrap.group(group) .channel(NioSocketChannel.class) .option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(handler); } }); Channel channel = bootstrap.connect("localhost", 8888).sync().channel(); channel.writeAndFlush(args[0]); channel.closeFuture().sync(); return handler.getResult(); } finally { group.shutdownGracefully(); } } } public class NettyProxyExample { public static void main(String[] args) { MyService myService = (MyService) Proxy.newProxyInstance( NettyProxyExample.class.getClassLoader(), new Class[]{MyService.class}, new MyServiceProxy() ); String result = myService.hello("World"); System.out.println(result); } } ``` 以上示例代码中,定义了一个`MyService`接口,代表需要代理的服务。`MyServiceHandler`是Netty的`ChannelInboundHandlerAdapter`的子类,用于处理接收到的响应消息,并将结果存储在`result`变量中。`MyServiceProxy`是动态代理实现类,它通过Netty客户端与服务器进行通信,并将代理方法的参数发送给服务器,然后等待服务器返回结果。 在`NettyProxyExample`的`main`方法中,使用`Proxy.newProxyInstance`方法创建了一个代理对象,该代理对象实现了`MyService`接口,并使用`MyServiceProxy`作为其调用处理器。通过代理对象调用接口方法时,实际上是调用了`MyServiceProxy`的`invoke`方法,在该方法内部使用Netty进行远程调用并获取结果。 请注意,以上示例仅仅是一个简单的示例,实际应用中可能需要根据具体需求进行适当修改和扩展。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值