1
2
3
4
5
6
7
8
9
10
11 package psiprobe.controllers;
12
13 import jakarta.servlet.http.HttpServletRequest;
14 import jakarta.servlet.http.HttpServletResponse;
15
16 import java.io.BufferedReader;
17 import java.io.ByteArrayInputStream;
18 import java.io.IOException;
19 import java.io.InputStreamReader;
20 import java.net.InetAddress;
21 import java.net.UnknownHostException;
22 import java.nio.charset.StandardCharsets;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.beans.factory.annotation.Value;
29 import org.springframework.stereotype.Controller;
30 import org.springframework.web.bind.ServletRequestUtils;
31 import org.springframework.web.bind.annotation.RequestMapping;
32 import org.springframework.web.servlet.ModelAndView;
33 import org.springframework.web.servlet.mvc.ParameterizableViewController;
34
35 import psiprobe.tools.Whois;
36
37
38
39
40 @Controller
41 public class WhoisController extends ParameterizableViewController {
42
43
44 private static final Logger logger = LoggerFactory.getLogger(WhoisController.class);
45
46
47 private long lookupTimeout;
48
49
50 private String defaultServer;
51
52
53 private int defaultPort;
54
55
56
57
58
59
60 public long getLookupTimeout() {
61 return lookupTimeout;
62 }
63
64
65
66
67
68
69 @Value("5")
70 public void setLookupTimeout(long lookupTimeout) {
71 this.lookupTimeout = lookupTimeout;
72 }
73
74
75
76
77
78
79 public String getDefaultServer() {
80 return defaultServer;
81 }
82
83
84
85
86
87
88 @Value("whois.arin.net")
89 public void setDefaultServer(String defaultServer) {
90 this.defaultServer = defaultServer;
91 }
92
93
94
95
96
97
98 public int getDefaultPort() {
99 return defaultPort;
100 }
101
102
103
104
105
106
107 @Value("43")
108 public void setDefaultPort(int defaultPort) {
109 this.defaultPort = defaultPort;
110 }
111
112 @RequestMapping(path = "/whois.ajax")
113 @Override
114 public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
115 throws Exception {
116 return super.handleRequest(request, response);
117 }
118
119 @Override
120 protected ModelAndView handleRequestInternal(HttpServletRequest request,
121 HttpServletResponse response) throws Exception {
122
123 boolean timeout = false;
124
125 String ipAddress = ServletRequestUtils.getStringParameter(request, "ip");
126
127 Whois.Response wh = null;
128 try {
129 wh = Whois.lookup(getDefaultServer(), getDefaultPort(), ipAddress, getLookupTimeout());
130 } catch (IOException e) {
131 timeout = true;
132 logger.trace("", e);
133 }
134
135 List<String> lines = null;
136 if (wh != null) {
137 lines = new ArrayList<>(50);
138 try (BufferedReader br = new BufferedReader(new InputStreamReader(
139 new ByteArrayInputStream(wh.getSummary().getBytes(StandardCharsets.UTF_8)),
140 StandardCharsets.UTF_8))) {
141 String line;
142 while ((line = br.readLine()) != null) {
143 lines.add(line);
144 }
145 }
146 }
147
148 String reverseName = null;
149 if (ipAddress != null) {
150 try {
151 reverseName = InetAddress.getByName(ipAddress).getCanonicalHostName();
152 } catch (UnknownHostException e) {
153 logger.error("could not run a DNS query on {}", ipAddress);
154 logger.trace("", e);
155 }
156 }
157 return new ModelAndView(getViewName(), "result", lines).addObject("timeout", timeout)
158 .addObject("whoisServer",
159 wh != null ? wh.getServer() + ":" + wh.getPort() : defaultServer + ":" + defaultPort)
160 .addObject("domainName", reverseName);
161 }
162
163 @Value("ajax/whois")
164 @Override
165 public void setViewName(String viewName) {
166 super.setViewName(viewName);
167 }
168
169 }