然而并没有什么东西┑( ̄Д  ̄)┍

0%

[LEDE/OpenWrt]-在路由器上搭建PPTP服务器,实现全校园联网

简介

因为常年奔波于寝室与实验室之间,但是学校上网只有通过NetKeeper拨号。因为NetKeeper会污染电脑,所以一直没装,同时因为寝室用的路由器,经常出现忘了下线的情况,所以目标就是:

  1. 能够通过外网连接到路由器,然后断开NetKeeper连接。//顺带实现内网外入
  2. 能够在校内通过校园网连接寝室路由器实现上网。
    顺便吐槽一下,之前网上很多谈openwrt+pptp的都是用的一个luci-app-pptpd.ipk,这个早就不被官方支持,而且试过并不能正常使用,所以还是推荐自己手工配置。

    Why LEDE?

    LEDE是从Openwrt中fork出来的,一个针对路由器的Linux发行版,可拓展、安全、稳定、高效 → 看官网说的好处都有啥

    Why PPTP?

    简单,方便,Windows、安卓之类的都内置了客户端,不需要单独安装
    服务器压力小(像OpenVPN这种生成一个密钥都够睡个午觉了)
    但是,不安全(又不干坏事,安全性不考虑233)

环境

  • 路由器: WNDR3800 //要能上网,怎么折腾的NetKeeper之后单独讲
  • LEDE: LEDE Reboot 17.01.3 r3533-d0bf257c46

安装

PPTP Server安装与配置

  1. 先用opkg安装pptpd
    1
    opkg install pptpd kmod-mppe
  2. 配置pptpd
  • 编辑/etc/config/pptpd配置PPTP服务器本地IP和客户端IP
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    config service 'pptpd'
    option 'enabled' '1'
    option 'localip' ‘xxx.yyy.www.zzz’

    config 'login'
    option 'username' ‘foo’
    option 'password' ‘bar’
    option 'remoteip' 'xxx.yyy.zzz.1’

    config 'login'
    option 'username' ‘foo’
    option 'password' ‘bar’
    option 'remoteip' 'xxx.yyy.zzz.2’
  • (可选)编辑/etc/ppp/options.pptpd添加DNS服务器
    省略这步有概率出现“PPTP能连上,但是无法访问域名的情况”
    解决办法:
    自己手动配置DNS,或者在/etc/ppp/options.pptpd文件末尾加上以下内容,分别是DNSPOD和Google的DNS服务器
    1
    2
    ms-dns 119.29.29.29
    ms-dns 8.8.8.8
  1. 配置防火墙
  • 实现本地连接和外网连接
    编辑/etc/config/firewall,添加以下内容:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    config rule
    option target 'ACCEPT'
    option _name 'pptp'
    option src 'wan'
    option proto 'tcp'
    option dest_port '1723'

    config rule
    option target 'ACCEPT'
    option _name 'gre'
    option src 'wan'
    option proto '47'

注意,在Openwrt官方Wiki中,官方将/etc/config/firewall错误写成了/etc/config/network

除此之外还可以在/etc/firewall.user中添加以下内容:

1
2
3
4
5
6
# Allow all traffic in and out of the ppp interface. No reason to specify nets.
iptables -A input_rule -i ppp+ -j ACCEPT
iptables -A output_rule -o ppp+ -j ACCEPT
# This rule will allow traffic towards internet
iptables -A forwarding_rule -i ppp+ -j ACCEPT
iptables -A forwarding_rule -o ppp+ -j ACCEPT

但是以上配置只能实现本地连接(直接连路由器)和外网连接(拨号连接),并不能直接从校园网连接(连接提示端口已关闭)

  • 实现校园网连接
    其实就是指明流量来源是wan口:
    在刚刚编辑的/etc/firewall.user基础之上,加上:
    1
    2
    3
    4
    5
    6
    7
    config 'rule'
    option 'target' 'ACCEPT'
    option '_name' 'pptpd'
    option 'proto' 'tcp'
    option 'dest_port' '1723'
    option 'family' 'ipv4'
    option 'src' 'wan'
    同理,在/etc/firewall.user末尾加上:
    1
    2
    3
    4
    iptables -t nat -A prerouting_rule -i $WAN -p tcp --dport 1723 -j ACCEPT
    iptables -A input_rule -i $WAN -p tcp --dport 1723 -j ACCEPT
    iptables -A output_rule -p 47 -j ACCEPT
    iptables -A input_rule -p 47 -j ACCEPT
  1. 启动服务

    1
    2
    /etc/init.d/pptpd enable
    /etc/init.d/pptpd start
  2. 添加静态路由
    路由器是以pppoe拨号的,拨号后获得的路由会覆盖掉原来的,所以要手动设定静态路由才能连校园网,在/etc/config/network中配置静态路由:

    1
    2
    3
    4
    5
    6
    config route
    option netmask '255.255.255.0'
    option gateway '寝室的网关'
    option metric '20'
    option interface 'wan'
    option target '实验室IP所在网段'

效果展示

  1. ping本地路由器
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    ➜  kurileo ping -c 5 192.168.2.1
    PING 192.168.2.1 (192.168.2.1) 56(84) bytes of data.
    64 bytes from 192.168.2.1: icmp_seq=1 ttl=64 time=0.915 ms
    64 bytes from 192.168.2.1: icmp_seq=2 ttl=64 time=0.871 ms
    64 bytes from 192.168.2.1: icmp_seq=3 ttl=64 time=0.836 ms
    64 bytes from 192.168.2.1: icmp_seq=4 ttl=64 time=0.894 ms
    64 bytes from 192.168.2.1: icmp_seq=5 ttl=64 time=1.59 ms

    --- 192.168.2.1 ping statistics ---
    5 packets transmitted, 5 received, 0% packet loss, time 4001ms
    rtt min/avg/max/mdev = 0.836/1.022/1.595/0.288 ms
  2. ping寝室路由器
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    ➜  kurileo ping -c 5 192.168.1.1
    PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
    64 bytes from 192.168.1.1: icmp_seq=1 ttl=63 time=2.46 ms
    64 bytes from 192.168.1.1: icmp_seq=2 ttl=63 time=2.57 ms
    64 bytes from 192.168.1.1: icmp_seq=3 ttl=63 time=6.06 ms
    64 bytes from 192.168.1.1: icmp_seq=4 ttl=63 time=2.29 ms
    64 bytes from 192.168.1.1: icmp_seq=5 ttl=63 time=3.08 ms

    --- 192.168.1.1 ping statistics ---
    5 packets transmitted, 5 received, 0% packet loss, time 4003ms
    rtt min/avg/max/mdev = 2.291/3.296/6.064/1.410 ms
  3. ping百度
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    ➜  kurileo ping -c 5 www.baidu.com
    PING www.a.shifen.com (180.97.33.108) 56(84) bytes of data.
    64 bytes from 180.97.33.108: icmp_seq=1 ttl=54 time=39.5 ms
    64 bytes from 180.97.33.108: icmp_seq=2 ttl=54 time=40.2 ms
    64 bytes from 180.97.33.108: icmp_seq=3 ttl=54 time=38.7 ms
    64 bytes from 180.97.33.108: icmp_seq=4 ttl=54 time=39.8 ms
    64 bytes from 180.97.33.108: icmp_seq=5 ttl=54 time=39.6 ms

    --- www.a.shifen.com ping statistics ---
    5 packets transmitted, 5 received, 0% packet loss, time 4003ms
    rtt min/avg/max/mdev = 38.721/39.628/40.291/0.545 ms
    总的来说,通过这种方式会增加2ms的延迟,可以接受。
    但是带宽方面,会损失接近10%(10M带宽,寝室可以跑满,实验室只有9.x M),也还是可以接受的。

Reference

https://wiki.openwrt.org/doc/howto/vpn.server.pptpd