View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
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   * The Class WhoisController.
39   */
40  @Controller
41  public class WhoisController extends ParameterizableViewController {
42  
43    /** The Constant logger. */
44    private static final Logger logger = LoggerFactory.getLogger(WhoisController.class);
45  
46    /** The lookup timeout. */
47    private long lookupTimeout;
48  
49    /** The default server. */
50    private String defaultServer;
51  
52    /** The default port. */
53    private int defaultPort;
54  
55    /**
56     * Gets the lookup timeout.
57     *
58     * @return the lookup timeout
59     */
60    public long getLookupTimeout() {
61      return lookupTimeout;
62    }
63  
64    /**
65     * Sets the lookup timeout.
66     *
67     * @param lookupTimeout the new lookup timeout in seconds
68     */
69    @Value("5")
70    public void setLookupTimeout(long lookupTimeout) {
71      this.lookupTimeout = lookupTimeout;
72    }
73  
74    /**
75     * Gets the default server.
76     *
77     * @return the default server
78     */
79    public String getDefaultServer() {
80      return defaultServer;
81    }
82  
83    /**
84     * Sets the default server.
85     *
86     * @param defaultServer the new default server
87     */
88    @Value("whois.arin.net")
89    public void setDefaultServer(String defaultServer) {
90      this.defaultServer = defaultServer;
91    }
92  
93    /**
94     * Gets the default port.
95     *
96     * @return the default port
97     */
98    public int getDefaultPort() {
99      return defaultPort;
100   }
101 
102   /**
103    * Sets the default port.
104    *
105    * @param defaultPort the new default port
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 }