feat: add portable GlobalProtect APIs and iOS tunnel support

This commit is contained in:
Kevin Yue committed 2026-11-01 23:34:18 +08:00
1 parent 4f8f828a0b
commit da90bc80e3
112 files changed
+3864 -3011

No files matched your search

+53
View File
@@ -75,6 +75,58 @@ int inspect_esp_packet(struct openconnect_info *vpninfo, struct pkt *pkt, uint8_
return 1;
}
/* Check the real request builder without a VPN server. Desktop payloads stay exact. */
static void getconfig_profile_parameters(void)
{
const char *platforms[] = { "linux", "mac-intel", "win", "apple-ios" };
const char *names[] = { "Linux", "Mac", "Windows", "iOS" };
int i;
for (i = 0; i < 4; i++) {
struct openconnect_info *vpninfo = openconnect_vpninfo_new("test", NULL, NULL, NULL, NULL, NULL);
struct oc_text_buf *body = buf_alloc();
char expected[2048];
assert(vpninfo && body);
assert(!openconnect_set_reported_os(vpninfo, platforms[i]));
assert(!openconnect_set_gp_app_version(vpninfo, "6.3.5-28"));
assert(!openconnect_set_gp_os_version(vpninfo, "test-os"));
assert(!openconnect_set_gp_host_id(vpninfo, "HOSTID"));
assert(!openconnect_set_cookie(vpninfo, "user=test&portal=portal&authcookie=fixture&computer=Phone&preferred-ip=192.0.2.5"));
assert(!gpst_build_getconfig_request(vpninfo, body));
if (i == 3) {
assert(strstr(body->data, "clientos=iOS"));
assert(strstr(body->data, "vpn-type=App%20Level%20VPN"));
assert(strstr(body->data, "client-ip=&client-ipv6=&serialno=&joined-domain="));
assert(strstr(body->data, "user=test") && strstr(body->data, "authcookie=fixture"));
assert(strstr(body->data, "preferred-ip=192.0.2.5"));
assert(!strstr(body->data, "host-id=") && !strstr(body->data, "clientgpversion=") && !strstr(body->data, "computer="));
buf_truncate(body);
vpninfo->ip_info.addr = strdup("192.0.2.6");
assert(!gpst_build_getconfig_request(vpninfo, body));
assert(strstr(body->data, "preferred-ip=192.0.2.6"));
assert(!strstr(body->data, "192.0.2.5"));
/* The connected HTTPS socket supplies the actual local client address. */
int fd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in local = { .sin_family = AF_INET, .sin_addr.s_addr = htonl(INADDR_LOOPBACK) };
assert(fd >= 0 && !bind(fd, (struct sockaddr *)&local, sizeof(local)));
vpninfo->ssl_fd = fd;
buf_truncate(body);
assert(!gpst_build_getconfig_request(vpninfo, body));
assert(strstr(body->data, "client-ip=127.0.0.1&client-ipv6="));
vpninfo->ssl_fd = -1;
close(fd);
} else {
snprintf(expected, sizeof(expected),
"client-type=1&protocol-version=p1&internal=no&app-version=6.3.5-28&ipv6-support=yes"
"&clientos=%s&os-version=test-os&hmac-algo=sha1%%2c&enc-algo=aes-256-gcm%%2caes-128-gcm%%2caes-128-cbc%%2c"
"&clientgpversion=6.3.5-28&host-id=HOSTID&user=test&portal=portal&authcookie=fixture&computer=Phone&preferred-ip=192.0.2.5",
names[i]);
assert(!strcmp(body->data, expected));
}
buf_free(body);
openconnect_vpninfo_free(vpninfo);
}
}
int main(int argc, char **argv)
{
static const char key[] =
@@ -92,6 +144,7 @@ int main(int argc, char **argv)
int sockets[2], nlb, invalid, length;
assert(argc == 2);
getconfig_profile_parameters();
nlb = !strncmp(argv[1], "nlb-", 4);
invalid = !strcmp(argv[1], "regular-invalid") || !strcmp(argv[1], "regular-missing");
if (!strcmp(argv[1], "regular-address") || !strcmp(argv[1], "nlb-address"))
@@ -160,6 +160,59 @@ static void reconnect_refusal_propagates(void)
openconnect_vpninfo_free(vpninfo);
}
static void external_packets_and_configuration(void)
{
struct state state = { .parent = getpid(), .reason = "connect" };
struct openconnect_info *vpninfo = new_vpn(&state);
int fds[2], families[] = { AF_INET, AF_INET6 };
int creations = device_creations;
assert(!socketpair(AF_UNIX, SOCK_DGRAM, 0, fds));
assert(!openconnect_setup_tun_fd_with_config(vpninfo, fds[0]));
assert(device_creations == creations);
assert(state.calls == 1);
for (int i = 0; i < 2; i++) {
unsigned char frame[1504] = {0}, received[1504];
struct pkt *packet = alloc_pkt(vpninfo, 1500);
int prefix = 0, length = i ? 40 : 20;
#if defined(__APPLE__) || defined(__OpenBSD__) || defined(__FreeBSD__)
uint32_t family = htonl(families[i]);
memcpy(frame, &family, 4);
prefix = 4;
#endif
frame[prefix] = i ? 0x60 : 0x45;
assert(write(fds[1], frame, length + prefix) == length + prefix);
packet->len = 1500;
assert(!os_read_tun(vpninfo, packet));
assert(packet->len == length);
assert(!memcmp(packet->data, frame + prefix, length));
assert(!os_write_tun(vpninfo, packet));
assert(read(fds[1], received, sizeof(received)) == length + prefix);
assert(!memcmp(received, frame, length + prefix));
free_pkt(vpninfo, packet);
}
state.reason = "disconnect";
openconnect_vpninfo_free(vpninfo);
assert(state.calls == 2);
assert(fcntl(fds[0], F_GETFD) == -1 && errno == EBADF);
close(fds[1]);
state.calls = 0;
state.reason = "connect";
state.failure_reason = "connect";
state.result = -EPERM;
const char *reasons[] = { "connect", "disconnect" };
state.expected_reasons = reasons;
state.expected_count = 2;
vpninfo = new_vpn(&state);
assert(!socketpair(AF_UNIX, SOCK_DGRAM, 0, fds));
assert(openconnect_setup_tun_fd_with_config(vpninfo, fds[0]) == -EPERM);
assert(vpninfo->tun_fd == -1);
assert(fcntl(fds[0], F_GETFD) == -1 && errno == EBADF);
openconnect_vpninfo_free(vpninfo);
assert(state.calls == 2);
close(fds[1]);
}
int main(void)
{
const char *reasons[] = { "pre-init", "connect", "attempt-reconnect", "reconnect", "disconnect" };
@@ -199,5 +252,6 @@ int main(void)
openconnect_vpninfo_free(vpninfo);
connect_failure_rolls_back();
reconnect_refusal_propagates();
external_packets_and_configuration();
return 0;
}