Add experimental scp support
This commit is contained in:
parent
272f41022b
commit
2daedef710
2 changed files with 68 additions and 21 deletions
|
@ -7,7 +7,9 @@
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <curl/curl.h>
|
#include <curl/curl.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
#include "curl/easy.h"
|
||||||
#include "options.h"
|
#include "options.h"
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "funcs.h"
|
#include "funcs.h"
|
||||||
|
@ -96,13 +98,15 @@ main(int argc, char **argv)
|
||||||
{ "ipv4", no_argument, 0, '4' },
|
{ "ipv4", no_argument, 0, '4' },
|
||||||
{ "ipv6", no_argument, 0, '6' },
|
{ "ipv6", no_argument, 0, '6' },
|
||||||
{ "paste", no_argument, 0, 'x' },
|
{ "paste", no_argument, 0, 'x' },
|
||||||
|
{ "key", required_argument, 0, 'k' },
|
||||||
{ 0, 0, 0, 0 }
|
{ 0, 0, 0, 0 }
|
||||||
};
|
};
|
||||||
|
|
||||||
int c = 0;
|
int c = 0;
|
||||||
while((c = getopt_long(
|
while(
|
||||||
argc, argv, "46hT:p:P:Ss:xC", long_options, &option_index)) !=
|
(c = getopt_long(
|
||||||
-1) {
|
argc, argv, "46hT:p:P:Ss:xCk:", long_options, &option_index)) !=
|
||||||
|
-1) {
|
||||||
switch(c) {
|
switch(c) {
|
||||||
case 's':
|
case 's':
|
||||||
server = optarg;
|
server = optarg;
|
||||||
|
@ -142,6 +146,10 @@ main(int argc, char **argv)
|
||||||
case 'C':
|
case 'C':
|
||||||
print_config();
|
print_config();
|
||||||
return 0;
|
return 0;
|
||||||
|
case 'k':
|
||||||
|
curl_easy_setopt(
|
||||||
|
easy_handle, CURLOPT_SSH_PRIVATE_KEYFILE, optarg);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
print_usage();
|
print_usage();
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -157,8 +165,11 @@ main(int argc, char **argv)
|
||||||
/* curl options */
|
/* curl options */
|
||||||
curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, write_data);
|
curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, write_data);
|
||||||
curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, buffer);
|
curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, buffer);
|
||||||
|
|
||||||
curl_easy_setopt(easy_handle, CURLOPT_URL, server);
|
curl_easy_setopt(easy_handle, CURLOPT_URL, server);
|
||||||
|
|
||||||
|
int protocol = get_protocol(server);
|
||||||
|
|
||||||
/* Proxy options */
|
/* Proxy options */
|
||||||
|
|
||||||
if(socks_proxy_flag && http_proxy_flag) {
|
if(socks_proxy_flag && http_proxy_flag) {
|
||||||
|
@ -168,7 +179,8 @@ main(int argc, char **argv)
|
||||||
curl_easy_setopt(easy_handle, CURLOPT_PROXY, socks_proxy_url);
|
curl_easy_setopt(easy_handle, CURLOPT_PROXY, socks_proxy_url);
|
||||||
curl_easy_setopt(
|
curl_easy_setopt(
|
||||||
easy_handle, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
|
easy_handle, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
|
||||||
} else if(http_proxy_flag) {
|
} else if(http_proxy_flag && ((protocol == CURLPROTO_HTTP) ||
|
||||||
|
(protocol == CURLPROTO_HTTPS))) {
|
||||||
curl_easy_setopt(easy_handle, CURLOPT_PROXY, http_proxy_url);
|
curl_easy_setopt(easy_handle, CURLOPT_PROXY, http_proxy_url);
|
||||||
curl_easy_setopt(easy_handle, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
|
curl_easy_setopt(easy_handle, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
|
||||||
}
|
}
|
||||||
|
@ -190,33 +202,65 @@ main(int argc, char **argv)
|
||||||
/* TODO: make it iterate on args so you can upload multiple files
|
/* TODO: make it iterate on args so you can upload multiple files
|
||||||
* at once (sakisafecli file1 file2 ... filen)
|
* at once (sakisafecli file1 file2 ... filen)
|
||||||
*/
|
*/
|
||||||
curl_mimepart *file_data;
|
|
||||||
file_data = curl_mime_addpart(mime);
|
|
||||||
char *filename = argv[optind];
|
|
||||||
|
|
||||||
if(paste_flag)
|
/* Process HTTP uploads */
|
||||||
filename = "/dev/stdin";
|
|
||||||
|
|
||||||
curl_mime_filedata(file_data, filename);
|
printf("%d\n", protocol);
|
||||||
curl_mime_name(file_data, form_key);
|
|
||||||
if(paste_flag)
|
|
||||||
curl_mime_filename(file_data, "-");
|
|
||||||
|
|
||||||
curl_easy_setopt(easy_handle, CURLOPT_NOPROGRESS, silent_flag);
|
if(protocol == CURLPROTO_HTTP || protocol == CURLPROTO_HTTPS) {
|
||||||
curl_easy_setopt(easy_handle, CURLOPT_PROGRESSFUNCTION, progress);
|
curl_mimepart *file_data;
|
||||||
|
file_data = curl_mime_addpart(mime);
|
||||||
|
char *filename = argv[optind];
|
||||||
|
|
||||||
curl_easy_setopt(easy_handle, CURLOPT_MIMEPOST, mime);
|
if(paste_flag)
|
||||||
curl_easy_perform(easy_handle);
|
filename = "/dev/stdin";
|
||||||
|
|
||||||
|
curl_mime_filedata(file_data, filename);
|
||||||
|
curl_mime_name(file_data, form_key);
|
||||||
|
if(paste_flag)
|
||||||
|
curl_mime_filename(file_data, "-");
|
||||||
|
|
||||||
|
curl_easy_setopt(easy_handle, CURLOPT_NOPROGRESS, silent_flag);
|
||||||
|
curl_easy_setopt(easy_handle, CURLOPT_PROGRESSFUNCTION, progress);
|
||||||
|
|
||||||
|
curl_easy_setopt(easy_handle, CURLOPT_MIMEPOST, mime);
|
||||||
|
curl_easy_perform(easy_handle);
|
||||||
|
puts(buffer);
|
||||||
|
if(protocol == CURLPROTO_HTTP)
|
||||||
|
curl_mime_free(mime);
|
||||||
|
} else if(protocol == CURLPROTO_SCP) {
|
||||||
|
curl_easy_setopt(easy_handle, CURLOPT_UPLOAD, true);
|
||||||
|
FILE *fp = fopen(argv[optind], "r");
|
||||||
|
struct stat st;
|
||||||
|
stat(argv[optind], &st);
|
||||||
|
curl_easy_setopt(easy_handle, CURLOPT_READDATA, fp);
|
||||||
|
curl_easy_setopt(easy_handle, CURLOPT_NOPROGRESS, silent_flag);
|
||||||
|
curl_easy_setopt(easy_handle, CURLOPT_PROGRESSFUNCTION, progress);
|
||||||
|
curl_easy_setopt(
|
||||||
|
easy_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t)st.st_size);
|
||||||
|
printf("%d\n", curl_easy_perform(easy_handle));
|
||||||
|
} else {
|
||||||
|
puts("Unsupported protocol");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
if(!silent_flag)
|
if(!silent_flag)
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
|
|
||||||
puts(buffer);
|
|
||||||
|
|
||||||
curl_mime_free(mime);
|
|
||||||
curl_easy_cleanup(easy_handle);
|
curl_easy_cleanup(easy_handle);
|
||||||
|
|
||||||
free(buffer);
|
free(buffer);
|
||||||
config_destroy(&runtime_config);
|
config_destroy(&runtime_config);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
get_protocol(char *server)
|
||||||
|
{
|
||||||
|
if(strstr(server, "http://") != NULL || strstr(server, "https://"))
|
||||||
|
return CURLPROTO_HTTP;
|
||||||
|
else if(strstr(server, "scp://") != NULL)
|
||||||
|
return CURLPROTO_SCP;
|
||||||
|
else
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
|
@ -20,5 +20,8 @@ progress(
|
||||||
void *clientp, double dltotal, double dlnow, double ultotal, double ulnow);
|
void *clientp, double dltotal, double dlnow, double ultotal, double ulnow);
|
||||||
|
|
||||||
void
|
void
|
||||||
parse_config_file(FILE *config);
|
parse_config_file(FILE *config);
|
||||||
|
int
|
||||||
|
get_protocol(char *server);
|
||||||
|
|
||||||
#endif /* SAKISAFECLI_H */
|
#endif /* SAKISAFECLI_H */
|
||||||
|
|
Loading…
Add table
Reference in a new issue