Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions packages/ns-api/files/ns.qos
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ from nethsec import utils
from nethsec.utils import ValidationError


def is_wan_interface(e_uci, interface_name):
"""Check if an interface is a WAN interface"""
try:
wan_interfaces = e_uci.get('firewall', 'ns_wan', 'network', dtype=str, default=[], list=True)
return interface_name in wan_interfaces
except:
# assume WAN to avoid inverting bandwidth for unknown interfaces
return True


def validate(data):
if 'interface' not in data:
raise ValidationError('name', 'required')
Expand Down Expand Up @@ -69,12 +79,19 @@ elif cmd == 'call':
device = [item['device'] for config_name, item in network_interfaces if config_name == key][0]
except:
continue
upload = int(interface['bandwidth_up'].removesuffix('mbit'))
download = int(interface['bandwidth_down'].removesuffix('mbit'))
# invert if not WAN interface
if not is_wan_interface(e_uci, key):
temp = upload
upload = download
download = temp
result.append({
'interface': key,
'device': device,
'disabled': interface['disabled'] == '1',
'upload': int(interface['bandwidth_up'].removesuffix('mbit')),
'download': int(interface['bandwidth_down'].removesuffix('mbit')),
'upload': upload,
'download': download,
})
print(json.dumps({'rules': result}))

Expand All @@ -86,6 +103,12 @@ elif cmd == 'call':
if data['interface'] not in utils.get_all_by_type(e_uci, 'network', 'interface').keys():
raise ValidationError('name', 'invalid')

# invert bandwidth for non-WAN interfaces before saving
if not is_wan_interface(e_uci, data['interface']):
upload = data['upload']
data['upload'] = data['download']
data['download'] = upload

e_uci.set('qosify', data['interface'], 'interface')
e_uci.set('qosify', data['interface'], 'name', data['interface'])
e_uci.set('qosify', data['interface'], 'disabled', data['disabled'])
Expand Down Expand Up @@ -129,6 +152,12 @@ elif cmd == 'call':
if data['interface'] not in utils.get_all_by_type(e_uci, 'qosify', 'interface').keys():
raise ValidationError('name', 'invalid')

# invert bandwidth for non-WAN interfaces before saving
if not is_wan_interface(e_uci, data['interface']):
upload = data['upload']
data['upload'] = data['download']
data['download'] = upload

e_uci.set('qosify', data['interface'], 'disabled', data['disabled'])
e_uci.set('qosify', data['interface'], 'bandwidth_up', f'{data["upload"]}mbit')
e_uci.set('qosify', data['interface'], 'bandwidth_down', f'{data["download"]}mbit')
Expand Down
Loading