mirror of
https://github.com/shadowsocks/shadowsocks-libev.git
synced 2026-10-22 06:53:46 +00:00
Add list API to list service ports (#1521)
* Fix #1518 config file with wrong json format Removing trailing "," to make the configuration file compliant with JSON specification * Remove extra "," in json file * server_port should be integer instead of string server_port in generated json configuration file should be integer instead of string * Add list active service ports feature A new management API has been added to list all active service ports. * Make list API output JSON compliant In the following format: [ { "server_port":"8388","password":"password"}, ... ] Note that server_port value is string instead of integer for easy display on the console. * Make safe room for the new list entry "\n\t{\"server_port\":\"%s\",\"password\":\"%s\"}," Reserve 50 char spaces for the extra characters in the above.
This commit is contained in:
@@ -736,6 +736,36 @@ manager_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
if (sendto(manager->fd, msg, msg_len, 0, (struct sockaddr *)&claddr, len) != 2) {
|
||||
ERROR("add_sendto");
|
||||
}
|
||||
} else if (strcmp(action, "list") == 0) {
|
||||
struct cork_hash_table_iterator iter;
|
||||
struct cork_hash_table_entry *entry;
|
||||
char buf[BUF_SIZE];
|
||||
memset(buf, 0, BUF_SIZE);
|
||||
sprintf(buf, "[");
|
||||
|
||||
cork_hash_table_iterator_init(server_table, &iter);
|
||||
while ((entry = cork_hash_table_iterator_next(&iter)) != NULL) {
|
||||
struct server *server = (struct server *)entry->value;
|
||||
size_t pos = strlen(buf);
|
||||
size_t entry_len = strlen(server->port)+strlen(server->password);
|
||||
if (pos > BUF_SIZE-entry_len-50) {
|
||||
if (sendto(manager->fd, buf, pos, 0, (struct sockaddr *)&claddr, len)
|
||||
!= pos) {
|
||||
ERROR("list_sendto");
|
||||
}
|
||||
memset(buf, 0, BUF_SIZE);
|
||||
pos = 0;
|
||||
}
|
||||
sprintf(buf + pos, "\n\t{\"server_port\":\"%s\",\"password\":\"%s\"},", server->port,server->password);
|
||||
}
|
||||
|
||||
size_t pos = strlen(buf);
|
||||
strcpy(buf + pos - 1, "\n]"); //Remove trailing ","
|
||||
pos = strlen(buf);
|
||||
if (sendto(manager->fd, buf, pos, 0, (struct sockaddr *)&claddr, len)
|
||||
!= pos) {
|
||||
ERROR("list_sendto");
|
||||
}
|
||||
} else if (strcmp(action, "remove") == 0) {
|
||||
struct server *server = get_server(buf, r);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user