Last active
July 14, 2017 15:36
-
-
Save sug0/7d1d61f28efb235b8b24e3e832615f39 to your computer and use it in GitHub Desktop.
libmill based pastebin clone
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// build: $ cc -o paste paste.c -lmill | |
// run: $ ./paste &; disown | |
// use: $ alias pastebin="nc localhost 6969" | |
// $ echo wat up pimp | pastebin | |
// $ pastebin < source_code.c | |
#include <stdio.h> | |
#include <string.h> | |
#include <ctype.h> | |
#include <assert.h> | |
#include <libmill.h> | |
#define tcpstr(S, STR) tcpsend(S, STR, sizeof(STR) - 1, -1) | |
static void rand_chars(char *buf, size_t size, size_t n) | |
{ | |
assert (n <= size); | |
bzero(buf, size); | |
int i, c, byte = 0; | |
FILE *fp = fopen("/dev/urandom", "r"); | |
if (!fp) | |
return; | |
for (i = 1; i <= n; i++) { | |
do { | |
c = fgetc(fp); | |
} while (!isalnum(c)); | |
buf[byte++] = c; | |
} | |
fclose(fp); | |
} | |
static coroutine void paste(tcpsock as) | |
{ | |
char filename[4096], chars[7]; | |
rand_chars(chars, sizeof(chars), 5); | |
snprintf(filename, sizeof(filename), | |
"/home/upload/p/%s.txt", chars); | |
char buf[20480]; // 20 kib | |
bzero(buf, sizeof(buf)); | |
tcprecv(as, buf, sizeof(buf), now() + 3000); // 3 secs | |
if (strlen(buf) == 0) { | |
tcpstr(as, "error: nothing to send\n"); | |
goto cleanup; | |
} | |
FILE *fp = fopen(filename, "w"); | |
if (!fp) { | |
tcpstr(as, "error: couldn't upload file\n"); | |
} else { | |
fprintf(fp, "%s", buf); | |
fclose(fp); | |
snprintf(filename, sizeof(filename), | |
"https://gandas.us.to/p/%s.txt\n", chars); | |
tcpsend(as, filename, strlen(filename), -1); | |
} | |
cleanup: | |
tcpflush(as, -1); | |
tcpclose(as); | |
} | |
int main(void) | |
{ | |
ipaddr addr = iplocal(NULL, 6969, 0); | |
tcpsock ls = tcplisten(addr, -1); | |
if (!ls) { | |
perror("fatal: failed to open listening socket"); | |
return 1; | |
} | |
while (1) { | |
tcpsock as = tcpaccept(ls, -1); | |
if (!as) | |
continue; | |
go (paste(as)); | |
} | |
tcpclose(ls); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment