-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cc
228 lines (208 loc) · 6.24 KB
/
main.cc
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "lib/conf.h"
#include "lib/socket.h"
#include "lib/macros.h"
#include "args.h"
#include "settings.h"
#include <fcntl.h>
#include <stdio.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <signal.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#if defined(__GLIBC__)
#include <pty.h>
#elif defined(__FreeBSD__)||defined(__FreeBSD_kernel__)||defined(__DragonFly__)
#include <libutil.h>
#else
#include <util.h>
#endif
void exitWithMessage(const char*);
struct termios termios_saved;
int fd,pid;
KVSocket*sock;
Config*config=NULL;
void chldfunc(int n){
exitWithMessage("broadcast end.");
}
void sendwinch(){
winsize win;
ioctl(STDOUT_FILENO,TIOCGWINSZ,&win);
char buf[SMALL_BUF_SIZE];
sprintf(buf,"{\"width\":%d,\"height\":%d}",win.ws_col,win.ws_row);
sock->send("winch",buf);
ioctl(fd,TIOCSWINSZ,&win);
}
bool resized=false;
void*winchwatchthread(void*){
while(true){
usleep(100*1000);
if(resized){
resized=false;
sendwinch();
}
}
}
void winchfunc(int n){
resized=true;
}
void*loop_fdread(void*){
char buf[8192];int rd;
while((rd=read(fd,buf,sizeof(buf)))>0){
write(STDOUT_FILENO,buf,rd);
sock->send("data",buf,rd);
}
fclose(stdin);
}
void*loop_fdwrite(void*){
char buf[8192];
int len;
while((len=read(STDIN_FILENO,buf,sizeof(buf)))>0){
write(fd,buf,len);
}
}
bool auth(Config*config){
char buf[1024];
while(true){
char username[SMALL_BUF_SIZE];
printf("user name> ");fflush(stdout);
fgets(username,sizeof(username),stdin);
char*usr=trim(username);
if(strlen(usr)==0)return false;
/* Read password without echoing input. */
printf("password> ");fflush(stdout);
char *password = (char *)malloc(SMALL_BUF_SIZE);
size_t n = SMALL_BUF_SIZE;
my_getpass(&password, &n, stdin);
printf("\n\n");
char*pswd=trim(password);
char key[SMALL_BUF_SIZE],value[SMALL_BUF_SIZE];
sprintf(buf,"{\"user\":\"%s\",\"password\":\"%s\"}",usr,pswd);
sock=new KVSocket("screenx.tv",8000);
sock->send("init",buf);
sock->read(key,value,sizeof(key),sizeof(value));
if(streq(key,"auth")){
config->put("user",usr);
value[0]=value[strlen(value)-1]='\0';
config->put("auth_key",value+1);
free(password);
return true;
}
else{
printf("Invalid credentials. Please try again.\n");
}
free(password);
}
}
int main(int argc, char *argv[]){
if(getenv("SCREENXTV_BROADCASTING")){
printf("cannot broadcast inside broadcasting screen\n");
return 0;
}
setenv("SCREENXTV_BROADCASTING","1",1);
winsize win;
char buf[LARGE_BUF_SIZE];
tcgetattr(STDIN_FILENO,&termios_saved);
OPTION option={0,0,0,0,0,0,0,0,0};
char default_file[1024];
sprintf(default_file,"%s/.screenxtv.yml",getenv("HOME"));
const char*file=default_file;
option=parseArgs(argv+1);
if(option.help){showHelp();return 0;}
if(option.file)file=option.file;
config=new Config(file);
if(option.reset)config->clear();
if(option.url)config->put("url",option.url);
if(option.title)config->put("title",option.title);
if(option.color)config->put("color",option.color);
if(option.url_read)config->put("url",NULL);
if(option.title_read)config->put("title",NULL);
if(option.color_read)config->put("color",NULL);
for(int i=0;SCAN_SETTINGS[i].key;i++){
ScanMsg sm=SCAN_SETTINGS[i];
if(config->get(sm.key))continue;
char*value=NULL;
if(sm.msg){
printf("%s\n> ",sm.msg);fflush(stdout);
fgets(buf,sizeof(buf),stdin);
value=trim(buf);
}
config->put(sm.key,value&&strlen(value)?value:sm.value);
}
char key[SMALL_BUF_SIZE],value[LARGE_BUF_SIZE];
while(true){
config->save();
ioctl(STDOUT_FILENO,TIOCGWINSZ,&win);
char user[SMALL_BUF_SIZE],auth_key[SMALL_BUF_SIZE];
if(config->get("user"))sprintf(user,"\"%s\"",config->get("user"));
else sprintf(user,"null");
if(config->get("auth_key"))sprintf(auth_key,"\"%s\"",config->get("auth_key"));
else sprintf(auth_key,"null");
sprintf(buf,"{\"width\":%d,\"height\":%d,\"slug\":\"%s#%s\",\"user\":%s,\"auth_key\":%s,\"info\":{\"color\":\"%s\",\"title\":\"%s\"}}",
win.ws_col,win.ws_row,config->get("url"),config->get("urlhash"),user,auth_key,config->get("color"),config->get("title"));
sock=new KVSocket("screenx.tv",8000);
sock->send("init",buf);
sock->read(key,value,sizeof(key),sizeof(value));
if(streq(key,"slug")){
char*hash=NULL;
value[0]=value[strlen(value)-1]='\0';
for(int i=1;value[i];i++){
if(value[i]!='#')continue;
value[i]='\0';
hash=value+i+1;
break;
}
config->put("url",value+1);
config->put("urlhash",hash);
break;
}
sock->fdclose();
if(strstr(value,"reserved")||strstr(value,"auth_key")){
printf("The url '%s' is reserved. Please sign in.\n",config->get("url"));
if(!auth(config)){
printf("Please set another url.\n");fflush(stdout);
fgets(buf,sizeof(buf),stdin);
config->put("url",trim(buf));
}
}else{
printf("url already in use. another url?\n> ");fflush(stdout);
fgets(buf,sizeof(buf),stdin);
config->put("url",trim(buf));
}
}
config->save();
printf("Your URL is http://screenx.tv/%s\n\n",config->get("url"));
printf("Press Enter to start broadcasting\n> ");fflush(stdout);
fgets(buf,sizeof(buf),stdin);
signal(SIGWINCH,winchfunc);
signal(SIGCHLD,chldfunc);
sendwinch();
pthread_t ptt;
pthread_create(&ptt,NULL,winchwatchthread,NULL);
struct termios tm=termios_saved;
cfmakeraw(&tm);
tcsetattr(STDIN_FILENO,TCSANOW,&tm);
setenv("TERM","vt100",1);
setenv("LANG","en_US.UTF-8",1);
if(!(pid=forkpty(&fd,NULL,NULL,&win)))
execlp("screen","screen","-x",config->get("screen"),"-R",NULL);
pthread_create(&ptt,NULL,loop_fdread,NULL);
loop_fdwrite(NULL);
exitWithMessage("broadcast end.");
}
void exitWithMessage(const char*msg){
tcsetattr(STDIN_FILENO,TCSANOW,&termios_saved);
tcsetattr(STDOUT_FILENO,TCSANOW,&termios_saved);
tcsetattr(STDERR_FILENO,TCSANOW,&termios_saved);
winsize win;
ioctl(STDOUT_FILENO,TIOCGWINSZ,&win);
printf("\e[?1l\e[>\e[1;%dr\e[%d;1H",win.ws_row,win.ws_row);
printf("%s\n",msg);
exit(0);
}