Autopsy  4.17.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
NetworkUtils.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2012-2018 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.coreutils;
20 
21 import java.io.IOException;
22 import java.net.MalformedURLException;
23 import java.net.URL;
24 import java.net.UnknownHostException;
25 import java.util.logging.Level;
26 import org.apache.commons.lang.StringUtils;
27 
28 public class NetworkUtils {
29 
30  private static final Logger logger = Logger.getLogger(NetworkUtils.class.getName());
31 
32  private NetworkUtils() {
33  }
34 
42  public static String getLocalHostName() {
43  String hostName = "";
44  try {
45  hostName = java.net.InetAddress.getLocalHost().getHostName();
46  } catch (UnknownHostException ex) {
47  // getLocalHost().getHostName() can fail in some situations.
48  // Use environment variable if so.
49  hostName = System.getenv("COMPUTERNAME"); //NON-NLS
50  }
51  if (hostName == null || hostName.isEmpty()) {
52  hostName = System.getenv("COMPUTERNAME"); //NON-NLS
53  }
54  return hostName;
55  }
56 
63  private static String getBaseDomain(String url) {
64  String host = null;
65 
66  //strip protocol
67  String cleanUrl = url.replaceFirst(".*:\\/\\/", "");
68 
69  //strip after slashes
70  String dirToks[] = cleanUrl.split("\\/");
71  if (dirToks.length > 0) {
72  host = dirToks[0];
73  } else {
74  host = cleanUrl;
75  }
76 
77  String base = host;
78  try {
79  base = DomainTokenizer.getInstance().getDomain(host);
80  } catch (IOException ex) {
81  logger.log(Level.WARNING, "Unable to load resources for domain categorization.", ex);
82  }
83 
84  // verify there are no special characters in there
85  if (base.matches(".*[~`!@#$%^&\\*\\(\\)\\+={}\\[\\];:\\?<>,/ ].*")) {
86  return "";
87  }
88 
89  //verify that the base domain actually has a '.', details JIRA-4609
90  if (!base.contains(".")) {
91  return "";
92  }
93 
94  return base;
95  }
96 
104  public static String extractDomain(String urlString) {
105  if (urlString == null) {
106  return "";
107  }
108  String urlHost = null;
109 
110  try {
111  URL url = new URL(urlString);
112  urlHost = url.getHost();
113  } catch (MalformedURLException ex) {
114  //do not log if not a valid URL - we will try to extract it ourselves
115  }
116 
117  // if there is a valid url host, get base domain from that host
118  // otherwise use urlString and parse the domain
119  String result = (StringUtils.isNotBlank(urlHost))
120  ? getBaseDomain(urlHost)
121  : getBaseDomain(urlString);
122 
123  return result;
124  }
125 
126 }
static String extractDomain(String urlString)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2021 Basis Technology. Generated on: Tue Jan 19 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.