CI / test (push) Failing after 50s
- remote/: shared-memory framebuffer mirror served as RFB 3.8 (VNC, port 5900) and an HTTP/WebSocket PWA (port 6080), bound to the Tailscale interface and loopback by default - Compositor: remote pointer, button, scroll, modifier, key, key-tap, text and clipboard input, routed past OLED blanking - chandra-ctl: add status, refresh and osk commands; add input-* and clipboard-set for remote control - Camera: debayer 10-bit CSI-2 Quad-Bayer frames for imx371 and high-quality still capture - Camera: rotate frames by the libcamera mounting angle (CCW for 90), fix the expected test values, and restyle the UI for a dark theme
768 lines
25 KiB
C
768 lines
25 KiB
C
#include "remote_internal.h"
|
|
|
|
#include <arpa/inet.h>
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <ifaddrs.h>
|
|
#include <net/if.h>
|
|
#include <openssl/rand.h>
|
|
#include <poll.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/mman.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/time.h>
|
|
#include <sys/un.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
static volatile sig_atomic_t g_running = 1;
|
|
static volatile sig_atomic_t g_reload = 0;
|
|
|
|
static void on_term(int sig) {
|
|
(void)sig;
|
|
g_running = 0;
|
|
}
|
|
|
|
static void on_hup(int sig) {
|
|
(void)sig;
|
|
g_reload = 1;
|
|
}
|
|
|
|
uint64_t remote_now_ms(void) {
|
|
struct timespec ts;
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
return (uint64_t)ts.tv_sec * 1000ULL + (uint64_t)ts.tv_nsec / 1000000ULL;
|
|
}
|
|
|
|
void remote_sleep_ms(int ms) {
|
|
if (ms <= 0) return;
|
|
struct timespec req = { .tv_sec = ms / 1000, .tv_nsec = (long)(ms % 1000) * 1000000L };
|
|
while (nanosleep(&req, &req) < 0 && errno == EINTR && g_running) {}
|
|
}
|
|
|
|
/* ---- Control socket client ---- */
|
|
|
|
static int unix_connect_path(const char *path) {
|
|
int fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
|
|
if (fd < 0) return -1;
|
|
struct timeval tv = { .tv_sec = 2, .tv_usec = 0 };
|
|
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof tv);
|
|
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof tv);
|
|
struct sockaddr_un sa;
|
|
memset(&sa, 0, sizeof sa);
|
|
sa.sun_family = AF_UNIX;
|
|
if (strlen(path) >= sizeof sa.sun_path) { close(fd); return -1; }
|
|
snprintf(sa.sun_path, sizeof sa.sun_path, "%s", path);
|
|
if (connect(fd, (struct sockaddr *)&sa, sizeof sa) < 0) {
|
|
close(fd);
|
|
return -1;
|
|
}
|
|
return fd;
|
|
}
|
|
|
|
static int resolve_control_socket(char *out, size_t cap) {
|
|
const char *env = getenv("CHANDRA_CONTROL");
|
|
if (env && env[0]) { snprintf(out, cap, "%s", env); return 0; }
|
|
char rt[256];
|
|
const char *xrt = getenv("XDG_RUNTIME_DIR");
|
|
if (xrt && xrt[0]) snprintf(rt, sizeof rt, "%s", xrt);
|
|
else snprintf(rt, sizeof rt, "/run/user/%u", (unsigned)getuid());
|
|
const char *wd = getenv("WAYLAND_DISPLAY");
|
|
if (wd && wd[0]) {
|
|
const char *base = strrchr(wd, '/');
|
|
base = base ? base + 1 : wd;
|
|
snprintf(out, cap, "%s/chandra/%s.sock", rt, base);
|
|
if (access(out, F_OK) == 0) return 0;
|
|
}
|
|
snprintf(out, cap, "%s/chandra/wayland-moon-0.sock", rt);
|
|
return 0;
|
|
}
|
|
|
|
static int resolve_shm_path(char *out, size_t cap) {
|
|
const char *env = getenv("CHANDRA_FB_SHM");
|
|
if (env && env[0]) { snprintf(out, cap, "%s", env); return 0; }
|
|
char rt[256];
|
|
const char *xrt = getenv("XDG_RUNTIME_DIR");
|
|
if (xrt && xrt[0]) snprintf(rt, sizeof rt, "%s", xrt);
|
|
else snprintf(rt, sizeof rt, "/run/user/%u", (unsigned)getuid());
|
|
snprintf(out, cap, "%s/chandra/fb.shm", rt);
|
|
return 0;
|
|
}
|
|
|
|
static void default_config_path(char *out, size_t cap) {
|
|
const char *xc = getenv("XDG_CONFIG_HOME");
|
|
if (xc && xc[0]) {
|
|
snprintf(out, cap, "%s/chandra/remote.conf", xc);
|
|
return;
|
|
}
|
|
const char *home = getenv("HOME");
|
|
snprintf(out, cap, "%s/.config/chandra/remote.conf", home && home[0] ? home : "/tmp");
|
|
}
|
|
|
|
static void default_pid_path(char *out, size_t cap) {
|
|
const char *xrt = getenv("XDG_RUNTIME_DIR");
|
|
if (xrt && xrt[0]) snprintf(out, cap, "%s/chandra/remote.pid", xrt);
|
|
else snprintf(out, cap, "/run/user/%u/chandra/remote.pid", (unsigned)getuid());
|
|
}
|
|
|
|
void ctl_init(CtlConn *c, const char *sock_path) {
|
|
pthread_mutex_init(&c->mu, NULL);
|
|
c->fd = -1;
|
|
c->last_try_ms = 0;
|
|
snprintf(c->sock_path, sizeof c->sock_path, "%s", sock_path);
|
|
}
|
|
|
|
void ctl_close(CtlConn *c) {
|
|
pthread_mutex_lock(&c->mu);
|
|
if (c->fd >= 0) { close(c->fd); c->fd = -1; }
|
|
pthread_mutex_unlock(&c->mu);
|
|
}
|
|
|
|
static int ctl_once_locked(CtlConn *c, const char *line, char *reply, size_t reply_cap) {
|
|
if (c->fd < 0) {
|
|
uint64_t now = remote_now_ms();
|
|
if (now - c->last_try_ms < 250) return -1;
|
|
c->last_try_ms = now;
|
|
c->fd = unix_connect_path(c->sock_path);
|
|
if (c->fd < 0) return -1;
|
|
}
|
|
size_t l = strlen(line);
|
|
char *msg = malloc(l + 2);
|
|
if (!msg) return -1;
|
|
memcpy(msg, line, l);
|
|
msg[l] = '\n';
|
|
size_t sent = 0;
|
|
while (sent < l + 1) {
|
|
ssize_t w = send(c->fd, msg + sent, l + 1 - sent, MSG_NOSIGNAL);
|
|
if (w <= 0) {
|
|
free(msg);
|
|
close(c->fd);
|
|
c->fd = -1;
|
|
c->last_try_ms = 0;
|
|
return -1;
|
|
}
|
|
sent += (size_t)w;
|
|
}
|
|
free(msg);
|
|
|
|
char rbuf[4096];
|
|
size_t rlen = 0;
|
|
int ok = -1;
|
|
for (;;) {
|
|
ssize_t r = recv(c->fd, rbuf + rlen, sizeof rbuf - 1 - rlen, 0);
|
|
if (r <= 0) {
|
|
close(c->fd);
|
|
c->fd = -1;
|
|
c->last_try_ms = 0;
|
|
return -1;
|
|
}
|
|
rlen += (size_t)r;
|
|
rbuf[rlen] = 0;
|
|
if (rbuf[rlen - 1] == '\n') {
|
|
/* Check the last non-empty line for ok / error terminator. */
|
|
char *last = rbuf;
|
|
for (size_t i = 0; i + 1 < rlen; i++) {
|
|
if (rbuf[i] == '\n') last = rbuf + i + 1;
|
|
}
|
|
if (strncmp(last, "ok", 2) == 0 && (last[2] == 0 || last[2] == ' ' || last[2] == '\n')) {
|
|
ok = 0;
|
|
break;
|
|
}
|
|
if (strncmp(last, "error", 5) == 0 && (last[5] == 0 || last[5] == ' ' || last[5] == '\n')) {
|
|
ok = 1;
|
|
break;
|
|
}
|
|
}
|
|
if (rlen + 1 >= sizeof rbuf) break;
|
|
}
|
|
if (reply && reply_cap > 0) {
|
|
/* Strip trailing newline into reply buffer. */
|
|
while (rlen > 0 && (rbuf[rlen - 1] == '\n' || rbuf[rlen - 1] == '\r')) rlen--;
|
|
size_t cp = rlen < reply_cap - 1 ? rlen : reply_cap - 1;
|
|
memcpy(reply, rbuf, cp);
|
|
reply[cp] = 0;
|
|
}
|
|
return ok;
|
|
}
|
|
|
|
int ctl_cmd(CtlConn *c, const char *line, char *reply, size_t reply_cap) {
|
|
pthread_mutex_lock(&c->mu);
|
|
int r = ctl_once_locked(c, line, reply, reply_cap);
|
|
if (r < 0) {
|
|
c->last_try_ms = 0;
|
|
r = ctl_once_locked(c, line, reply, reply_cap);
|
|
}
|
|
pthread_mutex_unlock(&c->mu);
|
|
return r;
|
|
}
|
|
|
|
int ctl_fire(CtlConn *c, const char *line) {
|
|
return ctl_cmd(c, line, NULL, 0);
|
|
}
|
|
|
|
/* ---- Shared-memory framebuffer reader ---- */
|
|
|
|
void fb_reader_init(FbReader *r, const char *shm_path) {
|
|
r->fd = -1;
|
|
r->hdr = NULL;
|
|
r->last_open_try_ms = 0;
|
|
snprintf(r->shm_path, sizeof r->shm_path, "%s", shm_path);
|
|
}
|
|
|
|
void fb_reader_close(FbReader *r) {
|
|
if (r->hdr) { munmap(r->hdr, CHANDRA_FB_TOTAL_BYTES); r->hdr = NULL; }
|
|
if (r->fd >= 0) { close(r->fd); r->fd = -1; }
|
|
}
|
|
|
|
static int fb_ensure_open(FbReader *r) {
|
|
if (r->hdr) return 0;
|
|
uint64_t now = remote_now_ms();
|
|
if (now - r->last_open_try_ms < 250) return -1;
|
|
r->last_open_try_ms = now;
|
|
int fd = open(r->shm_path, O_RDWR | O_CLOEXEC);
|
|
if (fd < 0) return -1;
|
|
struct stat st;
|
|
if (fstat(fd, &st) < 0 || (size_t)st.st_size < CHANDRA_FB_TOTAL_BYTES) {
|
|
close(fd);
|
|
return -1;
|
|
}
|
|
void *m = mmap(NULL, CHANDRA_FB_TOTAL_BYTES, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
|
if (m == MAP_FAILED) {
|
|
close(fd);
|
|
return -1;
|
|
}
|
|
ChandraFbHeader *hdr = (ChandraFbHeader *)m;
|
|
if (hdr->magic != CHANDRA_FB_MAGIC || hdr->version != CHANDRA_FB_VERSION ||
|
|
hdr->slot_bytes != CHANDRA_FB_SLOT_BYTES) {
|
|
munmap(m, CHANDRA_FB_TOTAL_BYTES);
|
|
close(fd);
|
|
return -1;
|
|
}
|
|
r->fd = fd;
|
|
r->hdr = hdr;
|
|
return 0;
|
|
}
|
|
|
|
void fb_reader_ping(FbReader *r) {
|
|
if (fb_ensure_open(r) < 0) return;
|
|
__atomic_store_n(&r->hdr->consumer_ping_ms, remote_now_ms(), __ATOMIC_RELEASE);
|
|
}
|
|
|
|
int fb_reader_grab(
|
|
FbReader *r,
|
|
uint64_t last_seq,
|
|
uint32_t last_w,
|
|
uint32_t last_h,
|
|
int *out_full,
|
|
uint32_t *out_w,
|
|
uint32_t *out_h,
|
|
int *out_x0,
|
|
int *out_y0,
|
|
int *out_x1,
|
|
int *out_y1,
|
|
uint64_t *out_seq,
|
|
uint8_t **scratch,
|
|
size_t *scratch_cap) {
|
|
if (fb_ensure_open(r) < 0) return -1;
|
|
ChandraFbHeader *hdr = r->hdr;
|
|
|
|
for (int attempt = 0; attempt < 3; attempt++) {
|
|
uint64_t seq1 = __atomic_load_n(&hdr->seq, __ATOMIC_ACQUIRE);
|
|
if (seq1 == 0) return 0;
|
|
uint32_t w = hdr->width;
|
|
uint32_t h = hdr->height;
|
|
if (w == 0 || h == 0 || w > 4096 || h > 4096 ||
|
|
(size_t)w * (size_t)h * 4 > hdr->slot_bytes) {
|
|
return 0;
|
|
}
|
|
if (seq1 == last_seq && w == last_w && h == last_h) return 0;
|
|
|
|
int full = (attempt > 0 || last_seq == 0 || seq1 < last_seq ||
|
|
seq1 > last_seq + 1 || w != last_w || h != last_h);
|
|
int x0 = full ? 0 : hdr->dmg_x0;
|
|
int y0 = full ? 0 : hdr->dmg_y0;
|
|
int x1 = full ? (int)w : hdr->dmg_x1;
|
|
int y1 = full ? (int)h : hdr->dmg_y1;
|
|
if (x0 < 0) x0 = 0;
|
|
if (y0 < 0) y0 = 0;
|
|
if (x1 > (int)w) x1 = (int)w;
|
|
if (y1 > (int)h) y1 = (int)h;
|
|
if (x0 >= x1 || y0 >= y1) {
|
|
x0 = 0; y0 = 0; x1 = (int)w; y1 = (int)h;
|
|
full = 1;
|
|
}
|
|
|
|
size_t need = (size_t)w * (size_t)h * 4;
|
|
if (*scratch_cap < need) {
|
|
uint8_t *ns = realloc(*scratch, need);
|
|
if (!ns) return -1;
|
|
*scratch = ns;
|
|
*scratch_cap = need;
|
|
}
|
|
|
|
uint32_t slot = __atomic_load_n(&hdr->active_slot, __ATOMIC_ACQUIRE) & 1u;
|
|
const uint8_t *src = chandra_fb_slot_ptr(hdr, slot, hdr->slot_bytes);
|
|
if (x0 == 0 && x1 == (int)w) {
|
|
size_t off = (size_t)y0 * (size_t)w * 4;
|
|
size_t bytes = (size_t)(y1 - y0) * (size_t)w * 4;
|
|
memcpy(*scratch + off, src + off, bytes);
|
|
} else {
|
|
size_t span = (size_t)(x1 - x0) * 4;
|
|
for (int y = y0; y < y1; y++) {
|
|
size_t off = ((size_t)y * (size_t)w + (size_t)x0) * 4;
|
|
memcpy(*scratch + off, src + off, span);
|
|
}
|
|
}
|
|
|
|
uint64_t seq2 = __atomic_load_n(&hdr->seq, __ATOMIC_ACQUIRE);
|
|
if (seq2 == seq1 && hdr->width == w && hdr->height == h) {
|
|
*out_full = full;
|
|
*out_w = w;
|
|
*out_h = h;
|
|
*out_x0 = x0;
|
|
*out_y0 = y0;
|
|
*out_x1 = x1;
|
|
*out_y1 = y1;
|
|
*out_seq = seq1;
|
|
return 1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/* ---- Network & Tailscale interface lookup ---- */
|
|
|
|
static int is_tailscale_ipv4(uint32_t host_order_ip) {
|
|
return (host_order_ip & 0xFFC00000u) == 0x64400000u; /* 100.64.0.0/10 */
|
|
}
|
|
|
|
static int is_loopback_ipv4(uint32_t host_order_ip) {
|
|
return (host_order_ip & 0xFF000000u) == 0x7F000000u; /* 127.0.0.0/8 */
|
|
}
|
|
|
|
int remote_find_tailscale_ipv4(char *out, size_t cap) {
|
|
struct ifaddrs *ifap = NULL;
|
|
if (getifaddrs(&ifap) < 0) return -1;
|
|
int found = -1;
|
|
for (struct ifaddrs *ifa = ifap; ifa; ifa = ifa->ifa_next) {
|
|
if (!ifa->ifa_addr || ifa->ifa_addr->sa_family != AF_INET) continue;
|
|
if (!ifa->ifa_name) continue;
|
|
if (strcmp(ifa->ifa_name, "tailscale0") != 0 &&
|
|
strncmp(ifa->ifa_name, "tailscale", 9) != 0 &&
|
|
strncmp(ifa->ifa_name, "ts", 2) != 0) {
|
|
continue;
|
|
}
|
|
const struct sockaddr_in *sin = (const struct sockaddr_in *)ifa->ifa_addr;
|
|
uint32_t ip = ntohl(sin->sin_addr.s_addr);
|
|
if (!is_tailscale_ipv4(ip)) continue;
|
|
if (inet_ntop(AF_INET, &sin->sin_addr, out, (socklen_t)cap)) {
|
|
found = 0;
|
|
if (strcmp(ifa->ifa_name, "tailscale0") == 0) break;
|
|
}
|
|
}
|
|
freeifaddrs(ifap);
|
|
return found;
|
|
}
|
|
|
|
int remote_peer_allowed(const RemoteConfig *cfg, const struct sockaddr_storage *ss, socklen_t slen) {
|
|
if (!remote_bind_is_tailscale(cfg->bind) || cfg->allow_insecure_open) return 1;
|
|
if (ss->ss_family == AF_INET && slen >= (socklen_t)sizeof(struct sockaddr_in)) {
|
|
const struct sockaddr_in *s = (const struct sockaddr_in *)ss;
|
|
uint32_t ip = ntohl(s->sin_addr.s_addr);
|
|
return is_tailscale_ipv4(ip) || (cfg->bind_loopback && is_loopback_ipv4(ip));
|
|
}
|
|
if (ss->ss_family == AF_INET6 && slen >= (socklen_t)sizeof(struct sockaddr_in6)) {
|
|
const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *)ss;
|
|
if (IN6_IS_ADDR_V4MAPPED(&s6->sin6_addr)) {
|
|
uint32_t ip = ((uint32_t)s6->sin6_addr.s6_addr[12] << 24) |
|
|
((uint32_t)s6->sin6_addr.s6_addr[13] << 16) |
|
|
((uint32_t)s6->sin6_addr.s6_addr[14] << 8) |
|
|
(uint32_t)s6->sin6_addr.s6_addr[15];
|
|
return is_tailscale_ipv4(ip) || (cfg->bind_loopback && is_loopback_ipv4(ip));
|
|
}
|
|
if (s6->sin6_addr.s6_addr[0] == 0xFD && s6->sin6_addr.s6_addr[1] == 0x7A) return 1;
|
|
if (cfg->bind_loopback && IN6_IS_ADDR_LOOPBACK(&s6->sin6_addr)) return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int listen_tcp(const char *ip, int port) {
|
|
int fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
|
|
if (fd < 0) return -1;
|
|
int one = 1;
|
|
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
|
|
struct sockaddr_in sin;
|
|
memset(&sin, 0, sizeof sin);
|
|
sin.sin_family = AF_INET;
|
|
sin.sin_port = htons((uint16_t)port);
|
|
if (strcmp(ip, "0.0.0.0") == 0) {
|
|
sin.sin_addr.s_addr = INADDR_ANY;
|
|
} else if (inet_pton(AF_INET, ip, &sin.sin_addr) != 1) {
|
|
close(fd);
|
|
return -1;
|
|
}
|
|
if (bind(fd, (struct sockaddr *)&sin, sizeof sin) < 0 || listen(fd, 16) < 0) {
|
|
close(fd);
|
|
return -1;
|
|
}
|
|
return fd;
|
|
}
|
|
|
|
typedef struct {
|
|
RemoteApp *app;
|
|
int fd;
|
|
int is_web;
|
|
} ClientArg;
|
|
|
|
static void *client_thread(void *arg) {
|
|
ClientArg *ca = (ClientArg *)arg;
|
|
RemoteApp *app = ca->app;
|
|
int fd = ca->fd;
|
|
int is_web = ca->is_web;
|
|
free(ca);
|
|
if (is_web) web_handle_client(app, fd);
|
|
else rfb_handle_client(app, fd);
|
|
return NULL;
|
|
}
|
|
|
|
static void spawn_client(RemoteApp *app, int cfd, int is_web) {
|
|
struct sockaddr_storage ss;
|
|
socklen_t slen = sizeof ss;
|
|
if (getpeername(cfd, (struct sockaddr *)&ss, &slen) == 0) {
|
|
RemoteConfig cfg;
|
|
pthread_mutex_lock(&app->cfg_mu);
|
|
cfg = app->cfg;
|
|
pthread_mutex_unlock(&app->cfg_mu);
|
|
if (!remote_peer_allowed(&cfg, &ss, slen)) {
|
|
close(cfd);
|
|
return;
|
|
}
|
|
}
|
|
ClientArg *ca = malloc(sizeof *ca);
|
|
if (!ca) { close(cfd); return; }
|
|
ca->app = app;
|
|
ca->fd = cfd;
|
|
ca->is_web = is_web;
|
|
pthread_t th;
|
|
pthread_attr_t attr;
|
|
pthread_attr_init(&attr);
|
|
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
|
if (pthread_create(&th, &attr, client_thread, ca) != 0) {
|
|
free(ca);
|
|
close(cfd);
|
|
}
|
|
pthread_attr_destroy(&attr);
|
|
}
|
|
|
|
static int read_magic_dns_fqdn(char *out, size_t cap) {
|
|
FILE *p = popen("tailscale status --json 2>/dev/null", "r");
|
|
if (!p) return -1;
|
|
char buf[8192];
|
|
size_t n = fread(buf, 1, sizeof buf - 1, p);
|
|
pclose(p);
|
|
buf[n] = 0;
|
|
char *self = strstr(buf, "\"Self\"");
|
|
if (!self) return -1;
|
|
char *dns = strstr(self, "\"DNSName\"");
|
|
if (!dns) return -1;
|
|
dns = strchr(dns + 9, '"');
|
|
if (!dns) return -1;
|
|
dns++;
|
|
char *end = strchr(dns, '"');
|
|
if (!end || end == dns) return -1;
|
|
size_t l = (size_t)(end - dns);
|
|
while (l > 0 && dns[l - 1] == '.') l--;
|
|
if (l >= cap) l = cap - 1;
|
|
memcpy(out, dns, l);
|
|
out[l] = 0;
|
|
return l > 0 ? 0 : -1;
|
|
}
|
|
|
|
static int cmd_status(const char *config_path, const char *pid_path) {
|
|
RemoteConfig cfg;
|
|
if (remote_load_or_init_config(config_path, &cfg) < 0) {
|
|
fprintf(stderr, "chandra-remote: cannot read config %s\n", config_path);
|
|
return 1;
|
|
}
|
|
long pid = 0;
|
|
FILE *pf = fopen(pid_path, "r");
|
|
if (pf) {
|
|
if (fscanf(pf, "%ld", &pid) != 1) pid = 0;
|
|
fclose(pf);
|
|
}
|
|
int running = (pid > 1 && kill((pid_t)pid, 0) == 0);
|
|
char ts_ip[64] = {0}, fqdn[128] = {0};
|
|
int has_ts = remote_find_tailscale_ipv4(ts_ip, sizeof ts_ip) == 0;
|
|
int has_fqdn = read_magic_dns_fqdn(fqdn, sizeof fqdn) == 0;
|
|
|
|
printf("daemon: %s", running ? "running" : "stopped");
|
|
if (running) printf(" (pid %ld)", pid);
|
|
printf("\nconfig: %s\nenabled: %s\nbind: %s (loopback=%s)\nauth: %s\n",
|
|
config_path, cfg.enabled ? "yes" : "no", cfg.bind,
|
|
cfg.bind_loopback ? "on" : "off",
|
|
cfg.auth_mode == REMOTE_AUTH_NONE ? "none (Tailscale/loopback trust)" : "password");
|
|
if (has_ts) printf("tailscale_ip: %s\n", ts_ip);
|
|
else printf("tailscale_ip: (waiting for tailscale0)\n");
|
|
if (has_fqdn) printf("tailscale_dns: %s\n", fqdn);
|
|
const char *host = has_fqdn ? fqdn : (has_ts ? ts_ip : "<tailscale-ip>");
|
|
printf("web_url: http://%s:%d/\nvnc: %s:%d\n", host, cfg.web_port, host, cfg.vnc_port);
|
|
|
|
char sock[512];
|
|
resolve_control_socket(sock, sizeof sock);
|
|
CtlConn ctl;
|
|
ctl_init(&ctl, sock);
|
|
char rep[256];
|
|
if (ctl_cmd(&ctl, "status", rep, sizeof rep) == 0) {
|
|
printf("compositor: %s\n", rep);
|
|
} else {
|
|
printf("compositor: offline (%s)\n", sock);
|
|
}
|
|
ctl_close(&ctl);
|
|
return 0;
|
|
}
|
|
|
|
static int cmd_passwd(const char *config_path, const char *pid_path, const char *arg) {
|
|
RemoteConfig cfg;
|
|
if (remote_load_or_init_config(config_path, &cfg) < 0) return 1;
|
|
if (strcmp(arg, "--none") == 0) {
|
|
if (!remote_bind_is_tailscale(cfg.bind) && !remote_bind_is_loopback(cfg.bind)) {
|
|
fprintf(stderr, "error: auth=none is only allowed with bind=tailscale or loopback\n");
|
|
return 1;
|
|
}
|
|
cfg.auth_mode = REMOTE_AUTH_NONE;
|
|
cfg.password[0] = 0;
|
|
printf("auth set to none (Tailscale/loopback trust)\n");
|
|
} else {
|
|
cfg.auth_mode = REMOTE_AUTH_PASSWORD;
|
|
snprintf(cfg.password, sizeof cfg.password, "%s", arg);
|
|
printf("password updated (auth=password)\n");
|
|
}
|
|
if (remote_save_config(config_path, &cfg) < 0) return 1;
|
|
FILE *pf = fopen(pid_path, "r");
|
|
if (pf) {
|
|
long pid = 0;
|
|
if (fscanf(pf, "%ld", &pid) == 1 && pid > 1) kill((pid_t)pid, SIGHUP);
|
|
fclose(pf);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
char config_path[512], sock_path[512], shm_path[512], web_dir[512], pid_path[512];
|
|
default_config_path(config_path, sizeof config_path);
|
|
resolve_control_socket(sock_path, sizeof sock_path);
|
|
resolve_shm_path(shm_path, sizeof shm_path);
|
|
default_pid_path(pid_path, sizeof pid_path);
|
|
const char *wd_env = getenv("CHANDRA_REMOTE_WEB_DIR");
|
|
snprintf(web_dir, sizeof web_dir, "%s",
|
|
(wd_env && wd_env[0]) ? wd_env : "/usr/local/share/chandra/remote");
|
|
|
|
const char *override_bind = NULL;
|
|
int override_no_loopback = 0;
|
|
int override_no_auth = 0;
|
|
int override_insecure = 0;
|
|
int override_vnc_port = 0;
|
|
int override_web_port = 0;
|
|
pid_t parent_pid = 0;
|
|
|
|
if (argc >= 2 && strcmp(argv[1], "status") == 0) {
|
|
for (int i = 2; i < argc - 1; i++) {
|
|
if (strcmp(argv[i], "--config") == 0) snprintf(config_path, sizeof config_path, "%s", argv[++i]);
|
|
}
|
|
return cmd_status(config_path, pid_path);
|
|
}
|
|
if (argc >= 2 && strcmp(argv[1], "passwd") == 0) {
|
|
const char *val = (argc >= 3) ? argv[2] : NULL;
|
|
char gen[16];
|
|
if (!val) { remote_generate_password(gen, 11); val = gen; printf("generated password: %s\n", val); }
|
|
return cmd_passwd(config_path, pid_path, val);
|
|
}
|
|
|
|
for (int i = 1; i < argc; i++) {
|
|
const char *a = argv[i];
|
|
if (strcmp(a, "--config") == 0 && i + 1 < argc) snprintf(config_path, sizeof config_path, "%s", argv[++i]);
|
|
else if (strcmp(a, "--socket") == 0 && i + 1 < argc) snprintf(sock_path, sizeof sock_path, "%s", argv[++i]);
|
|
else if (strcmp(a, "--shm") == 0 && i + 1 < argc) snprintf(shm_path, sizeof shm_path, "%s", argv[++i]);
|
|
else if (strcmp(a, "--web-dir") == 0 && i + 1 < argc) snprintf(web_dir, sizeof web_dir, "%s", argv[++i]);
|
|
else if (strcmp(a, "--bind") == 0 && i + 1 < argc) override_bind = argv[++i];
|
|
else if (strcmp(a, "--no-loopback") == 0) override_no_loopback = 1;
|
|
else if (strcmp(a, "--no-auth") == 0) override_no_auth = 1;
|
|
else if (strcmp(a, "--allow-insecure-open") == 0) override_insecure = 1;
|
|
else if (strcmp(a, "--vnc-port") == 0 && i + 1 < argc) override_vnc_port = atoi(argv[++i]);
|
|
else if (strcmp(a, "--web-port") == 0 && i + 1 < argc) override_web_port = atoi(argv[++i]);
|
|
else if (strcmp(a, "--parent-pid") == 0 && i + 1 < argc) parent_pid = (pid_t)atol(argv[++i]);
|
|
else if (strcmp(a, "-h") == 0 || strcmp(a, "--help") == 0) {
|
|
printf("usage: chandra-remote [status | passwd [PASS|--none] | --config PATH --socket PATH --shm PATH --web-dir DIR --bind IP --no-loopback --no-auth --vnc-port N --web-port N --parent-pid PID]\n");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
RemoteApp app;
|
|
memset(&app, 0, sizeof app);
|
|
app.running = &g_running;
|
|
pthread_mutex_init(&app.cfg_mu, NULL);
|
|
pthread_mutex_init(&app.ping_mu, NULL);
|
|
snprintf(app.config_path, sizeof app.config_path, "%s", config_path);
|
|
snprintf(app.web_dir, sizeof app.web_dir, "%s", web_dir);
|
|
snprintf(app.shm_path, sizeof app.shm_path, "%s", shm_path);
|
|
ctl_init(&app.ctl, sock_path);
|
|
if (RAND_bytes(app.hmac_key, sizeof app.hmac_key) != 1) {
|
|
fprintf(stderr, "chandra-remote: RAND_bytes failed\n");
|
|
return 1;
|
|
}
|
|
|
|
if (remote_load_or_init_config(config_path, &app.cfg) < 0) {
|
|
fprintf(stderr, "chandra-remote: failed to load/init config %s\n", config_path);
|
|
return 1;
|
|
}
|
|
if (override_bind) snprintf(app.cfg.bind, sizeof app.cfg.bind, "%s", override_bind);
|
|
if (override_no_loopback) app.cfg.bind_loopback = 0;
|
|
if (override_no_auth) app.cfg.auth_mode = REMOTE_AUTH_NONE;
|
|
if (override_insecure) app.cfg.allow_insecure_open = 1;
|
|
if (override_vnc_port > 0) app.cfg.vnc_port = override_vnc_port;
|
|
if (override_web_port > 0) app.cfg.web_port = override_web_port;
|
|
|
|
if (app.cfg.auth_mode == REMOTE_AUTH_NONE &&
|
|
!remote_bind_is_tailscale(app.cfg.bind) &&
|
|
!remote_bind_is_loopback(app.cfg.bind) &&
|
|
!app.cfg.allow_insecure_open) {
|
|
fprintf(stderr, "chandra-remote: refusing unauthenticated bind='%s' (use bind=tailscale or 127.0.0.1)\n",
|
|
app.cfg.bind);
|
|
return 1;
|
|
}
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
signal(SIGINT, on_term);
|
|
signal(SIGTERM, on_term);
|
|
signal(SIGHUP, on_hup);
|
|
|
|
/* Record PID for `chandra-remote status` / `passwd`. */
|
|
{
|
|
char pdir[512];
|
|
snprintf(pdir, sizeof pdir, "%s", pid_path);
|
|
char *slash = strrchr(pdir, '/');
|
|
if (slash) { *slash = 0; mkdir(pdir, 0700); }
|
|
FILE *pf = fopen(pid_path, "w");
|
|
if (pf) { fprintf(pf, "%ld\n", (long)getpid()); fclose(pf); }
|
|
}
|
|
|
|
int vnc_ts_fd = -1, web_ts_fd = -1;
|
|
int vnc_lo_fd = -1, web_lo_fd = -1;
|
|
char active_ts_ip[64] = {0};
|
|
|
|
if (remote_bind_is_tailscale(app.cfg.bind)) {
|
|
if (app.cfg.bind_loopback) {
|
|
vnc_lo_fd = listen_tcp("127.0.0.1", app.cfg.vnc_port);
|
|
web_lo_fd = listen_tcp("127.0.0.1", app.cfg.web_port);
|
|
fprintf(stderr, "chandra-remote: loopback VNC 127.0.0.1:%d, Web 127.0.0.1:%d\n",
|
|
vnc_lo_fd >= 0 ? app.cfg.vnc_port : -1,
|
|
web_lo_fd >= 0 ? app.cfg.web_port : -1);
|
|
}
|
|
} else {
|
|
vnc_ts_fd = listen_tcp(app.cfg.bind, app.cfg.vnc_port);
|
|
web_ts_fd = listen_tcp(app.cfg.bind, app.cfg.web_port);
|
|
if (vnc_ts_fd < 0 || web_ts_fd < 0) {
|
|
fprintf(stderr, "chandra-remote: failed to bind %s:%d/%d: %s\n",
|
|
app.cfg.bind, app.cfg.vnc_port, app.cfg.web_port, strerror(errno));
|
|
if (vnc_ts_fd >= 0) close(vnc_ts_fd);
|
|
if (web_ts_fd >= 0) close(web_ts_fd);
|
|
return 1;
|
|
}
|
|
fprintf(stderr, "chandra-remote: listening on %s VNC:%d Web:%d (auth=%s)\n",
|
|
app.cfg.bind, app.cfg.vnc_port, app.cfg.web_port,
|
|
app.cfg.auth_mode == REMOTE_AUTH_NONE ? "none" : "password");
|
|
}
|
|
|
|
uint64_t last_iface_check = 0;
|
|
uint64_t last_parent_check = 0;
|
|
|
|
while (g_running) {
|
|
if (g_reload) {
|
|
g_reload = 0;
|
|
RemoteConfig ncfg;
|
|
if (remote_load_or_init_config(config_path, &ncfg) == 0) {
|
|
pthread_mutex_lock(&app.cfg_mu);
|
|
ncfg.allow_insecure_open = app.cfg.allow_insecure_open;
|
|
if (override_bind) snprintf(ncfg.bind, sizeof ncfg.bind, "%s", override_bind);
|
|
if (override_no_loopback) ncfg.bind_loopback = 0;
|
|
if (override_no_auth) ncfg.auth_mode = REMOTE_AUTH_NONE;
|
|
if (override_vnc_port > 0) ncfg.vnc_port = override_vnc_port;
|
|
if (override_web_port > 0) ncfg.web_port = override_web_port;
|
|
app.cfg = ncfg;
|
|
pthread_mutex_unlock(&app.cfg_mu);
|
|
fprintf(stderr, "chandra-remote: reloaded config (auth=%s)\n",
|
|
ncfg.auth_mode == REMOTE_AUTH_NONE ? "none" : "password");
|
|
}
|
|
}
|
|
|
|
uint64_t now = remote_now_ms();
|
|
if (parent_pid > 1 && now - last_parent_check >= 1000) {
|
|
last_parent_check = now;
|
|
if (kill(parent_pid, 0) < 0 && errno == ESRCH) break;
|
|
}
|
|
|
|
if (remote_bind_is_tailscale(app.cfg.bind) &&
|
|
(last_iface_check == 0 || now - last_iface_check >= 2000)) {
|
|
last_iface_check = now;
|
|
char ts_ip[64] = {0};
|
|
int has = remote_find_tailscale_ipv4(ts_ip, sizeof ts_ip) == 0;
|
|
if (has && strcmp(ts_ip, active_ts_ip) != 0) {
|
|
if (vnc_ts_fd >= 0) { close(vnc_ts_fd); vnc_ts_fd = -1; }
|
|
if (web_ts_fd >= 0) { close(web_ts_fd); web_ts_fd = -1; }
|
|
vnc_ts_fd = listen_tcp(ts_ip, app.cfg.vnc_port);
|
|
web_ts_fd = listen_tcp(ts_ip, app.cfg.web_port);
|
|
snprintf(active_ts_ip, sizeof active_ts_ip, "%s", ts_ip);
|
|
fprintf(stderr, "chandra-remote: bound tailscale0 VNC %s:%d (%s), Web %s:%d (%s)\n",
|
|
ts_ip, app.cfg.vnc_port, vnc_ts_fd >= 0 ? "ok" : "fail",
|
|
ts_ip, app.cfg.web_port, web_ts_fd >= 0 ? "ok" : "fail");
|
|
} else if (!has && active_ts_ip[0]) {
|
|
if (vnc_ts_fd >= 0) { close(vnc_ts_fd); vnc_ts_fd = -1; }
|
|
if (web_ts_fd >= 0) { close(web_ts_fd); web_ts_fd = -1; }
|
|
active_ts_ip[0] = 0;
|
|
fprintf(stderr, "chandra-remote: tailscale0 went down; waiting for Tailscale IP\n");
|
|
}
|
|
}
|
|
|
|
struct pollfd pfds[4];
|
|
int types[4]; /* 0=vnc, 1=web */
|
|
nfds_t nfds = 0;
|
|
int fds[4] = { vnc_ts_fd, vnc_lo_fd, web_ts_fd, web_lo_fd };
|
|
int ftypes[4] = { 0, 0, 1, 1 };
|
|
for (int i = 0; i < 4; i++) {
|
|
if (fds[i] >= 0) {
|
|
pfds[nfds].fd = fds[i];
|
|
pfds[nfds].events = POLLIN;
|
|
pfds[nfds].revents = 0;
|
|
types[nfds] = ftypes[i];
|
|
nfds++;
|
|
}
|
|
}
|
|
if (nfds == 0) {
|
|
remote_sleep_ms(500);
|
|
continue;
|
|
}
|
|
int pr = poll(pfds, nfds, 500);
|
|
if (pr < 0) {
|
|
if (errno == EINTR) continue;
|
|
break;
|
|
}
|
|
for (nfds_t i = 0; i < nfds; i++) {
|
|
if (pfds[i].revents & POLLIN) {
|
|
int cfd = accept(pfds[i].fd, NULL, NULL);
|
|
if (cfd >= 0) spawn_client(&app, cfd, types[i]);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (vnc_ts_fd >= 0) close(vnc_ts_fd);
|
|
if (web_ts_fd >= 0) close(web_ts_fd);
|
|
if (vnc_lo_fd >= 0) close(vnc_lo_fd);
|
|
if (web_lo_fd >= 0) close(web_lo_fd);
|
|
ctl_close(&app.ctl);
|
|
unlink(pid_path);
|
|
return 0;
|
|
}
|