📄 servhs.cpp
字号:
if (!doneHandshake) while (sock->readLine(in,128)); if (getLocalURL(url)) { writePLSHeader(*sock,PlayList::T_SCPLS); PlayList *pls; pls = new PlayList(PlayList::T_SCPLS,num); for(int i=0; i<num; i++) pls->addChannel(url,cl[i]->info); pls->write(*sock); delete pls; }}// -----------------------------------bool Servent::getLocalURL(char *str){ if (!sock) throw StreamException("Not connected"); char ipStr[64]; Host h; if (sock->host.localIP()) h = sock->getLocalHost(); else h = servMgr->serverHost; h.port = servMgr->serverHost.port; h.toStr(ipStr); sprintf(str,"http://%s",ipStr); return true;}// -----------------------------------// Warning: testing RTSP/RTP stuff below.// .. moved over to seperate app now.// -----------------------------------void Servent::handshakePOST(){ char tmp[1024]; while (sock->readLine(tmp,sizeof(tmp))) LOG_DEBUG("POST: %s",tmp); throw HTTPException(HTTP_SC_BADREQUEST,400);}// -----------------------------------void Servent::handshakeRTSP(RTSP &rtsp){ throw HTTPException(HTTP_SC_BADREQUEST,400);}// -----------------------------------
bool Servent::handshakeAuth(HTTP &http,const char *args)
{
char user[64],pass[64];
user[0] = pass[0] = 0;
Cookie gotCookie;
while (http.nextHeader())
{
char *arg = http.getArgStr();
if (!arg)
continue;
switch (servMgr->authType)
{
case ServMgr::AUTH_HTTPBASIC:
if (http.isHeader("Authorization"))
http.getAuthUserPass(user,pass);
break;
case ServMgr::AUTH_COOKIE:
if (http.isHeader("Cookie"))
{
LOG_DEBUG("Got cookie: %s",arg);
char *idp=arg;
while ((idp = strstr(idp,"id=")))
{
idp+=3;
gotCookie.set(idp,sock->host.ip);
if (servMgr->cookieList.contains(gotCookie))
{
LOG_DEBUG("Cookie found");
cookie = gotCookie;
break;
}
}
}
break;
}
}
if (sock->host.isLocalhost())
return true;
char *pwd = getCGIarg(args, "pass=");
if ((pwd) && strlen(servMgr->password))
if (strcmp(pwd,servMgr->password)==0)
return true;
switch (servMgr->authType)
{
case ServMgr::AUTH_HTTPBASIC:
if ((strcmp(pass,servMgr->password)==0) && strlen(servMgr->password))
return true;
break;
case ServMgr::AUTH_COOKIE:
if (servMgr->cookieList.contains(cookie))
return true;
break;
}
http.writeLine(HTTP_SC_UNAUTHORIZED);
if (servMgr->authType == ServMgr::AUTH_HTTPBASIC)
http.writeLine("WWW-Authenticate: Basic realm=\"PeerCast Admin\"");
http.writeLine("%s %s",HTTP_HS_SERVER,PCX_AGENT);
http.writeLine("%s %s",HTTP_HS_CONTENT,MIME_HTML);
http.writeLine("%s %s",HTTP_HS_CACHE,"no-cache");
http.writeLine("%s %s",HTTP_HS_CONNECTION,"close");
http.writeLine("");
if (servMgr->authType == ServMgr::AUTH_COOKIE)
{
HTML html("",*sock);
char fname[128];
sprintf(fname,"%s/login.html",servMgr->htmlPath);
html.writeTemplate(fname,"");
}
return false;
}
// -----------------------------------void Servent::handshakeCMD(char *cmd){ char result[256]; char arg[512]; char curr[256]; char jumpStr[128]; char *jumpArg=NULL; bool retHTML=true; strcpy(result,"OK"); HTTP http(*sock); HTML html("",*sock);
if (!handshakeAuth(http,cmd))
return; try { if (cmpCGIarg(cmd,"cmd=","redirect")) { char *j = getCGIarg(cmd,"url="); if (j) { termArgs(cmd); String url; url.set(j,String::T_ESC); url.convertTo(String::T_ASCII); if (!url.contains("http://")) url.prepend("http://"); html.setRefreshURL(url.cstr()); html.startHTML(); html.addHead(); html.startBody(); html.startTagEnd("h3","Please wait..."); html.end(); html.end(); } }else{ if (cmpCGIarg(cmd,"cmd=","viewxml")) { handshakeXML(); retHTML = false; }else if (cmpCGIarg(cmd,"cmd=","clearlog")) { sys->logBuf->clear(); sprintf(jumpStr,"/%s/viewlog.html",servMgr->htmlPath); jumpArg = jumpStr; }else if (cmpCGIarg(cmd,"cmd=","save")) { peercastInst->saveSettings(); sprintf(jumpStr,"/%s/settings.html",servMgr->htmlPath);
jumpArg = jumpStr; }else if (cmpCGIarg(cmd,"cmd=","apply")) { servMgr->useIPSalt=false;
servMgr->numFilters = 0; ServFilter *currFilter=servMgr->filters;
bool brRoot=false;
bool getUpd=false;
int showLog=0;
int allowServer1=0;
int allowServer2=0;
char *cp = cmd; while (cp=nextCGIarg(cp,curr,arg)) { // server if (strcmp(curr,"serveractive")==0) servMgr->autoServe = getCGIargBOOL(arg); else if (strcmp(curr,"port")==0) servMgr->serverHost.port = getCGIargINT(arg); else if (strcmp(curr,"icymeta")==0) { int iv = getCGIargINT(arg); if (iv < 0) iv = 0; else if (iv > 16384) iv = 16384; chanMgr->icyMetaInterval = iv; }else if (strcmp(curr,"passnew")==0) strcpy(servMgr->password,arg); else if (strcmp(curr,"root")==0) servMgr->isRoot = getCGIargBOOL(arg); else if (strcmp(curr,"brroot")==0)
brRoot = getCGIargBOOL(arg);
else if (strcmp(curr,"getupd")==0)
getUpd = getCGIargBOOL(arg);
else if (strcmp(curr,"huint")==0)
chanMgr->setUpdateInterval(getCGIargINT(arg));
else if (strcmp(curr,"forceip")==0) strcpy(servMgr->forceIP,arg); else if (strcmp(curr,"ipsalt")==0)
servMgr->useIPSalt = atoi(arg)!=0;
else if (strcmp(curr,"htmlPath")==0)
{
strcpy(servMgr->htmlPath,"html/");
strcat(servMgr->htmlPath,arg);
}else if (strcmp(curr,"djmsg")==0) { String msg; msg.set(arg,String::T_ESC); msg.convertTo(String::T_UNICODE); chanMgr->setBroadcastMsg(msg); } else if (strcmp(curr,"pcmsg")==0) { servMgr->rootMsg.set(arg,String::T_ESC); servMgr->rootMsg.convertTo(String::T_UNICODE); } // connections
else if (strcmp(curr,"maxcin")==0)
servMgr->maxControl = getCGIargINT(arg);
else if (strcmp(curr,"maxup")==0) servMgr->maxBitrate = getCGIargINT(arg); else if (strcmp(curr,"maxstream")==0) servMgr->maxStreams = getCGIargINT(arg); else if (strcmp(curr,"maxlisten")==0) chanMgr->maxStreamsPerChannel = getCGIargINT(arg); else if (strncmp(curr,"filt_",5)==0) { char *fs = curr+5; { if (strncmp(fs,"ip",2)==0) // ip must be first { currFilter = &servMgr->filters[servMgr->numFilters]; currFilter->init(); currFilter->host.fromStrIP(arg,DEFAULT_PORT); if ((currFilter->host.ip) && (servMgr->numFilters < (ServMgr::MAX_FILTERS-1))) { servMgr->numFilters++; servMgr->filters[servMgr->numFilters].init(); // clear new entry } }else if (strncmp(fs,"bn",2)==0) currFilter->flags |= ServFilter::F_BAN; else if (strncmp(fs,"pr",2)==0) currFilter->flags |= ServFilter::F_PRIVATE; else if (strncmp(fs,"nw",2)==0) currFilter->flags |= ServFilter::F_NETWORK; else if (strncmp(fs,"di",2)==0) currFilter->flags |= ServFilter::F_DIRECT; } } // client else if (strcmp(curr,"clientactive")==0) servMgr->autoConnect = getCGIargBOOL(arg); else if (strcmp(curr,"yp")==0) { String str(arg,String::T_ESC); str.convertTo(String::T_ASCII); servMgr->rootHost = str; } else if (strcmp(curr,"deadhitage")==0) chanMgr->deadHitAge = getCGIargINT(arg); else if (strcmp(curr,"refresh")==0) servMgr->refreshHTML = getCGIargINT(arg); else if (strcmp(curr,"auth")==0) { if (strcmp(arg,"cookie")==0) servMgr->authType = ServMgr::AUTH_COOKIE; else if (strcmp(arg,"http")==0) servMgr->authType = ServMgr::AUTH_HTTPBASIC; }else if (strcmp(curr,"expire")==0) { if (strcmp(arg,"session")==0) servMgr->cookieList.neverExpire = false; else if (strcmp(arg,"never")==0) servMgr->cookieList.neverExpire = true; }
else if (strcmp(curr,"logDebug")==0)
showLog |= atoi(arg)?(1<<LogBuffer::T_DEBUG):0;
else if (strcmp(curr,"logErrors")==0)
showLog |= atoi(arg)?(1<<LogBuffer::T_ERROR):0;
else if (strcmp(curr,"logNetwork")==0)
showLog |= atoi(arg)?(1<<LogBuffer::T_NETWORK):0;
else if (strcmp(curr,"logChannel")==0)
showLog |= atoi(arg)?(1<<LogBuffer::T_CHANNEL):0;
else if (strcmp(curr,"allowHTML1")==0)
allowServer1 |= atoi(arg)?(ALLOW_HTML):0;
else if (strcmp(curr,"allowNetwork1")==0)
allowServer1 |= atoi(arg)?(ALLOW_NETWORK):0;
else if (strcmp(curr,"allowBroadcast1")==0)
allowServer1 |= atoi(arg)?(ALLOW_BROADCAST):0;
else if (strcmp(curr,"allowDirect1")==0)
allowServer1 |= atoi(arg)?(ALLOW_DIRECT):0;
else if (strcmp(curr,"allowHTML2")==0)
allowServer2 |= atoi(arg)?(ALLOW_HTML):0;
else if (strcmp(curr,"allowBroadcast2")==0)
allowServer2 |= atoi(arg)?(ALLOW_BROADCAST):0;
} servMgr->showLog = showLog;
servMgr->allowServer1 = allowServer1; servMgr->allowServer2 = allowServer2;
peercastInst->saveSettings();
peercastApp->updateSettings();
if ((servMgr->isRoot) && (brRoot))
servMgr->broadcastRootSettings(getUpd);
sprintf(jumpStr,"/%s/settings.html",servMgr->htmlPath);
jumpArg = jumpStr; }else if (cmpCGIarg(cmd,"cmd=","fetch")) { ChanInfo info; String curl; char *cp = cmd; while (cp=nextCGIarg(cp,curr,arg)) { if (strcmp(curr,"url")==0) { curl.set(arg,String::T_ESC); curl.convertTo(String::T_UNICODE); }else if (strcmp(curr,"name")==0) { info.name.set(arg,String::T_ESC); info.name.convertTo(String::T_UNICODE);
}else if (strcmp(curr,"desc")==0) { info.desc.set(arg,String::T_ESC); info.desc.convertTo(String::T_UNICODE); }else if (strcmp(curr,"genre")==0) { info.genre.set(arg,String::T_ESC); info.genre.convertTo(String::T_UNICODE); }else if (strcmp(curr,"contact")==0) { info.url.set(arg,String::T_ESC); info.url.convertTo(String::T_UNICODE); }else if (strcmp(curr,"bitrate")==0) { info.bitrate = atoi(arg); }else if (strcmp(curr,"type")==0) { info.contentType = ChanInfo::getTypeFromStr(arg); } } Channel *c = chanMgr->createChannel(info,NULL); if (c) c->startURL(curl.cstr()); sprintf(jumpStr,"/%s/relays.html",servMgr->htmlPath);
jumpArg = jumpStr; }else if (cmpCGIarg(cmd,"cmd=","stopserv")) { char *cp = cmd; while (cp=nextCGIarg(cp,curr,arg)) { if (strcmp(curr,"index")==0) { Servent *s = servMgr->findServentByIndex(atoi(arg)); if (s) s->abort(); } } sprintf(jumpStr,"/%s/connections.html",servMgr->htmlPath);
jumpArg = jumpStr; }else if (cmpCGIarg(cmd,"cmd=","hitlist")) { Channel *clist[ChanMgr::MAX_CHANNELS]; int numChans=0; bool stayConnected=hasCGIarg(cmd,"relay"); for(int i=0; i<ChanMgr::MAX_HITLISTS; i++) { ChanHitList *chl = &chanMgr->hitlists[i]; if (chl->isUsed()) { char tmp[64]; sprintf(tmp,"c%d=",i); if (cmpCGIarg(cmd,tmp,"1")) { Channel *c; if (!(c=chanMgr->findChannelByID(chl->info.id))) { c = chanMgr->createChannel(chl->info,NULL); if (!c) throw StreamException("out of channels"); c->stayConnected = stayConnected; c->startGet(); } clist[numChans++] = c; } } } char *findArg = getCGIarg(cmd,"keywords="); if (hasCGIarg(cmd,"relay")) { sys->sleep(500); sprintf(jumpStr,"/%s/relays.html",servMgr->htmlPath);
jumpArg = jumpStr;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -