python的端口转发

# -*- coding: utf-8 -*-
# tcp mapping

import sys
import socket
import logging
import threading

# 端口映射配置信息
CFG_REMOTE_IP = '127.0.0.1'
CFG_REMOTE_PORT = 23
CFG_LOCAL_IP = '0.0.0.0'
CFG_LOCAL_PORT = 1023

# 接收数据缓存大小
PKT_BUFF_SIZE = 2048

logger = logging.getLogger("Proxy Logging")
formatter = logging.Formatter('%(name)-12s %(asctime)s %(levelname)-8s %(lineno)-4d %(message)s',
                              '%Y %b %d %a %H:%M:%S', )

stream_handler = logging.StreamHandler(sys.stderr)
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)

logger.setLevel(logging.DEBUG)


# 单向流数据传递
def tcp_mapping_worker(conn_receiver, conn_sender):
    while True:
        try:
            data = conn_receiver.recv(PKT_BUFF_SIZE)
        except Exception:
            logger.debug('Connection closed.')
            break

        if not data:
            logger.info('No more data is received.')
            break

        try:
            conn_sender.sendall(data)
        except Exception:
            logger.error('Failed sending data.')
            break

        # logger.info('Mapping data > %s ' % repr(data))
        logger.info(
            'Mapping > %s -> %s > %d bytes.' % (conn_receiver.getpeername(), conn_sender.getpeername(), len(data)))

    conn_receiver.close()
    conn_sender.close()

    return


# 端口映射请求处理
def tcp_mapping_request(local_conn, remote_ip, remote_port):
    remote_conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    try:
        remote_conn.connect((remote_ip, remote_port))
    except Exception:
        local_conn.close()
        logger.error('Unable to connect to the remote server.')
        return

    threading.Thread(target=tcp_mapping_worker, args=(local_conn, remote_conn)).start()
    threading.Thread(target=tcp_mapping_worker, args=(remote_conn, local_conn)).start()

    return


# 端口映射函数
def tcp_mapping(remote_ip, remote_port, local_ip, local_port):
    local_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    local_server.bind((local_ip, local_port))
    local_server.listen(5)

    logger.debug('Starting mapping service on ' + local_ip + ':' + str(local_port) + ' ...')

    while True:
        try:
            (local_conn, local_addr) = local_server.accept()
        except KeyboardInterrupt as Exception:
            local_server.close()
            logger.debug('Stop mapping service.')
            break

        threading.Thread(target=tcp_mapping_request, args=(local_conn, remote_ip, remote_port)).start()

        logger.debug('Receive mapping request from %s:%d.' % local_addr)

    return


# 主函数
if __name__ == '__main__':
    tcp_mapping(CFG_REMOTE_IP, CFG_REMOTE_PORT, CFG_LOCAL_IP, CFG_LOCAL_PORT)

Ubuntu20.04中使用netplan配置网络

在Ubuntu20.04中已经不使用/etc/networks/interface来配置网络了,而是使用netplan配置网络:

# cat /etc/netplan/00-installer-config.yaml

这是DHCP配置IP:


# This is the network config written by 'subiquity'
network:
ethernets:
ens18:
dhcp4: true
version: 2

这是固定IP:


network:
ethernets:
ens18:
addresses: [172.18.176.239/24]
dhcp4: no
optional: true
gateway4: 172.18.176.254
nameservers:
addresses: [114.114.114.114]
version: 2

开启BBR

1 什么是BBR

TCP BBR是谷歌出品的TCP拥塞控制算法,BBR目的是要尽量跑满带宽,并且尽量不要有排队的情况。BBR可以起到单边加速TCP连接的效果。替代锐速再合适不过,毕竟免费。

Google提交到Linux主线并发表在ACM queue期刊上的TCP-BBR拥塞控制算法。继承了Google“先在生产环境上部署,再开源和发论文”的研究传统。TCP-BBR已经再YouTube服务器和Google跨数据中心的内部广域网(B4)上部署。由此可见出该算法的前途。

TCP-BBR的目标就是最大化利用网络上瓶颈链路的带宽。一条网络链路就像一条水管,要想最大化利用这条水管,最好的办法就是给这跟水管灌满水。

BBR解决了两个问题:

  1. 再有一定丢包率的网络链路上充分利用带宽。非常适合高延迟,高带宽的网络链路。
  2. 降低网络链路上的buffer占用率,从而降低延迟。非常适合慢速接入网络的用户。

项目地址:https://github.com/google/bbr

4.9以上版本的Linux内核已经集成BBR了。

2 开启BBR

2.1 修改SYSCTL.CONF

echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf

2.2 保存生效

sysctl -p

2.3 检测是否已启用BBR模块

lsmod | grep bbr

如果含有bbr即说明内核内已启用bbr模块。