· 6 years ago · Oct 05, 2019, 06:54 AM
1//
2// PiHoleAPI.swift
3// Deimos
4//
5// Created by H. Kamran on 10/4/19.
6// Copyright © 2019 H. Kamran. All rights reserved.
7//
8
9import Foundation
10
11class PiHoleAPI {
12 func enablePiHole(_ host_ip: String, _ api_key: String) {
13 let session = URLSession.shared
14
15 let ENDPOINT = "/admin/api.php"
16 let escaped_ip = host_ip.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)
17
18 guard var url = URL(string: "http://\(String(describing: escaped_ip))\(ENDPOINT)") else { return () }
19 if api_key.isEmpty == false {
20 var url = URL(string: "http://\(String(describing: escaped_ip))\(ENDPOINT)?enable&auth=\(api_key)")
21 } else {
22 var url = URL(string: "http://\(String(describing: escaped_ip))\(ENDPOINT)?enable")
23 }
24
25 let task = session.dataTask(with: url ) { data, response, err in
26 if let error = err {
27 NSLog("Pi-hole API error: \(error)")
28 }
29
30 if let httpResponse = response as? HTTPURLResponse {
31 switch httpResponse.statusCode {
32 case 200:
33 if let dataString = String(data: data!, encoding: .utf8) {
34 NSLog(dataString)
35 NSLog("Pi-hole API returned a 200 response: \(dataString)")
36 return ()
37 }
38 case 401:
39 NSLog("Pi-hole API returned an 'unauthorized' response. Did you set your API key?")
40 default:
41 NSLog("Pi-hole API returned response: %d %@", httpResponse.statusCode, HTTPURLResponse.localizedString(forStatusCode: httpResponse.statusCode))
42 }
43 }
44 }
45 task.resume()
46 }
47 func disablePiHole(_ host_ip: String, _ time_seconds: Int, _ api_key: String) {
48 NSLog("[DEIMOS::PiHoleAPI:LOG] Disabling...")
49 let session = URLSession.shared
50
51 let ENDPOINT = "/admin/api.php"
52
53 let url_string = "http://\(String(describing: host_ip.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)))\(ENDPOINT)?disable=\(time_seconds)&auth=\(api_key.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed))"
54 let url = URL(string: "http://\(String(describing: host_ip))\(ENDPOINT)?disable=\(time_seconds)&auth=\(api_key)")
55
56 if api_key.isEmpty {
57 NSLog("[DEIMOS::PiHoleAPI:LOG] API key empty")
58 let url = URL(string: "http://\(String(describing: host_ip))\(ENDPOINT)?disable=\(time_seconds)")
59 }
60 NSLog("[DEIMOS::PiHoleAPI:VAR] \(String(describing: url))")
61 NSLog("[DEIMOS::PiHoleAPI:VAR] \("http://\(host_ip.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed))\(ENDPOINT)?disable=\(time_seconds)&auth=\(String(describing: api_key.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)))")")
62
63 let url2 = URL(string: url_string)
64 let task = session.dataTask(with: url2! ) { data, response, err in
65 if let error = err {
66 NSLog("Pi-hole API error: \(error)")
67 }
68
69 if let httpResponse = response as? HTTPURLResponse {
70 switch httpResponse.statusCode {
71 case 200:
72 if let dataString = String(data: data!, encoding: .utf8) {
73 NSLog(dataString)
74 NSLog("Pi-hole API returned a 200 response: \(dataString)")
75 return ()
76 }
77 case 401:
78 NSLog("Pi-hole API returned an 'unauthorized' response. Did you set your API key?")
79 default:
80 NSLog("Pi-hole API returned response: %d %@", httpResponse.statusCode, HTTPURLResponse.localizedString(forStatusCode: httpResponse.statusCode))
81 }
82 }
83 }
84 task.resume()
85 }
86}