Skip to content

Commit d1fb3ea

Browse files
updated subdomain generation
1 parent 140a456 commit d1fb3ea

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

server/src/main/java/uz/server/domain/entity/Tunnel.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@ public class Tunnel {
1515
@GeneratedValue(strategy = GenerationType.IDENTITY)
1616
private Long id;
1717

18+
@Column(name = "session_id")
1819
private String sessionId;
1920

2021
private String subdomain;
2122

22-
private boolean active;
23-
2423
@ManyToOne
2524
private User user;
2625
}

server/src/main/java/uz/server/service/TunnelService.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
import uz.server.domain.exception.BaseException;
99
import uz.server.repository.TunnelRepository;
1010

11+
import java.security.SecureRandom;
12+
import java.util.Base64;
1113
import java.util.Optional;
12-
import java.util.UUID;
1314

1415
@Service
1516
@RequiredArgsConstructor
@@ -27,17 +28,22 @@ public String create(String sessionId, User user) {
2728
}
2829

2930
Tunnel save = repo.save(Tunnel.builder()
30-
.active(true)
3131
.sessionId(sessionId)
32-
.subdomain(UUID.randomUUID().toString())
32+
.subdomain(getUniqueString())
3333
.user(user)
3434
.build());
3535

3636
log.info("Tunnel created: subdomain={}", save.getSubdomain());
3737

3838
return save.getSubdomain();
3939
}
40-
40+
41+
private static String getUniqueString() {
42+
byte[] randomBytes = new byte[8];
43+
new SecureRandom().nextBytes(randomBytes);
44+
return Base64.getUrlEncoder().withoutPadding().encodeToString(randomBytes);
45+
}
46+
4147
public void deactivate(Long tunnelId) {
4248
log.info("Deactivating tunnel: tunnelId={}", tunnelId);
4349
Optional<Tunnel> tunnel = repo.findById(tunnelId);
@@ -47,12 +53,9 @@ public void deactivate(Long tunnelId) {
4753
throw new BaseException("Tunnel not found!");
4854
}
4955

50-
Tunnel tunnel1 = tunnel.get();
51-
tunnel1.setActive(false);
52-
53-
log.info("Tunnel deactivated: tunnelId={}", tunnelId);
56+
log.info("Tunnel deactivated and deleting: tunnelId={}", tunnelId);
5457

55-
repo.save(tunnel1);
58+
repo.deleteById(tunnelId);
5659
}
5760

5861
public Tunnel getTunnelBySubdomain(String subdomain) {

0 commit comments

Comments
 (0)