【问题标题】:How to programmatically create a PPTP VPN connection on macOS Sierra/High Sierra?如何以编程方式在 macOS Sierra/High Sierra 上创建 PPTP VPN 连接?
【发布时间】:2018-11-03 00:32:11
【问题描述】:

Apple 从其网络配置系统中删除了 macOS Sierra 中的高级 PPTP 支持。但是,PPP 内部仍然存在,包括/usr/sbin/pppd/etc/ppp/

如何以编程方式在 macOS Sierra / High Sierra 上使用剩余的内容启动 PPTP VPN 连接?

【问题讨论】:

    标签: macos perl macos-sierra ppp pptp


    【解决方案1】:

    答案:

    此方法创建一个 发送所有流量并且 覆盖其他 DNS 提供商的 PPTP 连接,这意味着它适用于多个同时具有不同的 VPN 连接DNS搜索域,并按顺序关闭它。

    不发送所有流量需要您事先知道 VPN 子网。如果不这样做,则必须发送所有流量(见下文),因为普通 PPP/LCP 无法告诉客户端其子网(尽管理论上 ip-upip-down 脚本可以从收到的 IP 地址中猜出它)。

    将此 perl 保存为 /usr/local/bin/pptp:

    #!/usr/bin/env perl
    if (@ARGV) {
        my $name = $ARGV[0];
        if (length $name && -e "/etc/ppp/peers/$name") {
            my $pid;
            $SIG{"INT"} = "IGNORE";
            die "fork: $!" unless defined ($pid = fork);
            if ($pid) { # parent
                $SIG{"INT"} = sub {
                    kill HUP => $pid;
                };
                wait;
                exit;
            } else { #child
                $SIG{"INT"} = "DEFAULT";
                exec "pppd", "call", $name;
                exit;
            }
        } else {
            print "Error: PPTP name: $name\n";
        }
    } else {
        opendir my $d, "/etc/ppp/peers" or die "Cannot read /etc/ppp/peers";
        while (readdir $d) {
            print "$_\n" if !($_ eq "." || $_ eq "..");
        }
        closedir $d;
    }
    

    sudo pptp AcmeOffice 运行它,其中AcmeOffice 是PPP 连接名称,然后使用单个Control-C/SIGINT 将其关闭。

    /etc/ppp/peers中,创建PPP连接文件,本例为/etc/ppp/peers/AcmeOffice

    plugin /System/Library/SystemConfiguration/PPPController.bundle/Contents/PlugIns/PPPDialogs.ppp
    plugin PPTP.ppp
    noauth
    # debug 
    redialcount 1
    redialtimer 5
    idle 1800
    #mru 1320
    mtu 1320
    receive-all
    novj 0:0
    ipcp-accept-local
    ipcp-accept-remote
    refuse-pap
    refuse-chap
    #refuse-chap-md5
    refuse-eap
    hide-password
    #noaskpassword
    #mppe-stateless 
    mppe-128 
    mppe-stateful 
    require-mppe 
    passive 
    looplocal 
    nodetach
    # defaultroute
    #replacedefaultroute
    # ms-dns 8.8.8.8
    # usepeerdns
    noipdefault
    # logfile /tmp/ppp.AcmeOffice.log 
    ipparam AcmeOffice
    remoteaddress office.acme.com
    user misteracme
    password acme1234
    

    最后 4 个选项是特定于连接的。请注意,密码以明文形式存储。推荐使用chown root:wheelchmod 600nodetachipcp-accept-localipcp-accept-remotenoipdefault 很关键。

    由于我们不会成为/替换默认路由,因此您必须手动更改路由表。在/etc/ppp/ip-up 脚本中添加AcmeOffice 条目:

    #!/bin/sh
    #params: interface-name tty-device speed local-IP-address remote-IP-address ipparam
    
    PATH=$PATH:/sbin:/usr/sbin
    
    case "$6" in
        AcmeOffice)
            route -n add -net 192.168.1.0/24 -interface "$1"
            ;;
        AcmeLab)
            route -n add -net 192.168.2.0/24 -interface "$1"
            ;;
        AcmeOffshore)
            route -n add -net 192.168.3.0/24 -interface "$1"
            ;;
        VPNBook)
            ;;
        *)
            ;;
    esac
    

    还有你的/etc/ppp/ip-down 脚本:

    #!/bin/sh
    #params: interface-name tty-device speed local-IP-address remote-IP-address ipparam
    
    PATH=$PATH:/sbin:/usr/sbin
    
    case "$6" in
        AcmeOffice)
            route -n delete -net 192.168.1.0/24 -interface "$1"
            ;;
        AcmeLab)
            route -n delete -net 192.168.2.0/24 -interface "$1"
            ;;
        AcmeOffshore)
            route -n delete -net 192.168.3.0/24 -interface "$1"
            ;;
        VPNBook)
            ;;
        *)
            ;;
    esac
    

    如果 VPN 有 DNS 搜索域(即somehost.office.acme.com),则在/etc/resolver/ 中创建一个以 DNS 后缀命名的文件,例如 /etc/resolver/office.acme.com,内容如下:

    nameserver 192.168.1.1
    domain office.acme.com
    

    请注意,这需要事先知道目标域和名称服务器。理论上ip-up & ip-down 可以按需创建和删除这个文件。

    要发送所有流量(如果您不知道目标子网),请取消注释 PPP 连接文件中的 #defaultroute 并将 ip-upip-down 条目留空(例如 VPNBook 示例)。要使用 VPN 覆盖您的 DNS,请取消注释 usepeerdns

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-23
      • 2015-06-28
      • 2018-12-05
      • 2015-03-22
      • 2019-02-03
      • 1970-01-01
      • 2023-03-15
      • 2018-12-08
      相关资源
      最近更新 更多