aboutsummaryrefslogtreecommitdiffstats
path: root/viensamoi/contrib/geoloc.java
blob: 06172036d246768708520d606023decf1a4488ce (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import java.io.*;  
import java.util.*;
import java.net.*;
import java.applet.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class getGPSLocation extends Applet{  
//public class getGPSLocation{
	//public static void main(String[] args) {
	public void init(){
		String result = null;
		if (isWindows()) {
			System.out.println("\nThis is Windows Machine");
			result=getWindows();
		} else if (isMac()) {
			System.out.println("\nThis is Mac Machine");
			result=getMac();
		} else {
			System.out.println("\nYour OS is not support!!");
		}
		//System.out.println(result);
	}
	
	public static String getWindows(){
			String result = null;
            try {  
			
				ArrayList ssidList = new ArrayList();
				ArrayList bssidList = new ArrayList();
				ArrayList rssiList = new ArrayList();
			
                Process p = Runtime.getRuntime().exec("netsh wlan show networks mode=bssid");  
                BufferedReader in = new BufferedReader(  
                                    new InputStreamReader(p.getInputStream()));  
                String line = null;  
				String signal = null;
				String ssidStr = null;
				
				while ((line = in.readLine()) != null) {  
	
					Pattern p1 = Pattern.compile("(SSID\\s\\d+\\s:)\\s([\\w\\s]*)");
					Matcher m1 = p1.matcher(line);
					if(m1.find()){
						ssidStr = m1.group(2);
						ssidStr = ssidStr.replaceAll(" ","%20");
						ssidList.add(ssidStr);
					}	
					Pattern p2 = Pattern.compile("(BSSID\\s1\\s*:)\\s((.?)*)");
					Matcher m2 = p2.matcher(line);
					if(m2.find()){
						bssidList.add(m2.group(2));
					}	
					Pattern p3 = Pattern.compile("(Signal\\s*):\\s((.?)*)");
					Matcher m3 = p3.matcher(line);
					if(m3.find()){
						signal = m3.group(2);
						signal = signal.replaceAll("%","");
						signal = signal.replaceAll(" ","");
						signal = "-"+signal;
						rssiList.add(signal);
					}											
				}
				int arraySize=ssidList.size();
				if(arraySize==0){
					result="I don't know where the target is";
				}
				else{
					result=googleLookup(bssidList,ssidList,rssiList);
				}
	         } catch (IOException e) {  
                e.printStackTrace();  
            }
		return result;          
	}
	
	public static String googleLookup(ArrayList bssidList,ArrayList ssidList,ArrayList rssiList){
		String result = null;
	    try {  
			int j=0;
			String queryString = "https://maps.googleapis.com/maps/api/browserlocation/json?browser=firefox&sensor=true";
			while(j<ssidList.size()){
				queryString+="&wifi=mac:";
				queryString+=bssidList.get(j);
				queryString+="%7C";
				
				queryString+="ssid:";
				queryString+=ssidList.get(j);
			
				queryString+="%7C";
				queryString+="ss:";
				queryString+=rssiList.get(j);
				j++;
			}
					
			//Get geocoordinates / Longitude and Latitude
			String geoCoordinates = null;		

			//System.out.println("Query string: "+queryString);

			URL url = new URL(queryString);
			URLConnection urlc = url.openConnection();
			urlc.setRequestProperty("User-Agent", "Mozilla 5.0 (Windows; U; "+ "Windows NT 5.1; en-US; rv:1.8.0.11) ");
			BufferedReader reader = new BufferedReader(new InputStreamReader(urlc.getInputStream()));		
			for (String output; (output = reader.readLine()) != null;) {
				if(output.indexOf("18000.0")>0){
					result+="Location is not accurate\n";
					System.out.println("Location is not accurate");
				}
				else{	
					if(output.indexOf("lat")>0){
						output = output.replace("\"lat\" : ","");
						output = output.replaceAll("^\\s+", "");
						geoCoordinates = output;
						result+="Latitude: ";
						result+=output;
						System.out.println("Latitude: "+output);
					}
					if(output.indexOf("lng")>0){
						output = output.replace("\"lng\" : ","");
						output = output.replaceAll("^\\s+", "");
						geoCoordinates += output;
						result+="Longitude: ";
						result+=output;
						System.out.println("Longitude: "+output);
					}
				}
				
			}
			
		
			//Reverse geocoordinates to street address
			String reverseGeo = "https://maps.googleapis.com/maps/geo?q="+geoCoordinates+"&output=json&sensor=true_or_false";
			
			System.out.println(reverseGeo);

				URL url1 = new URL(reverseGeo);
				URLConnection urlc1 = url1.openConnection();
				urlc1.setRequestProperty("User-Agent", "Mozilla 5.0 (Windows; U; "+ "Windows NT 5.1; en-US; rv:1.8.0.11) ");
			BufferedReader reader1 = new BufferedReader(new InputStreamReader(urlc1.getInputStream()));		
			for (String output1; (output1 = reader1.readLine()) != null;) {
				if(output1.indexOf("address")>0){
					output1 = output1.replace("\"address\": ",""); 
					output1 = output1.replace("\",",""); 
					output1 = output1.replace("\"",""); 
					output1 = output1.replaceAll("^\\s+", "");
					result+="Address is ";
					result+=output1;
					System.out.println("Address is "+output1);
				}
			}		
			String mapAddress = "http://maps.google.com/maps?q="+geoCoordinates+"+%28You+are+located+here%29&iwloc=A&hl=en";
			result+="\n"+mapAddress;
			System.out.println("\n"+mapAddress);
		} catch (IOException e) { 
			e.printStackTrace();  				
		}
		return result;		
	}
	
	public static String getMac(){
		String result = null;	
        try {  
                Process p = Runtime.getRuntime().exec("/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -s");  
                BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));  
                String line = null;  
				String ssidStr = null;
				String signal = null;
				
				String queryString = "https://maps.googleapis.com/maps/api/browserlocation/json?browser=firefox&sensor=true";
		
				ArrayList ssidList = new ArrayList();
				ArrayList bssidList = new ArrayList();
				ArrayList rssiList = new ArrayList();

				line = in.readLine();	
				while ((line = in.readLine()) != null) {  
					line = line.replaceAll("^\\s+", "");
					
					//Pattern p1 = Pattern.compile("((.?)*\\s\\w*):(\\w*:\\w*:\\w*:\\w*:\\w*)\\s((.?)*)\\s(\\d+)");
					Pattern p1 = Pattern.compile("((.?)+)\\s(..:..:..:..:..:..)\\s(-\\d*)");
					Matcher m1 = p1.matcher(line);
					if(m1.find()){
						ssidStr = m1.group(1);
						ssidStr = ssidStr.replaceAll(" ","%20");
						ssidList.add(ssidStr);
					
						//System.out.println("ssid: "+ssidStr);

						bssidList.add(m1.group(3));
						//System.out.println("bssid: "+m1.group(3));

						signal = m1.group(4);
						signal = signal.replaceAll(" ","");

						//System.out.println("signal: "+signal);
						rssiList.add(signal);
					}
					
				}
			int arraySize=ssidList.size();
			if(arraySize==0){
				result="I don't know where the target is";
				System.out.println("I don't know where the target is");
			}
			else{
				result=googleLookup(bssidList,ssidList,rssiList);

			}
		} catch (IOException e) {  
			e.printStackTrace();  
		}  
		return result;		
	}  

	public static boolean isWindows() {
 
		String os = System.getProperty("os.name").toLowerCase();
		// windows
		return (os.indexOf("win") >= 0);
 
	}
 
	public static boolean isMac() {
 
		String os = System.getProperty("os.name").toLowerCase();
		// Mac
		return (os.indexOf("mac") >= 0);
 
	}
 
	public static boolean isLinux() {
 
		String os = System.getProperty("os.name").toLowerCase();
		// linux or unix
		return (os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0);
 
	}

    }