插件

iOS vpn搭建

2018-09-03  本文已影响263人  hare27

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 注意

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
上一篇 下一篇

猜你喜欢

热点阅读