-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
增加LeastConnectionLoadBalancer负载均衡策略(config.json里面配置使用) (#35)
- Loading branch information
Showing
6 changed files
with
222 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
(( | ||
{ isDebugEnabled } = pipy.solve('config.js'), | ||
|
||
{ aclCounter } = pipy.solve('lib/metrics.js'), | ||
aclDomainCounter = aclCounter.withLabels('domain'), | ||
|
||
parseIpList = ipList => ( | ||
(ips, ipRanges) => ( | ||
(ipList || []).forEach( | ||
o => ( | ||
o.indexOf('/') > 0 ? ( | ||
!ipRanges && (ipRanges = []), | ||
ipRanges.push(new Netmask(o)) | ||
) : ( | ||
!ips && (ips = {}), | ||
ips[o] = true | ||
) | ||
) | ||
), | ||
(ips || ipRanges) ? ({ ips, ipRanges }) : undefined | ||
) | ||
)(), | ||
|
||
aclsCache = new algo.Cache( | ||
acls => ( | ||
{ | ||
blackList: parseIpList(acls?.blacklist), | ||
whiteList: parseIpList(acls?.whitelist), | ||
} | ||
) | ||
), | ||
|
||
checkACLs = ip => ( | ||
( | ||
acls = aclsCache.get(__domain?.AccessControlLists), | ||
white = acls?.whiteList, | ||
black = acls?.blackList, | ||
blackMode = true, | ||
block = false, | ||
pass = false, | ||
) => ( | ||
white && ( | ||
blackMode = false, | ||
(white?.ips?.[ip] && (pass = true)) || ( | ||
pass = white?.ipRanges?.find?.(r => r.contains(ip)) | ||
) | ||
), | ||
blackMode && ( | ||
(black?.ips?.[ip] && (block = true)) || ( | ||
block = black?.ipRanges?.find?.(r => r.contains(ip)) | ||
) | ||
), | ||
blackMode ? Boolean(!block) : Boolean(pass) | ||
) | ||
)(), | ||
) => pipy({ | ||
_ips: null, | ||
_pass: false, | ||
}) | ||
|
||
.import({ | ||
__domain: 'route', | ||
}) | ||
|
||
.pipeline() | ||
.handleMessageStart( | ||
msg => ( | ||
__domain?.AccessControlLists?.enableXFF && ( | ||
_ips = msg.head?.headers['x-forwarded-for'] | ||
), | ||
_ips ? ( | ||
_pass = _ips.split(',').every(ip => checkACLs(ip.trim())) | ||
) : ( | ||
_pass = checkACLs(__inbound.remoteAddress) | ||
) | ||
) | ||
) | ||
.branch( | ||
() => _pass, ( | ||
$=>$.chain() | ||
), ( | ||
$=>$ | ||
.branch( | ||
isDebugEnabled, ( | ||
$=>$.handleStreamStart( | ||
() => ( | ||
console.log('[access-control-domain] blocked XFF, IP address:', _ips, __inbound.remoteAddress, _pass) | ||
) | ||
) | ||
) | ||
) | ||
.replaceMessage( | ||
() => ( | ||
aclDomainCounter.increase(), | ||
new Message({ status: __domain?.AccessControlLists?.status || 403 }, __domain?.AccessControlLists?.message || '') | ||
) | ||
) | ||
) | ||
) | ||
|
||
)() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
(( | ||
{ isDebugEnabled } = pipy.solve('config.js'), | ||
|
||
{ aclCounter } = pipy.solve('lib/metrics.js'), | ||
aclRouteCounter = aclCounter.withLabels('route'), | ||
|
||
parseIpList = ipList => ( | ||
(ips, ipRanges) => ( | ||
(ipList || []).forEach( | ||
o => ( | ||
o.indexOf('/') > 0 ? ( | ||
!ipRanges && (ipRanges = []), | ||
ipRanges.push(new Netmask(o)) | ||
) : ( | ||
!ips && (ips = {}), | ||
ips[o] = true | ||
) | ||
) | ||
), | ||
(ips || ipRanges) ? ({ ips, ipRanges }) : undefined | ||
) | ||
)(), | ||
|
||
aclsCache = new algo.Cache( | ||
acls => ( | ||
{ | ||
blackList: parseIpList(acls?.blacklist), | ||
whiteList: parseIpList(acls?.whitelist), | ||
} | ||
) | ||
), | ||
|
||
checkACLs = ip => ( | ||
( | ||
acls = aclsCache.get(__route?.config?.AccessControlLists), | ||
white = acls?.whiteList, | ||
black = acls?.blackList, | ||
blackMode = true, | ||
block = false, | ||
pass = false, | ||
) => ( | ||
white && ( | ||
blackMode = false, | ||
(white?.ips?.[ip] && (pass = true)) || ( | ||
pass = white?.ipRanges?.find?.(r => r.contains(ip)) | ||
) | ||
), | ||
blackMode && ( | ||
(black?.ips?.[ip] && (block = true)) || ( | ||
block = black?.ipRanges?.find?.(r => r.contains(ip)) | ||
) | ||
), | ||
blackMode ? Boolean(!block) : Boolean(pass) | ||
) | ||
)(), | ||
) => pipy({ | ||
_ips: null, | ||
_pass: false, | ||
}) | ||
|
||
.import({ | ||
__route: 'route', | ||
}) | ||
|
||
.pipeline() | ||
.handleMessageStart( | ||
msg => ( | ||
__route?.config?.AccessControlLists?.enableXFF && ( | ||
_ips = msg.head?.headers['x-forwarded-for'] | ||
), | ||
_ips ? ( | ||
_pass = _ips.split(',').every(ip => checkACLs(ip.trim())) | ||
) : ( | ||
_pass = checkACLs(__inbound.remoteAddress) | ||
) | ||
) | ||
) | ||
.branch( | ||
() => _pass, ( | ||
$=>$.chain() | ||
), ( | ||
$=>$ | ||
.branch( | ||
isDebugEnabled, ( | ||
$=>$.handleStreamStart( | ||
() => ( | ||
console.log('[access-control-route] blocked XFF, IP address:', _ips, __inbound.remoteAddress, _pass) | ||
) | ||
) | ||
) | ||
) | ||
.replaceMessage( | ||
() => ( | ||
aclRouteCounter.increase(), | ||
new Message({ status: __route?.config?.AccessControlLists?.status || 403 }, __route?.config?.AccessControlLists?.message || '') | ||
) | ||
) | ||
) | ||
) | ||
|
||
)() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters