Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

安卓客户端的问题 #131

Open
fmnx opened this issue Jan 15, 2025 · 0 comments
Open

安卓客户端的问题 #131

fmnx opened this issue Jan 15, 2025 · 0 comments

Comments

@fmnx
Copy link

fmnx commented Jan 15, 2025

大佬好!这边提个小问题:
普通linux发行版的tun设备路径是/dev/net/tun
安卓的tun设备路径是/dev/tun, 并且要用策略路由
下面是我临时的解决方案,写得不好,但是可以参考下。

// for android
int calculate_table_number(const char *sub_net) {
    string sub_net_str(sub_net);

    std::hash<std::string> hash_fn;
    size_t hash_value = hash_fn(sub_net_str);

    int table_number = 100 + (hash_value % (65535 - 100 + 1));

    return table_number;
}
void setup_android_routing(const char *tun_dev, const char *sub_net) {
    int table_number = calculate_table_number(sub_net);

    char flush_table[256];
    char add_route[256];
    char del_rule[256];
    char add_rule[256];

    snprintf(flush_table, sizeof(flush_table), "ip route flush table %d", table_number);
    snprintf(add_route, sizeof(add_route), "ip route add default dev %s table %d", tun_dev, table_number);
    snprintf(del_rule, sizeof(del_rule), "ip rule del from all to %s/24 2>/dev/null", sub_net);
    snprintf(add_rule, sizeof(add_rule), "ip rule add to %s/24 lookup %d", sub_net, table_number);

    system(flush_table);
    system(add_route);
    system(del_rule);
    system(add_rule);
}
void cleanup_android_routing(const char *tun_dev, const char *sub_net) {
    int table_number = calculate_table_number(sub_net);

    char del_rule[256];
    char flush_table[256];

    snprintf(del_rule, sizeof(del_rule), "ip rule del from all to %s/24 lookup %d", sub_net, table_number);
    snprintf(flush_table, sizeof(flush_table), "ip route flush table %d", table_number);

    system(del_rule);
    system(flush_table);
}

int get_tun_fd(char *dev_name, bool *is_android) {
    int tun_fd = open("/dev/net/tun", O_RDWR);

    if (tun_fd < 0) {
        tun_fd = open("/dev/tun", O_RDWR);  // Android
        if (tun_fd < 0) {
            mylog(log_fatal, "open tun device failed.");
            myexit(-1);
        }
        *is_android = true;
    }

    struct ifreq ifr;
    memset(&ifr, 0, sizeof(ifr));
    ifr.ifr_flags = IFF_TUN | IFF_NO_PI;

    strncpy(ifr.ifr_name, dev_name, IFNAMSIZ);

    if (ioctl(tun_fd, TUNSETIFF, (void *)&ifr) != 0) {
        mylog(log_fatal, "open tun device failed. ioctl(tun_fd, TUNSETIFF, (void *)&ifr) != 0");
        myexit(-1);
    }

    if (persist_tun == 1) {
        if (ioctl(tun_fd, TUNSETPERSIST, 1) != 0) {
            mylog(log_warn, "failed to set tun persistent");
        }
    }

    return tun_fd;
}

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant