|
楼主 |
发表于 2015-10-24 20:51:35
|
显示全部楼层
- def get_upstream_frequency():
- try:
- output = shell_execute('%s dev %s link' % (IW_PATH, WIFI_INTERFACE))
- for line in output.splitlines():
- if not line:
- continue
- match = RE_FREQ.search(line)
- if match:
- return match.group(1)
- return None
- except:
- LOGGER.exception('failed to get upstream frequency')
- return None
- def setup_networking(hotspot_interface):
- control_socket_dir = get_wpa_supplicant_control_socket_dir()
- setup_network_interface_ip(hotspot_interface, '10.24.1.1', '255.255.255.0')
- try:
- shell_execute('%s dnsmasq' % KILLALL_PATH)
- except:
- LOGGER.exception('failed to killall dnsmasq')
- shell_execute('%s -i %s --dhcp-authoritative --no-negcache --user=root --no-resolv --no-hosts '
- '--server=8.8.8.8 --dhcp-range=10.24.1.2,10.24.1.254,12h '
- '--dhcp-leasefile=/data/data/fq.router2/dnsmasq.leases '
- '--pid-file=/data/data/fq.router2/dnsmasq.pid' % (DNSMASQ_PATH, hotspot_interface))
- log_upstream_wifi_status('after setup networking', control_socket_dir)
- def setup_lo_alias():
- setup_network_interface_ip('lo:1', '10.1.2.3', '255.255.255.255')
- enable_ipv4_forward()
- shell_execute('iptables -P FORWARD ACCEPT')
- iptables.insert_rules(RULES, to_fq_chain=False)
- def setup_network_interface_ip(iface, ip, netmask):
- shell_execute('%s %s %s netmask %s' % (IFCONFIG_PATH, iface, ip, netmask))
- def enable_ipv4_forward():
- LOGGER.info('enable ipv4 forward')
- if shell.USE_SU:
- shell_execute('/data/data/fq.router2/busybox echo 1 > /proc/sys/net/ipv4/ip_forward')
- else:
- with open('/proc/sys/net/ipv4/ip_forward', 'w') as f:
- f.write('1')
- def log_upstream_wifi_status(log, control_socket_dir):
- try:
- LOGGER.info('=== %s ===' % log)
- shell_execute('%s -p %s -i %s status' % (P2P_CLI_PATH, control_socket_dir, WIFI_INTERFACE))
- shell_execute('%s dev %s link' % (IW_PATH, WIFI_INTERFACE))
- shell_execute('netcfg')
- except:
- LOGGER.exception('failed to log upstream wifi status')
- def start_p2p_persistent_network(iface, control_socket_dir, ssid, password, sets_channel=False):
- wpa_supplicant_control_socket_dir = get_wpa_supplicant_control_socket_dir()
- try:
- shell_execute('%s -p %s -i %s p2p_set disabled 0' % (P2P_CLI_PATH, control_socket_dir, iface))
- except:
- LOGGER.exception('failed to p2p_set disabled')
- set_driver_param(control_socket_dir, iface, 'use_p2p_group_interface=1')
- if iface != WIFI_INTERFACE:
- try:
- shell_execute('%s -p %s -i %s p2p_set disabled 0' %
- (P2P_CLI_PATH, wpa_supplicant_control_socket_dir, WIFI_INTERFACE))
- except:
- LOGGER.exception('failed to p2p_set disabled')
- set_driver_param(wpa_supplicant_control_socket_dir, WIFI_INTERFACE, 'use_p2p_group_interface=1')
- index = shell_execute('%s -p %s -i %s add_network' % (P2P_CLI_PATH, control_socket_dir, iface)).strip()
- def set_network(*params):
- shell_execute([P2P_CLI_PATH, '-p', control_socket_dir, '-i', iface, 'set_network', index] + list(params))
- set_network('mode', '3')
- set_network('disabled', '2')
- set_network('ssid', '\'"%s"\'' % ssid if shell.USE_SU else '"%s"' % ssid)
- set_network('key_mgmt', 'WPA-PSK')
- set_network('proto', 'RSN')
- set_network('pairwise', 'CCMP')
- set_network('psk', '\'"%s"\'' % password if shell.USE_SU else '"%s"' % password)
- frequency, channel = get_upstream_frequency_and_channel()
- if channel:
- channel = channel if sets_channel else 0
- reg_class = 81 if sets_channel else 0
- reset_p2p_channels(iface, control_socket_dir, channel, reg_class)
- if iface != WIFI_INTERFACE:
- reset_p2p_channels(WIFI_INTERFACE, wpa_supplicant_control_socket_dir, channel, reg_class)
- do_p2p_group_add(iface, control_socket_dir, index, frequency)
- gevent.sleep(2)
- if get_working_hotspot_iface():
- return index
- if 'p2p0' != iface:
- return index
- LOGGER.error('restart p2p_supplicant to fix unexpected GO creation')
- restart_service('p2p_supplicant')
- do_p2p_group_add('p2p0', control_socket_dir, index, frequency)
- if get_working_hotspot_iface():
- return index
- LOGGER.error('reset wifi interface to fix unexpected GO creation')
- disable_wifi_p2p_service()
- shell_execute('stop p2p_supplicant')
- shell_execute('netcfg wlan0 down')
- gevent.sleep(1)
- shell_execute('netcfg wlan0 up')
- gevent.sleep(1)
- shell_execute('start p2p_supplicant')
- gevent.sleep(1)
- do_p2p_group_add('p2p0', get_p2p_supplicant_control_socket_dir(), index, frequency)
- return index
- def do_p2p_group_add(iface, control_socket_dir, index, frequency):
- try:
- if frequency:
- shell_execute('%s -p %s -i %s p2p_group_add persistent=%s freq=%s ' %
- (P2P_CLI_PATH, control_socket_dir, iface, index, frequency.replace('.', '')))
- else:
- shell_execute('%s -p %s -i %s p2p_group_add persistent=%s' %
- (P2P_CLI_PATH, control_socket_dir, iface, index))
- except:
- LOGGER.exception('failed to add p2p group')
- def disable_wifi_p2p_service():
- try:
- if not os.path.exists(P2P_SUPPLICANT_CONF_PATH):
- return
- with open(P2P_SUPPLICANT_CONF_PATH) as f:
- conf = f.read()
- if '-fqrouter-p2p' in conf:
- return
- control_socket_dir = get_p2p_supplicant_control_socket_dir()
- conf = conf.replace(control_socket_dir, '%s-fqrouter-p2p' % control_socket_dir)
- with open(P2P_SUPPLICANT_CONF_PATH, 'w') as f:
- f.write(conf)
- except:
- LOGGER.exception('failed to disable wifi p2p service')
- def enable_wifi_p2p_service():
- try:
- if not os.path.exists(P2P_SUPPLICANT_CONF_PATH):
- return
- with open(P2P_SUPPLICANT_CONF_PATH) as f:
- conf = f.read()
- conf = conf.replace('-fqrouter-p2p', '')
- with open(P2P_SUPPLICANT_CONF_PATH, 'w') as f:
- f.write(conf)
- except:
- LOGGER.exception('failed to enable wifi p2p service')
- def restart_service(name):
- was_running = is_process_exists(name)
- LOGGER.info('%s was running: %s' % (name, was_running))
- try:
- shell_execute('stop %s' % name)
- except:
- LOGGER.exception('failed to stop %s' % name)
- try:
- shell_execute('start %s' % name)
- except:
- LOGGER.exception('failed to start %s' % name)
- if was_running:
- gevent.sleep(1)
- def is_process_exists(name):
- try:
- shell_execute('%s -0 %s' % (KILLALL_PATH, name))
- return True
- except:
- return False
- def set_driver_param(control_socket_dir, iface, param):
- try:
- shell_execute(
- '%s -p %s -i %s set driver_param %s' %
- (P2P_CLI_PATH, control_socket_dir, iface, param))
- except:
- LOGGER.exception('failed to set driver_param %s' % param)
- def reset_p2p_channels(iface, control_socket_dir, channel, reg_class):
- try:
- shell_execute('%s -p %s -i %s set p2p_oper_channel %s' %
- (P2P_CLI_PATH, control_socket_dir, iface, channel))
- shell_execute('%s -p %s -i %s set p2p_oper_reg_class %s' %
- (P2P_CLI_PATH, control_socket_dir, iface, reg_class))
- shell_execute('%s -p %s -i %s set p2p_listen_channel %s' %
- (P2P_CLI_PATH, control_socket_dir, iface, channel))
- shell_execute('%s -p %s -i %s set p2p_listen_reg_class %s' %
- (P2P_CLI_PATH, control_socket_dir, iface, reg_class))
- shell_execute('%s -p %s -i %s save_config' % (P2P_CLI_PATH, control_socket_dir, iface))
- except:
- LOGGER.exception('failed to reset p2p channels')
- def get_p2p_persistent_iface():
- netcfg_output = shell_execute('netcfg')
- if 'tiwlan' in netcfg_output: # stop further action to avoid mobile restart
- raise Exception('tiwlan does not support wifi repeater')
- for line in netcfg_output.splitlines(False):
- if line.startswith('p2p-'):
- return line.split(' ')[0]
- if line.startswith('ap0'):
- return 'ap0'
- return None
- def stop_p2p_persistent_network(control_socket_dir, control_iface, iface):
- try:
- shell_execute(
- '%s -p %s -i %s p2p_group_remove %s' %
- (P2P_CLI_PATH, control_socket_dir, control_iface, iface))
- except:
- LOGGER.error('failed to stop p2p persistent network')
- def delete_existing_p2p_persistent_networks(iface, control_socket_dir):
- LOGGER.info('delete existing p2p persistent networks')
- existing_networks = list_existing_networks(iface, control_socket_dir)
- for i in sorted(existing_networks.keys(), reverse=True):
- network = existing_networks[i]
- if 'P2P-PERSISTENT' in network['status']:
- delete_network(iface, control_socket_dir, i)
- def delete_network(iface, control_socket_dir, index):
- shell_execute(
- '%s -p %s -i %s remove_network %s' %
- (P2P_CLI_PATH, control_socket_dir, iface, index))
复制代码
|
|