Skip to content

Commit

Permalink
Merge pull request #153 from JNU-econovation/fix_monitor_interface
Browse files Browse the repository at this point in the history
[FIX] Fix monitor interface
  • Loading branch information
inferior3x authored Jan 13, 2025
2 parents 6970765 + 5ff16e8 commit 6f4437c
Show file tree
Hide file tree
Showing 33 changed files with 214 additions and 212 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.whoz_in.domain.member.exception.NoMemberException;
import com.whoz_in.domain.member.model.Member;
import com.whoz_in.domain.member.model.MemberId;
import com.whoz_in.domain.member.model.SocialProvider;
import java.util.List;
import java.util.Optional;

Expand Down
6 changes: 6 additions & 0 deletions modules/infrastructure/api-redis/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dependencies {
compileOnly project(":modules:main-api")

implementation platform("org.springframework.boot:spring-boot-dependencies:${springBootVersion}")
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package com.whoz_in.domain_jpa.device;

import com.whoz_in.domain.member.model.Member;
import com.whoz_in.domain_jpa.shared.BaseEntity;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToMany;
Expand All @@ -17,7 +14,6 @@
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.UuidGenerator;

@Entity
@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.whoz_in.domain.device.DeviceRepository;
import com.whoz_in.domain.device.model.Device;
import com.whoz_in.domain.device.model.MacAddress;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@Repository
public interface ManagedLogEntityRepository extends JpaRepository<ManagedLogEntity, String> {

@Query("SELECT m FROM ManagedLogEntity m WHERE m.room = :room AND m.logId.ip = :ip AND m.updatedAt > :time ORDER BY m.updatedAt DESC")
@Query("SELECT m FROM ManagedLogEntity m WHERE m.room = :room AND m.logId.ip = :ip AND m.updatedAt > :time ORDER BY m.updatedAt DESC LIMIT 1")
Optional<ManagedLogEntity> findTopByRoomAndIpOrderByUpdatedAtDescAfter(
@Param("room") String room, @Param("ip") String ip, @Param("time") LocalDateTime time);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.UuidGenerator;


@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.whoz_in.domain.network_log.MonitorLog;
import com.whoz_in.domain.network_log.MonitorLogRepository;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Collection;
import lombok.RequiredArgsConstructor;
Expand Down
1 change: 1 addition & 0 deletions modules/main-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ dependencies {

//main-api 구현 모듈 (runtimeOnly - 순환 의존 방지)
runtimeOnly project(":modules:infrastructure:api-query-jpa")
// runtimeOnly project(":modules:infrastructure:api-redis")
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public Void handle(DeviceInfoAdd req) {
//이미 등록된 DeviceInfo가 아닌지 미리 확인
deviceInfoStore.mustNotExist(requesterId, deviceInfo);

//모니터 로그에서 시간 내에 존재하는 맥이 있는지 확인
monitorLogRepository.mustExistAfter(mac, LocalDateTime.now().minusDays(1));
//모니터 로그에서 현재 접속 중인 맥이 있는지 확인 (넉넉하게 15분)
monitorLogRepository.mustExistAfter(mac, LocalDateTime.now().minusMinutes(15));

//해당 맥으로 이미 등록된 기기가 있을경우
deviceRepository.findByMac(mac).ifPresent(device -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
@Component
@RequiredArgsConstructor
public final class DeviceInfoStore {
private final RoomSsidConfig ssidConfig;

private static final Cache<MemberId, CopyOnWriteArrayList<DeviceInfo>> store = CacheBuilder.newBuilder()
.expireAfterAccess(5, TimeUnit.MINUTES) // 5분 동안 접근이 없으면 만료
.maximumSize(10) // 최대 10개 항목 저장
.build();

private final RoomSsidConfig ssidConfig;


//이전에 등록되지 않은 DeviceInfo인지 검증
public void mustNotExist(MemberId ownerId, DeviceInfo deviceInfo){
List<DeviceInfo> deviceInfos = store.getIfPresent(ownerId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.whoz_in.main_api.command.device.presentation;

import com.whoz_in.main_api.command.device.application.DeviceRegister;
import com.whoz_in.main_api.command.device.application.DeviceInfoAdd;
import com.whoz_in.main_api.command.device.application.DeviceRegister;
import com.whoz_in.main_api.command.shared.application.CommandBus;
import com.whoz_in.main_api.command.shared.presentation.CommandController;
import com.whoz_in.main_api.shared.presentation.SuccessBody;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
import com.whoz_in.domain.member.model.OAuthCredentials;
import com.whoz_in.domain.shared.event.EventBus;
import com.whoz_in.main_api.command.shared.application.CommandHandler;
import com.whoz_in.main_api.config.security.oauth2.OAuth2UserInfo;
import com.whoz_in.main_api.shared.application.Handler;
import com.whoz_in.main_api.config.security.oauth2.OAuth2UserInfoStore;
import com.whoz_in.main_api.shared.jwt.tokens.OAuth2TempToken;
import com.whoz_in.main_api.shared.jwt.tokens.OAuth2TempTokenSerializer;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import com.whoz_in.main_api.config.security.oauth2.OAuth2UserInfoStore;
import com.whoz_in.main_api.shared.jwt.JwtProperties;
import com.whoz_in.main_api.shared.jwt.TokenType;
import com.whoz_in.main_api.shared.jwt.tokens.AccessToken;
import com.whoz_in.main_api.shared.jwt.tokens.OAuth2TempToken;
import com.whoz_in.main_api.shared.jwt.tokens.TokenSerializer;
import com.whoz_in.main_api.shared.presentation.ResponseEntityGenerator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.whoz_in.domain.member.service.PasswordEncoder;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

@Component
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.whoz_in.main_api.shared.jwt.tokens;


import static com.whoz_in.main_api.shared.jwt.JwtConst.TOKEN_ID;
import static com.whoz_in.main_api.shared.jwt.JwtConst.MEMBER_ID;
import static com.whoz_in.main_api.shared.jwt.JwtConst.TOKEN_ID;

import com.whoz_in.domain.member.model.MemberId;
import com.whoz_in.main_api.shared.jwt.JwtProperties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ public List<NetworkInterface> getLatest() {
}
// 새 인터페이스 정보 모으기 & 초기화
currentName = line.split("\\s+")[0];
if (line.contains("ESSID:"))
if (line.contains("ESSID:")) {
currentSsid = line.split("ESSID:")[1].split("\\s+")[0].replace("\"", "")
.trim();
else
currentSsid = "";
currentSsid = currentSsid.isEmpty() ? null : currentSsid;
}
else {
currentSsid = null;
}
currentMode = null; // 초기화
}
// Mode 추출
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
package com.whoz_in.network_api.common;

import jakarta.annotation.Nullable;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

@Getter
@ToString
@EqualsAndHashCode
public final class NetworkInterface{
//
@EqualsAndHashCode.Exclude
private final String altSsid; //network log의 ssid는 이걸로 저장된다. (ex: econo)
private final String interfaceName;
private final String realSsid; //실제 연결될 와이파이 이름 (ex: ECONO_5G)
private final String mode;

public NetworkInterface(String altSsid, String interfaceName, String realSsid, String mode) {
this.altSsid = altSsid;
@EqualsAndHashCode.Exclude
@Nullable private final String altSsid; //network log의 ssid는 이걸로 저장된다. (ex: econo)
@EqualsAndHashCode.Exclude
@Nullable private final String command; //실행할 명령어

public NetworkInterface(String interfaceName, String realSsid, String mode, String altSsid, String command) {
this.interfaceName = interfaceName;
this.realSsid = realSsid;
this.mode = mode;
this.altSsid = altSsid;
this.command = command;
}

public NetworkInterface(String interfaceName, String realSsid, String mode) {
this(realSsid, interfaceName, realSsid, mode);
this(interfaceName, realSsid, mode, realSsid, null);
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
package com.whoz_in.network_api.common.util;

import org.springframework.stereotype.Component;

@Component
public class IpHolder implements RequesterInfo{

private final ThreadLocal<String> ip = new ThreadLocal<>();

public void setIp(String ip) {
this.ip.set(ip);
}

public String getIp(){
return ip.get();
}

public void clear(){
ip.remove();
}

//요청의 아이피를 꺼낼 수 있는 인터페이스
public interface IpHolder {
String getIp();
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,95 +1,57 @@
package com.whoz_in.network_api.config;

import com.whoz_in.network_api.common.NetworkInterface;
import com.whoz_in.network_api.managed.ManagedInfo;
import com.whoz_in.network_api.monitor.MonitorInfo;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Stream;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.bind.ConstructorBinding;
import org.springframework.stereotype.Component;

//실행시킬 프로세스들이 필요로 하는 정보를 제공하는 역할을 한다.
//다른 모듈이 구현한 LogWriterConfig를 통해 정보를 초기화한다.
//이 서버의 전체적인 설정 값을 관리한다.
@Getter
@Component
public class NetworkConfig {
private final String room;
private final String sudoPassword;
private final List<NetworkInterface> networkInterfaces;
private final MonitorInfo monitorInfo;
private final List<ManagedInfo> mdnsList;
private final List<ManagedInfo> arpList;
private final NetworkInterface monitorNI;
private final List<NetworkInterface> mdnsNIs;
private final List<NetworkInterface> arpNIs;

public NetworkConfig(
NetworkConfigProperties properties,
NetworkInterfaceProperties properties,
@Value("${room-setting.room-name}") String roomName,
@Value("${sudo_password}") String sudoPassword,
@Value("${command.monitor}") String monitorCommandTemplate,
@Value("${command.managed.mdns}") String mdnsCommandTemplate,
@Value("${command.managed.arp}") String arpCommandTemplate
) {
this.room = properties.getRoom();
this.room = roomName;
this.sudoPassword = sudoPassword;
this.networkInterfaces = properties.getNIs();
this.monitorInfo = new MonitorInfo(properties.getMonitorNI(), generateCommand(monitorCommandTemplate, properties.getMonitorNI().getInterfaceName()));
this.mdnsList=properties.getMdnsNIs().stream()
.map(ni-> new ManagedInfo(ni, generateCommand(mdnsCommandTemplate, ni.getInterfaceName())))
this.monitorNI = new NetworkInterface(properties.getMonitor().interfaceName(), null, "monitor",
null, generateCommand(monitorCommandTemplate, properties.getMonitor().interfaceName()));
this.mdnsNIs = properties.getMdns().stream()
.map(mdns -> {
String altSsid = (mdns.altSsid() != null) ? mdns.altSsid() : mdns.realSsid();
return new NetworkInterface(mdns.interfaceName() , mdns.realSsid(), "managed", altSsid,
generateCommand(mdnsCommandTemplate, mdns.interfaceName()));
})
.toList();
this.arpList=properties.getArpNIs().stream()
.map(ni-> new ManagedInfo(ni, generateCommand(arpCommandTemplate, ni.getInterfaceName())))
this.arpNIs = properties.getArp().stream()
.map(arp -> {
String altSsid = (arp.altSsid() != null) ? arp.altSsid() : arp.realSsid();
return new NetworkInterface(arp.interfaceName(), arp.realSsid(), "managed", altSsid,
generateCommand(arpCommandTemplate, arp.interfaceName()));
})
.toList();
this.networkInterfaces = Stream.of(monitorNI, mdnsNIs, arpNIs)
.flatMap(list -> list instanceof Collection<?> ?
((Collection<NetworkInterface>) list).stream() : Stream.of((NetworkInterface) list))
.toList();
}

private String generateCommand(String commandTemplate, String interfaceName) {
private static String generateCommand(String commandTemplate, String interfaceName) {
return commandTemplate.replace("{{interface}}", interfaceName);
}


@Getter
@ConfigurationProperties(prefix = "room-setting")
public static class NetworkConfigProperties {

private final String room;
private final NetworkInterface monitorNI;
private final List<NetworkInterface> mdnsNIs;
private final List<NetworkInterface> arpNIs;

public record Monitor(String interfaceName) {}
public record Managed(List<Mdns> mdns, List<Arp> arp) {
public record Mdns(String altSsid, String realSsid, String interfaceName) {}
public record Arp(String altSsid, String realSsid, String interfaceName) {}
}

public List<NetworkInterface> getNIs() {
List<NetworkInterface> nis = new ArrayList<>();
nis.add(getMonitorNI());
nis.addAll(getMdnsNIs());
nis.addAll(getArpNIs());
return nis;
}

@ConstructorBinding
public NetworkConfigProperties(
String roomName,
Monitor monitor,
Managed managed
) {
this.room = roomName;
this.monitorNI = new NetworkInterface(null, monitor.interfaceName, null, "monitor");
this.mdnsNIs = managed.mdns.stream()
.map(ni -> {
String altSsid = (ni.altSsid != null) ? ni.altSsid : ni.realSsid;
return new NetworkInterface(altSsid, ni.interfaceName, ni.realSsid, "managed");
})
.toList();
this.arpNIs = managed.arp.stream()
.map(ni -> {
String altSsid = (ni.altSsid != null) ? ni.altSsid : ni.realSsid;
return new NetworkInterface(altSsid, ni.interfaceName, ni.realSsid, "managed");
})
.toList();
}
}
}
Loading

0 comments on commit 6f4437c

Please sign in to comment.