Skip to content

Commit

Permalink
add wifi_secure_mode, boots in insecure mode
Browse files Browse the repository at this point in the history
  • Loading branch information
geohot committed Jan 12, 2018
1 parent 2573d86 commit 38cc0ee
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 40 deletions.
80 changes: 45 additions & 35 deletions boardesp/proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
_a > _b ? _a : _b; })

char ssid[32];
char password[] = "testing123";
int wifi_secure_mode = 0;

static const int pin = 2;

// Structure holding the TCP connection information.
Expand Down Expand Up @@ -218,11 +221,50 @@ void ICACHE_FLASH_ATTR inter_recv_cb(void *arg, char *pusrdata, unsigned short l
}
}

void ICACHE_FLASH_ATTR wifi_configure(int secure) {
wifi_secure_mode = secure;

// start wifi AP
wifi_set_opmode(SOFTAP_MODE);
struct softap_config config = {0};
wifi_softap_get_config(&config);
strcpy(config.ssid, ssid);
if (wifi_secure_mode == 0) strcat(config.ssid, "-pair");
strcpy(config.password, password);
config.ssid_len = strlen(config.ssid);
config.authmode = wifi_secure_mode ? AUTH_WPA2_PSK : AUTH_OPEN;
config.beacon_interval = 100;
config.max_connection = 4;
wifi_softap_set_config(&config);

if (wifi_secure_mode) {
// setup tcp server
tcp_proto.local_port = 1337;
tcp_conn.type = ESPCONN_TCP;
tcp_conn.state = ESPCONN_NONE;
tcp_conn.proto.tcp = &tcp_proto;
espconn_regist_connectcb(&tcp_conn, tcp_connect_cb);
espconn_accept(&tcp_conn);
espconn_regist_time(&tcp_conn, 60, 0); // 60s timeout for all connections

// setup inter server
inter_proto.local_port = 1338;
const char udp_remote_ip[4] = {255, 255, 255, 255};
os_memcpy(inter_proto.remote_ip, udp_remote_ip, 4);
inter_proto.remote_port = 1338;

inter_conn.type = ESPCONN_UDP;
inter_conn.proto.udp = &inter_proto;

espconn_regist_recvcb(&inter_conn, inter_recv_cb);
espconn_create(&inter_conn);
}
}

void ICACHE_FLASH_ATTR wifi_init() {
// default ssid and password
memset(ssid, 0, 32);
os_sprintf(ssid, "panda-%08x-BROKEN", system_get_chip_id());
char password[] = "testing123";

// fetch secure ssid and password
// update, try 20 times, for 1 second
Expand All @@ -242,19 +284,7 @@ void ICACHE_FLASH_ATTR wifi_init() {
os_delay_us(50000);
}

// start wifi AP
wifi_set_opmode(SOFTAP_MODE);
struct softap_config config;
wifi_softap_get_config(&config);
strcpy(config.ssid, ssid);
strcpy(config.password, password);
config.ssid_len = strlen(ssid);
config.authmode = AUTH_WPA2_PSK;
config.beacon_interval = 100;
config.max_connection = 4;
wifi_softap_set_config(&config);

//set IP
// set IP
wifi_softap_dhcps_stop(); //stop DHCP before setting static IP
struct ip_info ip_config;
IP4_ADDR(&ip_config.ip, 192, 168, 0, 10);
Expand All @@ -265,27 +295,7 @@ void ICACHE_FLASH_ATTR wifi_init() {
wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, &stupid_gateway);
wifi_softap_dhcps_start();

// setup tcp server
tcp_proto.local_port = 1337;
tcp_conn.type = ESPCONN_TCP;
tcp_conn.state = ESPCONN_NONE;
tcp_conn.proto.tcp = &tcp_proto;
espconn_regist_connectcb(&tcp_conn, tcp_connect_cb);
espconn_accept(&tcp_conn);
espconn_regist_time(&tcp_conn, 60, 0); // 60s timeout for all connections

// setup inter server
inter_proto.local_port = 1338;
const char udp_remote_ip[4] = {255, 255, 255, 255};
os_memcpy(inter_proto.remote_ip, udp_remote_ip, 4);
inter_proto.remote_port = 1338;

inter_conn.type = ESPCONN_UDP;
inter_conn.proto.udp = &inter_proto;

espconn_regist_recvcb(&inter_conn, inter_recv_cb);

espconn_create(&inter_conn);
wifi_configure(0);
}

#define LOOP_PRIO 2
Expand Down
12 changes: 7 additions & 5 deletions boardesp/webserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ char OK_header[] = "HTTP/1.0 200 OK\nContent-Type: text/html\n\n";
static struct espconn web_conn;
static esp_tcp web_proto;
extern char ssid[];
extern int wifi_secure_mode;

char *st_firmware;
int real_content_length, content_length = 0;
Expand Down Expand Up @@ -215,8 +216,9 @@ static void ICACHE_FLASH_ATTR web_rx_cb(void *arg, char *data, uint16_t len) {

espconn_send_string(&web_conn, resp);
espconn_disconnect(conn);

} else if (memcmp(data, "GET /set_property?usb_mode=", 27) == 0) {
} else if (memcmp(data, "GET /secure", 11) == 0 && !wifi_secure_mode) {
wifi_configure(1);
} else if (memcmp(data, "GET /set_property?usb_mode=", 27) == 0 && wifi_secure_mode) {
char mode_value = data[27] - '0';
if (mode_value >= '\x00' && mode_value <= '\x02') {
memset(resp, 0, MAX_RESP);
Expand All @@ -228,7 +230,7 @@ static void ICACHE_FLASH_ATTR web_rx_cb(void *arg, char *data, uint16_t len) {
espconn_send_string(&web_conn, resp);
espconn_disconnect(conn);
}
} else if (memcmp(data, "PUT /stupdate ", 14) == 0) {
} else if (memcmp(data, "PUT /stupdate ", 14) == 0 && wifi_secure_mode) {
os_printf("init st firmware\n");
char *cl = strstr(data, "Content-Length: ");
if (cl != NULL) {
Expand All @@ -244,8 +246,8 @@ static void ICACHE_FLASH_ATTR web_rx_cb(void *arg, char *data, uint16_t len) {
state = RECEIVING_ST_FIRMWARE;
}

} else if ((memcmp(data, "PUT /espupdate1 ", 16) == 0) ||
(memcmp(data, "PUT /espupdate2 ", 16) == 0)) {
} else if (((memcmp(data, "PUT /espupdate1 ", 16) == 0) ||
(memcmp(data, "PUT /espupdate2 ", 16) == 0)) && wifi_secure_mode) {
// 0x1000 = user1.bin
// 0x81000 = user2.bin
// 0x3FE000 = blank.bin
Expand Down

0 comments on commit 38cc0ee

Please sign in to comment.