mirror of
https://github.com/3proxy/3proxy.git
synced 2026-11-22 23:59:41 +08:00
parent: support fraction and % syntaxis for weight; support fallbacks with 0 weight
This commit is contained in:
1 parent
6c4663a045
commit
41a08c11be
7 files changed
+610
-34
No files matched your search
+81
-4
@@ -903,6 +903,82 @@ static int parserange(unsigned char *arg, uint32_t *range)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Scan a parent weight into its share of WEIGHTSCALE, as an integer: there is
|
||||
* no floating point anywhere near a configuration file.
|
||||
*
|
||||
* A weight starting with 0 or . is a fraction of one, so .333 and 0.333 are
|
||||
* both a third. Anything else is the old notation, thousandths, where 1000 is
|
||||
* the whole share, and it may now carry more digits after a dot: 123.456 means
|
||||
* the same as .123456. Either way at most 9 digits are kept, which is the
|
||||
* resolution weights are held at.
|
||||
*
|
||||
* 1 followed by a point and nothing but zeroes is the one weight read as it
|
||||
* looks rather than as thousandths: 1.0 is the whole share, where a bare 1 is
|
||||
* a thousandth of it.
|
||||
*
|
||||
* A weight may also be written as a percentage, which is what a trailing %
|
||||
* makes it: 50.5% is .505 is 505.
|
||||
*/
|
||||
static int parseweight(unsigned char * s, unsigned * weight){
|
||||
static const unsigned pow10[10] = {1, 10, 100, 1000, 10000, 100000,
|
||||
1000000, 10000000, 100000000, 1000000000};
|
||||
unsigned char *p, *end;
|
||||
uint64_t val = 0, res;
|
||||
int ndigits = 0, atpoint = -1, after, percent = 0, fraction;
|
||||
|
||||
if(!s || !*s) return 1;
|
||||
end = s + strlen((char *)s);
|
||||
if(end[-1] == '%'){
|
||||
percent = 1;
|
||||
if(--end == s) return 1;
|
||||
}
|
||||
/* 1.0, with as many zeroes after it as anyone cares to write, is the one
|
||||
weight read as it looks rather than as thousandths: the whole share,
|
||||
where a bare 1 is a thousandth of it */
|
||||
if(!percent && s[0] == '1' && s[1] == '.' && s[2]){
|
||||
for(p = s + 2; *p == '0'; p++);
|
||||
if(!*p){
|
||||
*weight = WEIGHTSCALE;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
fraction = (*s == '.' || *s == '0');
|
||||
for(p = s; p < end; p++){
|
||||
if(*p == '.'){
|
||||
if(atpoint >= 0) return 1;
|
||||
atpoint = ndigits;
|
||||
continue;
|
||||
}
|
||||
if(*p < '0' || *p > '9') return 1;
|
||||
if(ndigits == 18) return 1;
|
||||
val = (val * 10) + (unsigned)(*p - '0');
|
||||
ndigits++;
|
||||
}
|
||||
if(!ndigits || val > WEIGHTSCALE) return 1;
|
||||
after = (atpoint < 0)? 0 : ndigits - atpoint;
|
||||
if(percent){
|
||||
/* a hundredth of the whole share for every 1% */
|
||||
if(after > 7) return 1;
|
||||
res = val * pow10[7 - after];
|
||||
}
|
||||
else if(fraction){
|
||||
/* the digits before the point are the leading zero and add
|
||||
nothing, so only the ones after it say what the share is */
|
||||
if(atpoint < 0) return val? 1 : (*weight = 0, 0);
|
||||
if(after > 9) return 1;
|
||||
res = val * pow10[9 - after];
|
||||
}
|
||||
else {
|
||||
/* thousandths, with the digits after the point carrying on
|
||||
from them: three digits of a whole share, six more after */
|
||||
if(after > 6) return 1;
|
||||
res = val * pow10[6 - after];
|
||||
}
|
||||
if(res > WEIGHTSCALE) return 1;
|
||||
*weight = (unsigned)res;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int h_parent(int argc, unsigned char **argv){
|
||||
struct ace *acl = NULL;
|
||||
struct chain *chains;
|
||||
@@ -922,9 +998,10 @@ static int h_parent(int argc, unsigned char **argv){
|
||||
return(21);
|
||||
}
|
||||
memset(chains, 0, sizeof(struct chain));
|
||||
chains->weight = (unsigned)atoi((char *)argv[1]);
|
||||
if(chains->weight == 0 || chains->weight >1000) {
|
||||
fprintf(stderr, "Chaining error: bad chain weight %u line %d\n", chains->weight, linenum);
|
||||
/* 0 is the fallback weight: such a parent is only used once every
|
||||
weighted parent of its group has failed */
|
||||
if(parseweight(argv[1], &chains->weight)) {
|
||||
fprintf(stderr, "Chaining error: bad chain weight %s line %d\n", argv[1], linenum);
|
||||
free(chains);
|
||||
return(3);
|
||||
}
|
||||
@@ -1497,7 +1574,7 @@ static int h_ace(int argc, unsigned char **argv){
|
||||
return 5;
|
||||
}
|
||||
*SAPORT(&acl->chains->addr) = htons((uint16_t)atoi((char *)argv[2]));
|
||||
acl->chains->weight = 1000;
|
||||
acl->chains->weight = WEIGHTSCALE;
|
||||
case ALLOW:
|
||||
case DENY:
|
||||
if(!conf.acl){
|
||||
|
||||
+1
-1
@@ -802,7 +802,7 @@ static struct property prop_pwlist[] = {
|
||||
static struct property prop_chain[] = {
|
||||
{"addr", ef_chain_addr, TYPE_SA, "parent address"},
|
||||
{"type", ef_chain_type, TYPE_STRING, "parent type"},
|
||||
{"weight", ef_chain_weight, TYPE_SHORT, "parent weight 0-1000"},
|
||||
{"weight", ef_chain_weight, TYPE_INTEGER, "parent weight, 1000000000 is the whole share, 0 is a fallback"},
|
||||
{"user", ef_chain_user, TYPE_STRING, "parent login"},
|
||||
{"password", ef_chain_password, TYPE_PASSWORD, "parent password"},
|
||||
{"secure", ef_chain_secure, TYPE_INTEGER, "secure mode"},
|
||||
|
||||
+88
-23
@@ -289,45 +289,105 @@ static void chainaddr(struct chain * cur, PROXYSOCKADDRTYPE * sa){
|
||||
*sa = fresh;
|
||||
}
|
||||
|
||||
static int parentfailed(struct clientparam * param, struct chain * ch){
|
||||
int i;
|
||||
|
||||
for(i = 0; i < param->nfailedparents; i++)
|
||||
if(param->failedparents[i] == ch) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Remember a parent which could not be used for this connection, so that a
|
||||
* retry picks another member of its group. The list is per connection: a
|
||||
* parent which is down for one client is not taken away from the others.
|
||||
*/
|
||||
static void parentfail(struct clientparam * param, struct chain * ch){
|
||||
if(!ch || param->nfailedparents >= MAXFAILEDPARENTS) return;
|
||||
if(parentfailed(param, ch)) return;
|
||||
param->failedparents[param->nfailedparents++] = ch;
|
||||
}
|
||||
|
||||
/* Pick the parent to use for one group.
|
||||
*
|
||||
* A group is the members whose weights add up to WEIGHTSCALE, together with
|
||||
* the zero weight members among them. *after is left pointing at the group
|
||||
* after this one, or at NULL. A group which lands within WEIGHTFUZZ of the
|
||||
* whole share counts as a whole one, so that 333 three times over is a group
|
||||
* rather than a group and a remainder of a thousandth.
|
||||
*
|
||||
* A member which already failed for this connection is not offered again and
|
||||
* its weight is given to the others, so a retry goes somewhere else. Zero
|
||||
* weight members are the fallback: they are only offered once every weighted
|
||||
* member of the group has failed, and they never take part in the share.
|
||||
* Where the weights add up to plainly less than the whole share the remainder
|
||||
* keeps its meaning of "no parent at all" and is still counted, so such a
|
||||
* group can still leave the connection direct.
|
||||
*
|
||||
* Returns NULL when the group adds no parent. *exhausted tells the two cases
|
||||
* apart: it is set when the group had parents and all of them are gone, which
|
||||
* is a failure rather than a reason to connect directly.
|
||||
*/
|
||||
static struct chain * pickchain(struct clientparam * param, struct chain * group,
|
||||
struct chain ** after, int * exhausted){
|
||||
struct chain *cur;
|
||||
uint64_t total = 0, avail = 0, slack;
|
||||
uint64_t r;
|
||||
|
||||
*exhausted = 0;
|
||||
for(cur = group; cur; cur = cur->next){
|
||||
if(total + WEIGHTFUZZ >= WEIGHTSCALE && cur->weight) break;
|
||||
total += cur->weight;
|
||||
if(cur->weight && !parentfailed(param, cur)) avail += cur->weight;
|
||||
}
|
||||
*after = cur;
|
||||
|
||||
if(avail){
|
||||
slack = (total + WEIGHTFUZZ < WEIGHTSCALE)? WEIGHTSCALE - total : 0;
|
||||
r = ((uint64_t)myrand() << 32 | myrand()) % (avail + slack);
|
||||
for(cur = group; cur != *after; cur = cur->next){
|
||||
if(!cur->weight || parentfailed(param, cur)) continue;
|
||||
if(r < cur->weight) return cur;
|
||||
r -= cur->weight;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
for(cur = group; cur != *after; cur = cur->next){
|
||||
if(!cur->weight && !parentfailed(param, cur)) return cur;
|
||||
}
|
||||
if(total) *exhausted = 1;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int handleredirect(struct clientparam * param, struct ace * acentry){
|
||||
int connected = 0;
|
||||
int weight = 1000;
|
||||
int res;
|
||||
int done = 0;
|
||||
int ha = 0;
|
||||
struct chain * cur;
|
||||
struct chain * after;
|
||||
struct chain * redir = NULL;
|
||||
int r2;
|
||||
int saved = 0;
|
||||
|
||||
if((SAISNULL(¶m->req) || !*SAPORT(¶m->req)) && param->operation != UDPASSOC) {
|
||||
return 100;
|
||||
}
|
||||
|
||||
r2 = (myrand()%1000);
|
||||
for(cur = acentry->chains; cur; cur = after){
|
||||
struct chain * sel;
|
||||
int exhausted;
|
||||
|
||||
for(cur = acentry->chains; cur; cur=cur->next){
|
||||
if(((weight = weight - cur->weight) > r2)|| done) {
|
||||
if(weight <= 0) {
|
||||
weight += 1000;
|
||||
done = 0;
|
||||
r2 = (myrand()%1000);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
sel = pickchain(param, cur, &after, &exhausted);
|
||||
/* every parent of the group is gone: connecting direct instead
|
||||
would be a way around the rule, so the request fails */
|
||||
if(exhausted) return 13;
|
||||
if(!sel) continue;
|
||||
cur = sel;
|
||||
if(cur->type != R_EXTIP && cur->type != R_HA &&
|
||||
cur->type != R_EXTPORT && cur->type != R_INTPORT) param->redirected++;
|
||||
done = 1;
|
||||
if(weight <= 0) {
|
||||
weight += 1000;
|
||||
done = 0;
|
||||
r2 = (myrand()%1000);
|
||||
}
|
||||
if(!connected){
|
||||
if(cur->type == R_EXTPORT || cur->type == R_INTPORT){
|
||||
if(cur->type == R_EXTPORT) param->extport = cur->range;
|
||||
else param->intport = cur->range;
|
||||
if(cur->next)continue;
|
||||
if(after)continue;
|
||||
return 0;
|
||||
}
|
||||
if(cur->type == R_EXTIP){
|
||||
@@ -349,7 +409,7 @@ int handleredirect(struct clientparam * param, struct ace * acentry){
|
||||
}
|
||||
}
|
||||
#endif
|
||||
if(cur->next)continue;
|
||||
if(after)continue;
|
||||
return 0;
|
||||
}
|
||||
else if(SAISNULL(&cur->addr) && !*SAPORT(&cur->addr)){
|
||||
@@ -375,7 +435,7 @@ int handleredirect(struct clientparam * param, struct ace * acentry){
|
||||
if(cur->type == R_HA){
|
||||
ha = 1;
|
||||
}
|
||||
if(cur->next)continue;
|
||||
if(after)continue;
|
||||
if(!ha) return 0;
|
||||
if(param->operation == UDPASSOC) return 0;
|
||||
}
|
||||
@@ -397,6 +457,7 @@ int handleredirect(struct clientparam * param, struct ace * acentry){
|
||||
saved = 1;
|
||||
}
|
||||
if((res = alwaysauth(param))){
|
||||
parentfail(param, cur);
|
||||
return (res >= 10)? res : 60+res;
|
||||
}
|
||||
if(ha) {
|
||||
@@ -420,7 +481,10 @@ int handleredirect(struct clientparam * param, struct ace * acentry){
|
||||
|
||||
chainaddr(cur, &next);
|
||||
res = (redir)?clientnegotiate(redir, param, (struct sockaddr *)&next, cur->exthost):0;
|
||||
if(res) return res;
|
||||
if(res) {
|
||||
parentfail(param, cur);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
redir = cur;
|
||||
param->redirtype = redir->type;
|
||||
@@ -444,6 +508,7 @@ int handleredirect(struct clientparam * param, struct ace * acentry){
|
||||
|
||||
if(!connected || !redir) return 0;
|
||||
res = clientnegotiate(redir, param, (struct sockaddr *)¶m->req, param->hostname);
|
||||
if(res) parentfail(param, redir);
|
||||
if(saved){
|
||||
SOCKET s;
|
||||
|
||||
|
||||
+13
-1
@@ -62,6 +62,13 @@ typedef struct _3proxy_sem_s {
|
||||
#endif
|
||||
#endif
|
||||
#define MAXBANDLIMS 10
|
||||
#define MAXFAILEDPARENTS 16
|
||||
/* Parent weights are kept as a share of WEIGHTSCALE, which is what the
|
||||
weights of a group add up to. WEIGHTFUZZ is how far short of it a group may
|
||||
fall and still be taken for a whole one, a thousandth of the share: three
|
||||
weights of 333, or of .333333333, otherwise leave a remainder. */
|
||||
#define WEIGHTSCALE 1000000000u
|
||||
#define WEIGHTFUZZ 1000000u
|
||||
|
||||
#ifdef WITH_POLL
|
||||
#include <poll.h>
|
||||
@@ -336,7 +343,7 @@ struct chain {
|
||||
unsigned char * exthost;
|
||||
unsigned char * extuser;
|
||||
unsigned char * extpass;
|
||||
unsigned short weight;
|
||||
unsigned weight;
|
||||
unsigned short cidr;
|
||||
/* local port range for extport/intport, first in the low half */
|
||||
uint32_t range;
|
||||
@@ -763,6 +770,11 @@ struct clientparam {
|
||||
one connection rather than for the service, which is how a redirect
|
||||
and a service name standing in for a mail proxy reach tlspr. */
|
||||
PROXYSERVICE starttls;
|
||||
/* Parents which failed for this connection. A retry picks another
|
||||
member of the group instead of the same one again, and a zero weight
|
||||
member is only reached once every weighted one is in here. */
|
||||
struct chain *failedparents[MAXFAILEDPARENTS];
|
||||
int nfailedparents;
|
||||
};
|
||||
|
||||
struct filemon {
|
||||
|
||||
Reference in new issue
Block a user