Mercurial > hg > nnchat
comparison main.c @ 608:a8fa7803284a
Change how proxy is specified on commandline.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 20 May 2014 04:35:24 +0300 |
parents | c7b8e299c612 |
children | b10d81ddfc55 |
comparison
equal
deleted
inserted
replaced
607:c7b8e299c612 | 608:a8fa7803284a |
---|---|
118 { 4, 'C', "color", "Initial color in RGB hex 000000", OPT_ARGREQ }, | 118 { 4, 'C', "color", "Initial color in RGB hex 000000", OPT_ARGREQ }, |
119 { 6, 'D', "daemon", "A pseudo-daemon mode for logging", OPT_NONE }, | 119 { 6, 'D', "daemon", "A pseudo-daemon mode for logging", OPT_NONE }, |
120 { 7, 'f', "force-site", "Force site (default: NN)", OPT_ARGREQ }, | 120 { 7, 'f', "force-site", "Force site (default: NN)", OPT_ARGREQ }, |
121 { 8, 'd', "debug", "Enable various debug features", OPT_NONE }, | 121 { 8, 'd', "debug", "Enable various debug features", OPT_NONE }, |
122 | 122 |
123 {10, '4', "socks4", "SOCKS4 proxy server", OPT_ARGREQ }, | 123 {10, 'P', "proxy", "Set proxy data, see below for syntax", OPT_ARGREQ }, |
124 {11, 'A', "socks4a", "SOCKS4A proxy server", OPT_ARGREQ }, | |
125 {12, 'P', "proxy-port", "Proxy port (default: 1080)", OPT_ARGREQ }, | |
126 | 124 |
127 {13, 'r', "room", "Connect to room (main, pit)", OPT_ARGREQ }, | 125 {13, 'r', "room", "Connect to room (main, pit)", OPT_ARGREQ }, |
128 }; | 126 }; |
129 | 127 |
130 const int optListN = (sizeof(optList) / sizeof(optList[0])); | 128 const int optListN = (sizeof(optList) / sizeof(optList[0])); |
135 int i; | 133 int i; |
136 th_print_banner(stdout, th_prog_name, "[options] <username> <password>"); | 134 th_print_banner(stdout, th_prog_name, "[options] <username> <password>"); |
137 | 135 |
138 th_args_help(stdout, optList, optListN); | 136 th_args_help(stdout, optList, optListN); |
139 | 137 |
140 printf("Supported rooms (for option '-r'):\n"); | 138 printf( |
139 "\n" | |
140 "Supported proxy types are SOCKS 4/4A and SOCKS 5.\n" | |
141 "These can be set with the -P option as follows:\n" | |
142 "\n" | |
143 " -P <type>://[<userid>[:passwd]@]<host>[:<port>]\n" | |
144 "\n" | |
145 "Type can be socks4, socks4a or socks5. Only socks5\n" | |
146 "supports user/pass authentication.\n" | |
147 "\n" | |
148 "Supported rooms (for option '-r'):\n"); | |
149 | |
141 for (i = 0; i < nn_room_data_n; i++) | 150 for (i = 0; i < nn_room_data_n; i++) |
142 { | 151 { |
143 printf(" %s - %s (port %d)\n", | 152 printf(" %s - %s (port %d)\n", |
144 nn_room_data[i].name, | 153 nn_room_data[i].name, |
145 nn_room_data[i].desc, | 154 nn_room_data[i].desc, |
146 nn_room_data[i].port); | 155 nn_room_data[i].port); |
147 } | 156 } |
148 } | 157 } |
149 | 158 |
150 | 159 |
160 BOOL argSplitStr(const char *src, const char *at, char **res1, char **res2) | |
161 { | |
162 char *pos, *tmp = th_strdup(src); | |
163 | |
164 if (tmp != NULL && (pos = strstr(tmp, at)) != NULL) | |
165 { | |
166 *pos = 0; | |
167 pos += strlen(at); | |
168 *res1 = th_strdup_trim(tmp, TH_TRIM_BOTH); | |
169 *res2 = th_strdup_trim(pos, TH_TRIM_BOTH); | |
170 th_free(tmp); | |
171 return TRUE; | |
172 } | |
173 else | |
174 { | |
175 th_free(tmp); | |
176 return FALSE; | |
177 } | |
178 } | |
179 | |
180 | |
181 BOOL argHandleProxyURI(const char *uri) | |
182 { | |
183 // Attempt to parse the proxy URI | |
184 BOOL ret = FALSE; | |
185 char *proto = NULL, *rest = NULL, *host = NULL, | |
186 *auth = NULL, *port = NULL; | |
187 | |
188 if (!argSplitStr(uri, "://", &proto, &rest)) | |
189 { | |
190 THERR("Malformed proxy URI, should be <type>://[<userid>[:passwd]@]<host>[:<port>]\n"); | |
191 goto out; | |
192 } | |
193 | |
194 // Validate proxy type | |
195 if (strcasecmp(proto, "socks4") == 0) | |
196 optProxyType = NN_PROXY_SOCKS4; | |
197 else | |
198 if (strcasecmp(proto, "socks4a") == 0) | |
199 optProxyType = NN_PROXY_SOCKS4A; | |
200 else | |
201 { | |
202 THERR("Invalid proxy type specified: '%s'\n", proto); | |
203 goto out; | |
204 } | |
205 | |
206 // Does the URI contain anything else? | |
207 if (strlen(rest) == 0) | |
208 { | |
209 THERR("Malformed proxy URI, no host specified.\n"); | |
210 goto out; | |
211 } | |
212 | |
213 // Check for auth credentials | |
214 if (argSplitStr(rest, "@", &auth, &host)) | |
215 { | |
216 if (strlen(auth) == 0) | |
217 { | |
218 THERR("Malformed proxy URI, zero length authentication credentials.\n"); | |
219 goto out; | |
220 } | |
221 | |
222 // Should have authentication credentials | |
223 if (!argSplitStr(auth, ":", &optProxyUserID, &optProxyPassword)) | |
224 optProxyUserID = th_strdup(auth); | |
225 } | |
226 else | |
227 host = th_strdup(rest); | |
228 | |
229 // Check if proxy port was specified | |
230 if (argSplitStr(host, ":", &optProxyServer, &port)) | |
231 optProxyPort = atoi(port); | |
232 else | |
233 optProxyServer = th_strdup(host); | |
234 | |
235 // Check what authentication type to use | |
236 if (optProxyType == NN_PROXY_SOCKS5 && | |
237 optProxyUserID != NULL && optProxyPassword != NULL) | |
238 optProxyAuthType = NN_PROXY_AUTH_USER; | |
239 else | |
240 optProxyAuthType = NN_PROXY_AUTH_NONE; | |
241 | |
242 ret = TRUE; | |
243 | |
244 out: | |
245 th_free(proto); | |
246 th_free(rest); | |
247 th_free(host); | |
248 th_free(auth); | |
249 th_free(port); | |
250 | |
251 return ret; | |
252 } | |
253 | |
151 BOOL argHandleOpt(const int optN, char *optArg, char *currArg) | 254 BOOL argHandleOpt(const int optN, char *optArg, char *currArg) |
152 { | 255 { |
153 switch (optN) | 256 switch (optN) |
154 { | 257 { |
155 case 0: | 258 case 0: |
193 THMSG(1, "Debug mode enabled.\n"); | 296 THMSG(1, "Debug mode enabled.\n"); |
194 break; | 297 break; |
195 | 298 |
196 | 299 |
197 case 10: | 300 case 10: |
198 optProxyServer = optArg; | 301 if (!argHandleProxyURI(optArg)) |
199 optProxyType = NN_PROXY_SOCKS4; | 302 return FALSE; |
200 break; | |
201 | |
202 case 11: | |
203 optProxyServer = optArg; | |
204 optProxyType = NN_PROXY_SOCKS4A; | |
205 break; | |
206 | |
207 case 12: | |
208 optProxyPort = atoi(optArg); | |
209 break; | 303 break; |
210 | 304 |
211 case 13: | 305 case 13: |
212 { | 306 { |
213 int i; | 307 int i; |