Рефакторинг
This commit is contained in:
@@ -2,12 +2,16 @@ package ru.serega6531.packmate.service;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.serega6531.packmate.model.CtfService;
|
||||
import ru.serega6531.packmate.model.enums.SubscriptionMessageType;
|
||||
import ru.serega6531.packmate.model.pojo.SubscriptionMessage;
|
||||
import ru.serega6531.packmate.repository.ServiceRepository;
|
||||
|
||||
import java.net.Inet4Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -20,18 +24,23 @@ public class ServicesService {
|
||||
private final ServiceRepository repository;
|
||||
private final StreamSubscriptionService subscriptionService;
|
||||
|
||||
private final InetAddress localIp;
|
||||
|
||||
private final Map<Integer, CtfService> services = new HashMap<>();
|
||||
|
||||
@Autowired
|
||||
public ServicesService(ServiceRepository repository, StreamSubscriptionService subscriptionService) {
|
||||
public ServicesService(ServiceRepository repository,
|
||||
StreamSubscriptionService subscriptionService,
|
||||
@Value("${local-ip}") String localIpString) throws UnknownHostException {
|
||||
this.repository = repository;
|
||||
this.subscriptionService = subscriptionService;
|
||||
this.localIp = InetAddress.getByName(localIpString);
|
||||
|
||||
repository.findAll().forEach(s -> services.put(s.getPort(), s));
|
||||
log.info("Loaded {} services", services.size());
|
||||
}
|
||||
|
||||
public Optional<CtfService> findService(String localIp, String firstIp, int firstPort, String secondIp, int secondPort) {
|
||||
public Optional<CtfService> findService(Inet4Address firstIp, int firstPort, Inet4Address secondIp, int secondPort) {
|
||||
if (firstIp.equals(localIp)) {
|
||||
return findByPort(firstPort);
|
||||
} else if (secondIp.equals(localIp)) {
|
||||
|
||||
@@ -43,7 +43,7 @@ public class StreamService {
|
||||
private final String localIp;
|
||||
private final boolean ignoreEmptyPackets;
|
||||
|
||||
private final byte[] GZIP_HEADER = {0x1f, (byte) 0x8b, 0x08};
|
||||
private static final byte[] GZIP_HEADER = {0x1f, (byte) 0x8b, 0x08};
|
||||
private final java.util.regex.Pattern userAgentPattern = java.util.regex.Pattern.compile("User-Agent: (.+)\\r\\n");
|
||||
|
||||
@Autowired
|
||||
@@ -347,7 +347,11 @@ public class StreamService {
|
||||
private String calculateUserAgentHash(String ua) {
|
||||
char[] alphabet = "abcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
|
||||
int l = alphabet.length;
|
||||
final int hash = Math.abs(ua.hashCode()) % (l * l * l);
|
||||
int hashCode = ua.hashCode();
|
||||
if(hashCode == Integer.MIN_VALUE) {
|
||||
hashCode = Integer.MAX_VALUE;
|
||||
}
|
||||
final int hash = Math.abs(hashCode) % (l * l * l);
|
||||
return "" + alphabet[hash % l] + alphabet[(hash / l) % l] + alphabet[(hash / (l * l)) % l];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user