Autopsy  4.20.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 import org.apache.commons.validator.routines.DomainValidator;
29 
30 public class NetworkUtils {
31 
32  private static final Logger logger = Logger.getLogger(NetworkUtils.class.getName());
33 
34  private NetworkUtils() {
35  }
36 
44  public static String getLocalHostName() {
45  String hostName = "";
46  try {
47  hostName = java.net.InetAddress.getLocalHost().getHostName();
48  } catch (UnknownHostException ex) {
49  // getLocalHost().getHostName() can fail in some situations.
50  // Use environment variable if so.
51  hostName = System.getenv("COMPUTERNAME"); //NON-NLS
52  }
53  if (hostName == null || hostName.isEmpty()) {
54  hostName = System.getenv("COMPUTERNAME"); //NON-NLS
55  }
56  return hostName;
57  }
58 
65  private static String getBaseDomain(String url) {
66  String host = null;
67 
68  //strip protocol
69  String cleanUrl = url.replaceFirst(".*:\\/\\/", "");
70 
71  //strip after slashes
72  String dirToks[] = cleanUrl.split("\\/");
73  if (dirToks.length > 0) {
74  host = dirToks[0];
75  } else {
76  host = cleanUrl;
77  }
78 
79  String base = host;
80  try {
81  base = DomainTokenizer.getInstance().getDomain(host);
82  } catch (IOException ex) {
83  logger.log(Level.WARNING, "Unable to load resources for domain categorization.", ex);
84  }
85 
86  // verify there are no special characters in there
87  if (base.matches(".*[~`!@#$%^&\\*\\(\\)\\+={}\\[\\];:\\?<>,/ ].*")) {
88  return "";
89  }
90 
91  //verify that the base domain actually has a '.', details JIRA-4609
92  if (!base.contains(".")) {
93  return "";
94  }
95 
96  return base;
97  }
98 
106  public static String extractDomain(String urlString) {
107  if (urlString == null) {
108  return "";
109  }
110  String urlHost = null;
111 
112  try {
113  URL url = new URL(urlString);
114  urlHost = url.getHost();
115  } catch (MalformedURLException ex) {
116  //do not log if not a valid URL - we will try to extract it ourselves
117  }
118 
119  String result = (StringUtils.isNotBlank(urlHost))
120  ? getBaseDomain(urlHost)
121  : getBaseDomain(urlString);
122 
123  // if there is a valid url host, get base domain from that host
124  // otherwise use urlString and parse the domain
125  DomainValidator validator = DomainValidator.getInstance(true);
126  if (validator.isValid(result)) {
127  return result;
128  } else {
129  final String validIpAddressRegex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";
130  if (result.matches(validIpAddressRegex)) {
131  return result;
132  } else {
133  return "";
134  }
135  }
136  }
137 
138 }
static String extractDomain(String urlString)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2022 Basis Technology. Generated on: Tue Aug 1 2023
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.