-
Notifications
You must be signed in to change notification settings - Fork 5
/
config.hpp
140 lines (112 loc) · 2.93 KB
/
config.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
static bool parse_int(std::string &str, int &ret, int base=10)
{
char *e;
ret = strtol(str.c_str(), &e, base);
return (*e == 0);
}
static bool parse_float(std::string &str, float &ret)
{
char *e;
ret = strtod(str.c_str(), &e);
return (*e == 0);
}
template <class T>
static bool parse_enum_or_int(std::string &str, int &ret, int def=-1)
{
T val;
if (str.length())
{
if (!parse_int(str, ret))
{
if (find_enum_item(&val, str))
ret = val;
else
return false;
}
}
else
ret = def;
return true;
}
static vector<string> split(const char *str, char c = ' ')
{
vector<string> result;
do
{
const char *begin = str;
while(*str != c && *str)
str++;
result.push_back(string(begin, str));
} while (0 != *str++);
return result;
}
static bool load_config()
{
bool enabled = false;
std::ifstream fseed("data/init/remote.txt");
if(!fseed.is_open())
return false;
char *tmp = getenv("DFREMOTE_PWD");
if (tmp)
pwd_hash = hash_password(std::string(tmp));
else
{
tmp = getenv("DFREMOTE_PWD_HASH");
pwd_hash = tmp ? tmp : "";
}
string str;
while(std::getline(fseed,str))
{
size_t b = str.find("[");
size_t e = str.rfind("]");
if (b == string::npos || e == string::npos || str.find_first_not_of(" ") < b)
continue;
str = str.substr(b+1, e-1);
vector<string> tokens = split(str.c_str(), ':');
if (tokens[0] == "ENABLED" && tokens.size() == 2)
{
int val;
if (parse_int(tokens[1], val))
enabled = val;
continue;
}
if (tokens[0] == "PORT" && tokens.size() == 2)
{
int val;
if (parse_int(tokens[1], val))
enet_port = val;
continue;
}
if (tokens[0] == "PUBLISH_NAME" && tokens.size() == 2)
{
publish_name = tokens[1];
continue;
}
if (tokens[0] == "PWD" && tokens.size() == 2)
{
pwd_hash = tokens[1];
continue;
}
if (tokens[0] == "ADVFLAGS" && tokens.size() == 2)
{
int val;
if (parse_int(tokens[1], val))
advflags = val;
continue;
}
}
return enabled;
}
void save_config()
{
std::ofstream ofs;
ofs.open("data/init/remote.txt", std::ofstream::out|std::ofstream::trunc);
if(!ofs.is_open())
return;
ofs << "[ENABLED:" << (int)remote_on << "]" << std::endl;
ofs << "[PORT:" << enet_port << "]" << std::endl;
ofs << "[PUBLISH_NAME:" << publish_name << "]" << std::endl;
ofs << "[PWD:" << pwd_hash << "]" << std::endl;
if (advflags)
ofs << "[ADVFLAGS:" << advflags << "]" << std::endl;
}