Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.51 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266WebServer.h>
  3. #include <MySQL_Connection.h>
  4. #include <MySQL_Cursor.h>
  5.  
  6. #include "lwip/lwip_napt.h"
  7. #include "lwip/app/dhcpserver.h"
  8. #include "lwip/netif.h"
  9. #include "netif/etharp.h"
  10. #include "lwip/udp.h"
  11. #include "index.h" //Our HTML webpage contents
  12. #include "pass.h" //Our HTML webpage contents
  13.  
  14. // credentials for ESP8266 STA
  15. const char* sta_ssid = "###############";
  16. const char* sta_password = "###############";
  17. String s = MAIN_page;
  18. String v = PASS_page;
  19. // name of the ESP8266 AP
  20. #define AP_SSID "GET THE WIFI"
  21.  
  22. const int MAX_CLIENTS = 16;
  23.  
  24.  
  25. byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  26.  
  27. IPAddress server_addr(#################); // IP of the MySQL *server* here
  28. char user[] = "##################"; // MySQL user login username
  29. char password[] = "###################"; // MySQL user login password
  30.  
  31. WiFiClient client; // Use this for WiFi instead of EthernetClient
  32. MySQL_Connection conn((Client *)&client);
  33.  
  34. #define VIEWPORT "<meta name='viewport' content='width=device-width, initial-scale=1'>"
  35.  
  36. String acceptedHTML = "<!DOCTYPE html>"
  37. "<html>"
  38. "<head>"
  39. "<title>" AP_SSID "</title>"
  40. "</head>"
  41. VIEWPORT
  42. "<meta http-equiv=\"refresh\" content=\"3;url=http://www.google.com/\" />"
  43. "<body>"
  44. "<h1>Terms accepted!</h1>"
  45. "</body>"
  46. "</html>";
  47.  
  48. const byte DHCP_PORT = 67;
  49. const byte DNS_PORT = 53;
  50. const byte HTTP_PORT = 80;
  51.  
  52. IPAddress myIP;
  53. ESP8266WebServer webServer(80);
  54.  
  55.  
  56. PACK_STRUCT_BEGIN
  57. struct tcp_hdr {
  58. PACK_STRUCT_FIELD(u16_t src);
  59. PACK_STRUCT_FIELD(u16_t dest);
  60. PACK_STRUCT_FIELD(u32_t seqno);
  61. PACK_STRUCT_FIELD(u32_t ackno);
  62. PACK_STRUCT_FIELD(u16_t _hdrlen_rsvd_flags);
  63. PACK_STRUCT_FIELD(u16_t wnd);
  64. PACK_STRUCT_FIELD(u16_t chksum);
  65. PACK_STRUCT_FIELD(u16_t urgp);
  66. } PACK_STRUCT_STRUCT;
  67. PACK_STRUCT_END
  68.  
  69. // some magic from inside the NAT lwip for address rewriting
  70. extern "C" {
  71. void ip_napt_modify_addr_tcp(struct tcp_hdr *tcphdr, ip_addr_p_t *oldval, u32_t newval);
  72. void ip_napt_modify_addr(struct ip_hdr *iphdr, ip_addr_p_t *field, u32_t newval);
  73. }
  74.  
  75. static netif_input_fn orig_input_ap;
  76. static netif_linkoutput_fn orig_output_ap;
  77. struct eth_addr curr_mac;
  78. uint32_t curr_IP;
  79.  
  80. struct eth_addr allowed_macs[MAX_CLIENTS];
  81. int max_client = 0;
  82.  
  83. bool check_packet_in(struct pbuf *p) {
  84. struct eth_hdr *mac_h;
  85. struct ip_hdr *ip_h;
  86. struct udp_hdr *udp_he;
  87. struct tcp_hdr *tcp_h;
  88.  
  89. if (p->len < sizeof(struct eth_hdr))
  90. return false;
  91.  
  92. mac_h = (struct eth_hdr *)p->payload;
  93.  
  94. // Check only IPv4 traffic
  95. if (ntohs(mac_h->type) != ETHTYPE_IP)
  96. return true;
  97.  
  98. if (p->len < sizeof(struct eth_hdr)+sizeof(struct ip_hdr))
  99. return false;
  100.  
  101. ip_h = (struct ip_hdr *)(p->payload + sizeof(struct eth_hdr));
  102.  
  103. // Known MACs can pass
  104. for(int i = 0; i<max_client; i++) {
  105. if (memcmp(mac_h->src.addr, allowed_macs[i].addr, sizeof(mac_h->src.addr)) == 0) {
  106. return true;
  107. }
  108. }
  109.  
  110. // DHCP and DNS is okay
  111. if (IPH_PROTO(ip_h) == IP_PROTO_UDP) {
  112. if (p->len < sizeof(struct eth_hdr)+sizeof(struct ip_hdr)+sizeof(struct udp_hdr))
  113. return false;
  114.  
  115. udp_he = (struct udp_hdr *)(p->payload + sizeof(struct eth_hdr) + sizeof(struct ip_hdr));
  116.  
  117. if (ntohs(udp_he->dest) == DHCP_PORT)
  118. return true;
  119.  
  120. if (ntohs(udp_he->dest) == DNS_PORT)
  121. return true;
  122.  
  123. return false;
  124. }
  125.  
  126. // HTTP is redirected
  127. if (IPH_PROTO(ip_h) == IP_PROTO_TCP) {
  128. if (p->len < sizeof(struct eth_hdr)+sizeof(struct ip_hdr)+sizeof(struct tcp_hdr))
  129. return false;
  130.  
  131. tcp_h = (struct tcp_hdr *)(p->payload + sizeof(struct eth_hdr) + sizeof(struct ip_hdr));
  132.  
  133. if (ntohs(tcp_h->dest) == HTTP_PORT) {
  134. curr_mac = mac_h->src;
  135. curr_IP = ip_h->dest.addr;
  136. ip_napt_modify_addr_tcp(tcp_h, &ip_h->dest, (uint32_t)myIP);
  137. ip_napt_modify_addr(ip_h, &ip_h->dest, (uint32_t)myIP);
  138. return true;
  139. }
  140. }
  141.  
  142. // drop anything else
  143. return false;
  144. }
  145.  
  146. err_t my_input_ap (struct pbuf *p, struct netif *inp) {
  147.  
  148. if (check_packet_in(p)) {
  149. return orig_input_ap(p, inp);
  150. } else {
  151. pbuf_free(p);
  152. return ERR_OK;
  153. }
  154. }
  155.  
  156. bool check_packet_out(struct pbuf *p) {
  157. struct eth_hdr *mac_h;
  158. struct ip_hdr *ip_h;
  159. struct tcp_hdr *tcp_h;
  160.  
  161. if (p->len < sizeof(struct eth_hdr)+sizeof(struct ip_hdr)+sizeof(struct tcp_hdr))
  162. return true;
  163.  
  164. ip_h = (struct ip_hdr *)(p->payload + sizeof(struct eth_hdr));
  165.  
  166. if (IPH_PROTO(ip_h) != IP_PROTO_TCP)
  167. return true;
  168.  
  169. tcp_h = (struct tcp_hdr *)(p->payload + sizeof(struct eth_hdr) + sizeof(struct ip_hdr));
  170.  
  171. // rewrite packet from our HTTP server
  172. if (ntohs(tcp_h->src) == HTTP_PORT && ip_h->src.addr == (uint32_t)myIP) {
  173. ip_napt_modify_addr_tcp(tcp_h, &ip_h->src, curr_IP);
  174. ip_napt_modify_addr(ip_h, &ip_h->src, curr_IP);
  175. }
  176.  
  177. return true;
  178. }
  179.  
  180. err_t my_output_ap (struct netif *outp, struct pbuf *p) {
  181.  
  182. if (check_packet_out(p)) {
  183. return orig_output_ap(outp, p);
  184. } else {
  185. pbuf_free(p);
  186. return ERR_OK;
  187. }
  188. }
  189.  
  190. // patches the netif to insert the filter functions
  191. void patch_netif(ip_addr_t netif_ip, netif_input_fn ifn, netif_input_fn *orig_ifn, netif_linkoutput_fn ofn, netif_linkoutput_fn *orig_ofn)
  192. {
  193. struct netif *nif;
  194.  
  195. for (nif = netif_list; nif != NULL && nif->ip_addr.addr != netif_ip.addr; nif = nif->next);
  196. if (nif == NULL) return;
  197.  
  198. if (ifn != NULL && nif->input != ifn) {
  199. *orig_ifn = nif->input;
  200. nif->input = ifn;
  201. }
  202. if (ofn != NULL && nif->linkoutput != ofn) {
  203. *orig_ofn = nif->linkoutput;
  204. nif->linkoutput = ofn;
  205. }
  206. }
  207.  
  208. void setup()
  209. {
  210. Serial.begin(115200);
  211. Serial.println();
  212.  
  213. WiFi.mode(WIFI_AP_STA);
  214.  
  215. Serial.println("Connecting to STA");
  216.  
  217. WiFi.begin(sta_ssid, sta_password);
  218.  
  219. //Wifi connection
  220. while (WiFi.status() != WL_CONNECTED) {
  221. delay(500);
  222. Serial.print(".");
  223. }
  224.  
  225. Serial.println("");
  226. Serial.print("Connected to ");
  227. Serial.println(sta_ssid);
  228. Serial.print("IP address: ");
  229. Serial.println(WiFi.localIP());
  230. Serial.print("dnsIP address: ");
  231. Serial.println(WiFi.dnsIP());
  232. Serial.print("gatewayIP address: ");
  233. Serial.println(WiFi.gatewayIP());
  234. Serial.print("subnetMask address: ");
  235. Serial.println(WiFi.subnetMask());
  236.  
  237.  
  238. Serial.println("");
  239. Serial.println("Configuring access point...");
  240. WiFi.softAP(AP_SSID, NULL, 1, 0, 8);
  241.  
  242. myIP = WiFi.softAPIP();
  243. Serial.print("AP IP address: ");
  244. Serial.println(myIP);
  245.  
  246. // Insert the filter functions
  247. patch_netif(myIP, my_input_ap, &orig_input_ap, my_output_ap, &orig_output_ap);
  248.  
  249. // Initialize the NAT feature
  250. ip_napt_init(IP_NAPT_MAX, IP_PORTMAP_MAX);
  251.  
  252. // Enable NAT on the AP interface
  253. ip_napt_enable_no(1, 1);
  254.  
  255. // Set the DNS server for clients of the AP to the one we also use for the STA interface
  256. dhcps_set_DNS(WiFi.dnsIP());
  257.  
  258.  
  259.  
  260.  
  261.  
  262. Serial.println("Connecting To MySQL Database");
  263. if (conn.connect(server_addr, 3306, user, password)) {
  264. delay(1000);
  265. }
  266. else
  267. Serial.println("Connection failed.");
  268.  
  269. char query[] = "SELECT code FROM gilde.code LIMIT 1";
  270.  
  271. Serial.println("\nRunning SELECT from CODE and printing results\n");
  272.  
  273. // Initiate the query class instance
  274. MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
  275. // Execute the query with the PROGMEM option
  276. cur_mem->execute(query, true);
  277. // Show the results
  278. //cur_mem->show_results();
  279. // Deleting the cursor also frees up memory used
  280.  
  281. column_names *cols = cur_mem->get_columns();
  282. Serial.println();
  283. row_values *row = NULL;
  284. row = cur_mem->get_next_row();
  285.  
  286. Serial.println("\nThe Code:");
  287.  
  288. String thecode = row->values[0];
  289. Serial.println(thecode);
  290.  
  291. Serial.println("\nVerwijder SQL Memory");
  292. delete cur_mem;
  293. conn.close();
  294.  
  295.  
  296.  
  297.  
  298.  
  299. webServer.on("/", []() {
  300. webServer.send(200, "text/html", s);
  301. });
  302. webServer.on("/pass", []() {
  303. webServer.send(200, "text/html", v);
  304. });
  305.  
  306. webServer.on("/accepted", []() {
  307. for (int i = 0; i < 6; i++) {
  308. Serial.print(curr_mac.addr[i]);Serial.print(":");
  309. }
  310. Serial.println(" allowed");
  311.  
  312. if (max_client < MAX_CLIENTS) {
  313. allowed_macs[max_client++] = curr_mac;
  314. }
  315. webServer.send(200, "text/html", acceptedHTML);
  316. });
  317.  
  318. // redirect all other URIs to our "/"
  319. webServer.onNotFound([]() {
  320. webServer.sendHeader("Location", String("http://")+myIP.toString()+String("/"), true);
  321. webServer.send (302, "text/plain", "");
  322. });
  323. webServer.begin();
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340. }
  341.  
  342. void loop()
  343. {
  344. webServer.handleClient();
  345. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement