You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

33 lines
919 B

package app.utils;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.time.Instant;
import java.util.HashMap;
public class RealIPService {
private final RestTemplate restTemplate;
private final long lst_update = 0;
private String lst_ip = "";
public RealIPService() {
this.restTemplate = new RestTemplate();
}
public String getIP() {
if (Instant.now().getEpochSecond() - lst_update < 300)
return lst_ip;
try {
HashMap response = restTemplate.getForEntity("https://ifconfig.co/json", HashMap.class).getBody();
lst_ip = (String) response.getOrDefault("ip", "");
} catch (Exception e) {}
return lst_ip;
}
public String genIPWithPort(Integer port) {
return new StringBuilder().append(getIP()).append(":").append(port).toString();
}
}