Browse Source

Refactor all indexedDB code

DSR! 3 years ago
parent
commit
6e04c0325d
3 changed files with 134 additions and 252 deletions
  1. 31 166
      src/pineapple/js/directives.js
  2. 89 6
      src/pineapple/js/services.js
  3. 14 80
      src/pineapple/modules/Networking/js/module.js

+ 31 - 166
src/pineapple/js/directives.js

@@ -179,107 +179,39 @@
                     });
                 };
 
-                $scope.loadOUIFile = (function() {
-                    if (typeof(Storage) === "undefined") {
-                        return false;
-                    }
-                    var ouiText = localStorage.getItem("ouiText");
-                    if (ouiText === null) {
-                            $scope.gettingOUI = true;
-                            $http.get('https://www.wifipineapple.com/oui.txt').then(
-                            function(response) {
-                                localStorage.setItem("ouiText", response.data);
-                                $scope.populateDB();
-                            },
-                            function() {
-                                $api.request({
-                                    module: "Networking",
-                                    action: "getOUI"
-                                }, function(response) {
-                                    if (response.error === undefined) {
-                                        localStorage.setItem("ouiText", response.ouiText);
-                                        $scope.populateDB();
-                                    } else {
-                                        return false;
-                                    }
-                                });
-                            });
-                    }
-                    return true;
-                });
+                $scope.ouiPresent = $api.ouiPresent;
+
+                $scope.loadOUIFile = function() {
+                    $scope.gettingOUI = true;
+                    $api.loadOUIFile((function() {
+                        $scope.ouiLoading = true;
+                        $scope.lookupOUI();
+                    }));
+                };
 
                 $scope.lookupOUI = function() {
-                    $scope.ouiLoading = true;
                     if (!$scope.ouiPresent()) {
                         return;
                     }
 
-                    var request = window.indexedDB.open("pineapple", 1);
-                    request.onsuccess = function() {
-                        var db = request.result;
-                        var prefix = $scope.content.substring(0,8).replace(/:/g,'');
-                        var transaction = db.transaction(["oui"], 'readwrite');
-                        transaction.onerror = function() {
-                            $scope.oui = "Error retrieving OUI. Please clear your browsers cache."
-                        };
-                        var objectStore = transaction.objectStore("oui");
-                        var lookupReq = objectStore.get(prefix);
-                        lookupReq.onerror = function() {
-                            window.indexedDB.deleteDatabase("pineapple");
-                            $scope.oui = "Error retrieving OUI";
-                        };
-                        lookupReq.onsuccess = function() {
-                            if (lookupReq.result) {
-                                $scope.oui = lookupReq.result.name;
-                            } else {
-                                $scope.oui = "Unknown MAC prefix";
-                            }
-                        };
+                    $scope.ouiLoading = true;
+                    $api.lookupOUI($scope.content, (function(text) {
+                        $scope.oui = text;
                         $scope.ouiLoading = false;
-                    }
-                };
-
-                $scope.ouiPresent = function() {
-                    return localStorage.getItem("ouiText") !== null;
+                    }));
                 };
 
-                $scope.populateDB = function() {
-                    $scope.ouiLoading = true;
-                    var request = window.indexedDB.open("pineapple", 1);
-
-                    request.onsuccess = function() {
-                        $scope.lookupOUI();
-                    };
-
-                    request.onerror = function(event) {};
-
-                    request.onupgradeneeded = function(event) {
-                        var db = event.target.result;
-                        var objectStore = db.createObjectStore("oui", { keyPath: "macPrefix"});
-                        var text = localStorage.getItem("ouiText");
-                        var pos = 0;
-                        do {
-                            var line = text.substring(pos, text.indexOf("\n", pos + 1)).replace('\n', '');
-                            var arr = [line.substring(0, 6), line.substring(6)];
-                            objectStore.add({
-                                macPrefix: arr[0],
-                                name: arr[1]
-                            });
-                            pos += line.length + 1;
-                        } while (text.indexOf("\n", pos + 1) !== -1);
-                    };
-                };
                 $scope.deleteOUI = function() {
-                    localStorage.removeItem('ouiText');
-                    window.indexedDB.deleteDatabase('pineapple').onsuccess = function() {
+                    $api.deleteOUI((function() {
                         $scope.success = true;
                         $scope.ouiLoading = false;
                         $scope.gettingOUI = false;
                         $timeout(function() {
                             $scope.success = false;
                         }, 2000);
-                    };
+                    }));
                 };
+
                 $scope.getNoteData = function() {
                     $api.request({
                         module: "Notes",
@@ -583,104 +515,37 @@
                     });
                 };
 
-                $scope.loadOUIFile = (function() {
-                    if (typeof(Storage) === "undefined") {
-                        return false;
-                    }
-                    var ouiText = localStorage.getItem("ouiText");
-                    if (ouiText === null) {
-                        $scope.gettingOUI = true;
-                        $http.get('https://www.wifipineapple.com/oui.txt').then(
-                            function(response) {
-                                localStorage.setItem("ouiText", response.data);
-                                $scope.populateDB();
-                            },
-                            function() {
-                                $api.request({
-                                    module: "Networking",
-                                    action: "getOUI"
-                                }, function(response) {
-                                    if (response.error === undefined) {
-                                        localStorage.setItem("ouiText", response.ouiText);
-                                        $scope.populateDB();
-                                    } else {
-                                        return false;
-                                    }
-                                });
-                            });
-                    }
-                    return true;
-                });
+                $scope.ouiPresent = $api.ouiPresent;
+
+                $scope.loadOUIFile = function() {
+                    $scope.gettingOUI = true;
+                    $api.loadOUIFile((function() {
+                        $scope.ouiLoading = true;
+                        $scope.lookupOUI();
+                    }));
+                };
 
                 $scope.lookupOUI = function() {
-                    $scope.ouiLoading = true;
                     if (!$scope.ouiPresent()) {
                         return;
                     }
 
-                    var request = window.indexedDB.open("pineapple", 1);
-                    request.onsuccess = function() {
-                        var db = request.result;
-                        var prefix = $scope.content.bssid.substring(0,8).replace(/:/g,'');
-                        var transaction = db.transaction("oui");
-                        var objectStore = transaction.objectStore("oui");
-                        var lookupReq = objectStore.get(prefix);
-                        lookupReq.onerror = function() {
-                            window.indexedDB.deleteDatabase("pineapple");
-                            $scope.oui = "Error retrieving OUI";
-                        };
-                        lookupReq.onsuccess = function() {
-                            if (lookupReq.result) {
-                                $scope.oui = lookupReq.result.name;
-                            } else {
-                                $scope.oui = "Unknown MAC prefix";
-                            }
-                        };
+                    $scope.ouiLoading = true;
+                    $api.lookupOUI($scope.content.bssid, (function(text) {
+                        $scope.oui = text;
                         $scope.ouiLoading = false;
-                    }
-                };
-
-                $scope.ouiPresent = function() {
-                    return localStorage.getItem("ouiText") !== null;
+                    }));
                 };
 
-                $scope.populateDB = function() {
-                    $scope.ouiLoading = true;
-                    var request = window.indexedDB.open("pineapple", 1);
-
-                    request.onsuccess = function() {
-                        $scope.lookupOUI();
-                    };
-
-                    request.onerror = function(event) {
-                    };
-
-                    request.onupgradeneeded = function(event) {
-                        var db = event.target.result;
-                        var objectStore = db.createObjectStore("oui", { keyPath: "macPrefix"});
-                        var text = localStorage.getItem("ouiText");
-                        var pos = 0;
-                        do {
-                            var line = text.substring(pos, text.indexOf("\n", pos + 1)).replace('\n', '');
-                            var arr = [line.substring(0, 6), line.substring(6)];
-                            objectStore.add({
-                                macPrefix: arr[0],
-                                name: arr[1]
-                            });
-                            pos += line.length + 1;
-                        } while (text.indexOf("\n", pos + 1) !== -1);
-                    };
-                };
                 $scope.deleteOUI = function() {
-                    localStorage.removeItem('ouiText');
-                    window.indexedDB.deleteDatabase('pineapple').onsuccess = function() {
+                    $api.deleteOUI((function() {
                         $scope.success = true;
                         $scope.ouiLoading = false;
                         $scope.gettingOUI = false;
                         $timeout(function() {
                             $scope.success = false;
                         }, 2000);
-                    };
+                    }));
                 };
 
                 $scope.lookupOUI();

+ 89 - 6
src/pineapple/js/services.js

@@ -33,19 +33,19 @@
         this.request = (function(data, callback, scope) {
             return $http.post('/api/', data, { timeout: 30000 }).
             then(function(response){
-                if (response.data.error === "Not Authenticated") {
+                if (response.data.error === 'Not Authenticated') {
                     if (response.data.setupRequired === true) {
-                        if (window.location.hash !== "#!/modules/Setup") {
-                            window.location.hash = "#!/modules/Setup";
+                        if (window.location.hash !== '#!/modules/Setup') {
+                            window.location.hash = '#!/modules/Setup';
                         }
                     } else {
-                        $("#loginModal").modal({
+                        $('#loginModal').modal({
                             show: true,
                             keyboard: false,
                             backdrop: 'static'
                         });
                     }
-                    $(".logout").hide();
+                    $('.logout').hide();
                 }
                 if (callback !== undefined) {
                     if (scope !== undefined) {
@@ -119,12 +119,95 @@
             }
         };
 
+        this.ouiPresent = function() {
+            return localStorage.getItem('ouiText') !== null;
+        };
+
+        this.loadOUIFile = function(callback) {
+            $http.get('https://www.wifipineapple.com/oui.txt').then(
+                function(response) {
+                    window.pineapple.populateDB(response.data, callback);
+                },
+                function() {
+                    $api.request({
+                        module: 'Networking',
+                        action: 'getOUI'
+                    }, function(response) {
+                        if (response.error === undefined) {
+                            window.pineapple.populateDB(response.ouiText, callback);
+                        }
+                    }
+                );
+            });
+        };
+
+        this.populateDB = function(text, callback) {
+            var request = window.indexedDB.open('pineapple', 1);
+
+            request.onsuccess = function() {
+                localStorage.setItem('ouiText', true);
+                if (callback) {
+                    callback();
+                }
+            };
+
+            request.onupgradeneeded = function(event) {
+                var db = event.target.result;
+                var objectStore = db.createObjectStore('oui', { keyPath: 'macPrefix' });
+                var pos = 0;
+                var totalLines = 0;
+                do {
+                    var line = text.substring(pos, text.indexOf('\n', pos + 1)).replace('\n', '');
+                    objectStore.add({
+                        macPrefix: line.substring(0, 6),
+                        name: line.substring(6)
+                    });
+                    pos += line.length + 1;
+                    totalLines++;
+                } while (text.indexOf('\n', pos + 1) !== -1);
+
+                console.log('[*] Lines inserted: ' + totalLines)
+            };
+        };
+
+        this.deleteOUI = function(callback) {
+            localStorage.removeItem('ouiText');
+            window.indexedDB.deleteDatabase('pineapple').onsuccess = function() {
+                if (callback) {
+                    callback();
+                }
+            };
+        };
+
+        this.lookupOUI = function(mac, callback) {
+            var request = window.indexedDB.open('pineapple', 1);
+            request.onsuccess = function() {
+                var db = request.result;
+                var transaction = db.transaction(['oui'], 'readwrite');
+                transaction.onerror = function() {
+                    callback('Error retrieving OUI. Please clear your browser cache.');
+                };
+                
+                var prefix = mac.substring(0, 8).replace(/:/g,'');
+                console.log({ mac, prefix });
+                var lookupReq = transaction.objectStore('oui').get(prefix);
+                lookupReq.onerror = function() {
+                    window.indexedDB.deleteDatabase('pineapple');
+                    callback('Error retrieving OUI');
+                };
+                
+                lookupReq.onsuccess = function() {
+                    callback(lookupReq.result ? lookupReq.result.name : 'Unknown MAC prefix');
+                };
+            }
+        };
+
         this.request({
             module: 'Configuration',
             action: 'getDevice'
         }, function(response, scope){
             scope.device = response.device;
-            for (var i = scope.deviceCallbacks.length-1; i >=0; --i) {
+            for (var i = scope.deviceCallbacks.length - 1; i >= 0; --i) {
                 var callbackObj = scope.deviceCallbacks[i];
                 callbackObj.callback(scope.device, callbackObj.scope);
             }

+ 14 - 80
src/pineapple/modules/Networking/js/module.js

@@ -402,101 +402,35 @@ registerController('NetworkingAdvancedController', ['$api', '$scope', '$timeout'
 registerController("OUILookupController", ['$api', '$scope', '$timeout', '$http', function($api, $scope, $timeout, $http) {
     $scope.macAddress = "";
     $scope.vendor = "";
-    $scope.OUIDBPresent = false;
 
-    $scope.isOUIPresent = function () {
-        return localStorage.getItem("ouiText") !== null;
-    };
+    $scope.isOUIPresent = $api.ouiPresent;
 
     $scope.downloadOUIDatabase = function () {
-        if (typeof(Storage) === "undefined") {
-            return false;
-        }
-        var ouiText = localStorage.getItem("ouiText");
-        if (ouiText === null) {
-            $scope.gettingOUI = true;
-            $http.get('https://www.wifipineapple.com/oui.txt').then(
-                function (response) {
-                    localStorage.setItem("ouiText", response.data);
-                    $scope.populateDB();
-                },
-                function () {
-                    $api.request({
-                        module: "Networking",
-                        action: "getOUI"
-                    }, function (response) {
-                        if (response.error === undefined) {
-                            localStorage.setItem("ouiText", response.ouiText);
-                            $scope.populateDB();
-                        } else {
-                            return false;
-                        }
-                    });
-                });
-        }
-        return true;
-    };
-
-    $scope.populateDB = function () {
+        $scope.gettingOUI = true;
         $scope.ouiLoading = true;
-        var request = window.indexedDB.open("pineapple", 1);
-
-        request.onupgradeneeded = function (event) {
-            var db = event.target.result;
-            var objectStore = db.createObjectStore("oui", {keyPath: "macPrefix"});
-            var text = localStorage.getItem("ouiText");
-            var pos = 0;
-            do {
-                var line = text.substring(pos, text.indexOf("\n", pos + 1)).replace('\n', '');
-                var arr = [line.substring(0, 6), line.substring(6)];
-                objectStore.add({
-                    macPrefix: arr[0],
-                    name: arr[1]
-                });
-                pos += line.length + 1;
-            } while (text.indexOf("\n", pos + 1) !== -1);
-        };
-        $scope.ouiLoading = false;
+        $api.loadOUIFile((function() {
+            $scope.ouiLoading = false;
+        }));
     };
 
     $scope.lookupMACAddress = function() {
-        $scope.ouiLoading = true;
-        if (!$scope.isOUIPresent()) {
+        if (!$api.ouiPresent()) {
             return;
         }
-        var request = window.indexedDB.open("pineapple", 1);
-        request.onsuccess = function() {
-            var db = request.result;
-            var mac = convertMACAddress($scope.macAddress);
-            var prefix = mac.substring(0, 8).replace(/:/g, '');
-            var transaction = db.transaction("oui");
-            var objectStore = transaction.objectStore("oui");
-            var lookupReq = objectStore.get(prefix);
-            lookupReq.onerror = function () {
-                window.indexedDB.deleteDatabase("pineapple");
-                $scope.vendor = "Error retrieving OUI";
-            };
-            lookupReq.onsuccess = function () {
-                if (lookupReq.result) {
-                    $scope.vendor = lookupReq.result.name;
-                } else {
-                    $scope.vendor = "Unknown MAC prefix";
-                }
-            };
+
+        $scope.ouiLoading = true;
+        var mac = convertMACAddress($scope.macAddress.trim());
+        $api.lookupOUI(mac, (function(text) {
+            $scope.vendor = text;
             $scope.ouiLoading = false;
-        }
+        }));
     };
 
     $scope.removeOUIDatabase = function() {
-        localStorage.removeItem('ouiText');
-        window.indexedDB.deleteDatabase('pineapple').onsuccess = function() {
-            $scope.success = true;
+        $api.deleteOUI((function() {
             $scope.ouiLoading = false;
             $scope.gettingOUI = false;
-            $timeout(function() {
-                $scope.success = false;
-            }, 2000);
-        };
+        }));
     };
 }]);