Example #1
0
def run():
    iptables.init_fq_chains()
    shutdown_hook.add(iptables.flush_fq_chain)
    iptables.insert_rules(DNS_RULES)
    shutdown_hook.add(functools.partial(iptables.delete_rules, DNS_RULES))
    iptables.insert_rules(SOCKS_RULES)
    shutdown_hook.add(functools.partial(iptables.delete_rules, SOCKS_RULES))
    wifi.setup_lo_alias()
    try:
        comp_scrambler.start()
        shutdown_hook.add(comp_scrambler.stop)
    except:
        LOGGER.exception('failed to start comp_scrambler')
        comp_scrambler.stop()
    try:
        comp_shortcut.start()
        shutdown_hook.add(comp_shortcut.stop)
    except:
        LOGGER.exception('failed to start comp_shortcut')
        comp_shortcut.stop()
    args = [
        '--log-level', 'INFO',
        '--log-file', '/data/data/fq.router2/log/fqsocks.log',
        '--ifconfig-command', '/data/data/fq.router2/busybox',
        '--ip-command', '/data/data/fq.router2/busybox',
        '--outbound-ip', '10.1.2.3',
        '--tcp-gateway-listen', '10.1.2.3:12345',
        '--dns-server-listen', '10.1.2.3:12345']
    args = config.configure_fqsocks(args)
    fqsocks.fqsocks.main(args)
Example #2
0
def run():
    iptables.init_fq_chains()
    shutdown_hook.add(iptables.flush_fq_chain)
    iptables.insert_rules(DNS_RULES)
    shutdown_hook.add(functools.partial(iptables.delete_rules, DNS_RULES))
    iptables.insert_rules(SOCKS_RULES)
    shutdown_hook.add(functools.partial(iptables.delete_rules, SOCKS_RULES))
    wifi.setup_lo_alias()
    args = [
        '--log-level', 'INFO',
        '--log-file', '/data/data/fq.router2/log/fqsocks.log',
        '--ifconfig-command', '/data/data/fq.router2/busybox',
        '--ip-command', '/data/data/fq.router2/busybox',
        '--outbound-ip', '10.1.2.3',
        '--tcp-gateway-listen', '10.1.2.3:12345',
        '--dns-server-listen', '10.1.2.3:12345']
    if shell.USE_SU:
        args.append('--no-tcp-scrambler')
    args = config.configure_fqsocks(args)
    fqsocks.fqsocks.init_config(args)
    if fqsocks.config_file.read_config()['tcp_scrambler_enabled']:
        try:
            comp_scrambler.start()
            shutdown_hook.add(comp_scrambler.stop)
        except:
            LOGGER.exception('failed to start comp_scrambler')
            comp_scrambler.stop()
    if fqsocks.config_file.read_config()['china_shortcut_enabled']:
        try:
            comp_shortcut.start()
            shutdown_hook.add(comp_shortcut.stop)
        except:
            LOGGER.exception('failed to start comp_shortcut')
            comp_shortcut.stop()
    fqsocks.fqsocks.main()
Example #3
0
def start_full_proxy():
    try:
        shutdown_hook.add(redsocks_monitor.kill_redsocks)
        thread.start_new(refresh_proxies, ())
    except:
        LOGGER.exception('failed to start keep proxies fresh thread')
        proxies.clear()
    handle_nfqueue()
Example #4
0
def start_full_proxy():
    try:
        shutdown_hook.add(redsocks_monitor.kill_redsocks)
        thread = threading.Thread(target=refresh_proxies)
        thread.setDaemon(False)
        thread.start()
    except:
        LOGGER.exception('failed to start keep proxies fresh thread')
        proxies.clear()
    handle_nfqueue()
Example #5
0
def keep_proxies_fresh():
    global redsocks_started_at
    global uses_goagent
    shutdown_hook.add(kill_redsocks)
    try:
        while True:
            if not proxies:
                LOGGER.info('no proxies, start redsocks now')
                try:
                    if kill_redsocks():
                        LOGGER.info('existing redsocks killed')
                        time.sleep(2)
                    start_redsocks()
                except:
                    LOGGER.exception('failed to start redsocks')
                    kill_redsocks()
                    return
                if proxies:
                    redsocks_started_at = time.time()
                    if not can_access_twitter():
                        LOGGER.info(
                            'still can not access twitter, retry in 30 seconds'
                        )
                        proxies.clear()
                        time.sleep(30)
                else:
                    LOGGER.info(
                        'still no proxies after redsocks started, retry in 30 seconds'
                    )
                    time.sleep(30)
            if time.time() - redsocks_started_at > 60 * 30:
                LOGGER.info('refresh now, restart redsocks')
                proxies.clear()
            if uses_goagent and not goagent_service.enabled:
                LOGGER.info('goagent service disabled, restart redsocks')
                uses_goagent = False
                proxies.clear()
            time.sleep(1)
            dump_redsocks_client_list()
    except:
        LOGGER.exception('failed to keep proxies fresh')
Example #6
0
def run():
    global fqdns_process
    shutdown_hook.add(clean)
    insert_iptables_rules()
    fqdns_process = subprocess.Popen(
        [shell.PYTHON_PATH, '-m', 'fqdns',
         '--log-level', 'INFO',
         '--log-file', '/data/data/fq.router/dns.log',
         '--outbound-ip', '10.1.2.3', # send from 10.1.2.3 so we can skip redirecting those traffic
         'serve', '--listen', '10.1.2.3:5353',
         '--enable-china-domain', '--enable-hosted-domain'],
        stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    time.sleep(2)
    if fqdns_process.poll() is not None:
        try:
            output, _ = fqdns_process.communicate()
            LOGGER.error('fqdns exit output: %s' % output)
        except:
            LOGGER.exception('failed to log fqdns exit output')
        raise Exception('failed to start fqdns')
    thread.start_new(monitor_fqdns, ())
Example #7
0
def run():
    iptables.init_fq_chains()
    shutdown_hook.add(iptables.flush_fq_chain)
    iptables.insert_rules(DNS_RULES)
    shutdown_hook.add(functools.partial(iptables.delete_rules, DNS_RULES))
    iptables.insert_rules(SOCKS_RULES)
    shutdown_hook.add(functools.partial(iptables.delete_rules, SOCKS_RULES))
    wifi.setup_lo_alias()
    args = [
        '--log-level', 'INFO',
        '--log-file', '/data/data/fq.router2/log/fqsocks.log',
        '--ifconfig-command', '/data/data/fq.router2/busybox',
        '--ip-command', '/data/data/fq.router2/busybox',
        '--outbound-ip', '10.1.2.3',
        '--tcp-gateway-listen', '10.1.2.3:12345',
        '--dns-server-listen', '10.1.2.3:12345']
    if shell.USE_SU:
        args.append('--no-tcp-scrambler')
    args = config.configure_fqsocks(args)
    fqsocks.fqsocks.init_config(args)
    if fqsocks.config_file.read_config()['tcp_scrambler_enabled']:
        try:
            comp_scrambler.start()
            shutdown_hook.add(comp_scrambler.stop)
        except:
            LOGGER.exception('failed to start comp_scrambler')
            comp_scrambler.stop()
    if fqsocks.config_file.read_config()['china_shortcut_enabled']:
        try:
            comp_shortcut.start()
            shutdown_hook.add(comp_shortcut.stop)
        except:
            LOGGER.exception('failed to start comp_shortcut')
            comp_shortcut.stop()
    fqsocks.fqsocks.DNS_HANDLER.set_dns_bypass(fqsocks.config_file.read_config()['dns']['bypass'])
    fqsocks.fqsocks.get_default_dns_server = config.get_default_dns_server
    fqsocks.fqsocks.main()
Example #8
0
def keep_proxies_fresh():
    global redsocks_started_at
    global uses_goagent
    shutdown_hook.add(kill_redsocks)
    try:
        while True:
            if not proxies:
                LOGGER.info('no proxies, start redsocks now')
                try:
                    if kill_redsocks():
                        LOGGER.info('existing redsocks killed')
                        time.sleep(2)
                    start_redsocks()
                except:
                    LOGGER.exception('failed to start redsocks')
                    kill_redsocks()
                    return
                if proxies:
                    redsocks_started_at = time.time()
                    if not can_access_twitter():
                        LOGGER.info('still can not access twitter, retry in 30 seconds')
                        proxies.clear()
                        time.sleep(30)
                else:
                    LOGGER.info('still no proxies after redsocks started, retry in 30 seconds')
                    time.sleep(30)
            if time.time() - redsocks_started_at > 60 * 30:
                LOGGER.info('refresh now, restart redsocks')
                proxies.clear()
            if uses_goagent and not goagent_service.enabled:
                LOGGER.info('goagent service disabled, restart redsocks')
                uses_goagent = False
                proxies.clear()
            time.sleep(1)
            dump_redsocks_client_list()
    except:
        LOGGER.exception('failed to keep proxies fresh')
Example #9
0
import tornado.web
import tornado.template
import dpkt

import shutdown_hook


LOGGER = logging.getLogger(__name__)
ERROR_NO_DATA = 11

ROOT_DIR = os.path.dirname(__file__)
template_loader = tornado.template.Loader(ROOT_DIR)

raw_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
shutdown_hook.add(raw_socket.close)
raw_socket.setsockopt(socket.SOL_IP, socket.IP_HDRINCL, 1)

icmp_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)
shutdown_hook.add(icmp_socket.close)
icmp_socket.settimeout(0)


class SelfCheckHandler(tornado.web.RequestHandler):
    def get(self):
        action = self.get_argument('action', default=None)
        if 'resolve-domain' == action:
            domain = self.get_argument('domain')
            ip = socket.gethostbyname(domain)
            self.write(ip)
            return
Example #10
0
def insert_iptables_rules():
    shutdown_hook.add(delete_iptables_rules)
    iptables.insert_rules(RULES)
Example #11
0
def run():
    iptables.tables = {}
    iptables.init_fq_chains()
    shutdown_hook.add(iptables.flush_fq_chain)
    if not os.getenv('NO_FQDNS'):
        iptables.insert_rules(DNS_RULES)
        shutdown_hook.add(functools.partial(iptables.delete_rules, DNS_RULES))
    setup_nat()
    iptables.insert_rules(SOCKS_RULES)
    shutdown_hook.add(functools.partial(iptables.delete_rules, SOCKS_RULES))
    wifi.setup_lo_alias()

    if not os.getenv('NO_TEREDO'):
        LOGGER.info('init teredo and tun')
        sock = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
        sock.bind(('10.1.2.3', 0))
        teredo_client = teredo.teredo_client(
            sock, teredo.get_default_teredo_server())
        teredo_ip = None
        try:
            teredo_ip = teredo_client.start()
        except:
            LOGGER.exception('start teredo fail')
        if not teredo_ip:
            LOGGER.error('start teredo client fail, use default:%s' %
                         default_loacl_teredo_ip)
            teredo_ip = default_loacl_teredo_ip
        else:
            LOGGER.info('teredo start succeed, teredo ip:%s' % teredo_ip)

        tun_fd = init_tun(teredo_ip)
        if not tun_fd:
            LOGGER.error('init tun fail!')
        else:
            teredo.tun_fd = tun_fd
            teredo_client.server_forever(teredo_ip)
            gevent.spawn(redirect_tun_traffic, tun_fd, teredo_client)

    args = [
        '--log-level',
        'DEBUG' if os.getenv('DEBUG') else 'INFO',
        '--log-file',
        LOG_DIR + '/fqsocks.log',
        '--ifconfig-command',
        home_path + '/../busybox',
        #'--ip-command', 'ip',
        '--outbound-ip',
        '10.1.2.3',
        '--tcp-gateway-listen',
        '10.1.2.3:12345',
        '--dns-server-listen',
        '10.1.2.3:12345'
    ]
    if shell.USE_SU:
        args.append('--no-tcp-scrambler')
    args = config.configure_fqsocks(args)
    fqsocks.fqsocks.init_config(args)
    if fqsocks.config_file.read_config()['tcp_scrambler_enabled']:
        try:
            comp_scrambler.start()
            shutdown_hook.add(comp_scrambler.stop)
        except:
            LOGGER.exception('failed to start comp_scrambler')
            comp_scrambler.stop()
    if fqsocks.config_file.read_config(
    )['china_shortcut_enabled'] and comp_shortcut_enabled:
        try:
            comp_shortcut.start()
            shutdown_hook.add(comp_shortcut.stop)
        except:
            LOGGER.exception('failed to start comp_shortcut')
            comp_shortcut.stop()
    iptables.tables = {}
    fqsocks.fqsocks.main()
Example #12
0
def insert_iptables_rules():
    shutdown_hook.add(delete_iptables_rules)
    iptables.insert_rules(RULES)
Example #13
0
            'target': 'RETURN',
            'source': lan_ip_range,
            'iface_in': iface
        }, ('filter', 'scramble_FORWARD',
            '-i %s -s %s -j RETURN' % (iface, lan_ip_range))))


for iface in network_interface.list_data_network_interfaces():
    add_input_chain(iface)
    add_output_chain(iface)
    add_forward_chain(iface)
    add_rules(iface, is_forward=False)
    add_rules(iface, is_forward=True)

raw_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
shutdown_hook.add(raw_socket.close)
raw_socket.setsockopt(socket.SOL_IP, socket.IP_HDRINCL, 1)
SO_MARK = 36
raw_socket.setsockopt(socket.SOL_SOCKET, SO_MARK, 0xcafe)


class TcpServiceStatus(object):
    def __init__(self):
        self.last_activity_at = None
        self.error = None

    def get_status_description(self):
        if self.error:
            return 'ERROR'
        if not self.last_activity_at:
            return 'NOT STARTED'