openwrt中添加mt7628an对sim7600ce的支持

按照模块的使用手册,我们知道设备的usb会虚拟出5个串口设备。
在openwrt中默认是支持usb转uart的驱动的,默认是可以支持此设备的,我们只要在menuconfig中选择相应的开关就可以完成支持

Kernel Modules → USB Support
<*> kmod-usb2
<*> kmod-usb-ohci
<*> kmod-usb-serial
                <*> kmod-usb-serial-option
                -*- kmod-usb-serial-wwan
<*> kmod-usb-core

ssh强制密钥登陆

在网路上有大量的扫描程序和爬虫在扫描肉鸡,其中一个最常用的方式就是扫描ssh权限。这种肉鸡主要用来【翻腔】或者DDOS或者挖矿等,总之是可以牟利的。

新注册的vps,还没多久就发现:

There were 2816 failed login attempts since the last successful login.

而避免被暴力的一种简单的方法就是强制密钥登陆。

  1. 创建密钥对
    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    按照流程走完后会在 ~/.ssh目录下看到id_rsa, id_rsa.pub文件 第一个是私有密钥 第二个是公有密钥。
  2. 将公钥的*.pub内容写入:.ssh/authorized_keys
    如果没有目录创建目录。
  3. 修改服务器配置 /etc/ssh/sshd_config
    PasswordAuthentication no 默认yes,改为no。是否允许使用基于密码的认证。
    (建议但非必选)
    GSSAPICleanupCredentials yes 默认no,改为yes。是否在用户退出登录后自动销毁用户凭证缓存
  4. 修改完后重启sshd服务
然后私钥就可以用来在客户端登录时使用,如果密钥没密码,就可以实现无密码登录了

APNs调试工具Knuff介绍

作为一个ios开发者,在你调试APNs时,正常的情况下你需要弄好证书给到后台的同事上线到测试环境,然后才能开始测试调试推送的相关功能。

如果这个过程遇到证书错误,后台代码错误,苹果变更接口等,等到能调试ios的功能,需要等到好久的时间。

那么ios可以独立调试APNs的相关功能吗?当然可以,Knuff就是一款简洁简单的推送调试工具。下面的以MAC的为介绍对象,其他平台的请自行理解。

软件界面如下:

Knuff功能说明

Custom:自定义模式,可以自行选择证书,我们测试自己的应用,就用这个模式

Knuff iOS: Knuff iOS表示使用Knuff的证书,直接推送给Knuff配套的ios应用。

Choose:选择推送的push证书,我们需要选择我们证书,关于如何生成正确的证书,请自行google。

Production:选择证书后,就会出现这个选择,production表示appstore证书,推送给appstore版本的APP。

Sandbox:表示开发调试的非AppStore版本。

Priority:测试时可随意选择。

Payload:表示要推送的报文,具体字段含义这里就不展开了,请自行了解。

Token:device token,在注册苹果 APNs服务时获取的。参考代码如下:

注册服务

[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings
                                                                             settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)
                                                                             categories:nil]];

[[UIApplication sharedApplication] registerForRemoteNotifications];

回调事件
调用didRegisterForRemoteNotificationsWithDeviceToken

至此我们就可以开始测试了,通过这个工具我们就能轻易的测试我们的device token是否正确,测试我们生成的证书是否正确。给自己发送任意想测试的payload。

have fun!

APNs php客户端及静默推送

简介

关于APNs的工作原理,网上有大量的文章,建议学习apple的官方文档。通过APNs我们就可以通过服务器想APP推送想要的内容和事件。从而实现通知或者激活应用的目的。本文我们讲介绍如何搭建php的APNs推送以及如何实现静默推送。

APNs PHP 客户端

经过多方的验证,目前推荐这个:
https://github.com/immobiliare/ApnsPHP

证书验证

想要通过苹果服务器推送信息,必须要有合法的证书。很多人在推送这部分花了很多时间,很多情况是没搞清楚失败到底是代码的问题还是证书的问题。所以,我们需要先验证证书的合法和正确性。

推送时,我们要制作推送证书,我们要选择正确的类型,具体的制作过程可自行搜索,最终的证书类型如下:
push cert

Easy APNS Provider

先前我的文章推荐过Knuff,经过深入的实践,这里我推荐Easy APNS Provider,可以从MAC store下载。相比较Knuff,EAP更加的稳定强大。通过工具我们就可以先行验证证书的正确性,避免后面定位问题浪费更多的时间。
[2020.03.31更新]:另一款值得推荐的是: https://github.com/noodlewerk/NWPusher

在MAC上用客户端向苹果发送推送服务器请求时,我们选择上面安装的证书就可以了。
easy apns provider config

PHP服务器证书生成

上面我们讲了怎么选择正确的客户端证书,当我们使用php 服务器推送时,我们还要做些额外的工作:

  • 打开“钥匙串”程序,(证书助理->从证书颁发机构请求证书),只填邮箱和常用名称,ca不用填,然后保存.certSigningRequest文件到磁盘。
  • 在iOS Dev Center 点击App IDs进入App ID列表。
  • 为 App 开启 Push Notification 功能。(推送证书分为两个版本,一个是Development版,一个是Production版,分别对应开发证书和发布证书。)
  • 上传刚才生成的.certSigningRequest文件,生成aps_development.cer推送证书,双击安装。
  • 打开“钥匙串”程序,(选择登录与我的证书选项后)找到IOS Push Services那条.
  • 右键导出,存储为cert.p12,(输入密码P*d)。然后将该证书的折叠向下展开打开,导出“专用密钥”,存储为key.p12。
  • 接下来打开终端执行下面的命令,生成的ck.pem就是我们在php脚本中要使用的证书。
    #生成cert.pem
    openssl pkcs12 -clcerts -nokeys -out cert.pem -in cert.p12
    #生成key.pem(先输出导出key.p12时设置的密码P*d, 然后设置一个新的密码,如pushpwd,这个密码后面php推送时要设置)
    openssl pkcs12 -nocerts -out key.pem -in key.p12
    #合并成ck.pem
    cat cert.pem key.pem > ck.pem

    APNs PHP客户端配置

这个软件包的最新版本支持HTTP2,如果你的PHP的版本支持HTTP2,建议优先使用。开发包里面自带了几个sample,简单配置后基本就可以使用。这里我们以sample_push.php为测试对象。
sample push demo code

如图所示,配置要点如下:

  • 目标环境:sandbox还是production,对于appstore下载的目标,请选择ENVIRONMENT_PRODUCTION,对于开发中的版本,清选择ENVIRONMENT_SANDBOX
  • 证书:填写证书的相对路径,这里最好时按照上面的步骤验证通过的证书
  • 在上面的步骤中我们生成pem证书时,要求一个最少4位的密码,所以这个地方一定要设置密码,否则connect会失败
  • device token:这里是我们要推送的目标的token。

测试通过的返回如下:

Fri, 13 Jan 2017 13:31:35 +0100 ApnsPHP[15355]: INFO: Trying tls://gateway.sandbox.push.apple.com:2195... Fri, 13 Jan 2017 13:31:35 +0100 ApnsPHP[15355]: INFO: Connected to tls://gateway.sandbox.push.apple.com:2195. Fri, 13 Jan 2017 13:31:35 +0100 ApnsPHP[15355]: INFO: Sending messages queue, run #1: 1 message(s) left in queue. Fri, 13 Jan 2017 13:31:35 +0100 ApnsPHP[15355]: STATUS: Sending message ID 1 [custom identifier: Message-Badge-3] (1/3): 167 bytes. Fri, 13 Jan 2017 13:31:36 +0100 ApnsPHP[15355]: INFO: Disconnected.

device token

在上面我们提到了设备的device token,那么调试阶段如何获取这个token呢?在我们发起了注册请求后,系统会触发相应的消息回调。相关的参考代码如下:

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    //读取APNS token
    NSString* _deviceToken = [[[[deviceToken description]
        stringByReplacingOccurrencesOfString: @"<" withString: @""]
        stringByReplacingOccurrencesOfString: @">" withString: @""]
        stringByReplacingOccurrencesOfString: @" " withString: @""];
    QMLogDebug(@"apns token:%@",_deviceToken);
    NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setObject:_deviceToken forKey:APNSTOKEN];
    [userDefault synchronize];
}

上面的deviceToken就获取了我们需要的token。这里的deviceToken是NSData对象,可以手动导出二进制,然后这个导出文件就可以在测试客户端中使用。

全部的证书

除了ck.pem证书,我们的项目还需要 entrust_root_certification_authority.pem 这个证书,一共两个证书。

静默推送

ios最新的版本中已经支持了静默推送,静默推送时,客户端会收到消息,执行相应的动作,但不会做任何给用户的提示。在普通的推送中我们可以通过如下的代码实现静默推送的效果:

{
  "aps" : {
    "badge" : 0
  }
}

但是苹果还是专门定义了一个开关来表示静默推送 "content-available" : 1,由于此开关优先级较低,在静默推送的报文中不能出现alert,sound 等,否则就会变成普通推送。

静默推送的payload如下:

{
  "aps" : {
    "content-available" : 1
  }
}

静默推送的前提:

  1. 只有app还在后台运行时,才能收到静默推送,否则推送了也没有用
  2. 跟普通推送类似,苹果对频率和到达性都不做保证。

参考:
How to generate a Push Notification certificate and download the Entrust Root Authority certificate

基于certbot的letsencrypt安装使用

起因

  • 前面的文章讲过,运营商环节会在用户加载内容的时候,在流中进行注入frame ,强行展示广告。
  • 如果你的server是给App提供web api的接口,由于苹果ATS的执行,你不得不所有接口都升级成HTTPS。

免费ssl证书

首选当时letsencrypt了,官方网站是:https://letsencrypt.org. 网上大把文章,但按照本人一贯的习惯还是去官网,避免被过时的信息误导,反而浪费更多的时间。
官方推荐的是cetrbot:https://certbot.eff.org/
根据本人的系统,实际的文档路径:https://certbot.eff.org/#centosrhel7-nginx

letsencrypt

安装

首先需要安装certbot:
默认centos7是不支持certot,你需要先安装EPEL:https://fedoraproject.org/wiki/EPEL#How_can_I_use_these_extra_packages.3F
实际的安装包为:https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
下载后安装rpm。然后执行:

sudo yum install certbot

一路‘y’,安装完成:

Installed:
  certbot.noarch 0:0.9.3-1.el7

Dependency Installed:
  dialog.x86_64 0:1.2-4.20130523.el7                 pyOpenSSL.x86_64 0:0.13.1-3.el7                     python-cffi.x86_64 0:1.6.0-5.el7
  python-chardet.noarch 0:2.2.1-1.el7_1              python-enum34.noarch 0:1.0.4-1.el7                  python-idna.noarch 0:2.0-1.el7
  python-ipaddress.noarch 0:1.0.16-2.el7             python-ndg_httpsclient.noarch 0:0.3.2-1.el7         python-parsedatetime.noarch 0:1.5-3.el7
  python-ply.noarch 0:3.4-10.el7                     python-psutil.x86_64 0:2.2.1-1.el7                  python-pycparser.noarch 0:2.14-1.el7
  python-requests.noarch 0:2.6.0-1.el7_1             python-six.noarch 0:1.9.0-2.el7                     python-urllib3.noarch 0:1.10.2-2.el7_1
  python-zope-component.noarch 1:4.1.0-1.el7         python-zope-event.noarch 0:4.0.3-2.el7              python-zope-interface.x86_64 0:4.0.5-4.el7
  python2-acme.noarch 0:0.9.3-1.el7                  python2-certbot.noarch 0:0.9.3-1.el7                python2-configargparse.noarch 0:0.11.0-1.el7
  python2-cryptography.x86_64 0:1.3.1-3.el7          python2-dialog.noarch 0:3.3.0-6.el7                 python2-mock.noarch 0:1.0.1-9.el7
  python2-pyasn1.noarch 0:0.1.9-7.el7                python2-pyrfc3339.noarch 0:1.0-2.el7                pytz.noarch 0:2012d-5.el7

Complete!

开始

在已经有webserver运行的情况下,certbot推荐我们使用“webroot”插件。可以实现在不暂停web服务的情况下更新证书。

webroot:不需要停机你的web服务

certbot certonly --webroot -w AAA -d BBB

BBB是你要支持的域名,AAA是BBB当前已经可以访问的目录。

standalone:需要停机你当前服务器的80端口的服务

certbot certonly --standalone  -d xxx.com

以上每一条命令生成一个对立的目录,如果是多个域名就是合在一起的一个文件。如果要每个域名都生成独立的目录和文件,就要执行多次。

成功后:

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at
   /etc/letsencrypt/live/xxx/fullchain.pem. Your cert
   will expire on 2017-03-18. To obtain a new or tweaked version of
   this certificate in the future, simply run certbot again. To
   non-interactively renew *all* of your certificates, run "certbot
   renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

手动在nginx的conf中配置相关的路径就可以了:

ssl_certificate /etc/letsencrypt/live/xxx/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/xxx/privkey.pem;

续期

certbot renew

撤销

证书没有过期的话,我们可以撤销:

[root@rxblog certbot-0.22.2]# ./certbot revoke --cert-path /etc/letsencrypt/archive/xxx.com/cert1.pem
Saving debug log to /var/log/letsencrypt/letsencrypt.log

-------------------------------------------------------------------------------
Would you like to delete the cert(s) you just revoked?
-------------------------------------------------------------------------------
(Y)es (recommended)/(N)o: Y

-------------------------------------------------------------------------------
Deleted all files relating to certificate iwwenbo.com.
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
Congratulations! You have successfully revoked the certificate that was located
at /etc/letsencrypt/archive/iwwenbo.com/cert1.pem

-------------------------------------------------------------------------------

删除

如果证书已经过期了,我们只能删除了

certbot delete --cert-name example.com

OK,暂时告一段落。

关于苹果前后台定位的使用

其实主要就是要理解这两个函数的功能和适用场景:
startUpdatingLocation
startMonitoringSignificantLocationChanges

可以参看苹果的这个demo:https://developer.apple.com/library/content/samplecode/Regions/Introduction/Intro.html
理解了,自己改动下,就能实现强大的后台定位

2016.12.25:
了解更多的细节,可以参看这里,全部确认到了,基本就搞定了:
https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html

linker command failed with exit code 1 (use -v to see invocation)

最近在重构之前的代码,所以在本地游多个分支,经常出现如下的问题:
linker command failed with exit code 1 (use -v to see invocation)

之前也是基本重新update下pod基本就ok,今天发现之前ok的提交到git的代码,拉到本地又出现这个问题了,所有有必要弄清楚根源,避免每次都各种莫名尝试浪费时间。

从其中的一条错误提示入手:

ld: warning: directory not found for option '-L/Users/xxx/Library/Developer/Xcode/DerivedData/xxx-gttrbsolrpknxhbtjxulraplunas/Build/Products/Debug-iphonesimulator/AFNetworking'

在这个路径下确实发现缺少东西,对于目前成功编译的项目,我们手动删除目标后,再此编译,发现也提示这个错误这个很奇怪,按理应该成功才对。所以初步的原因是这个目标对象对应的pod库没有。那么是什么时候生成的呢,我们进一步分析。

在尝试了pod install和update后,目标并没有生成,所以pod阶段只是创建关联project。应该还是在编译阶段生成的。

按照正常的流程,应该是build时生成相应的target的库,现在却没有生成,进一步在网上搜索,看到“Build Active Architecture Only”
这个选项,当为 YES时,表示只编译当前的architecture版本。所以改为yes后,便可以生成当前 architecture的库了。

所以失败的原因是,这个选项为NO时默认生成所有的target的库,但是是按默认顺序的,还并没有来的及生成当前的目标库,所以link时就失败了。改为YES,相当于当前平台更改为了最高优先级,所以就直接生成了。

NSManageObjectContext – Class is not key value coding-compliant for the key @count

最近把ios的代码升级到了ios10和xcode8 ,出现了大量:

NSManageObjectContext - Class is not key value coding-compliant for the key @count

解决:
参考:http://stackoverflow.com/questions/39511997/nsmanageobjectcontext-class-is-not-key-value-coding-compliant-for-the-key-cou

更新RestKit:
pod 'RestKit', :git => 'https://github.com/RestKit/RestKit.git', :commit => '9cbba9eb1b490c3c5e2873c8fba8e9c0fec1bf07'

更新后会有新的错误,依照提示修复就可以了。

centos开启swap

os:centos6
type:vps
1. 创建用于交换分区的文件
2. 设置交换分区文件
3. 立即启用交换分区文件
4. 设置开机时自启用swap分区,需要修改文件/etc/fstab中的swap行,添加记录。

dd if=/dev/zero of=/data/swap bs=1M count=4096
mkswap /data/swap
swapon /data/swap
/data/swap swap swap defaults 0 0