Compare commits

...

6 Commits

Author SHA1 Message Date
hamjin
151f7c9076
Merge c9e273e856 into 063836fe5d 2024-06-06 16:40:38 +08:00
wwqgtxx
063836fe5d chore: sync hysteria2 bbr changes
e0e75c4630
2024-06-05 11:56:27 +08:00
hamjin
c9e273e856
Merge branch 'Alpha' into Alpha 2024-03-12 15:11:43 +08:00
hamjin
9b4857649d fix: GeoIPMatcher doesn't obey the config when using GeoData 2024-02-26 14:38:54 +08:00
hamjin
c49cede02b Fix for GeodataMode 2024-02-26 14:33:44 +08:00
hamjin
088fdb2ddd Make private ip the same as geoip:private
Currently ip.IsPrivate and netip.Addr.IsPrivate only matches a part of private IPs.
2024-02-25 22:14:48 +08:00
3 changed files with 47 additions and 10 deletions

View File

@ -6,6 +6,28 @@ import (
"net/netip"
)
// Private IP CIDRs
var privateIPCIDRs = []string{
"0.0.0.0/8",
"10.0.0.0/8",
"100.64.0.0/10",
"127.0.0.0/8",
"169.254.0.0/16",
"172.16.0.0/12",
"192.0.0.0/24",
"192.0.2.0/24",
"192.88.99.0/24",
"192.168.0.0/16",
"198.18.0.0/15",
"198.51.100.0/24",
"203.0.113.0/24",
"224.0.0.0/3",
"::/127",
"fc00::/7",
"fe80::/10",
"ff00::/8",
}
// IpToAddr converts the net.IP to netip.Addr.
// If slice's length is not 4 or 16, IpToAddr returns netip.Addr{}
func IpToAddr(slice net.IP) netip.Addr {
@ -51,3 +73,18 @@ func UnMasked(p netip.Prefix) netip.Addr {
}
return addr
}
// IsPrivateIP returns whether IP is private
// If IP is private, return true, else return false
func IsPrivateIP(ip netip.Addr) bool {
for _, network := range privateIPCIDRs {
_, subnet, err := net.ParseCIDR(network)
if err != nil {
continue
}
if subnet.Contains(ip.AsSlice()) {
return true
}
}
return false
}

View File

@ -4,6 +4,7 @@ import (
"net/netip"
"strings"
"github.com/metacubex/mihomo/common/nnip"
"github.com/metacubex/mihomo/component/geodata"
"github.com/metacubex/mihomo/component/geodata/router"
"github.com/metacubex/mihomo/component/mmdb"
@ -26,7 +27,7 @@ func (gf *geoipFilter) Match(ip netip.Addr) bool {
if !C.GeodataMode {
codes := mmdb.IPInstance().LookupCode(ip.AsSlice())
for _, code := range codes {
if !strings.EqualFold(code, gf.code) && !ip.IsPrivate() {
if !strings.EqualFold(code, gf.code) && !nnip.IsPrivateIP(ip) {
return true
}
}
@ -35,13 +36,13 @@ func (gf *geoipFilter) Match(ip netip.Addr) bool {
if geoIPMatcher == nil {
var err error
geoIPMatcher, _, err = geodata.LoadGeoIPMatcher("CN")
geoIPMatcher, _, err = geodata.LoadGeoIPMatcher(gf.code)
if err != nil {
log.Errorln("[GeoIPFilter] LoadGeoIPMatcher error: %s", err.Error())
return false
}
}
return !geoIPMatcher.Match(ip)
return !geoIPMatcher.Match(ip) && !nnip.IsPrivateIP(ip)
}
type ipnetFilter struct {

View File

@ -62,7 +62,7 @@ const (
// Flag.
defaultStartupFullLossCount = 8
quicBbr2DefaultLossThreshold = 0.02
maxBbrBurstPackets = 3
maxBbrBurstPackets = 10
)
type bbrMode int
@ -334,6 +334,8 @@ func (b *bbrSender) OnPacketSent(
}
b.sampler.OnPacketSent(sentTime, packetNumber, bytes, bytesInFlight, isRetransmittable)
b.maybeAppLimited(bytesInFlight)
}
// CanSend implements the SendAlgorithm interface.
@ -413,8 +415,6 @@ func (b *bbrSender) OnCongestionEventEx(priorInFlight congestion.ByteCount, even
// packet in lost_packets.
var lastPacketSendState sendTimeState
b.maybeApplimited(priorInFlight)
// Update bytesInFlight
b.bytesInFlight = priorInFlight
for _, p := range ackedPackets {
@ -541,7 +541,7 @@ func (b *bbrSender) setDrainGain(drainGain float64) {
b.drainGain = drainGain
}
// What's the current estimated bandwidth in bytes per second.
// Get the current bandwidth estimate. Note that Bandwidth is in bits per second.
func (b *bbrSender) bandwidthEstimate() Bandwidth {
return b.maxBandwidth.GetBest()
}
@ -700,14 +700,13 @@ func (b *bbrSender) checkIfFullBandwidthReached(lastPacketSendState *sendTimeSta
}
}
func (b *bbrSender) maybeApplimited(bytesInFlight congestion.ByteCount) {
func (b *bbrSender) maybeAppLimited(bytesInFlight congestion.ByteCount) {
congestionWindow := b.GetCongestionWindow()
if bytesInFlight >= congestionWindow {
return
}
availableBytes := congestionWindow - bytesInFlight
drainLimited := b.mode == bbrModeDrain && bytesInFlight > congestionWindow/2
if !drainLimited || availableBytes > maxBbrBurstPackets*b.maxDatagramSize {
if availableBytes > maxBbrBurstPackets*b.maxDatagramSize {
b.sampler.OnAppLimited()
}
}