iOS vpn搭建

作者: hare27 | 来源:发表于2018-09-03 21:43 被阅读263次

1. 这个是干啥的?

大家在appstore上会搜到很多的vpn App,例如下图所示。这些其实都是用apple自带的NetworkExtension搭建的,先看下apple官方介绍

Overview
The Network Extension framework contains APIs that can be used to customize and extend the core networking features of iOS and macOS.

翻译过来就是

Network Extension框架包含可用于定制和扩展iOS和macOS核心网络功能的API。

而我们主要使用的事其中的NEVPNManager

The `NEVPNManager`API gives apps the ability to create and manage a Personal VPN configuration on iOS and macOS. Personal VPN configurations are typically used to provide a service to users that protects their Internet browsing activity on insecure networks such as public Wi-Fi networks.

翻译过来也就是说,可以使用NEVPNManager来创建一个个人的vpn

1.1 注意

  • 经过试验,只支持IPSecIKEv2两个方式
  • 本文只讲解IPSec方式,IKEv2也差不多,网上一堆都是IKEv2方式的,自己百度去

2. 步骤

2.1 创建一个项目,并登陆你个人的开发者账号,因为我比较懒,所以用的xcode自己的证书管理

1535980871170.jpg

2.2 打开vpn开关

1535980811368.jpg

2.3 导入头文件

#import <NetworkExtension/NetworkExtension.h>

2.4 加载vpn管理对象

    __weak typeof(self) _weakSelf = self;
    _vpnM = [NEVPNManager sharedManager];
    [_vpnM loadFromPreferencesWithCompletionHandler:^(NSError * _Nullable error) {
        if (error) {
            NSLog(@"vpnManager 加载失败 : %@",error.userInfo);
        }else{
            NSLog(@"%@",@"vpnManage 加载成功");
            // 配置相关参数
            [_weakSelf setVpnConfig];
        }
    }];

2.5 配置相关参数

-(void)setVpnConfig{
    
    // 创建配置对象
    NEVPNProtocolIPSec *sec = [[NEVPNProtocolIPSec alloc] init];
    // 用户名
    sec.username = @"As1";
    // 服务器地址
    sec.serverAddress = @"222.184.112.38";
    
    // 密码,必须从keychain导出
    [self createKeychainValue:@"88" forIdentifier:@"VPN_PASSWORD"];
    sec.passwordReference = [self searchKeychainCopyMatching:@"VPN_PASSWORD"];
    
    // 秘钥,必须从keychain导出
    [self createKeychainValue:@"888888" forIdentifier:@"VPN_shared_PASSWORD"];
    sec.sharedSecretReference = [self searchKeychainCopyMatching:@"VPN_shared_PASSWORD"];

    // 验证方式:共享秘钥
    sec.authenticationMethod = NEVPNIKEAuthenticationMethodSharedSecret;
    // 不验证
//    sec.authenticationMethod = NEVPNIKEAuthenticationMethodNone;
    // 显示的名字
    sec.localIdentifier = @"小兔子vpn";
    // 不知道干啥的,想知道的自己百度
    sec.remoteIdentifier = @"小兔子vpn";
    sec.useExtendedAuthentication = YES;
    sec.disconnectOnSleep = false;
    
    self.vpnM.onDemandEnabled = NO;
    [self.vpnM setProtocolConfiguration:sec];
    self.vpnM.localizedDescription = @"小兔子vpn";
    self.vpnM.enabled = true;
    [self.vpnM saveToPreferencesWithCompletionHandler:^(NSError * _Nullable error) {
        if (error) {
            NSLog(@"vpn 配置失败 : %@",error.userInfo);
        }else{
            NSLog(@"%@",@"vpn 配置成功");
            // 监听vpn状态变化
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onVpnStateChange:) name:NEVPNStatusDidChangeNotification object:nil];
        }
    }];
}

2.6 启动

    [self.vpnM.connection startVPNTunnelAndReturnError:&error];
    if(error) {
        NSLog(@"Start error: %@", error.localizedDescription);
    }else{
        NSLog(@"Connection established!");
    }

3. 完整代码

https://github.com/hare27/vpnDemo

相关文章

  • iOS vpn搭建

    1. 这个是干啥的? 大家在appstore上会搜到很多的vpn App,例如下图所示。这些其实都是用apple自...

  • 使用bandwagon(搬瓦工)搭建svn服务器

    前言 半年前购买过 bandwagon 的服务,主要用于 vpn 的使用,vpn 搭建教程看这里。最近有想自己搭建...

  • 在centos搭建vpn iOS可用

    整体流程在这里:完美搭建 shadowsocks 和 VPN 服务器,亲测有效!。本文只是总结踩的一些坑。 购买v...

  • 通过设置Linux启动任务和定时任务优化搬瓦工

    转载注明出处:简书-十个雨点 之前我自己通过搬瓦工搭建了个人服务器和VPN,参见用搬瓦工搭建个人博客和VPN,有朋...

  • iOS开发使用NetworkExtension框架编写VPN应用

    最近闲来无聊,想自己做个ios平台代理上网的应用,最直接的方式就是通过VPN了,刚好iOS对VPN有很好的支持。于...

  • 如何搭建自己的vpn服务器

    要搭建自己的VPN服务器,您可以按照以下步骤进行操作: 选择您要使用的VPN协议 VPN服务器可以使用多种协议,例...

  • vpn搭建(linux)

    由于内网的网络限制(如不能访问视频网站),就想能不能通过租用服务器来实现对内网限制的突破。想起现在如火如荼的云计算...

  • iOS VPN

    苹果是从iOS 8开始,才开放了新的框架 NetworkExtension ,该框架提供了配置和控制VPN支持和w...

  • VPN P2TP一键安装脚本

    IPsec VPN 服务器一键安装脚本 使用 Linux 脚本一键快速搭建自己的 IPsec VPN 服务器。支持...

  • 「三天计划」第一天

    1.27@ 1.vpn搭建 2.搭建hexo个人博客 3.github搭建 4.上传个人博客到github 5.计...

网友评论

  • f16c444f3ea3:提示:与 VPN 服务器协议失败。是怎么回事:pray:

本文标题:iOS vpn搭建

本文链接:https://www.haomeiwen.com/subject/ujoiwftx.html