分享一个基于 netty 的 java 开源项目

1、简介

中微子代理(neutrino-proxy)是一个基于 netty 的、开源的 java 内网穿透项目。遵循 MIT 许可,因此您可以对它进行复制、修改、传播并用于任何个人或商业行为。

2、项目结构

  • neutrino-proxy
    • neutrino-core 与代理无关的基础封装
    • neutrino-proxy-core 与代理相关的公共常量、编解码器
    • neutrino-proxy-client 代理客户端项目
    • neutrino-proxy-server 代理服务端项目
    • neutrino-proxy-admin 代理监控项目(暂时空缺)

3、运行

3.1、使用 keytool 工具生成 ssl 证书,若不需要 ssl 加密可跳过

keytool -genkey -alias test1 -keyalg RSA -keysize 1024 -validity 3650 -keypass 123456 -storepass 123456 -keystore  "./test.jks"

3.2、修改服务端配置(application.yml)

application:
  name: neutrino-proxy-server

proxy:
  protocol:
    max-frame-length: 2097152
    length-field-offset: 0
    length-field-length: 4
    initial-bytes-to-strip: 0
    length-adjustment: 0
    read-idle-time: 60
    write-idle-time: 40
    all-idle-time-seconds: 0
  server:
    # 服务端端口,用于保持与客户端的连接,非SSL
    port: 9000    
    # 服务端端口,用于保持与客户端的连接,SSL,需要jks证书文件,若不需要ssl支持,可不配置
    ssl-port: 9002
    # 证书密码
    key-store-password: 123456
    key-manager-password: 123456
    # 证书存放路径,若不想打进jar包,可不带classpath:前缀
    jks-path: classpath:/test.jks
  # license配置, 客户端连接时需要用这个进行校验
  license:
    # license数为3表示用该license连接的客户端最多可代理3个端口,-1为不限
    79419a1a8691413aa5e845b9e3e90051: 3
    9352b1c25f564c81a5677131d7769876: 2

3.3、启动服务端

fun.asgc.neutrino.proxy.server.ProxyServer

3.4、修改客户端配置

application:
  name: neutrino-proxy-client

proxy:
  protocol:
    max-frame-length: 2097152
    length-field-offset: 0
    length-field-length: 4
    initial-bytes-to-strip: 0
    length-adjustment: 0
    read-idle-time: 60
    write-idle-time: 30
    all-idle-time-seconds: 0
  client:
    # ssl证书密码
    key-store-password: 123456
    # ssl证书存放位置
    jks-path: classpath:/test.jks
    # 服务端ip,若部署到服务器,则配置服务器的ip
    server-ip: localhost
    # 服务端端口,若使用ssl,则需要配置为服务端的"ssl-port"
    server-port: 9000
    # 是否启用ssl,启用则必须配置ssl相关参数
    ssl-enable: false

3.5、准备代理信息配置文件 config.json

{
    "environment": "我的Mac",
    "clientKey": "79419a1a8691413aa5e845b9e3e90051",  # 对应服务端配置license中的key
    "proxy": [
        {
            "serverPort": 9100,         # 外网服务器对外暴露的端口
            "clientInfo": "127.0.0.1:3306" # 需要代理的本地端口(mysql)
        },
        {
            "serverPort": 9101, # 外网服务器对外暴露的端口
            "clientInfo": "rm-xxxx.mysql.rds.aliyuncs.com:3306" # 代理外网端口本身无意义,仅供测试
        },
        {
            "serverPort": 9102, # 外网服务器对外暴露的端口
            "clientInfo": "127.0.0.1:8080" # 需要代理的本地端口(http)
        }
    ]
}

3.6、启动客户端

fun.asgc.neutrino.proxy.client.ProxyClient 默认情况下,客户端会加载当前目录下的 config.json 文件作为代理配置,可通过命令行参数指定,如:java -jar neutrino-proxy-client.jar/xxx/proxy.json

4、未来迭代方向

  • 优化代码、增强稳定性
  • 服务端增加管理页面,提供报表、授权、限流等功能
  • 从项目中分离、孵化出另一个开源项目 (neutrino-framework)

5、联系我们

微信: yuyunshize

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
基本原理: 代理服务器打开一个端口接收浏览器发来的访问某个站点的请求,从请求的字符串中解析出用户想访问哪个网页,让后通过URL对象建立输入流读取相应的网页内容,最后按照web服务器的工作方式将网页内容发送给用户浏览器 源程序: import java.net.*; import java.io.*; public class MyProxyServer { public static void main(String args[]) { try { ServerSocket ss=new ServerSocket(8080); System.out.println("proxy server OK"); while (true) { Socket s=ss.accept(); process p=new process(s); Thread t=new Thread(p); t.start(); } } catch (Exception e) { System.out.println(e); } } }; class process implements Runnable { Socket s; public process(Socket s1) { s=s1; } public void run() { String content=" "; try { PrintStream out=new PrintStream(s.getOutputStream()); BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream())); String info=in.readLine(); System.out.println("now got "+info); int sp1=info.indexOf(' '); int sp2=info.indexOf(' ',sp1+1); String gotourl=info.substring(sp1,sp2); System.out.println("now connecting "+gotourl); URL con=new URL(gotourl); InputStream gotoin=con.openStream(); int n=gotoin.available(); byte buf[]=new byte[1024]; out.println("HTTP/1.0 200 OK"); out.println("MIME_version:1.0"); out.println("Content_Type:text/html"); out.println("Content_Length:"+n); out.println(" "); while ((n=gotoin.read(buf))>=0) { out.write(buf,0,n); } out.close(); s.close(); } catch (IOException e) { System.out.println("Exception:"+e); } } };

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值