diff --git a/config.example b/config.example new file mode 100644 index 0000000..470ea1d --- /dev/null +++ b/config.example @@ -0,0 +1,16 @@ +# Basically this file is the equivalent to splitting the command line options into multiple lines +# Each line should contain an option + +# This is client +-c +# Or use -s if you use it on server side +# Define local address +-l 127.0.0.1:56789 +# Define remote address +-r 45.66.77.88:45678 +# Password +-k my_awesome_password +# Mode +--raw-mode faketcp +# Log Level +--log-level 4 diff --git a/main.cpp b/main.cpp index bbc30ec..942a484 100755 --- a/main.cpp +++ b/main.cpp @@ -3,6 +3,9 @@ #include "log.h" #include "lib/md5.h" #include "encrypt.h" +#include +#include +#include char local_ip[100]="0.0.0.0", remote_ip[100]="255.255.255.255",source_ip[100]="0.0.0.0"; u32_t local_ip_uint32,remote_ip_uint32,source_ip_uint32; @@ -2517,6 +2520,7 @@ void print_help() printf(" this option disables port changing while re-connecting\n"); // printf(" \n"); printf("other options:\n"); + printf(" --config-file read options from a configuration file instead of command line\n"); printf(" --log-level 0:never 1:fatal 2:error 3:warn \n"); printf(" 4:info (default) 5:debug 6:trace\n"); // printf("\n"); @@ -2540,6 +2544,43 @@ void print_help() //printf("common options,these options must be same on both side\n"); } +void process_arg(int argc, char *argv[]); +void load_config(char *config_file, char *argv0) +{ + // Load configurations from config_file instead of the command line. + // See config.example for example configurations + std::ifstream conf_file(config_file); + std::string line; + std::vector arguments; + while(std::getline(conf_file,line)) + { + if(line.at(0)=='#') + { + continue; + } + auto pos = line.find(" ",0); + if(pos==std::string::npos) + { + arguments.push_back(line); + } + else + { + auto p1 = line.substr(0,pos); + auto p2 = line.substr(pos+1,line.length() - pos - 1); + arguments.push_back(p1); + arguments.push_back(p2); + } + } + conf_file.close(); + int argc = arguments.size(); + char *argv[argc+1]; + argv[0]=argv0; + for(int i=0; i