sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

slstatus-leakedip-20230105-c919def.diff (3664B)


      1 From 82914c53f777c3d69bb593a0108c6326f61a6d86 Mon Sep 17 00:00:00 2001
      2 From: Matvey <mathway.home@gmail.com>
      3 Date: Thu, 5 Jan 2023 03:34:23 +0300
      4 Subject: [PATCH] added leaked_ip function - ip as seen by internet
      5 
      6 ---
      7  components/ip.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++--
      8  config.def.h    |  1 +
      9  slstatus.h      |  1 +
     10  3 files changed, 80 insertions(+), 2 deletions(-)
     11 
     12 diff --git a/components/ip.c b/components/ip.c
     13 index 9476549..bed3003 100644
     14 --- a/components/ip.c
     15 +++ b/components/ip.c
     16 @@ -1,4 +1,6 @@
     17  /* See LICENSE file for copyright and license details. */
     18 +#include <errno.h>
     19 +#include <unistd.h>
     20  #include <ifaddrs.h>
     21  #include <netdb.h>
     22  #include <stdio.h>
     23 @@ -27,9 +29,9 @@ ip(const char *interface, unsigned short sa_family)
     24  	}
     25  
     26  	for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
     27 -		if (!ifa->ifa_addr)
     28 +		if (!ifa->ifa_addr) {
     29  			continue;
     30 -
     31 +		}
     32  		s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6),
     33  		                host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
     34  		if (!strcmp(ifa->ifa_name, interface) &&
     35 @@ -59,3 +61,77 @@ ipv6(const char *interface)
     36  {
     37  	return ip(interface, AF_INET6);
     38  }
     39 +
     40 +const char *
     41 +leaked_ip(void)
     42 +{
     43 +	static const char *check_domain = "ip-api.com";
     44 +	static const char *check_request =
     45 +		"GET /line/?fields=query,country HTTP/1.1\r\n"
     46 +		"Host: ip-api.com\r\n\r\n";
     47 +	static const char *bad_addr = "X.X.X.X(Unavailable)";
     48 +	
     49 +	struct addrinfo *ai;
     50 +	int remote_fd;
     51 +	char *p;
     52 +	int s, n;
     53 +	int gai_errno;
     54 +
     55 +	if ((gai_errno = getaddrinfo(check_domain, "http", NULL, &ai)))
     56 +	{
     57 +		warn("Can't resolve domain %s: %s", check_domain, gai_strerror(gai_errno));
     58 +		return bad_addr;
     59 +	}
     60 +	if ((remote_fd = socket(ai->ai_family, ai->ai_socktype, 0)) == -1)
     61 +	{
     62 +		freeaddrinfo(ai);
     63 +		warn("socket:");
     64 +		return bad_addr;
     65 +	}
     66 +	
     67 +	if (connect(remote_fd, ai->ai_addr, ai->ai_addrlen) == -1)
     68 +	{
     69 +		freeaddrinfo(ai);
     70 +		close(remote_fd);
     71 +		warn("connect:", check_domain);
     72 +		return bad_addr;
     73 +	}
     74 +	freeaddrinfo(ai);
     75 +
     76 +	// send request
     77 +	n = strlen(check_request);
     78 +	p = (char *) check_request;
     79 +	while (n)
     80 +	{
     81 +		s = write(remote_fd, p, n);
     82 +
     83 +		if (s == -1)
     84 +		{
     85 +			if (errno == EINTR)
     86 +				continue;
     87 +			close(remote_fd);
     88 +			warn("write:");
     89 +			return bad_addr;
     90 +		}
     91 +		n -= s;
     92 +		p += s;
     93 +	}
     94 +	
     95 +	p = buf;
     96 +	n = sizeof(buf);
     97 +	s = read(remote_fd, p, n);
     98 +	close(remote_fd);
     99 +	p = strstr(buf, "\r\n\r\n");
    100 +	if (!p)
    101 +	{
    102 +		warn("Can't resolve %s: Bad response from server", check_domain);
    103 +		return bad_addr;
    104 +	}
    105 +	p += 4;
    106 +	sscanf(p, "%*s%s", buf);
    107 +	strcat(buf, "(");
    108 +	sscanf(p, "%s", buf+strlen(buf));
    109 +	strcat(buf, ")");
    110 +	
    111 +	return buf;
    112 +}
    113 diff --git a/config.def.h b/config.def.h
    114 index d805331..75c282b 100644
    115 --- a/config.def.h
    116 +++ b/config.def.h
    117 @@ -31,6 +31,7 @@ static const char unknown_str[] = "n/a";
    118   * hostname            hostname                        NULL
    119   * ipv4                IPv4 address                    interface name (eth0)
    120   * ipv6                IPv6 address                    interface name (eth0)
    121 + * leaked_ip           IP address leaked to Internet   NULL
    122   * kernel_release      `uname -r`                      NULL
    123   * keyboard_indicators caps/num lock indicators        format string (c?n?)
    124   *                                                     see keyboard_indicators.c
    125 diff --git a/slstatus.h b/slstatus.h
    126 index 8ef5874..6f67ccd 100644
    127 --- a/slstatus.h
    128 +++ b/slstatus.h
    129 @@ -30,6 +30,7 @@ const char *hostname(const char *unused);
    130  /* ip */
    131  const char *ipv4(const char *interface);
    132  const char *ipv6(const char *interface);
    133 +const char *leaked_ip(void);
    134  
    135  /* kernel_release */
    136  const char *kernel_release(const char *unused);
    137 -- 
    138 2.35.1
    139