📄 servhs.cpp
字号:
} char *findArg = getCGIarg(cmd,"keywords="); if (hasCGIarg(cmd,"play")) { handshakePLS(clist,numChans,true); retHTML = false; }else if (hasCGIarg(cmd,"relay")) { sys->sleep(500); sprintf(jumpStr,"/admin?page=mychans"); 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,"packets")==0) { stats.clearRange(Stats::PACKETSSTART,Stats::PACKETSEND); servMgr->numVersions = 0; } } sprintf(jumpStr,"/admin?page=index"); 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,"play")) { handshakePLS(clist2,num2,true); retHTML = false; } if (hasCGIarg(cmd,"bump")) { for(int i=0; i<num2; i++) clist2[i]->bump = true; delay = true; } if (hasCGIarg(cmd,"stop")) { for(int i=0; i<num2; i++) clist2[i]->thread.active = false; delay = true; } if (delay) sys->sleep(500); sprintf(jumpStr,"/admin?page=mychans"); jumpArg = jumpStr; }else if (cmpCGIarg(cmd,"cmd=","connect")) { for(int i=0; i<ServMgr::MAX_SERVENTS; i++) { Servent *s = &servMgr->servents[i]; char tmp[64]; sprintf(tmp,"c%d=",i); if (cmpCGIarg(cmd,tmp,"1")) { if (hasCGIarg(cmd,"stop")) s->thread.active = false; } } sprintf(jumpStr,"/admin?page=connections"); jumpArg = jumpStr; }else if (cmpCGIarg(cmd,"cmd=","shutdown")) { servMgr->shutdownTimer = 1; }else if (cmpCGIarg(cmd,"cmd=","stop")) { GnuID id; char *cp = cmd; while (cp=nextCGIarg(cp,curr,arg)) { if (strcmp(curr,"id")==0) id.fromStr(arg); } Channel *c = chanMgr->findChannelByID(id); if (c) c->thread.active = false; sys->sleep(500); sprintf(jumpStr,"/admin?page=mychans"); jumpArg = jumpStr; }else if (cmpCGIarg(cmd,"cmd=","bump")) { GnuID id; char *cp = cmd; while (cp=nextCGIarg(cp,curr,arg)) { if (strcmp(curr,"id")==0) id.fromStr(arg); } Channel *c = chanMgr->findChannelByID(id); if (c) c->bump = true; sprintf(jumpStr,"/admin?page=mychans"); jumpArg = jumpStr; }else if (cmpCGIarg(cmd,"cmd=","relay")) { GnuID id; char *cp = cmd; while (cp=nextCGIarg(cp,curr,arg)) { if (strcmp(curr,"id")==0) id.fromStr(arg); } if (!chanMgr->findChannelByID(id)) { ChanHitList *chl = chanMgr->findHitListByID(id); if (!chl) throw StreamException("channel not found"); Channel *c = chanMgr->createChannel(chl->info,NULL); if (!c) throw StreamException("out of channels"); c->stayConnected = true; c->startGet(); } sprintf(jumpStr,"/admin?page=mychans"); jumpArg = jumpStr; }else if (cmpCGIarg(cmd,"page=","chans")) { ChanInfo info; info.init(); SORT sort=SORT_NAME; bool dir=false; char *cp = cmd; while (cp=nextCGIarg(cp,curr,arg)) { if (strcmp(curr,"name")==0) info.name.set(arg); else if (strcmp(curr,"genre")==0) info.genre.set(arg); else if (strcmp(curr,"bitrate")==0) info.bitrate = atoi(arg); else if (strcmp(curr,"id")==0) info.id.fromStr(arg); else if (strcmp(curr,"sort")==0) { if (strcmp(arg,"name")==0) sort = SORT_NAME; else if (strcmp(arg,"bitrate")==0) sort = SORT_BITRATE; else if (strcmp(arg,"listeners")==0) sort = SORT_LISTENERS; else if (strcmp(arg,"hosts")==0) sort = SORT_HOSTS; else if (strcmp(arg,"type")==0) sort = SORT_TYPE; else if (strcmp(arg,"genre")==0) sort = SORT_GENRE; }else if (strcmp(curr,"dir")==0) { if (strcmp(arg,"up")==0) dir = true; else if (strcmp(arg,"down")==0) dir = false; } } addAllChannelsPage(html,sort,dir,&info); }else if (cmpCGIarg(cmd,"page=","winamp-settings")) { addWinampSettingsPage(html); }else if (cmpCGIarg(cmd,"page=","winamp-chans")) { char wildcard[128],type[128]; bool stop=false; wildcard[0] = type[0]=0; char *cp = cmd; while (cp=nextCGIarg(cp,curr,arg)) { if (strcmp(curr,"wildcard")==0) strcpy(wildcard,arg); else if (strcmp(curr,"type")==0) strcpy(type,arg); else if (strcmp(curr,"stop")==0) stop=true; } addWinampChansPage(html,wildcard,type,stop); }else if (cmpCGIarg(cmd,"page=","mychans")) { addMyChannelsPage(html); }else if (cmpCGIarg(cmd,"page=","index")) { addAdminPage(html); }else if (cmpCGIarg(cmd,"page=","settings")) { addSettingsPage(html); }else if (cmpCGIarg(cmd,"page=","viewlog")) { addLogPage(html); }else if (cmpCGIarg(cmd,"page=","viewnet")) { addNetStatsPage(html); }else if (cmpCGIarg(cmd,"page=","shutdown")) { addShutdownPage(html); }else if (cmpCGIarg(cmd,"page=","logout")) { addLogoutPage(html); }else if (cmpCGIarg(cmd,"page=","connections")) { addConnectionsPage(html); }else if (cmpCGIarg(cmd,"page=","broadcast")) { addBroadcastPage(html); }else if (cmpCGIarg(cmd,"net=","add")) { GnuID id; id.clear(); while (cmd=nextCGIarg(cmd,curr,arg)) { if (strcmp(curr,"ip")==0) { Host h; h.fromStr(arg,DEFAULT_PORT); if (servMgr->addOutgoing(h,id,true)) LOG_NETWORK("Added connection: %s",arg); }else if (strcmp(curr,"id")==0) { id.fromStr(arg); } } }else if (cmpCGIarg(cmd,"cmd=","logout")) { jumpArg = "/"; servMgr->cookieList.remove(cookie); }else if (cmpCGIarg(cmd,"cmd=","login")) { GnuID id; char idstr[64]; id.generate(); id.toStr(idstr); cookie.set(idstr,sock->host.ip); servMgr->cookieList.add(cookie); http.writeLine(HTTP_SC_FOUND); if (servMgr->cookieList.neverExpire) http.writeLine("%s id=%s; path=/; expires=\"Mon, 01-Jan-3000 00:00:00 GMT\";",HTTP_HS_SETCOOKIE,idstr); else http.writeLine("%s id=%s; path=/;",HTTP_HS_SETCOOKIE,idstr); http.writeLine("Location: /"); http.writeLine(""); }else{ addAdminPage(html); } } }catch(StreamException &) { //html.startTagEnd("h1","ERROR - %s",e.msg); LOG_ERROR(result); } if (retHTML) { if (jumpArg) { String jmp(jumpArg,String::T_HTML); jmp.convertTo(String::T_ASCII); html.locateTo(jmp.cstr()); } }}// -----------------------------------static XML::Node *createChannelXML(Channel *c){ XML::Node *n = c->info.createChannelXML(); n->add(c->createRelayXML(true)); n->add(c->info.createTrackXML());// n->add(c->info.createServentXML()); return n;}// -----------------------------------static XML::Node *createChannelXML(ChanHitList *chl){ XML::Node *n = chl->info.createChannelXML(); n->add(chl->createHitsXML()); n->add(chl->info.createTrackXML());// n->add(chl->info.createServentXML()); return n;}// -----------------------------------void Servent::handshakeXML(){ int i; sock->writeLine(HTTP_SC_OK); sock->writeLine("%s %s",HTTP_HS_SERVER,PCX_AGENT); sock->writeLine("%s %s",HTTP_HS_CONTENT,MIME_XML); sock->writeLine("Connection: close"); sock->writeLine(""); XML xml; XML::Node *rn = new XML::Node("peercast"); xml.setRoot(rn); rn->add(new XML::Node("servent uptime=\"%d\"",servMgr->getUptime())); rn->add(new XML::Node("bandwidth out=\"%d\" in=\"%d\"",servMgr->totalOutput(false),chanMgr->totalInput())); rn->add(new XML::Node("connections total=\"%d\" streams=\"%d\"",servMgr->totalConnected(),servMgr->numStreams(false))); Channel *clist[ChanMgr::MAX_CHANNELS]; ChanInfo info; int num = chanMgr->findChannels(info,clist,ChanMgr::MAX_CHANNELS); XML::Node *an = new XML::Node("channels_relayed total=\"%d\"",num); rn->add(an); for(i=0; i<num; i++) an->add(createChannelXML(clist[i])); XML::Node *fn = new XML::Node("channels_found total=\"%d\"",chanMgr->numHitLists()); rn->add(fn); for(i=0; i<ChanMgr::MAX_HITLISTS; i++) if (chanMgr->hitlists[i].isUsed()) fn->add(createChannelXML(&chanMgr->hitlists[i])); XML::Node *hc = new XML::Node("host_cache"); for(i=0; i<ServMgr::MAX_HOSTCACHE; i++) { ServHost *sh = &servMgr->hostCache[i]; if (sh->type != ServHost::T_NONE) { char ipstr[64]; sh->host.toStr(ipstr); if (sh->netid.isSet()) { char netidStr[64]; sh->netid.toStr(netidStr); hc->add(new XML::Node("host ip=\"%s\" type=\"%s\" time=\"%d\" networkID=\"%s\"",ipstr,ServHost::getTypeStr(sh->type),sh->time,netidStr)); }else{ hc->add(new XML::Node("host ip=\"%s\" type=\"%s\" time=\"%d\"",ipstr,ServHost::getTypeStr(sh->type),sh->time)); } } } rn->add(hc); xml.write(*sock);}// -----------------------------------void Servent::handshakeHTML(char *cmd){ HTTP http(*sock); char tmp[512]; while (http.nextHeader()) LOG_DEBUG("HTTP: %s",http.cmdLine); char name[128]; //servMgr->serverHost.toResolvedName(name); servMgr->serverHost.toStr(name); sprintf(tmp,"PeerCast on %s",name); HTML html(tmp,*sock); bool showLogin=true; if (!strlen(servMgr->password) && sock->host.isLocalhost()) showLogin = false; sock->writeLine(HTTP_SC_FOUND); //sock->writeLine("Location: /admin?page=index"); sock->writeLine("Location: /admin?"); sock->writeLine(""); return; if (showLogin) { addLoginPage(html); }else{ sock->writeLine(HTTP_SC_FOUND); //sock->writeLine("Location: /admin?page=index"); sock->writeLine("Location: /admin?"); sock->writeLine(""); }}// -----------------------------------void Servent::readICYHeader(HTTP &http, ChanInfo &info, char *pwd){ char *arg = http.getArgStr(); if (!arg) return; if (http.isHeader("x-audiocast-name") || http.isHeader("icy-name") || http.isHeader("ice-name")) info.name.set(arg,String::T_ASCII); else if (http.isHeader("x-audiocast-url") || http.isHeader("icy-url") || http.isHeader("ice-url")) info.url.set(arg,String::T_ASCII); else if (http.isHeader("x-audiocast-bitrate") || http.isHeader("ice-bitrate")) info.bitrate = atoi(arg); else if (http.isHeader("x-audiocast-genre") || http.isHeader("ice-genre")) info.genre.set(arg,String::T_ASCII); else if (http.isHeader("x-audiocast-description") || http.isHeader("ice-description")) info.desc.set(arg,String::T_ASCII); else if ((http.isHeader("icy-br")) || http.isHeader("ice-bitrate")) info.bitrate = atoi(arg); else if (http.isHeader("icy-genre")) info.genre.set(arg,String::T_ASCII); else if (http.isHeader("Authorization")) http.getAuthUserPass(NULL,pwd); else if (http.isHeader(PCX_HS_CHANNELID)) info.id.fromStr(arg); else if (http.isHeader("ice-password")) { if (pwd) if (strlen(arg) < 64) strcpy(pwd,arg); }else if (http.isHeader("content-type")) { if (stristr(arg,MIME_OGG)) info.srcType = ChanInfo::T_OGG; else if (stristr(arg,MIME_XOGG)) info.srcType = ChanInfo::T_OGG; else if (stristr(arg,MIME_MP3)) info.srcType = ChanInfo::T_MP3; else if (stristr(arg,MIME_XMP3)) info.srcType = ChanInfo::T_MP3; else if (stristr(arg,MIME_NSV)) info.srcType = ChanInfo::T_NSV; else if (stristr(arg,MIME_XPEERCAST)) info.srcType = ChanInfo::T_PEERCAST; else if (stristr(arg,MIME_RAW)) info.srcType = ChanInfo::T_RAW; }}// -----------------------------------void Servent::handshakeICY(Channel::SRC_TYPE type, bool isHTTP){ ChanInfo info; HTTP http(*sock); // default to mp3 for shoutcast DSP (doesn`t send content-type) if (type == Channel::SRC_SHOUTCAST) info.srcType = ChanInfo::T_MP3; while (http.nextHeader()) { LOG_DEBUG("ICY %s",http.cmdLine); readICYHeader(http,info,loginPassword); } // check password before anything else, if needed if (strcmp(servMgr->password,loginPassword)!=0) throw HTTPException(HTTP_SC_UNAUTHORIZED,401); // attach channel ID to name, channel ID is also encoded with IP address // to help prevent channel hijacking. Host sh = servMgr->serverHost; info.id = chanMgr->broadcastID; info.id.encode(&sh,info.name.cstr(),loginMount,info.bitrate); info.contentType = info.srcType; LOG_DEBUG("Incoming source: %s : %s",info.name.cstr(),ChanInfo::getTypeStr(info.contentType)); if (isHTTP) sock->writeString("%s\n\n",HTTP_SC_OK); else sock->writeLine("OK"); Channel *c = chanMgr->findChannelByID(info.id); if (c) c->thread.active = false; info.comment = chanMgr->broadcastMsg; c = chanMgr->createChannel(info,loginMount); if (!c) throw HTTPException(HTTP_SC_UNAVAILABLE,503); c->startICY(sock,type);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -