📄 servmgr.cpp
字号:
if (s->isConnected())
cnt++;
s=s->next;
}
return cnt;
}
// -----------------------------------
unsigned int ServMgr::numServents()
{
unsigned int cnt=0;
Servent *s = servents;
while (s)
{
cnt++;
s=s->next;
}
return cnt;
}
// -----------------------------------unsigned int ServMgr::numUsed(int type){ unsigned int cnt=0; Servent *s = servents; while (s) { if (s->type == type) cnt++; s=s->next; } return cnt;}// -----------------------------------
unsigned int ServMgr::numActiveOnPort(int port)
{
unsigned int cnt=0;
Servent *s = servents;
while (s)
{
if (s->thread.active && s->sock && (s->servPort == port))
cnt++;
s=s->next;
}
return cnt;
}
// -----------------------------------
unsigned int ServMgr::numActive(Servent::TYPE tp)
{
unsigned int cnt=0;
Servent *s = servents;
while (s)
{
if (s->thread.active && s->sock && (s->type == tp))
cnt++;
s=s->next;
}
return cnt;
}
// -----------------------------------unsigned int ServMgr::totalOutput(bool all){ unsigned int tot = 0; Servent *s = servents; while (s) { if (s->isConnected())
if (all || !s->isPrivate()) if (s->sock) tot += s->sock->bytesOutPerSec; s=s->next; } return tot;}// -----------------------------------unsigned int ServMgr::numOutgoing(){ int cnt=0; Servent *s = servents; while (s) {// if ((s->type == Servent::T_INCOMING) ||// (s->type == Servent::T_OUTGOING)) // cnt++; s=s->next; } return cnt;}// -----------------------------------bool ServMgr::seenPacket(GnuPacket &p){ Servent *s = servents; while (s) { if (s->isConnected())
if (s->seenIDs.contains(p.id)) return true; s=s->next; } return false;}
// -----------------------------------
void ServMgr::quit()
{
LOG_DEBUG("ServMgr is quitting..");
serverThread.shutdown();
idleThread.shutdown();
Servent *s = servents;
while (s)
{
try
{
if (s->thread.active)
{
s->thread.shutdown();
}
}catch(StreamException &)
{
}
s=s->next;
}
}
// -----------------------------------int ServMgr::broadcast(GnuPacket &pack,Servent *src){ int cnt=0; if (pack.ttl) { Servent *s = servents; while (s) { if (s != src) if (s->isConnected())
if (s->type == Servent::T_PGNU) if (!s->seenIDs.contains(pack.id)) { if (src) if (!src->networkID.isSame(s->networkID)) continue; if (s->outputPacket(pack,false)) cnt++; } s=s->next; } } LOG_NETWORK("broadcast: %s (%d) to %d servents",GNU_FUNC_STR(pack.func),pack.ttl,cnt); return cnt;}// -----------------------------------int ServMgr::route(GnuPacket &pack, GnuID &routeID, Servent *src){ int cnt=0; if (pack.ttl) { Servent *s = servents; while (s) { if (s != src) if (s->isConnected())
if (s->type == Servent::T_PGNU)
if (!s->seenIDs.contains(pack.id)) if (s->seenIDs.contains(routeID)) { if (src) if (!src->networkID.isSame(s->networkID)) continue; if (s->outputPacket(pack,true)) cnt++; } s=s->next; } } LOG_NETWORK("route: %s (%d) to %d servents",GNU_FUNC_STR(pack.func),pack.ttl,cnt); return cnt;}
// -----------------------------------
bool ServMgr::checkForceIP()
{
if (!forceIP.isEmpty())
{
unsigned int newIP = ClientSocket::getIP(forceIP.cstr());
if (serverHost.ip != newIP)
{
serverHost.ip = newIP;
char ipstr[64];
serverHost.IPtoStr(ipstr);
LOG_DEBUG("Server IP changed to %s",ipstr);
return true;
}
}
return false;
}
// -----------------------------------
void ServMgr::checkFirewall()
{
if ((getFirewall() == FW_UNKNOWN) && !servMgr->rootHost.isEmpty())
{
LOG_DEBUG("Checking firewall..");
Host host;
host.fromStrName(servMgr->rootHost.cstr(),DEFAULT_PORT);
ClientSocket *sock = sys->createSocket();
if (!sock)
throw StreamException("Unable to create socket");
sock->setReadTimeout(30000);
sock->open(host);
sock->connect();
AtomStream atom(*sock);
atom.writeInt(PCP_CONNECT,1);
GnuID remoteID;
String agent;
Servent::handshakeOutgoingPCP(atom,sock->host,remoteID,agent,true);
atom.writeInt(PCP_QUIT,PCP_ERROR_QUIT);
sock->close();
delete sock;
}
}
// -----------------------------------void ServMgr::setFirewall(FW_STATE state){ if (firewalled != state) { char *str; switch (state) { case FW_ON: str = "ON"; break; case FW_OFF: str = "OFF"; break; case FW_UNKNOWN: default: str = "UNKNOWN"; break; } LOG_DEBUG("Firewall is set to %s",str); firewalled = state; }}// -----------------------------------bool ServMgr::isFiltered(int fl, Host &h){ for(int i=0; i<numFilters; i++) if (filters[i].flags & fl) if (h.isMemberOf(filters[i].host)) return true; return false;}#if 0// -----------------------------------bool ServMgr::canServeHost(Host &h){ if (server) { Host sh = server->getHost(); if (sh.globalIP() || (sh.localIP() && h.localIP())) return true; } return false;}#endif// --------------------------------------------------void writeServerSettings(IniFile &iniFile, unsigned int a){ iniFile.writeBoolValue("allowHTML",a & Servent::ALLOW_HTML); iniFile.writeBoolValue("allowBroadcast",a & Servent::ALLOW_BROADCAST);
iniFile.writeBoolValue("allowNetwork",a & Servent::ALLOW_NETWORK); iniFile.writeBoolValue("allowDirect",a & Servent::ALLOW_DIRECT);
}// --------------------------------------------------void writeFilterSettings(IniFile &iniFile, ServFilter &f){ char ipstr[64]; f.host.IPtoStr(ipstr); iniFile.writeStrValue("ip",ipstr); iniFile.writeBoolValue("private",f.flags & ServFilter::F_PRIVATE); iniFile.writeBoolValue("ban",f.flags & ServFilter::F_BAN); iniFile.writeBoolValue("network",f.flags & ServFilter::F_NETWORK); iniFile.writeBoolValue("direct",f.flags & ServFilter::F_DIRECT);}// --------------------------------------------------static void writeServHost(IniFile &iniFile, ServHost &sh){ iniFile.writeSection("Host"); char ipStr[64]; sh.host.toStr(ipStr); iniFile.writeStrValue("type",ServHost::getTypeStr(sh.type)); iniFile.writeStrValue("address",ipStr); iniFile.writeIntValue("time",sh.time); iniFile.writeLine("[End]");}// --------------------------------------------------void ServMgr::saveSettings(const char *fn){ IniFile iniFile; if (!iniFile.openWriteReplace(fn)) { LOG_ERROR("Unable to open ini file"); }else{ LOG_DEBUG("Saving settings to: %s",fn); char idStr[64]; iniFile.writeSection("Server"); iniFile.writeIntValue("serverPort",servMgr->serverHost.port); iniFile.writeBoolValue("autoServe",servMgr->autoServe); iniFile.writeStrValue("forceIP",servMgr->forceIP); iniFile.writeBoolValue("isRoot",servMgr->isRoot); iniFile.writeIntValue("maxBitrateOut",servMgr->maxBitrateOut); iniFile.writeIntValue("maxRelays",servMgr->maxRelays); iniFile.writeIntValue("maxDirect",servMgr->maxDirect);
iniFile.writeIntValue("maxRelaysPerChannel",chanMgr->maxRelaysPerChannel); iniFile.writeIntValue("firewallTimeout",firewallTimeout); iniFile.writeBoolValue("forceNormal",forceNormal); iniFile.writeStrValue("rootMsg",rootMsg.cstr()); iniFile.writeStrValue("authType",servMgr->authType==ServMgr::AUTH_COOKIE?"cookie":"http-basic"); iniFile.writeStrValue("cookiesExpire",servMgr->cookieList.neverExpire==true?"never":"session"); iniFile.writeStrValue("htmlPath",servMgr->htmlPath);
iniFile.writeIntValue("minPGNUIncoming",servMgr->minGnuIncoming);
iniFile.writeIntValue("maxPGNUIncoming",servMgr->maxGnuIncoming);
iniFile.writeIntValue("maxServIn",servMgr->maxServIn);
iniFile.writeStrValue("chanLog",servMgr->chanLog.cstr()); networkID.toStr(idStr); iniFile.writeStrValue("networkID",idStr);
iniFile.writeSection("Broadcast");
iniFile.writeIntValue("broadcastMsgInterval",chanMgr->broadcastMsgInterval);
iniFile.writeStrValue("broadcastMsg",chanMgr->broadcastMsg.cstr());
iniFile.writeIntValue("icyMetaInterval",chanMgr->icyMetaInterval);
chanMgr->broadcastID.toStr(idStr);
iniFile.writeStrValue("broadcastID",idStr);
iniFile.writeIntValue("hostUpdateInterval",chanMgr->hostUpdateInterval);
iniFile.writeIntValue("maxControlConnections",servMgr->maxControl);
iniFile.writeStrValue("rootHost",servMgr->rootHost.cstr());
iniFile.writeSection("Client"); iniFile.writeIntValue("refreshHTML",refreshHTML); iniFile.writeIntValue("relayBroadcast",servMgr->relayBroadcast); iniFile.writeIntValue("minBroadcastTTL",chanMgr->minBroadcastTTL); iniFile.writeIntValue("maxBroadcastTTL",chanMgr->maxBroadcastTTL); iniFile.writeIntValue("pushTries",chanMgr->pushTries); iniFile.writeIntValue("pushTimeout",chanMgr->pushTimeout); iniFile.writeIntValue("maxPushHops",chanMgr->maxPushHops); iniFile.writeIntValue("autoQuery",chanMgr->autoQuery); iniFile.writeIntValue("queryTTL",servMgr->queryTTL);
iniFile.writeSection("Privacy"); iniFile.writeStrValue("password",servMgr->password); iniFile.writeIntValue("maxUptime",chanMgr->maxUptime); int i; for(i=0; i<servMgr->numFilters; i++) { iniFile.writeSection("Filter"); writeFilterSettings(iniFile,servMgr->filters[i]); iniFile.writeLine("[End]"); } iniFile.writeSection("Notify"); iniFile.writeBoolValue("PeerCast",notifyMask&NT_PEERCAST); iniFile.writeBoolValue("Broadcasters",notifyMask&NT_BROADCASTERS); iniFile.writeBoolValue("TrackInfo",notifyMask&NT_TRACKINFO); iniFile.writeLine("[End]"); iniFile.writeSection("Server1"); writeServerSettings(iniFile,allowServer1); iniFile.writeLine("[End]"); iniFile.writeSection("Server2"); writeServerSettings(iniFile,allowServer2); iniFile.writeLine("[End]"); iniFile.writeSection("Debug"); iniFile.writeBoolValue("logDebug",(showLog&(1<<LogBuffer::T_DEBUG))!=0); iniFile.writeBoolValue("logErrors",(showLog&(1<<LogBuffer::T_ERROR))!=0); iniFile.writeBoolValue("logNetwork",(showLog&(1<<LogBuffer::T_NETWORK))!=0); iniFile.writeBoolValue("logChannel",(showLog&(1<<LogBuffer::T_CHANNEL))!=0); iniFile.writeBoolValue("pauseLog",pauseLog); iniFile.writeIntValue("idleSleepTime",sys->idleSleepTime);
if (servMgr->validBCID)
{
BCID *bcid = servMgr->validBCID;
while (bcid)
{
iniFile.writeSection("ValidBCID");
char idstr[128];
bcid->id.toStr(idstr);
iniFile.writeStrValue("id",idstr);
iniFile.writeStrValue("name",bcid->name.cstr());
iniFile.writeStrValue("email",bcid->email.cstr());
iniFile.writeStrValue("url",bcid->url.cstr());
iniFile.writeBoolValue("valid",bcid->valid);
iniFile.writeLine("[End]");
bcid=bcid->next;
}
}
Channel *c = chanMgr->channel; while (c) { char idstr[64]; if (c->isActive() && c->stayConnected) { c->getIDStr(idstr);
iniFile.writeSection("RelayChannel"); iniFile.writeStrValue("name",c->getName()); iniFile.writeStrValue("genre",c->info.genre.cstr()); if (!c->sourceURL.isEmpty()) iniFile.writeStrValue("sourceURL",c->sourceURL.cstr()); iniFile.writeStrValue("sourceProtocol",ChanInfo::getProtocolStr(c->info.srcProtocol)); iniFile.writeStrValue("contentType",ChanInfo::getTypeStr(c->info.contentType)); iniFile.writeIntValue("bitrate",c->info.bitrate); iniFile.writeStrValue("contactURL",c->info.url.cstr()); iniFile.writeStrValue("id",idstr); iniFile.writeBoolValue("stayConnected",c->stayConnected);
ChanHitList *chl = chanMgr->findHitListByID(c->info.id);
if (chl)
{
ChanHitSearch chs;
chs.trackersOnly = true;
if (chl->pickHits(chs))
{
char ipStr[64];
chs.best[0].host.toStr(ipStr);
iniFile.writeStrValue("tracker",ipStr);
}
} iniFile.writeLine("[End]"); }
c=c->next; } #if 0 Servent *s = servents; while (s) { if (s->type == Servent::T_OUTGOING) if (s->isConnected())
{ ServHost sh; Host h = s->getHost(); sh.init(h,ServHost::T_SERVENT,0,s->networkID); writeServHost(iniFile,sh); } s=s->next; }#endif
for(i=0; i<ServMgr::MAX_HOSTCACHE; i++) { ServHost *sh = &servMgr->hostCache[i]; if (sh->type != ServHost::T_NONE) writeServHost(iniFile,*sh); } iniFile.close(); }}// --------------------------------------------------unsigned int readServerSettings(IniFile &iniFile, unsigned int a){ while (iniFile.readNext()) { if (iniFile.isName("[End]")) break; else if (iniFile.isName("allowHTML")) a = iniFile.getBoolValue()?a|Servent::ALLOW_HTML:a&~Servent::ALLOW_HTML; else if (iniFile.isName("allowDirect")) a = iniFile.getBoolValue()?a|Servent::ALLOW_DIRECT:a&~Servent::ALLOW_DIRECT; else if (iniFile.isName("allowNetwork")) a = iniFile.getBoolValue()?a|Servent::ALLOW_NETWORK:a&~Servent::ALLOW_NETWORK; else if (iniFile.isName("allowBroadcast")) a = iniFile.getBoolValue()?a|Servent::ALLOW_BROADCAST:a&~Servent::ALLOW_BROADCAST; } return a;}// --------------------------------------------------void readFilterSettings(IniFile &iniFile, ServFilter &sv){ sv.host.init(); while (iniFile.readNext()) { if (iniFile.isName("[End]")) break; else if (iniFile.isName("ip")) sv.host.fromStrIP(iniFile.getStrValue(),0); else if (iniFile.isName("private")) sv.flags = (sv.flags & ~ServFilter::F_PRIVATE) | (iniFile.getBoolValue()?ServFilter::F_PRIVATE:0); else if (iniFile.isName("ban")) sv.flags = (sv.flags & ~ServFilter::F_BAN) | (iniFile.getBoolValue()?ServFilter::F_BAN:0); else if (iniFile.isName("allow") || iniFile.isName("network")) sv.flags = (sv.flags & ~ServFilter::F_NETWORK) | (iniFile.getBoolValue()?ServFilter::F_NETWORK:0); else if (iniFile.isName("direct")) sv.flags = (sv.flags & ~ServFilter::F_DIRECT) | (iniFile.getBoolValue()?ServFilter::F_DIRECT:0); }}// --------------------------------------------------void ServMgr::loadSettings(const char *fn){ IniFile iniFile;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -