Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Support build from current source and support domain parsing when used under docker-compose #383

Open
wants to merge 3 commits into
base: unified
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ FROM alpine:3.6 as builder

WORKDIR /

RUN apk add --no-cache git build-base linux-headers && \
git clone https://github.com/wangyu-/udp2raw-tunnel.git && \
cd udp2raw-tunnel && \
make dynamic
COPY . udp2raw-tunnel

RUN apk add --no-cache git build-base linux-headers && cd udp2raw-tunnel && make dynamic

#RUN apk add --no-cache git build-base linux-headers && \
# git clone https://github.com/wangyu-/udp2raw-tunnel.git && \
# cd udp2raw-tunnel && \
# make dynamic

FROM alpine:3.6
RUN apk add --no-cache libstdc++ iptables
Expand Down
57 changes: 53 additions & 4 deletions common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,31 @@
#include "log.h"
#include "misc.h"

#include <assert.h>
#include <random>
#include <cmath>
#include <string>
#include <iostream>

//static int random_number_fd=-1;
int force_socket_buf=0;

int address_t::from_str(char *str)
int address_t::from_str(std::string str)
{
clear();

char ip_addr_str[100];u32_t port;
mylog(log_info,"parsing address: %s\n",str);
mylog(log_info,"parsing address: %s\n",str.c_str());
int is_ipv6=0;
if(sscanf(str, "[%[^]]]:%u", ip_addr_str,&port)==2)

#if 0
if(sscanf(str.c_str(), "[%[^]]]:%u", ip_addr_str,&port)==2)
{
mylog(log_info,"its an ipv6 adress\n");
inner.ipv6.sin6_family=AF_INET6;
is_ipv6=1;
}
else if(sscanf(str, "%[^:]:%u", ip_addr_str,&port)==2)
else if(sscanf(str.c_str(), "%[^:]:%u", ip_addr_str,&port)==2)
{
mylog(log_info,"its an ipv4 adress\n");
inner.ipv4.sin_family=AF_INET;
Expand All @@ -39,6 +44,50 @@ int address_t::from_str(char *str)
myexit(-1);
}

#else
auto found_colon = str.rfind(":");
if (found_colon == std::string::npos) {
mylog(log_error, "failed to parse\n");
myexit(-1);
}
std::string hostname = str.substr(0, found_colon);
std::string portstr = str.substr(found_colon+1);
if (hostname.empty() || portstr.empty()) {
mylog(log_error, "failed to parse\n");
myexit(-1);
}
assert(sscanf(portstr.c_str(), "%u", &port) == 1);
mylog(log_info, "check hostname: %s\n", hostname.c_str());
struct addrinfo *addr_ret = nullptr;
int h_ret = getaddrinfo(hostname.c_str(), NULL, NULL, &addr_ret); // TODO fill hint
if (h_ret != 0) {
mylog(log_error, "getaddrinfo failed: %d\n", h_ret);
myexit(-1);
}
if (addr_ret == nullptr) {
mylog(log_error, "cannot resolve hostname\n");
myexit(-1);
}
// just use the first host info
auto rp = addr_ret;
// TODO Maybe I can just assign getaddrinfo results to inner...
switch (rp->ai_family) {
case AF_INET:
inner.ipv4.sin_family=AF_INET;
strcpy(ip_addr_str, inet_ntoa(((struct sockaddr_in*)(rp->ai_addr))->sin_addr));
break;
case AF_INET6:
inner.ipv6.sin6_family=AF_INET6;
is_ipv6=1;
inet_ntop(AF_INET6, &(((struct sockaddr_in6*)(rp->ai_addr))->sin6_addr), ip_addr_str, INET6_ADDRSTRLEN);
break;
default:
mylog(log_error,"failed to parse\n");
myexit(-1);
}

#endif

mylog(log_info,"ip_address is {%s}, port is {%u}\n",ip_addr_str,port);

if(port>65535)
Expand Down
5 changes: 4 additions & 1 deletion common.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ const int is_udp2raw_mp=0;
#if defined(__MINGW32__)
#include <winsock2.h>
#include <ws2ipdef.h>
#include <ws2def.h>
#include <ws2tcpip.h>
typedef unsigned char u_int8_t;
typedef unsigned short u_int16_t;
typedef unsigned int u_int32_t;
Expand All @@ -78,6 +80,7 @@ typedef int socklen_t;
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#endif


Expand Down Expand Up @@ -238,7 +241,7 @@ struct address_t //TODO scope id
return 0;
}

int from_str(char * str);
int from_str(std::string str);

int from_str_ip_only(char * str);

Expand Down
8 changes: 4 additions & 4 deletions misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ int process_lower_level_arg()//handle --lower-level option
lower_level_manual=1;
if (strchr(optarg, '#') == 0) {
mylog(log_fatal,
"lower-level parameter invaild,check help page for format\n");
"lower-level parameter invalid,check help page for format\n");
myexit(-1);
}
lower_level = 1;
Expand Down Expand Up @@ -359,12 +359,12 @@ void process_arg(int argc, char *argv[]) //process all options
}
if(len==1&&argv[i][0]=='-' )
{
mylog(log_fatal,"invaild option '-' in argv\n");
mylog(log_fatal,"invalid option '-' in argv\n");
myexit(-1);
}
if(len==2&&argv[i][0]=='-'&&argv[i][1]=='-' )
{
mylog(log_fatal,"invaild option '--' in argv\n");
mylog(log_fatal,"invalid option '--' in argv\n");
myexit(-1);
}
}
Expand All @@ -386,7 +386,7 @@ void process_arg(int argc, char *argv[]) //process all options

if(all_options.find(a.c_str())==all_options.end())
{
mylog(log_fatal,"invaild option %s\n",a.c_str());
mylog(log_fatal,"invalid option %s\n",a.c_str());
myexit(-1);
}
for(j=i+1;j<argc;j++)
Expand Down