servhs.cpp
来自「这是和p2p相关的一份源码」· C++ 代码 · 共 1,601 行 · 第 1/3 页
CPP
1,601 行
// -----------------------------------void Servent::handshakeRTSP(RTSP &rtsp){ throw HTTPException(HTTP_SC_BADREQUEST,400);}// -----------------------------------
bool Servent::handshakeAuth(HTTP &http,const char *args,bool local)
{
char user[64],pass[64];
user[0] = pass[0] = 0;
char *pwd = getCGIarg(args, "pass=");
if ((pwd) && strlen(servMgr->password))
{
String tmp = pwd;
char *as = strstr(tmp.cstr(),"&");
if (as) *as = 0;
if (strcmp(tmp,servMgr->password)==0)
{
while (http.nextHeader());
return true;
}
}
Cookie gotCookie;
cookie.clear();
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;
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;
}
if (servMgr->authType == ServMgr::AUTH_HTTPBASIC)
{
http.writeLine(HTTP_SC_UNAUTHORIZED);
http.writeLine("WWW-Authenticate: Basic realm=\"PeerCast Admin\"");
}else if (servMgr->authType == ServMgr::AUTH_COOKIE)
{
String file = servMgr->htmlPath;
file.append("/login.html");
if (local)
handshakeLocalFile(file);
else
handshakeRemoteFile(file);
}
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,true))
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->numFilters = 0; ServFilter *currFilter=servMgr->filters;
bool brRoot=false;
bool getUpd=false;
int showLog=0;
int allowServer1=0;
int allowServer2=0;
int newPort=servMgr->serverHost.port;
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) newPort = 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) servMgr->forceIP = arg; 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); }else if (strcmp(curr,"minpgnu")==0)
servMgr->minGnuIncoming = atoi(arg);
else if (strcmp(curr,"maxpgnu")==0)
servMgr->maxGnuIncoming = atoi(arg);
// connections
else if (strcmp(curr,"maxcin")==0)
servMgr->maxControl = getCGIargINT(arg);
else if (strcmp(curr,"maxup")==0) servMgr->maxBitrateOut = getCGIargINT(arg); else if (strcmp(curr,"maxrelays")==0) servMgr->setMaxRelays(getCGIargINT(arg)); else if (strcmp(curr,"maxdirect")==0)
servMgr->maxDirect = getCGIargINT(arg);
else if (strcmp(curr,"maxrelaypc")==0) chanMgr->maxRelaysPerChannel = 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;
if (servMgr->serverHost.port != newPort)
{
Host lh(ClientSocket::getIP(NULL),newPort);
char ipstr[64];
lh.toStr(ipstr);
sprintf(jumpStr,"http://%s/%s/settings.html",ipstr,servMgr->htmlPath);
servMgr->serverHost.port = newPort;
servMgr->restartServer=true;
//html.setRefresh(3);
//html.setRefreshURL(jumpStr);
//html.startHTML();
//html.addHead();
// html.startBody();
// html.startTagEnd("h1","Please wait...");
// html.end();
//html.end();
//char ipstr[64];
//servMgr->serverHost.toStr(ipstr);
//sprintf(jumpStr,"/%s/settings.html",ipstr,servMgr->htmlPath);
jumpArg = jumpStr;
}else
{
sprintf(jumpStr,"/%s/settings.html",servMgr->htmlPath);
jumpArg = jumpStr;
}
peercastInst->saveSettings();
peercastApp->updateSettings();
if ((servMgr->isRoot) && (brRoot))
servMgr->broadcastRootSettings(getUpd);
}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; } }else if (cmpCGIarg(cmd,"cmd=","clear")) { char *cp = cmd; while (cp=nextCGIarg(cp,curr,arg)) { if (strcmp(curr,"hostcache")==0) servMgr->clearHostCache(ServHost::T_SERVENT); else if (strcmp(curr,"hitlists")==0)
chanMgr->clearHitLists();
else if (strcmp(curr,"packets")==0) { stats.clearRange(Stats::PACKETSSTART,Stats::PACKETSEND); servMgr->numVersions = 0; } } sprintf(jumpStr,"/%s/index.html",servMgr->htmlPath);
jumpArg = jumpStr; }else if (cmpCGIarg(cmd,"cmd=","upgrade")) { if (servMgr->downloadURL[0]) { sprintf(jumpStr,"/admin?cmd=redirect&url=%s",servMgr->downloadURL); jumpArg = jumpStr; } }else if (cmpCGIarg(cmd,"cmd=","chan")) { Channel *clist2[ChanMgr::MAX_CHANNELS]; int num2=0; Channel *clist[ChanMgr::MAX_CHANNELS]; ChanInfo info; int num = chanMgr->findChannels(info,clist,ChanMgr::MAX_CHANNELS); for(int i=0; i<num; i++) { Channel *c = clist[i]; char tmp[64]; sprintf(tmp,"c%d=",c->index); if (cmpCGIarg(cmd,tmp,"1")) clist2[num2++] = c; } bool delay=false; if (hasCGIarg(cmd,"bump")) { for(int i=0; i<num2; i++) clist2[i]->bump = true; delay = true; } if (hasCGIarg(cmd,"keep")) { for(int i=0; i<num2; i++) clist2[i]->stayConnected = true; delay = true; } if (hasCGIarg(cmd,"stop")) { for(int i=0; i<num2; i++) clist2[i]->thread.active = false;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?