Skip to content
This repository was archived by the owner on Mar 5, 2020. It is now read-only.

Commit 03c600f

Browse files
committed
Adding protection against duplicate host address
1 parent 7151f77 commit 03c600f

File tree

7 files changed

+131
-58
lines changed

7 files changed

+131
-58
lines changed

src/main/java/pl/simplemethod/codebin/repository/ContainersRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public interface ContainersRepository extends JpaRepository<Containers, Long> {
2525

2626
Containers getFirstByName(String name);
2727

28-
List<Containers> getByHostPorts(Integer ports);
28+
Containers getByHostPorts(Integer ports);
2929

3030
List<Containers> getByIdDocker(String dockerId);
3131
}

src/main/java/pl/simplemethod/codebin/srv/SrvRestController.java

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.time.Instant;
1919
import java.util.ArrayList;
2020
import java.util.List;
21+
import java.util.Random;
2122

2223
@RestController
2324
@RequestMapping("srv")
@@ -38,6 +39,48 @@ public class SrvRestController {
3839
@Autowired
3940
private UsersRepository usersRepository;
4041

42+
/**
43+
* Method is used to get data on the container from the database.
44+
* @param hostPort Server port
45+
* @return Object JSON with data
46+
*/
47+
@GetMapping("users/container/info/{hostPort}")
48+
public @ResponseBody
49+
ResponseEntity checkports(@PathVariable(value = "hostPort") String hostPort) {
50+
HttpHeaders httpHeaders = new HttpHeaders();
51+
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
52+
org.json.JSONObject body = new org.json.JSONObject();
53+
54+
/* Containers containers = containersRepository.getByHostPorts(Integer.valueOf(hostPort));
55+
try{
56+
if(containers.getId()!=null)
57+
{
58+
body.put("name", containers.getName());
59+
body.put("docker_id", containers.getIdDocker());
60+
body.put("exposed_ports", containers.getExposedPorts());
61+
body.put("host_ports", containers.getHostPorts());
62+
body.put("disk_quota", containers.getDiskQuota());
63+
body.put("ram_memory",containers.getRamMemory());
64+
body.put("share_url",containers.getShareUrl());
65+
body.put("create_time", containers.getCreateTime());
66+
return new ResponseEntity<>(body.toString(),httpHeaders,HttpStatus.valueOf(200));
67+
}
68+
else
69+
{
70+
body.put("error","Not found");
71+
return new ResponseEntity<>(body.toString(),httpHeaders,HttpStatus.valueOf(404));
72+
}
73+
}
74+
catch (NullPointerException e)
75+
{
76+
body.put("error",e);
77+
return new ResponseEntity<>(body.toString(),httpHeaders,HttpStatus.valueOf(404));
78+
}
79+
*/
80+
return new ResponseEntity<>(checkPort(Integer.valueOf(hostPort)),httpHeaders,HttpStatus.valueOf(200));
81+
82+
}
83+
4184
/**
4285
* List of containers of any user
4386
*
@@ -94,6 +137,35 @@ ResponseEntity userContainerCheck(@RequestParam("dockergithubid") String dockerG
94137
}
95138
}
96139

140+
private Integer checkPort(Integer hostPorts)
141+
{
142+
Containers containers = containersRepository.getByHostPorts(hostPorts);
143+
Random rand = new Random();
144+
try
145+
{
146+
if(containers.getHostPorts()!=null)
147+
{
148+
if(containers.getHostPorts().equals(hostPorts))
149+
{
150+
return checkPort(hostPorts + rand.nextInt((9999 - 1) + 1) + 1);
151+
}
152+
else
153+
{
154+
return hostPorts;
155+
}
156+
157+
}
158+
else
159+
{
160+
return hostPorts;
161+
}
162+
}
163+
catch (NullPointerException e)
164+
{
165+
return hostPorts;
166+
}
167+
}
168+
97169
/**
98170
* REST for container creation
99171
*
@@ -111,6 +183,7 @@ ResponseEntity createContainerForUser(@RequestParam("dockerimage") String docker
111183
HttpHeaders headers = new HttpHeaders();
112184
headers.setContentType(MediaType.APPLICATION_JSON);
113185
Images images = imagesRepository.getFirstById(Integer.valueOf(dockerImage));
186+
hostPorts=checkPort(hostPorts);
114187

115188
org.json.JSONObject response = srvClient.createAndRunContainer(srvClient.generateCreateConfig(images.getName(), exposedPorts, hostPorts, ramMemory, diskQuota), name);
116189
int status;
@@ -129,14 +202,11 @@ ResponseEntity createContainerForUser(@RequestParam("dockerimage") String docker
129202
return new ResponseEntity<>(e.toString(), headers, HttpStatus.NOT_FOUND);
130203
}
131204

132-
try
133-
{
134-
Thread.sleep(1000);
205+
try {
206+
Thread.sleep(4000);
135207
Containers containers1 = containersRepository.getFirstByName(name);
136208
response.put("exec", srvClient.execContainer(containers1.getIdDocker(), images.getExec(), gitUrl));
137-
}
138-
catch (InterruptedException e)
139-
{
209+
} catch (InterruptedException e) {
140210

141211
}
142212

src/main/resources/public/app.js

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ angular.module("CombineModule", ["mainApp", "dashboard"]);
4444

4545
app.controller('ContainersCreateController', ['$filter', '$routeParams', '$scope', '$http', function ($filter, $routeParams, $scope, $http) {
4646
$scope.supportedPlatforms = ["Java", "C", "C++", "Python", "HTML", "JavaScript", "CSS"];
47-
$scope.referId=$routeParams.id;
48-
$scope.progressBar=0;
47+
$scope.referId = $routeParams.id;
48+
$scope.progressBar = 0;
49+
50+
4951
$http({
5052
url: 'http://127.0.0.1/github/user/repos/' + $routeParams.id,
5153
method: 'GET'
@@ -61,18 +63,19 @@ app.controller('ContainersCreateController', ['$filter', '$routeParams', '$scope
6163
$scope.diskquota = null;
6264
if (!response.data.container_create) {
6365
if ($scope.supportedPlatforms.indexOf(response.data.language) !== -1) {
64-
$scope.progressBar=40;
66+
$scope.progressBar = 40;
6567
$http({
6668
url: 'http://127.0.0.1/github/local',
6769
method: 'GET'
6870
}).then(
6971
function (local) {
70-
$scope.progressBar=70;
72+
$scope.progressBar = 70;
7173
$scope.userId = local.data.user_id;
7274
$scope.subscriptionStatus = local.data.subscription_status;
7375
$scope.github_id = local.data.github_id;
7476
$scope.premium = null;
7577
$scope.dockerImages = null;
78+
7679
if ($scope.subscriptionStatus === "") {
7780
$scope.premium = 0;
7881
console.log("Free user");
@@ -86,7 +89,7 @@ app.controller('ContainersCreateController', ['$filter', '$routeParams', '$scope
8689
$scope.dockerImages = 4;
8790
}
8891
$scope.rammemory = $scope.diskquota = 1000000000;
89-
$scope.premiumtext="Free";
92+
$scope.premiumtext = "Free";
9093
} else {
9194
console.log("Pro user");
9295
if ($scope.supportedPlatforms.indexOf(response.data.language) === 0) {
@@ -99,23 +102,21 @@ app.controller('ContainersCreateController', ['$filter', '$routeParams', '$scope
99102
$scope.dockerImages = 2;
100103
}
101104
$scope.premium = 1;
102-
$scope.premiumtext="Premium";
105+
$scope.premiumtext = "Premium";
103106
$scope.rammemory = $scope.diskquota = 2000000000;
104107
}
105108

106109
var str = "" + $scope.id;
107110
var res = str.substring(0, 3);
108111
$scope.exposedports = 8080;
109-
$scope.hostports = "8" + res;
112+
$scope.hostports = (1 + res);
110113
$scope.dockerName = $scope.id;
114+
$scope.progressBar = 90;
111115
console.log("Status konta:" + $scope.premium);
112116
console.log("Obraz:" + $scope.dockerImages);
113117
console.log("Port wewnętrzny:" + $scope.exposedports);
114-
console.log("Port zewnętrzny:" + $scope.hostports);
115118
console.log("Nazwa dokeru:" + $scope.id);
116119
console.log("Ilość pamięci ram: " + $scope.rammemory + " i dysku: " + $scope.diskquota);
117-
$scope.progressBar=90;
118-
119120
$http({
120121
url: 'http://127.0.0.1/srv/container/create',
121122
method: 'POST',
@@ -132,37 +133,37 @@ app.controller('ContainersCreateController', ['$filter', '$routeParams', '$scope
132133
}).then(
133134
function (create) {
134135
console.log(create.data);
135-
$scope.progressBar=100;
136-
$scope.finish=1;
136+
$scope.progressBar = 100;
137+
$scope.finish = 1;
137138
},
138139
function (create) {
139-
$scope.errors= "Problem with creating a container";
140-
$scope.erorType=0;
140+
$scope.errors = "Problem with creating a container";
141+
$scope.erorType = 0;
141142
console.error(create.data);
142143
}
143144
);
144145

145146
},
146-
function (response) {
147-
$scope.errors= "Problem with loading user data";
148-
$scope.erorType=1;
147+
function () {
148+
$scope.errors = "Problem with loading user data";
149+
$scope.erorType = 1;
149150
}
150151
);
151152
} else {
152-
$scope.errors= "No support for the specified repository";
153-
$scope.erorType=2;
153+
$scope.errors = "No support for the specified repository";
154+
$scope.erorType = 2;
154155

155156
}
156157
} else {
157-
$scope.errors= "The repository does exist";
158-
$scope.erorType=3;
158+
$scope.errors = "The repository does exist";
159+
$scope.erorType = 3;
159160
console.error("The repository does exist");
160161
}
161162

162163
},
163-
function (response) {
164-
$scope.errors= "Fatal Error";
165-
$scope.erorType=4;
164+
function () {
165+
$scope.errors = "Fatal Error";
166+
$scope.erorType = 4;
166167
window.location = "/";
167168
}
168169
);
@@ -180,7 +181,7 @@ app.controller('ContainersController', ['$filter', '$routeParams', '$scope', '$h
180181
$scope.creationTime = response.data.createTime;
181182
$scope.containersId = response.data.idDocker;
182183
$scope.hostPorts = response.data.hostPorts;
183-
if ($scope.status == 1) {
184+
if ($scope.status === 1) {
184185
$scope.status = "Premium";
185186
$scope.shareStatus = response.data.shareUrl;
186187
} else {
@@ -195,8 +196,8 @@ app.controller('ContainersController', ['$filter', '$routeParams', '$scope', '$h
195196
}).then(
196197
function (response) {
197198
},
198-
function (response) {
199-
$scope.errors= "Problem with restarting a container";
199+
function () {
200+
$scope.errors = "Problem with restarting a container";
200201
}
201202
);
202203
};
@@ -205,11 +206,11 @@ app.controller('ContainersController', ['$filter', '$routeParams', '$scope', '$h
205206
url: 'http://127.0.0.1/srv/container/' + $scope.containersId + '/delete',
206207
method: 'DELETE'
207208
}).then(
208-
function (response) {
209+
function () {
209210
window.location = "/";
210211
},
211-
function (response) {
212-
$scope.errors= "Problem with deleteing a container";
212+
function () {
213+
$scope.errors = "Problem with deleteing a container";
213214
}
214215
);
215216
};
@@ -220,8 +221,8 @@ app.controller('ContainersController', ['$filter', '$routeParams', '$scope', '$h
220221
function (logGet) {
221222
$scope.logMachine = logGet.data.logs;
222223
},
223-
function (logGet) {
224-
$scope.errors= "Problem with downloading logs from containers";
224+
function () {
225+
$scope.errors = "Problem with downloading logs from containers";
225226
}
226227
);
227228
$http({
@@ -231,13 +232,13 @@ app.controller('ContainersController', ['$filter', '$routeParams', '$scope', '$h
231232
function (response) {
232233
$scope.tops = response.data.Processes;
233234
},
234-
function (response) {
235-
$scope.errors= "Problem with getting processes from a container";
235+
function () {
236+
$scope.errors = "Problem with getting processes from a container";
236237
}
237238
);
238239
},
239-
function (response) {
240-
$scope.errors= "Fatal error";
240+
function () {
241+
$scope.errors = "Fatal error";
241242
window.location = "/";
242243
}
243244
);
@@ -249,7 +250,6 @@ app.controller('HomeController', function ($scope, $http, $cookies) {
249250
$http({
250251
url: 'http://127.0.0.1/github/user',
251252
method: 'GET'
252-
// params: {token: $scope.lastVal}
253253
}).then(
254254
function (response) {
255255
$scope.passCheck = response;
@@ -259,7 +259,7 @@ app.controller('HomeController', function ($scope, $http, $cookies) {
259259
$scope.followers = response.data.followers;
260260
$scope.following = response.data.following;
261261
},
262-
function (response) {
262+
function () {
263263
$scope.passCheck = false;
264264
}
265265
);
@@ -275,10 +275,10 @@ app.controller('ProjectsController', function ($scope, $http) {
275275
function (response) {
276276
$scope.jsondata = response.data;
277277
$scope.repoid = response.data.id;
278-
$scope.progressBar=100;
278+
$scope.progressBar = 100;
279279
},
280-
function (response) {
281-
$scope.errors="Fatal error";
280+
function () {
281+
$scope.errors = "Fatal error";
282282
}
283283
);
284284
});
@@ -299,32 +299,37 @@ app.controller('CheckLoginStatus', function ($scope, $http, $cookies) {
299299
method: 'GET',
300300
params: {token: $scope.lastVal}
301301
}).then(
302-
function (response) {
302+
function () {
303303
$scope.passCheck = true;
304304
},
305-
function (response) {
305+
function () {
306306
$scope.passCheck = false;
307307
}
308308
);
309309
});
310310

311311

312-
app.controller('dashboardGithub', function ($scope, $http, $cookies) {
312+
app.controller('dashboardGithub', function ($scope, $http, $cookies, $window) {
313313
$scope.lastVal = $cookies.get('token');
314314
$http({
315315
url: 'http://127.0.0.1/github/user',
316316
method: 'GET'
317-
// params: {token: $scope.lastVal}
318317
}).then(
319318
function (response) {
320319
$scope.passCheck = response;
321320
$scope.avatar = response.data.avatar_url;
322321
$scope.name = response.data.name;
323322
},
324-
function (response) {
323+
function () {
325324
$scope.passCheck = false;
326325
}
327326
);
327+
328+
$scope.logout = function () {
329+
$cookies.remove('token');
330+
$cookies.remove('id');
331+
window.location = "/";
332+
}
328333
});
329334

330335
$('ul.nav > li > a.nav-link').click(function (e) {

src/main/resources/public/dashboard.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
<a class="dropdown-item" href="dashboard/dashboard-profile.html">
9292
<i class="material-icons">&#xE7FD;</i> Profile</a>
9393
<div class="dropdown-divider"></div>
94-
<a class="dropdown-item text-danger" href="index.html">
94+
<a class="dropdown-item text-danger" ng-click="logout()">
9595
<i class="material-icons text-danger">&#xE879;</i> Logout</a>
9696
</div>
9797
</li>

0 commit comments

Comments
 (0)