TCP透明代理

基于Mininet的网络拓扑图:3h.py

#!/usr/bin/python

from mininet.topo import Topo

from mininet.net import Mininet

from mininet.cli import CLI

from mininet.link import TCLink

import time

import datetime

import subprocess

import os,signal

import sys

# 

#    h1------h2------h3             

#

bottleneckbw=6

nonbottlebw=500;  

max_queue_size =bottleneckbw*1000*30/(1500*8) 

net = Mininet( cleanup=True )

h1 = net.addHost('h1',ip='10.0.1.1')

h2 = net.addHost('h2',ip='10.0.1.2')

h3 = net.addHost('h3',ip='10.0.2.2')

c0 = net.addController('c0')

net.addLink(h1,h2,intfName1='h1-eth0',intfName2='h2-eth0',cls=TCLink , bw=nonbottlebw, delay='50ms', max_queue_size=max_queue_size)

net.addLink(h2,h3,intfName1='h2-eth1',intfName2='h3-eth0',cls=TCLink , bw=nonbottlebw, delay='50ms', max_queue_size=max_queue_size)

net.build()

h1.cmd("ifconfig h1-eth0 10.0.1.1/24")

h1.cmd("route add default gw 10.0.1.2 dev h1-eth0")

h1.cmd('sysctl net.ipv4.ip_forward=1')



h2.cmd("ifconfig h2-eth0 10.0.1.2/24")

h2.cmd("ifconfig h2-eth1 10.0.2.1/24")

h2.cmd("ip route add to 10.0.2.0/24 via 10.0.2.2")

h2.cmd("ip route add to 10.0.1.0/24 via 10.0.1.1")

#tproxy
h2.cmd(" iptables -t nat -N MY_TCP")

h2.cmd("iptables -t nat -A PREROUTING -j MY_TCP")

h2.cmd("iptables -t nat -A MY_TCP -p tcp -d 10.0.2.2 -j REDIRECT --to-ports 2223")

h2.cmd("iptables -N MY_TCP")

h2.cmd("iptables -A INPUT -j MY_TCP")

h2.cmd(" iptables -A MY_TCP -p tcp --dport 2223 -j ACCEPT")

h2.cmd('sysctl net.ipv4.ip_forward=1')
h3.cmd("ifconfig h3-eth0 10.0.2.2/24")
h3.cmd("route add default gw 10.0.2.1 dev h3-eth0")
h3.cmd('sysctl net.ipv4.ip_forward=1')


h3.cmd('sysctl net.ipv4.ip_forward=1')



net.start()

time.sleep(1)

CLI(net)

net.stop()

客户端程序:socket_client_tcp.c

#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <linux/netfilter_ipv4.h>
#include <unistd.h>
 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值