Skip to content

Commit

Permalink
修复了端口转发无法正确移除的BUG
Browse files Browse the repository at this point in the history
  • Loading branch information
lixinyang123 committed Jan 19, 2021
1 parent 8193a45 commit d1be1a6
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 62 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
cloudshell:
image: lixinyang/cloudshell
ports:
- "80:80"
- "3000:80"
build:
context: .
dockerfile: Dockerfile
Expand Down
36 changes: 0 additions & 36 deletions src/forward.js

This file was deleted.

7 changes: 2 additions & 5 deletions src/nginx.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ var common = require("./common");
var config,location;
var configPath = "/etc/nginx/sites-enabled/default";

function generator(){

let containers = require("./ttyd").containers;
let forwardList = require("./forward").forwardList;

function generator(containers,forwardList){

var configStr = undefined;

if(containers.length + forwardList.length == 0){
Expand Down
3 changes: 1 addition & 2 deletions src/rotate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var fs = require("fs");
var ttyd = require("./ttyd");
var forward = require("./forward");

module.exports = (req,res,path)=>{

Expand Down Expand Up @@ -66,7 +65,7 @@ module.exports = (req,res,path)=>{

ttyd.containers.forEach((container) => {
if(container.id == id){
forward.createForward(id,port);
ttyd.createForward(id,port);
res.end("createForward successful");
}
else{
Expand Down
59 changes: 41 additions & 18 deletions src/ttyd.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
var child_process = require("child_process");
var fs = require("fs");
nginx = require("./nginx");
var common = require("./common");
var forward = require("./forward");

var nginx,config,containers,usefulPorts;
var nginx,config,containers,forwardList,usefulPorts;

function container (id,containerId,port,endTime){
this.id = id;
this.containerId = containerId;
this.port = port;
this.endTime = endTime;
this.forward = [];
}

function showStatus(){
console.log("Containers " + containers.length);
console.log("UsefulPorts " + usefulPorts.length);
function forward(id, port) {
this.id = id;
this.port = port;
}

function create(image,callback){
Expand All @@ -39,7 +40,7 @@ function create(image,callback){
stdout = stdout.replace("\n","");
containers.push(new container(id,stdout,port,new Date().getTime() + 1000 * 60 * config.delayedTime));
//配置反向代理
nginx.apply(nginx.generator(),(flag)=>{
nginx.apply(nginx.generator(containers,forwardList),(flag)=>{
if(flag){
callback(id);
}
Expand Down Expand Up @@ -67,10 +68,10 @@ function kill(id,callback){
containers.splice(containers.indexOf(container),1);
//回收端口
usefulPorts.push(container.port);
//移除端口转发
forward.deleteForward(id);
//删除转发端口
deleteForward(id);
//配置反向代理
nginx.apply(nginx.generator(),(flag)=>{
nginx.apply(nginx.generator(containers,forwardList),(flag)=>{
if(flag){
callback(stdout);
}
Expand Down Expand Up @@ -118,35 +119,57 @@ function autoRecycling(){
setTimeout(autoRecycling, 1000 * 60 * config.recyclingTime);
}


function createForward(containerId, port) {
forwardList.push(new forward(containerId, port));
//配置反向代理
nginx.apply(nginx.generator(containers,forwardList),(flag)=>{
if(flag){
console.log("创建转发成功");
}
});
}

function deleteForward(containerId) {
let list = new Array();

for (let i = 0; i < forwardList.length; i++) {
if(forwardList[i].id != containerId)
list.push(forwardList[i]);
}

forwardList = list;
}


function init(){

config = JSON.parse(fs.readFileSync("./assets/config.json"));

usefulPorts = new Array();
containers = new Array();
forwardList = new Array();

//初始化20个可用端口
for(var i=7681;i<=7700;i++){
usefulPorts.push(i);
}

autoRecycling();

module.exports = {
create,kill,delayedLife,showStatus,containers
}

nginx = require("./nginx");

//按容器列表初始化nginx
nginx.apply(nginx.generator(),(flag)=>{
nginx.apply(nginx.generator(containers,forwardList),(flag)=>{
if(flag){
console.log("nginx init successful");
}
else{
console.log("nginx init defeat")
}
});

autoRecycling();

module.exports = {
create,kill,delayedLife,createForward,containers
}
}

init();

0 comments on commit d1be1a6

Please sign in to comment.