Autopsy  4.14.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.net.MalformedURLException;
22 import java.net.URL;
23 import java.net.UnknownHostException;
24 import java.util.StringTokenizer;
25 
26 public class NetworkUtils {
27 
28  private NetworkUtils() {
29  }
30 
38  public static String getLocalHostName() {
39  String hostName = "";
40  try {
41  hostName = java.net.InetAddress.getLocalHost().getHostName();
42  } catch (UnknownHostException ex) {
43  // getLocalHost().getHostName() can fail in some situations.
44  // Use environment variable if so.
45  hostName = System.getenv("COMPUTERNAME"); //NON-NLS
46  }
47  if (hostName == null || hostName.isEmpty()) {
48  hostName = System.getenv("COMPUTERNAME"); //NON-NLS
49  }
50  return hostName;
51  }
52 
59  private static String getBaseDomain(String url) {
60  String host = null;
61 
62  //strip protocol
63  String cleanUrl = url.replaceFirst(".*:\\/\\/", "");
64 
65  //strip after slashes
66  String dirToks[] = cleanUrl.split("\\/");
67  if (dirToks.length > 0) {
68  host = dirToks[0];
69  } else {
70  host = cleanUrl;
71  }
72 
73  //get the domain part from host (last 2)
74  StringTokenizer tok = new StringTokenizer(host, ".");
75  StringBuilder hostB = new StringBuilder();
76  int toks = tok.countTokens();
77 
78  for (int count = 0; count < toks; ++count) {
79  String part = tok.nextToken();
80  int diff = toks - count;
81  if (diff < 3) {
82  hostB.append(part);
83  }
84  if (diff == 2) {
85  hostB.append(".");
86  }
87  }
88 
89 
90  String base = hostB.toString();
91  // verify there are no special characters in there
92  if (base.matches(".*[~`!@#$%^&\\*\\(\\)\\+={}\\[\\];:\\?<>,/ ].*")) {
93  return "";
94  }
95 
96  //verify that the base domain actually has a '.', details JIRA-4609
97  if(!base.contains(".")) {
98  return "";
99  }
100 
101  return base;
102  }
103 
112  public static String extractDomain(String urlString) {
113  if (urlString == null) {
114  return "";
115  }
116  String result = "";
117 
118  try {
119  URL url = new URL(urlString);
120  result = url.getHost();
121  } catch (MalformedURLException ex) {
122  //do not log if not a valid URL - we will try to extract it ourselves
123  }
124 
125  //was not a valid URL, try a less picky method
126  if (result == null || result.trim().isEmpty()) {
127  return getBaseDomain(urlString);
128  }
129  return result;
130  }
131 
132 }
static String extractDomain(String urlString)

Copyright © 2012-2020 Basis Technology. Generated on: Wed Apr 8 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.