Skip to content

Commit

Permalink
Some changes & first working prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
gxosty committed Mar 6, 2024
1 parent e086f9f commit 317d9b4
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 88 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ google-services.json

# Android Profiling
*.hprof

# My Stuff
.mystuff
1 change: 1 addition & 0 deletions app/src/main/cpp/include/tunmode/tunmode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace tunmode
namespace params
{
extern TunSocket tun;
extern in_addr net_iface;
extern in_addr dns_address;
extern jobject TunModeService_object;
extern std::atomic<bool> stop_flag;
Expand Down
29 changes: 27 additions & 2 deletions app/src/main/cpp/main.cxx
Original file line number Diff line number Diff line change
@@ -1,15 +1,40 @@
#include <jni.h>
#include <random>
#include <ctime>
#include <cstring>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <unistd.h>

#include <tunmode/tunmode.hpp>

extern "C"
JNIEXPORT void JNICALL
Java_git_gxosty_tunmode_interceptor_services_TunModeService_tunnelOpenNative(JNIEnv* env, jclass cls, int fd, jstring dns_address)
Java_git_gxosty_tunmode_interceptor_services_TunModeService_tunnelOpenNative(JNIEnv* env, jclass cls, int fd, jstring net_iface, jstring dns_address)
{
tunmode::params::dns_address.s_addr = 0;
tunmode::params::tun = fd;
tunmode::params::dns_address.s_addr = 0;

const char* str_net_iface = env->GetStringUTFChars(net_iface, NULL);
const char* str_dns_address = env->GetStringUTFChars(dns_address, NULL);

struct ifreq ifr;
int if_fd = socket(AF_INET, SOCK_DGRAM, 0);

ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name, str_net_iface, IFNAMSIZ);
ioctl(if_fd, SIOCGIFADDR, &ifr);

close(if_fd);
tunmode::params::net_iface = ((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr;

inet_pton(AF_INET, str_dns_address, &tunmode::params::dns_address);

env->ReleaseStringUTFChars(net_iface, str_net_iface);
env->ReleaseStringUTFChars(dns_address, str_dns_address);

tunmode::open_tunnel();
}

Expand Down
22 changes: 11 additions & 11 deletions app/src/main/cpp/src/tunmode/common/utils.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,17 @@ namespace tunmode::utils
LOGD_("------[Packet]------");
LOGD_("--------[IP]--------");

LOGD_("ip_hl: %u", (unsigned) ip_header->ip_hl);
LOGD_("ip_v: %u", (unsigned) ip_header->ip_v);
LOGD_("ip_tos: %u", (unsigned) ip_header->ip_tos);
LOGD_("ip_len: %u", (unsigned) ntohs(ip_header->ip_len));
LOGD_("ip_id: %u", (unsigned) ntohs(ip_header->ip_id));
LOGD_("ip_off: %u", (unsigned) ntohs(ip_header->ip_off));
LOGD_("ip_ttl: %u", (unsigned) ip_header->ip_ttl);
LOGD_("ip_p: %u", (unsigned) ip_header->ip_p);
LOGD_("ip_sum: %u", (unsigned) ntohs(ip_header->ip_sum));
LOGD_("ip_src: %s", inet_ntoa(ip_header->ip_src));
LOGD_("ip_dst: %s", inet_ntoa(ip_header->ip_dst));
LOGD_("ip_hl: %u", (unsigned)ip_header->ip_hl);
LOGD_("ip_v: %u", (unsigned)ip_header->ip_v);
LOGD_("ip_tos: %u", (unsigned)ip_header->ip_tos);
LOGD_("ip_len: %u", (unsigned)ntohs(ip_header->ip_len));
LOGD_("ip_id: %u", (unsigned)ntohs(ip_header->ip_id));
LOGD_("ip_off: %u", (unsigned)ntohs(ip_header->ip_off));
LOGD_("ip_ttl: %u", (unsigned)ip_header->ip_ttl);
LOGD_("ip_p: %u", (unsigned)ip_header->ip_p);
LOGD_("ip_sum: %u", (unsigned)ntohs(ip_header->ip_sum));
LOGD_("ip_src: %s", inet_ntoa(ip_header->ip_src));
LOGD_("ip_dst: %s", inet_ntoa(ip_header->ip_dst));

LOGD_("-------[TCP]--------");

Expand Down
4 changes: 0 additions & 4 deletions app/src/main/cpp/src/tunmode/session/session.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@

#include <thread>

#include <misc/logger.hpp>

namespace tunmode
{
Session::Session(uint64_t id)
{
LOGD_("Session::Session");
this->id = id;
}

Session::~Session()
{
delete this->client_socket;
delete this->server_socket;
LOGD_("Session::~Session");
}

uint64_t Session::get_id()
Expand Down
33 changes: 0 additions & 33 deletions app/src/main/cpp/src/tunmode/session/tcpsession.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#include <thread>
#include <errno.h>

#include <misc/logger.hpp>

namespace tunmode
{
TCPSession::TCPSession(TCPManager* manager, uint64_t id) : Session(id)
Expand Down Expand Up @@ -65,7 +63,6 @@ namespace tunmode
if (fds[0].revents)
{
_ret++;
LOGD_("poll2: revents == %d", (int)fds[0].revents);
if (fds[0].revents & POLLIN)
{
int status = cl_socket->recv(client_buffer);
Expand Down Expand Up @@ -93,13 +90,6 @@ namespace tunmode
{
fds[1].revents = POLLHUP;
}

#ifndef RELEASE_MODE
if (server_buffer.get_size() == 0)
{
LOGD_("server_buffer.get_size() == 0");
}
#endif
}

_ret++;
Expand All @@ -112,8 +102,6 @@ namespace tunmode
}
}

LOGE_("REACHED POLL2 END");

return -1;
}

Expand All @@ -128,10 +116,8 @@ namespace tunmode
TCPSocket* cl_socket = reinterpret_cast<TCPSocket*>(this->client_socket);
Socket*& sv_socket = this->server_socket;

LOGD_("Calling connect");
if (cl_socket->connect(sv_socket))
{
LOGE_("Connection failed");
cl_socket->close();
sv_socket->close();
return;
Expand All @@ -153,11 +139,9 @@ namespace tunmode
fds[0].revents = 0;
fds[1].revents = 0;
int ret = this->poll2(fds, client_buffer, server_buffer);
LOGD_("_loop: revents == %d", (int)fds[0].revents);

if (ret == -1)
{
LOGE_("Session poll error");
break;
}
else if (ret == 0)
Expand All @@ -168,36 +152,19 @@ namespace tunmode
{
if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL))
{
LOGD_("cl_socket revents == %d", fds[0].revents);
break;
}
else if (fds[0].revents & POLLIN)
{
LOGD_("-> server: %lu | %d", client_buffer.get_size(), (int)(fds[0].revents & POLLMSG ? MSG_MORE : 0));
sv_socket->send(&client_buffer, fds[0].revents & POLLMSG ? MSG_MORE : 0);
}

if (fds[1].revents & (POLLERR | POLLHUP | POLLNVAL))
{
LOGD_("sv_socket revents == %d", fds[1].revents);
if (fds[1].revents & POLLERR)
{
LOGE_("POLLERR: %s", std::strerror(errno));
}
if (fds[1].revents & POLLHUP)
{
LOGE_("POLLHUP");
}
if (fds[1].revents & POLLNVAL)
{
LOGE_("POLLNVAL");
}

break;
}
else if (fds[1].revents & POLLIN)
{
LOGD_("<- client: %lu", server_buffer.get_size());
cl_socket->send(server_buffer);
}
}
Expand Down
4 changes: 1 addition & 3 deletions app/src/main/cpp/src/tunmode/session/udpsession.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#include <thread>
#include <errno.h>

#include <misc/logger.hpp>

namespace tunmode
{
UDPSession::UDPSession(UDPManager* manager, uint64_t id) : Session(id)
Expand All @@ -27,7 +25,7 @@ namespace tunmode

int UDPSession::poll(struct pollfd fds[2])
{
return ::poll(fds, 2, 60000);
return ::poll(fds, 2, this->poll_timeout);
}

void UDPSession::loop()
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/cpp/src/tunmode/socket/sessionsocket.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include <unistd.h>
#include <netinet/ip.h>

#include <misc/logger.hpp>

namespace tunmode
{
TunSocket* SessionSocket::tun = nullptr;
Expand Down
18 changes: 2 additions & 16 deletions app/src/main/cpp/src/tunmode/socket/tcpsocket.cxx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <tunmode/socket/tcpsocket.hpp>
#include <tunmode/common/utils.hpp>
#include <tunmode/definitions.hpp>
#include <tunmode/tunmode.hpp>

#include <sys/socket.h>
#include <arpa/inet.h>
Expand All @@ -12,8 +13,6 @@
#include <cstdlib>
#include <random>

#include <misc/logger.hpp>

namespace tunmode
{
TCPSocket::TCPSocket() : SessionSocket()
Expand Down Expand Up @@ -58,27 +57,20 @@ namespace tunmode

if (tcp_header->th_flags != TH_SYN)
{
LOGE_("Packet isn't SYN");
this->set_state(TCPSTATE_CLOSED);
this->reset(client_packet);
return -1;
}

this->set_state(TCPSTATE_SYN_RECEIVED);

LOGD_("Binding server socket...");
in_addr sv_addr;
inet_pton(AF_INET, "192.168.1.101", &sv_addr);
skt->bind(sv_addr, 0);
skt->bind(params::net_iface, 0);

LOGD_("Connecting server socket...");
if (skt->connect(this->server_addr, this->server_port) == -1)
{
LOGE_("Couldn't connect server socket");
this->reset(client_packet);
return -1;
}
LOGD_("Connected server socket");

utils::build_tcp_packet(&server_packet);
utils::point_headers_tcp(&server_packet, &ip_header, &tcp_header);
Expand All @@ -96,9 +88,7 @@ namespace tunmode
memcpy((void*)(tcp_header + 1), (void*)hs_opts, 12);
server_packet.set_size(server_packet.get_size() + 12);

LOGD_("Sending SYN/ACK to client");
this->send_tun(server_packet);
LOGD_("Waiting for ACK response");

do {
*this > client_packet;
Expand All @@ -107,12 +97,10 @@ namespace tunmode

if ((tcp_header->th_flags != TH_ACK) && (tcp_header->th_flags != TH_RST))
{
LOGE_("Packet wasn't ACK");
this->set_state(TCPSTATE_CLOSED);
this->reset(client_packet);
return -1;
}
LOGD_("Got ACK packet");

this->vars.snd.nxt++;
this->vars.snd.una = this->vars.snd.nxt;
Expand Down Expand Up @@ -360,8 +348,6 @@ namespace tunmode
break;
}

LOGD_("recv switch ended");

return -1;
}

Expand Down
7 changes: 2 additions & 5 deletions app/src/main/cpp/src/tunmode/socket/udpsocket.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <tunmode/common/utils.hpp>
#include <tunmode/common/inbuffer.hpp>
#include <tunmode/definitions.hpp>
#include <tunmode/tunmode.hpp>

#include <unistd.h>
#include <cstring>
Expand All @@ -12,8 +13,6 @@
#include <netinet/ip.h>
#include <netinet/udp.h>

#include <misc/logger.hpp>

namespace tunmode
{
UDPSocket::UDPSocket() : SessionSocket() {}
Expand All @@ -39,9 +38,7 @@ namespace tunmode
this->server_addr = ip_header->ip_dst;
this->server_port = udp_header->uh_dport;

in_addr sv_addr;
inet_pton(AF_INET, "192.168.1.101", &sv_addr);
skt->bind(sv_addr, 0);
skt->bind(params::net_iface, 0);
skt->connect(this->server_addr, this->server_port);

InBuffer in_buffer = client_packet.get_data();
Expand Down
7 changes: 3 additions & 4 deletions app/src/main/cpp/src/tunmode/tunmode.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <tunmode/socket/sessionsocket.hpp>
#include <tunmode/manager/tcpmanager.hpp>
#include <tunmode/manager/udpmanager.hpp>
#include <misc/logger.hpp>

#include <future>
#include <string>
Expand All @@ -12,12 +11,15 @@
#include <poll.h>
#include <unistd.h>

#include <misc/logger.hpp>

namespace tunmode
{
namespace params
{
JavaVM* jvm;
TunSocket tun;
in_addr net_iface;
in_addr dns_address;
jobject TunModeService_object;
std::atomic<bool> stop_flag;
Expand Down Expand Up @@ -51,10 +53,8 @@ namespace tunmode

if (status == JNI_EDETACHED) {
if (params::jvm->AttachCurrentThread(env, nullptr) != 0) {
LOGE(TAG, "Couldn't attach thread");
return 2; // Failed to attach
}
LOGI(TAG, "Attached JNIEnv*");
return 1; // Attached, need detach
}

Expand Down Expand Up @@ -87,7 +87,6 @@ namespace tunmode

if (ret == -1)
{
LOGE(TAG, "Poll Error");
break;
}
else if (ret == 0)
Expand Down
Loading

0 comments on commit 317d9b4

Please sign in to comment.