|
| 1 | +package datacap |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "net/http/httputil" |
| 7 | + "sync" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/dustin/go-humanize" |
| 11 | + "github.com/getlantern/http-proxy-lantern/v2/common" |
| 12 | + "github.com/getlantern/http-proxy-lantern/v2/domains" |
| 13 | + "github.com/getlantern/http-proxy-lantern/v2/instrument" |
| 14 | + "github.com/getlantern/http-proxy-lantern/v2/listeners" |
| 15 | + "github.com/getlantern/http-proxy-lantern/v2/usage" |
| 16 | + "github.com/getlantern/proxy/v3/filters" |
| 17 | +) |
| 18 | + |
| 19 | +var ( |
| 20 | + epoch = time.Date(2016, 1, 1, 0, 0, 0, 0, time.UTC) |
| 21 | + |
| 22 | + alwaysThrottle = listeners.NewRateLimiter(10, 10) // this is basically unusably slow, only used for malicious or really old/broken clients |
| 23 | + |
| 24 | + defaultThrottleRate = int64(5000 * 1024 / 8) // 5 Mbps |
| 25 | +) |
| 26 | + |
| 27 | +// deviceFilter handles filtering and throttling of requests based on datacap |
| 28 | +type deviceFilter struct { |
| 29 | + datacapClient DatacapSidecarClient |
| 30 | + instrument instrument.Instrument |
| 31 | + sendXBQHeader bool |
| 32 | + limitersByDevice map[string]*listeners.RateLimiter |
| 33 | + limitersByDeviceMx sync.Mutex |
| 34 | +} |
| 35 | + |
| 36 | +// Settings represents the datacap settings for a device |
| 37 | +type Settings struct { |
| 38 | + Threshold int64 |
| 39 | +} |
| 40 | + |
| 41 | +// NewFilter creates a new datacap filter |
| 42 | +func NewFilter(datacapClient DatacapSidecarClient, instrument instrument.Instrument, sendXBQHeader bool) *deviceFilter { |
| 43 | + return &deviceFilter{ |
| 44 | + datacapClient: datacapClient, |
| 45 | + instrument: instrument, |
| 46 | + sendXBQHeader: sendXBQHeader, |
| 47 | + limitersByDevice: make(map[string]*listeners.RateLimiter, 0), |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// Apply applies the datacap filter to the request |
| 52 | +func (f *deviceFilter) Apply(cs *filters.ConnectionState, req *http.Request, next filters.Next) (*http.Response, *filters.ConnectionState, error) { |
| 53 | + |
| 54 | + if log.IsTraceEnabled() { |
| 55 | + reqStr, _ := httputil.DumpRequest(req, true) |
| 56 | + log.Tracef("DeviceFilter Middleware received request:\n%s", reqStr) |
| 57 | + } |
| 58 | + |
| 59 | + wc := cs.Downstream().(listeners.WrapConn) |
| 60 | + lanternDeviceID := req.Header.Get(common.DeviceIdHeader) |
| 61 | + if lanternDeviceID == "" { |
| 62 | + // Old lantern versions and possible cracks do not include the device |
| 63 | + // ID. Just throttle them. |
| 64 | + f.instrument.Throttle(req.Context(), true, "no-device-id") |
| 65 | + wc.ControlMessage("throttle", alwaysThrottle) |
| 66 | + return next(cs, req) |
| 67 | + } |
| 68 | + if lanternDeviceID == "~~~~~~" { |
| 69 | + // This is checkfallbacks, don't throttle it |
| 70 | + f.instrument.Throttle(req.Context(), false, "checkfallbacks") |
| 71 | + return next(cs, req) |
| 72 | + } |
| 73 | + |
| 74 | + // Even if a device hasn't hit its data cap, we always throttle to a default throttle rate to |
| 75 | + // keep bandwidth hogs from using too much bandwidth. Note - this does not apply to pro proxies |
| 76 | + // which don't use the devicefilter at all. |
| 77 | + throttleDefault := func(message string) { |
| 78 | + if defaultThrottleRate <= 0 { |
| 79 | + f.instrument.Throttle(req.Context(), false, message) |
| 80 | + } |
| 81 | + limiter := f.rateLimiterForDevice(lanternDeviceID, defaultThrottleRate, defaultThrottleRate) |
| 82 | + if log.IsTraceEnabled() { |
| 83 | + log.Tracef("Throttling connection to %v per second by default", |
| 84 | + humanize.Bytes(uint64(defaultThrottleRate))) |
| 85 | + } |
| 86 | + f.instrument.Throttle(req.Context(), true, "default") |
| 87 | + wc.ControlMessage("throttle", limiter) |
| 88 | + } |
| 89 | + |
| 90 | + // Some domains are excluded from being throttled and don't count towards the |
| 91 | + // bandwidth cap. |
| 92 | + if domains.ConfigForRequest(req).Unthrottled { |
| 93 | + throttleDefault("domain-excluded") |
| 94 | + return next(cs, req) |
| 95 | + } |
| 96 | + |
| 97 | + // Check usage from cache only - no eager fetching |
| 98 | + u := usage.Get(lanternDeviceID) |
| 99 | + if u == nil { |
| 100 | + // No usage data available yet, allow the request |
| 101 | + f.instrument.Throttle(req.Context(), false, "no-usage-data") |
| 102 | + return next(cs, req) |
| 103 | + } |
| 104 | + |
| 105 | + settings, err := f.datacapClient.GetDatacapUsage(req.Context(), lanternDeviceID) |
| 106 | + if err != nil { |
| 107 | + log.Errorf("failed to get datacap usage for device %s: %v", lanternDeviceID, err) |
| 108 | + f.instrument.Throttle(req.Context(), false, "datacap-error") |
| 109 | + //allow the request to proceed if we fail to get datacap usage |
| 110 | + settings = &TrackDatacapResponse{ |
| 111 | + Allowed: true, |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + measuredCtx := map[string]interface{}{ |
| 116 | + "throttled": false, |
| 117 | + } |
| 118 | + |
| 119 | + var capOn bool |
| 120 | + |
| 121 | + // To turn the datacap off we simply set the threshold to 0 or below |
| 122 | + if settings.Allowed { |
| 123 | + log.Tracef("Got datacap settings: %v", settings) |
| 124 | + capOn = settings.CapLimit > 0 |
| 125 | + |
| 126 | + measuredCtx["datacap_settings"] = settings |
| 127 | + if capOn { |
| 128 | + measuredCtx["datacap_threshold"] = settings.CapLimit |
| 129 | + measuredCtx["datacap_usage"] = u.Bytes |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + if capOn && u.Bytes > settings.CapLimit { |
| 134 | + f.instrument.Throttle(req.Context(), true, "over-datacap") |
| 135 | + measuredCtx["throttled"] = true |
| 136 | + limiter := f.rateLimiterForDevice(lanternDeviceID, defaultThrottleRate, defaultThrottleRate) |
| 137 | + if log.IsTraceEnabled() { |
| 138 | + log.Tracef("Throttling connection from device %s to %v per second", lanternDeviceID, |
| 139 | + humanize.Bytes(uint64(defaultThrottleRate))) |
| 140 | + } |
| 141 | + f.instrument.Throttle(req.Context(), true, "datacap") |
| 142 | + wc.ControlMessage("throttle", limiter) |
| 143 | + measuredCtx["throttled"] = true |
| 144 | + } else { |
| 145 | + throttleDefault("") |
| 146 | + } |
| 147 | + |
| 148 | + wc.ControlMessage("measured", measuredCtx) |
| 149 | + |
| 150 | + resp, nextCtx, err := next(cs, req) |
| 151 | + if resp == nil || err != nil { |
| 152 | + return resp, nextCtx, err |
| 153 | + } |
| 154 | + if !capOn || !f.sendXBQHeader { |
| 155 | + return resp, nextCtx, err |
| 156 | + } |
| 157 | + if resp.Header == nil { |
| 158 | + resp.Header = make(http.Header, 1) |
| 159 | + } |
| 160 | + uMiB := u.Bytes / (1024 * 1024) |
| 161 | + xbq := fmt.Sprintf("%d/%d/%d", uMiB, settings.CapLimit/(1024*1024), int64(u.AsOf.Sub(epoch).Seconds())) |
| 162 | + xbqv2 := fmt.Sprintf("%s/%d", xbq, u.TTLSeconds) |
| 163 | + resp.Header.Set(common.XBQHeader, xbq) // for backward compatibility with older clients |
| 164 | + resp.Header.Set(common.XBQHeaderv2, xbqv2) // for new clients that support different bandwidth cap expirations |
| 165 | + f.instrument.XBQHeaderSent(req.Context()) |
| 166 | + return resp, nextCtx, err |
| 167 | +} |
| 168 | + |
| 169 | +func (f *deviceFilter) rateLimiterForDevice(deviceID string, rateLimitRead, rateLimitWrite int64) *listeners.RateLimiter { |
| 170 | + f.limitersByDeviceMx.Lock() |
| 171 | + defer f.limitersByDeviceMx.Unlock() |
| 172 | + |
| 173 | + limiter := f.limitersByDevice[deviceID] |
| 174 | + if limiter == nil || limiter.GetRateRead() != rateLimitRead || limiter.GetRateWrite() != rateLimitWrite { |
| 175 | + limiter = listeners.NewRateLimiter(rateLimitRead, rateLimitWrite) |
| 176 | + f.limitersByDevice[deviceID] = limiter |
| 177 | + } |
| 178 | + return limiter |
| 179 | +} |
0 commit comments