From 4e0fa1e32493e4cd61ed047b71e689c96ba08f84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=BC=A0?= <1170881778@qq.com> Date: Fri, 12 Oct 2018 09:13:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=80=80=E5=87=BAhandler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../conf/client.properties | 1 - .../conf/nginx-domainforward.conf | 19 ---- .../conf/nginx-portforward.conf | 92 +------------------ .../proxy-server-domain-1.0/conf/proxy.yaml | 4 +- .../conf/proxy_back.yaml | 29 ++++++ .../java/com/proxy/server/ProxyServer.java | 33 +++++-- .../com/proxy/server/dao/ProxyChannelDao.java | 10 +- .../com/proxy/server/service/LifeCycle.java | 10 ++ .../server/service/ProxyChannelService.java | 24 ++++- .../server/service/ServerBeanManager.java | 14 +++ .../com/proxy/server/task/ExitHandler.java | 21 +++++ 11 files changed, 134 insertions(+), 123 deletions(-) create mode 100644 distribution/proxy-server-domain-1.0/conf/proxy_back.yaml create mode 100644 proxy-server/src/main/java/com/proxy/server/service/LifeCycle.java create mode 100644 proxy-server/src/main/java/com/proxy/server/task/ExitHandler.java diff --git a/distribution/proxy-client-domain-1.0/conf/client.properties b/distribution/proxy-client-domain-1.0/conf/client.properties index 586045a..af60cff 100644 --- a/distribution/proxy-client-domain-1.0/conf/client.properties +++ b/distribution/proxy-client-domain-1.0/conf/client.properties @@ -4,7 +4,6 @@ key=ztgreat #proxy-server地址 server.host=127.0.0.1 #server.host=67.216.203.138 -#server.host=47.97.111.38 #proxy-server 服务端口 server.port=6666 \ No newline at end of file diff --git a/distribution/proxy-server-domain-1.0/conf/nginx-domainforward.conf b/distribution/proxy-server-domain-1.0/conf/nginx-domainforward.conf index 29e5618..718b95e 100644 --- a/distribution/proxy-server-domain-1.0/conf/nginx-domainforward.conf +++ b/distribution/proxy-server-domain-1.0/conf/nginx-domainforward.conf @@ -1,31 +1,16 @@ -#user nobody; worker_processes 1; -#error_log logs/error.log; -#error_log logs/error.log notice; -#error_log logs/error.log info; - -#pid logs/nginx.pid; - - events { worker_connections 1024; } - http { include mime.types; default_type application/octet-stream; client_max_body_size 10M; - #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' - # '$status $body_bytes_sent "$http_referer" ' - # '"$http_user_agent" "$http_x_forwarded_for"'; - - #access_log logs/access.log main; sendfile on; - #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65s 65s; @@ -35,14 +20,10 @@ http { # 配置dsn 服务器,后面解析host的时候需要 resolver 8.8.8.8 ipv6=off; - server { listen 80; server_name windows10.microdone.cn; - #charset koi8-r; - - #access_log logs/host.access.log main; location /{ proxy_pass http://$host:9090; # 需要配置dns diff --git a/distribution/proxy-server-domain-1.0/conf/nginx-portforward.conf b/distribution/proxy-server-domain-1.0/conf/nginx-portforward.conf index 84a29c0..a14132c 100644 --- a/distribution/proxy-server-domain-1.0/conf/nginx-portforward.conf +++ b/distribution/proxy-server-domain-1.0/conf/nginx-portforward.conf @@ -2,28 +2,12 @@ #user nobody; worker_processes 1; -#error_log logs/error.log; -#error_log logs/error.log notice; -#error_log logs/error.log info; - -#pid logs/nginx.pid; - - events { worker_connections 1024; } - - http { include mime.types; default_type application/octet-stream; - - #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' - # '$status $body_bytes_sent "$http_referer" ' - # '"$http_user_agent" "$http_x_forwarded_for"'; - - #access_log logs/access.log main; - sendfile on; #tcp_nopush on; @@ -36,82 +20,10 @@ http { listen 80; server_name localhost; - #charset koi8-r; - - #access_log logs/host.access.log main; - - - location / { + location / { proxy_pass http://127.0.0.1:9091; #proxy_http_version 1.1; proxy_redirect ~^http://127.0.0.1:9090(.*) http://127.0.0.1$1; + } } - - # redirect server error pages to the static page /50x.html - # - error_page 500 502 503 504 /50x.html; - location = /50x.html { - root html; - } - - # proxy the PHP scripts to Apache listening on 127.0.0.1:80 - # - #location ~ \.php$ { - # proxy_pass http://127.0.0.1; - #} - - # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 - # - #location ~ \.php$ { - # root html; - # fastcgi_pass 127.0.0.1:9000; - # fastcgi_index index.php; - # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; - # include fastcgi_params; - #} - - # deny access to .htaccess files, if Apache's document root - # concurs with nginx's one - # - #location ~ /\.ht { - # deny all; - #} - } - - - # another virtual host using mix of IP-, name-, and port-based configuration - # - #server { - # listen 8000; - # listen somename:8080; - # server_name somename alias another.alias; - - # location / { - # root html; - # index index.html index.htm; - # } - #} - - - # HTTPS server - # - #server { - # listen 443 ssl; - # server_name localhost; - - # ssl_certificate cert.pem; - # ssl_certificate_key cert.key; - - # ssl_session_cache shared:SSL:1m; - # ssl_session_timeout 5m; - - # ssl_ciphers HIGH:!aNULL:!MD5; - # ssl_prefer_server_ciphers on; - - # location / { - # root html; - # index index.html index.htm; - # } - #} - } diff --git a/distribution/proxy-server-domain-1.0/conf/proxy.yaml b/distribution/proxy-server-domain-1.0/conf/proxy.yaml index 192c3fc..a71ff74 100644 --- a/distribution/proxy-server-domain-1.0/conf/proxy.yaml +++ b/distribution/proxy-server-domain-1.0/conf/proxy.yaml @@ -1,11 +1,11 @@ port: 6666 -server: 67.216.203.138 +server: 127.0.0.1 #每秒 并发量 concurrent: 1000 httpPort: 9090 client: ztgreat: - - domain: www.ztgreat.cn + - domain: windows10.microdone.cn proxyType: http realhost: 127.0.0.1 realhostport: 8080 diff --git a/distribution/proxy-server-domain-1.0/conf/proxy_back.yaml b/distribution/proxy-server-domain-1.0/conf/proxy_back.yaml new file mode 100644 index 0000000..526cdb4 --- /dev/null +++ b/distribution/proxy-server-domain-1.0/conf/proxy_back.yaml @@ -0,0 +1,29 @@ +port: 6666 +server: 67.216.203.138 +#每秒 并发量 +concurrent: 1000 +httpPort: 9090 +client: + ztgreat: + - domain: proxy.ztgreat.cn + proxyType: http + realhost: 127.0.0.1 + realhostport: 8000 + description: http代理 + - serverport: 9091 + proxyType: http + realhost: 127.0.0.1 + realhostport: 8081 + description: http代理 + + - serverport: 3307 + proxyType: tcp + realhost: 127.0.0.1 + realhostport: 3306 + description: mysql 代理 + - serverport: 2222 + proxyType: tcp + realhost: 172.16.254.63 + realhostport: 22 + description: ssh 代理 + diff --git a/proxy-server/src/main/java/com/proxy/server/ProxyServer.java b/proxy-server/src/main/java/com/proxy/server/ProxyServer.java index 2329878..9704040 100644 --- a/proxy-server/src/main/java/com/proxy/server/ProxyServer.java +++ b/proxy-server/src/main/java/com/proxy/server/ProxyServer.java @@ -6,9 +6,11 @@ import com.proxy.common.entity.server.ClientNode; import com.proxy.common.entity.server.ProxyRealServer; import com.proxy.common.protocol.CommonConstant; import com.proxy.server.handler.*; +import com.proxy.server.service.LifeCycle; import com.proxy.server.service.LogBackConfigLoader; import com.proxy.server.service.ServerBeanManager; import com.proxy.server.service.SharableHandlerManager; +import com.proxy.server.task.ExitHandler; import com.proxy.server.util.ProxyUtil; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.*; @@ -25,8 +27,9 @@ import org.slf4j.LoggerFactory; import java.util.List; import java.util.Map; -public class ProxyServer { +public class ProxyServer implements LifeCycle{ + private static Logger logger = LoggerFactory.getLogger(ProxyServer.class); /** * 最大帧长度 @@ -52,7 +55,7 @@ public class ProxyServer { * 并发量 */ public static int concurrent = 1000; - private static Logger logger = LoggerFactory.getLogger(ProxyServer.class); + /** * 绑定端口,默认6666 */ @@ -62,7 +65,10 @@ public class ProxyServer { */ public Integer httpPort; - + /** + * 服务端channel + */ + public Channel channel; @@ -76,12 +82,14 @@ public class ProxyServer { public static void main(String[] args)throws Exception{ - //加载日志 LogBackConfigLoader.load(); - try { - new ProxyServer().start(); + //退出钩子 + Runtime.getRuntime().addShutdownHook(new ExitHandler()); + ProxyServer proxyServer = new ProxyServer(); + ServerBeanManager.setProxyServer(proxyServer); + proxyServer.start(); }catch (Exception e){ e.printStackTrace(); } @@ -144,7 +152,6 @@ public class ProxyServer { this.httpPort = (int) ServerBeanManager.getConfigService().getConfigure("httpPort"); } - try { //配置代理信息 configurProxy(); @@ -178,10 +185,9 @@ public class ProxyServer { public ChannelFuture startMainServer(){ - - //根据配置文件启动服务 ChannelFuture future=bind(); + this.channel = future.channel(); return future; } @@ -297,4 +303,13 @@ public class ProxyServer { return null; } + @Override + public void shutDown() { + try { + this.channel.close(); + logger.debug("{}端口:代理服务退出:",this.port); + }catch (Exception e){ + + } + } } diff --git a/proxy-server/src/main/java/com/proxy/server/dao/ProxyChannelDao.java b/proxy-server/src/main/java/com/proxy/server/dao/ProxyChannelDao.java index 04e8eea..d1b441e 100644 --- a/proxy-server/src/main/java/com/proxy/server/dao/ProxyChannelDao.java +++ b/proxy-server/src/main/java/com/proxy/server/dao/ProxyChannelDao.java @@ -12,6 +12,8 @@ import io.netty.channel.ChannelFutureListener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Map; + /** * 本地 端口(tcp)/域名 绑定 具体操作层 * @author ztgreat @@ -71,7 +73,9 @@ public class ProxyChannelDao { ProxyChannel proxyChannel= proxyChannelCache.get(serverPort); if (proxyChannel==null) return false; - proxyChannel.getChannel().close(); + if(proxyChannel.getChannel()!=null && proxyChannel.getChannel().isActive()){ + proxyChannel.getChannel().close(); + } //不移除 // proxyChannelCache.remove(serverPort); return true; @@ -136,4 +140,8 @@ public class ProxyChannelDao { public ProxyChannel getServerProxy(Object key) { return proxyChannelCache.get(key); } + + public MapgetAll(){ + return proxyChannelCache.getAll(); + } } diff --git a/proxy-server/src/main/java/com/proxy/server/service/LifeCycle.java b/proxy-server/src/main/java/com/proxy/server/service/LifeCycle.java new file mode 100644 index 0000000..0f8762a --- /dev/null +++ b/proxy-server/src/main/java/com/proxy/server/service/LifeCycle.java @@ -0,0 +1,10 @@ +package com.proxy.server.service; + +/** + * 生命周期控制 + */ +public interface LifeCycle { + + public void shutDown(); + +} diff --git a/proxy-server/src/main/java/com/proxy/server/service/ProxyChannelService.java b/proxy-server/src/main/java/com/proxy/server/service/ProxyChannelService.java index dfea6ff..8d737bd 100644 --- a/proxy-server/src/main/java/com/proxy/server/service/ProxyChannelService.java +++ b/proxy-server/src/main/java/com/proxy/server/service/ProxyChannelService.java @@ -5,13 +5,18 @@ import com.proxy.common.entity.server.ProxyRealServer; import com.proxy.server.dao.ProxyChannelDao; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Map; /** * 本地 端口(tcp)/域名 绑定服务层 * @author ztgreat */ -public class ProxyChannelService { +public class ProxyChannelService implements LifeCycle{ + private static Logger logger = LoggerFactory.getLogger(ProxyChannelService.class); private static ProxyChannelDao proxyChannelDao =new ProxyChannelDao(); @@ -80,4 +85,21 @@ public class ProxyChannelService { proxyChannelDao.addByServerdomain(domain,proxyRealServer); } + public Map getAll(){ + return proxyChannelDao.getAll(); + } + + @Override + public void shutDown() { + + try { + Map map = this.getAll(); + for(Map.Entryentry:map.entrySet()){ + entry.getValue().getChannel().close(); + logger.debug("代理服务(端口/域名):{} 退出",entry.getKey()); + } + }catch (Exception e){ + + } + } } diff --git a/proxy-server/src/main/java/com/proxy/server/service/ServerBeanManager.java b/proxy-server/src/main/java/com/proxy/server/service/ServerBeanManager.java index 43ff74c..493b865 100644 --- a/proxy-server/src/main/java/com/proxy/server/service/ServerBeanManager.java +++ b/proxy-server/src/main/java/com/proxy/server/service/ServerBeanManager.java @@ -2,6 +2,7 @@ package com.proxy.server.service; import com.proxy.common.util.NumberGenerate; import com.proxy.common.util.SessionIDGenerate; +import com.proxy.server.ProxyServer; import com.proxy.server.handler.traffic.handler.TrafficLimitHandler; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.EventLoopGroup; @@ -20,6 +21,11 @@ public class ServerBeanManager { */ private static ServerBootstrap bootstrap; + /** + * 代理服务端实例 + */ + private static ProxyServer proxyServer; + /** * 用于生成sessionID */ @@ -93,4 +99,12 @@ public class ServerBeanManager { public static TrafficLimitHandler getTrafficLimitHandler() { return trafficLimitHandler; } + + public static ProxyServer getProxyServer() { + return proxyServer; + } + + public static void setProxyServer(ProxyServer proxyServer) { + ServerBeanManager.proxyServer = proxyServer; + } } diff --git a/proxy-server/src/main/java/com/proxy/server/task/ExitHandler.java b/proxy-server/src/main/java/com/proxy/server/task/ExitHandler.java new file mode 100644 index 0000000..7e1dffb --- /dev/null +++ b/proxy-server/src/main/java/com/proxy/server/task/ExitHandler.java @@ -0,0 +1,21 @@ +package com.proxy.server.task; + +import com.proxy.server.service.ServerBeanManager; + +/** + * ctrl +c + * 当在控制台使用 ctrl+c退出时,清理数据,准备退出 + */ +public class ExitHandler extends Thread{ + @Override + public void run() { + System.out.println("正在退出..."); + try { + ServerBeanManager.getProxyServer().shutDown(); + ServerBeanManager.getProxyChannelService().shutDown(); + }catch (Exception e){ + + } + + } +} -- GitLab