mirror of
https://github.com/taigrr/tplinkController
synced 2025-01-18 04:43:13 -08:00
Compare commits
18 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
68931c71bd | ||
|
60cc5c8bb5 | ||
|
b42c1e293c | ||
|
0f1f472cf0 | ||
|
de6ac2b222 | ||
|
fbe8da7a4b | ||
|
b24673100f | ||
|
4ca74c5eba | ||
|
c57babc9b1 | ||
|
80581e5c9c | ||
|
4be0d86405 | ||
|
5d16c96ef0 | ||
|
94d4f10d87 | ||
|
4bbecdaea0 | ||
|
f870b6458d | ||
|
e24f5d53f1 | ||
|
89f819707d | ||
|
2e1fe081a1 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
hs100
|
hs100
|
||||||
*.o
|
*.o
|
||||||
*.swp
|
*.swp
|
||||||
|
*~
|
||||||
|
1
Makefile
1
Makefile
@ -1,6 +1,7 @@
|
|||||||
target ?= hs100
|
target ?= hs100
|
||||||
objects := $(patsubst %.c,%.o,$(wildcard *.c))
|
objects := $(patsubst %.c,%.o,$(wildcard *.c))
|
||||||
|
|
||||||
|
LDFLAGS=-lm
|
||||||
CFLAGS=-std=c99 -Os
|
CFLAGS=-std=c99 -Os
|
||||||
ifdef DEBUG
|
ifdef DEBUG
|
||||||
CFLAGS+=-Wall -Werror -ggdb
|
CFLAGS+=-Wall -Werror -ggdb
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
A tool for using TP-Link HS100/HS105/HS110 wi-fi smart plugs. You can turn
|
A tool for using TP-Link HS100/HS105/HS110 wi-fi smart plugs. You can turn
|
||||||
them on and off, reboot them, and so on. You can even set them up without
|
them on and off, reboot them, and so on. You can even set them up without
|
||||||
using the app (see Initial Setup).
|
using TP-Link's app (see Initial Setup).
|
||||||
|
|
||||||
Tested to work on Linux, OSX, IRIX, and Windows under WSL.
|
Tested to work on Linux, OSX, IRIX, and Windows under WSL.
|
||||||
|
|
||||||
@ -51,7 +51,7 @@ plug's AP.
|
|||||||
- Disable cloud nonsense by setting a bogus server URL: `hs100 192.168.0.1 set_server localhost`
|
- Disable cloud nonsense by setting a bogus server URL: `hs100 192.168.0.1 set_server localhost`
|
||||||
- Scan for your wifi AP using `hs100 192.168.0.1 scan`. Find your AP in the
|
- Scan for your wifi AP using `hs100 192.168.0.1 scan`. Find your AP in the
|
||||||
list and note its `key_type`; you will need this to associate.
|
list and note its `key_type`; you will need this to associate.
|
||||||
- Associate with your AP using `hs100 192.168.0.1 <ssid> <password> <key_type>`
|
- Associate with your AP using `hs100 192.168.0.1 associate <ssid> <password> <key_type>`
|
||||||
. Your key\_type is a number that indicates the kind of wifi security that
|
. Your key\_type is a number that indicates the kind of wifi security that
|
||||||
your AP is using. You can find it by doing a wifi scan (see previous step).
|
your AP is using. You can find it by doing a wifi scan (see previous step).
|
||||||
|
|
||||||
|
117
comms.c
117
comms.c
@ -1,27 +1,28 @@
|
|||||||
#include <stddef.h>
|
#include <arpa/inet.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <stddef.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <arpa/inet.h>
|
#include <string.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "comms.h"
|
#include "comms.h"
|
||||||
|
|
||||||
#define RECV_BUF_SIZE 4096
|
|
||||||
|
|
||||||
bool hs100_encrypt(uint8_t *d, uint8_t *s, size_t len)
|
bool hs100_encrypt(uint8_t *d, uint8_t *s, size_t len)
|
||||||
{
|
{
|
||||||
if(d == NULL)
|
uint8_t key, temp;
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
if (d == NULL)
|
||||||
return false;
|
return false;
|
||||||
if(s == NULL)
|
if (s == NULL)
|
||||||
return false;
|
return false;
|
||||||
if(len == 0)
|
if (len == 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
uint8_t key = 0xab;
|
key = 0xab;
|
||||||
for(size_t i=0; i<len; i++) {
|
for (i = 0; i < len; i++) {
|
||||||
uint8_t temp = key ^ s[i];
|
temp = key ^ s[i];
|
||||||
key = temp;
|
key = temp;
|
||||||
d[i] = temp;
|
d[i] = temp;
|
||||||
}
|
}
|
||||||
@ -30,16 +31,19 @@ bool hs100_encrypt(uint8_t *d, uint8_t *s, size_t len)
|
|||||||
|
|
||||||
bool hs100_decrypt(uint8_t *d, uint8_t *s, size_t len)
|
bool hs100_decrypt(uint8_t *d, uint8_t *s, size_t len)
|
||||||
{
|
{
|
||||||
if(d == NULL)
|
uint8_t key, temp;
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
if (d == NULL)
|
||||||
return false;
|
return false;
|
||||||
if(s == NULL)
|
if (s == NULL)
|
||||||
return false;
|
return false;
|
||||||
if(len == 0)
|
if (len == 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
uint8_t key = 0xab;
|
key = 0xab;
|
||||||
for(size_t i=0; i<len; i++) {
|
for (i = 0; i < len; i++) {
|
||||||
uint8_t temp = key ^ s[i];
|
temp = key ^ s[i];
|
||||||
key = s[i];
|
key = s[i];
|
||||||
d[i] = temp;
|
d[i] = temp;
|
||||||
}
|
}
|
||||||
@ -48,17 +52,23 @@ bool hs100_decrypt(uint8_t *d, uint8_t *s, size_t len)
|
|||||||
|
|
||||||
uint8_t *hs100_encode(size_t *outlen, char *srcmsg)
|
uint8_t *hs100_encode(size_t *outlen, char *srcmsg)
|
||||||
{
|
{
|
||||||
if(srcmsg == NULL) return NULL;
|
size_t srcmsg_len;
|
||||||
|
uint8_t *d;
|
||||||
|
uint32_t temp;
|
||||||
|
|
||||||
size_t srcmsg_len = strlen(srcmsg);
|
if (srcmsg == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
srcmsg_len = strlen(srcmsg);
|
||||||
*outlen = srcmsg_len + 4;
|
*outlen = srcmsg_len + 4;
|
||||||
uint8_t *d = calloc(1, *outlen);
|
d = calloc(1, *outlen);
|
||||||
if(d == NULL) return NULL;
|
if (d == NULL)
|
||||||
if(!hs100_encrypt(d+4, (uint8_t *)srcmsg, srcmsg_len)) {
|
return NULL;
|
||||||
|
if (!hs100_encrypt(d + 4, (uint8_t *) srcmsg, srcmsg_len)) {
|
||||||
free(d);
|
free(d);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
uint32_t temp = htonl(srcmsg_len);
|
temp = htonl(srcmsg_len);
|
||||||
memcpy(d, &temp, 4);
|
memcpy(d, &temp, 4);
|
||||||
|
|
||||||
return d;
|
return d;
|
||||||
@ -66,20 +76,24 @@ uint8_t *hs100_encode(size_t *outlen, char *srcmsg)
|
|||||||
|
|
||||||
char *hs100_decode(uint8_t *s, size_t s_len)
|
char *hs100_decode(uint8_t *s, size_t s_len)
|
||||||
{
|
{
|
||||||
if(s == NULL) return NULL;
|
|
||||||
if(s_len <= 4) return NULL;
|
|
||||||
|
|
||||||
uint32_t in_s_len;
|
uint32_t in_s_len;
|
||||||
|
char *outbuf;
|
||||||
|
|
||||||
|
if (s == NULL)
|
||||||
|
return NULL;
|
||||||
|
if (s_len <= 4)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
memcpy(&in_s_len, s, 4);
|
memcpy(&in_s_len, s, 4);
|
||||||
in_s_len = ntohl(in_s_len);
|
in_s_len = ntohl(in_s_len);
|
||||||
if((s_len - 4) < in_s_len) {
|
if ((s_len - 4) < in_s_len) {
|
||||||
// packet was cut short- adjust in_s_len
|
/* packet was cut short- adjust in_s_len */
|
||||||
in_s_len = s_len - 4;
|
in_s_len = s_len - 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *outbuf = calloc(1,in_s_len+1);
|
outbuf = calloc(1, in_s_len + 1);
|
||||||
|
|
||||||
if(!hs100_decrypt((uint8_t*)outbuf, s+4, in_s_len)) {
|
if (!hs100_decrypt((uint8_t *) outbuf, s + 4, in_s_len)) {
|
||||||
free(outbuf);
|
free(outbuf);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -90,33 +104,44 @@ char *hs100_decode(uint8_t *s, size_t s_len)
|
|||||||
char *hs100_send(char *servaddr, char *msg)
|
char *hs100_send(char *servaddr, char *msg)
|
||||||
{
|
{
|
||||||
size_t s_len;
|
size_t s_len;
|
||||||
uint8_t *s = hs100_encode(&s_len, msg);
|
int sock;
|
||||||
if(s == NULL)
|
uint8_t *s, *recvbuf;
|
||||||
|
struct sockaddr_in address;
|
||||||
|
uint32_t msglen;
|
||||||
|
size_t recvsize;
|
||||||
|
char *recvmsg;
|
||||||
|
|
||||||
|
s = hs100_encode(&s_len, msg);
|
||||||
|
if (s == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
if (sock < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
int sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
||||||
if(sock < 0) return NULL;
|
|
||||||
|
|
||||||
struct sockaddr_in address;
|
|
||||||
memset(&address, '0', sizeof(struct sockaddr_in));
|
memset(&address, '0', sizeof(struct sockaddr_in));
|
||||||
|
|
||||||
address.sin_family = AF_INET;
|
address.sin_family = AF_INET;
|
||||||
address.sin_port = htons(9999);
|
address.sin_port = htons(9999);
|
||||||
|
|
||||||
if(inet_pton(AF_INET, servaddr, &address.sin_addr)<=0)
|
if (inet_pton(AF_INET, servaddr, &address.sin_addr) <= 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if(connect(sock, (struct sockaddr *)&address,
|
if (connect(sock, (struct sockaddr *)&address,
|
||||||
sizeof(struct sockaddr_in)) < 0)
|
sizeof(struct sockaddr_in)) < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
send(sock, s, s_len, 0);
|
send(sock, s, s_len, 0);
|
||||||
free(s);
|
free(s);
|
||||||
uint8_t recvbuf[RECV_BUF_SIZE];
|
recvsize = recv(sock, &msglen, sizeof(msglen), MSG_PEEK);
|
||||||
size_t received_size = recv(sock, recvbuf, RECV_BUF_SIZE, 0);
|
if (recvsize != sizeof(msglen)) {
|
||||||
close(sock);
|
|
||||||
if(received_size == 0)
|
|
||||||
return NULL;
|
return NULL;
|
||||||
char *recvmsg = hs100_decode(recvbuf, received_size);
|
}
|
||||||
|
msglen = ntohl(msglen) + 4;
|
||||||
|
recvbuf = calloc(1, (size_t) msglen);
|
||||||
|
recvsize = recv(sock, recvbuf, msglen, MSG_WAITALL);
|
||||||
|
close(sock);
|
||||||
|
recvmsg = hs100_decode(recvbuf, msglen);
|
||||||
|
free(recvbuf);
|
||||||
return recvmsg;
|
return recvmsg;
|
||||||
}
|
}
|
||||||
|
65
handlers.c
Normal file
65
handlers.c
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
#include <errno.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "comms.h"
|
||||||
|
|
||||||
|
char *handler_associate(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
const char *template =
|
||||||
|
"{\"netif\":{\"set_stainfo\":{\"ssid\":\"%s\",\"password\":"
|
||||||
|
"\"%s\",\"key_type\":%d}}}";
|
||||||
|
char *plug_addr = argv[1];
|
||||||
|
char *ssid = argv[3];
|
||||||
|
char *password = argv[4];
|
||||||
|
char *key_type = argv[5];
|
||||||
|
char *endptr, *msg, *response;
|
||||||
|
int key_type_num;
|
||||||
|
size_t len;
|
||||||
|
|
||||||
|
if (argc < 6) {
|
||||||
|
fprintf(stderr, "not enough arguments\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
errno = 0;
|
||||||
|
key_type_num = (int)strtol(key_type, &endptr, 10);
|
||||||
|
if (errno || endptr == key_type) {
|
||||||
|
fprintf(stderr, "invalid key type: %s\n", key_type);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
len = snprintf(NULL, 0, template, ssid, password,
|
||||||
|
key_type_num);
|
||||||
|
len++; /* snprintf does not count the null terminator */
|
||||||
|
|
||||||
|
msg = calloc(1, len);
|
||||||
|
snprintf(msg, len, template, ssid, password, key_type_num);
|
||||||
|
|
||||||
|
response = hs100_send(plug_addr, msg);
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *handler_set_server(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
const char *template =
|
||||||
|
"{\"cnCloud\":{\"set_server_url\":{\"server\":\"%s\"}}}";
|
||||||
|
char *plug_addr = argv[1];
|
||||||
|
char *server = argv[3];
|
||||||
|
size_t len;
|
||||||
|
char *msg, *response;
|
||||||
|
|
||||||
|
if (argc < 4) {
|
||||||
|
fprintf(stderr, "not enough arguments\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
len = snprintf(NULL, 0, template, server);
|
||||||
|
len++; /* snprintf does not count the null terminator */
|
||||||
|
|
||||||
|
msg = calloc(1, len);
|
||||||
|
snprintf(msg, len, template, server);
|
||||||
|
|
||||||
|
response = hs100_send(plug_addr, msg);
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
94
hs100.c
94
hs100.c
@ -1,75 +1,18 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <inttypes.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
|
||||||
#include "version.h"
|
#include "version.h"
|
||||||
#include "comms.h"
|
#include "comms.h"
|
||||||
|
|
||||||
char *handler_associate(int argc, char *argv[])
|
// handlers for more complicated commands
|
||||||
{
|
extern char *handler_associate(int argc, char *argv[]);
|
||||||
if(argc < 6) {
|
extern char *handler_set_server(int argc, char *argv[]);
|
||||||
fprintf(stderr, "not enough arguments\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
char *plug_addr = argv[1];
|
|
||||||
char *ssid = argv[3];
|
|
||||||
char *password = argv[4];
|
|
||||||
char *key_type = argv[5];
|
|
||||||
|
|
||||||
errno = 0;
|
|
||||||
char *endptr;
|
|
||||||
int key_type_num = (int)strtol(key_type, &endptr, 10);
|
|
||||||
if(errno || endptr == key_type) {
|
|
||||||
fprintf(stderr, "invalid key type: %s\n", key_type);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *template =
|
|
||||||
"{\"netif\":{\"set_stainfo\":{\"ssid\":\"%s\",\"password\":"
|
|
||||||
"\"%s\",\"key_type\":%d}}}";
|
|
||||||
|
|
||||||
size_t len = snprintf(NULL, 0, template, ssid, password,
|
|
||||||
key_type_num);
|
|
||||||
len++; // snprintf does not count the null terminator
|
|
||||||
|
|
||||||
char *msg = calloc(1, len);
|
|
||||||
snprintf(msg, len, template, ssid, password, key_type_num);
|
|
||||||
|
|
||||||
char *response = hs100_send(plug_addr, msg);
|
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *handler_set_server(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
if(argc < 4) {
|
|
||||||
fprintf(stderr, "not enough arguments\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
char *plug_addr = argv[1];
|
|
||||||
char *server = argv[3];
|
|
||||||
|
|
||||||
const char *template =
|
|
||||||
"{\"cnCloud\":{\"set_server_url\":{\"server\":\"%s\"}}}";
|
|
||||||
|
|
||||||
size_t len = snprintf(NULL, 0, template, server);
|
|
||||||
len++; // snprintf does not count the null terminator
|
|
||||||
|
|
||||||
char *msg = calloc(1, len);
|
|
||||||
snprintf(msg, len, template, server);
|
|
||||||
|
|
||||||
char *response = hs100_send(plug_addr, msg);
|
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
struct cmd_s {
|
struct cmd_s {
|
||||||
char *command;
|
char *command;
|
||||||
char *help;
|
char *help;
|
||||||
char *json;
|
char *json;
|
||||||
char *(*handler)(int argc, char *argv[]);
|
char *(*handler) (int argc, char *argv[]);
|
||||||
int end;
|
int end;
|
||||||
};
|
};
|
||||||
struct cmd_s cmds[] = {
|
struct cmd_s cmds[] = {
|
||||||
@ -124,9 +67,8 @@ struct cmd_s cmds[] = {
|
|||||||
struct cmd_s *get_cmd_from_name(char *needle)
|
struct cmd_s *get_cmd_from_name(char *needle)
|
||||||
{
|
{
|
||||||
int cmds_index = 0;
|
int cmds_index = 0;
|
||||||
while(!cmds[cmds_index].end)
|
while (!cmds[cmds_index].end) {
|
||||||
{
|
if (!strcmp(cmds[cmds_index].command, needle))
|
||||||
if(!strcmp(cmds[cmds_index].command, needle))
|
|
||||||
return &cmds[cmds_index];
|
return &cmds[cmds_index];
|
||||||
cmds_index++;
|
cmds_index++;
|
||||||
}
|
}
|
||||||
@ -135,16 +77,14 @@ struct cmd_s *get_cmd_from_name(char *needle)
|
|||||||
|
|
||||||
void print_usage()
|
void print_usage()
|
||||||
{
|
{
|
||||||
fprintf(stderr, "hs100 version " VERSION_STRING ", Copyright (C) 2018 Jason Benaim.\n"
|
fprintf(stderr, "hs100 version " VERSION_STRING
|
||||||
"A tool for using certain wifi smart plugs.\n"
|
", Copyright (C) 2018-2019 Jason Benaim.\n"
|
||||||
"\n"
|
"A tool for using certain wifi smart plugs.\n\n"
|
||||||
"usage: hs100 <ip> <command>\n"
|
"usage: hs100 <ip> <command>\n\n"
|
||||||
"\n"
|
|
||||||
"Commands:\n"
|
"Commands:\n"
|
||||||
);
|
);
|
||||||
int cmds_index = 0;
|
int cmds_index = 0;
|
||||||
while(!cmds[cmds_index].end)
|
while (!cmds[cmds_index].end) {
|
||||||
{
|
|
||||||
fprintf(stderr, "\t%s\n\n", cmds[cmds_index].help);
|
fprintf(stderr, "\t%s\n\n", cmds[cmds_index].help);
|
||||||
cmds_index++;
|
cmds_index++;
|
||||||
}
|
}
|
||||||
@ -153,7 +93,7 @@ void print_usage()
|
|||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
if(argc < 3) {
|
if (argc < 3) {
|
||||||
print_usage();
|
print_usage();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -162,17 +102,17 @@ int main(int argc, char *argv[])
|
|||||||
char *response = NULL;
|
char *response = NULL;
|
||||||
|
|
||||||
struct cmd_s *cmd = get_cmd_from_name(cmd_string);
|
struct cmd_s *cmd = get_cmd_from_name(cmd_string);
|
||||||
if(cmd != NULL) {
|
if (cmd != NULL) {
|
||||||
if(cmd->handler != NULL)
|
if (cmd->handler != NULL)
|
||||||
response = cmd->handler(argc, argv);
|
response = cmd->handler(argc, argv);
|
||||||
else if(cmd->json != NULL)
|
else if (cmd->json != NULL)
|
||||||
response = hs100_send(plug_addr, cmd->json);
|
response = hs100_send(plug_addr, cmd->json);
|
||||||
} else {
|
} else {
|
||||||
// command not recognized, so send it to the plug raw
|
// command not recognized, so send it to the plug raw
|
||||||
response = hs100_send(plug_addr, cmd_string);
|
response = hs100_send(plug_addr, cmd_string);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(response == NULL) {
|
if (response == NULL) {
|
||||||
fprintf(stderr, "failed to send command\n");
|
fprintf(stderr, "failed to send command\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user