文章

Libs

网络收藏夹

微信开发 http://mp.weixin.qq.com/wiki/8/f9a0b8382e0b77d87b3bcc1ce6fbc104.html

http://mp.weixin.qq.com/wiki/2/ae9782fb42e47ad79eb7b361c2149d16.html

libuv 中文教程 http://luohaha.github.io/Chinese-uvbook/index.html

libevent网络库 http://libevent.org/

https://www.flash2voip.com 网盘加速 http://suxia.xyz MSDN资源 http://www.itellyou.cn/ 电驴镜像 http://www.gdajie.com/ CEF下载 https://cefbuilds.co NGROK HTTP 反向代理 https://github.com/inconshreveable/ngrok/

SQLITE ios封装 https://github.com/ccgus/fmdb C语言爬虫 https://github.com/xonce/CSpider LAV 解码器 https://github.com/Nevcairiel/LAVFilters/releases mpc_hc https://github.com/mpc-hc/mpc-hc ngrok-c https://github.com/dosgo/ngrok-c xbmc http://github.com/xbmc ffmpeg_svn svn://svn.mplayerhq.hu/ffmpeg/trunk

Visual Studio Professional 2015 简体中文专业版 专业版 激活密钥:HMGNV-WCYXV-X7G9W-YCX63-B98R2 http://download.microsoft.com/download/B/8/9/B898E46E-CBAE-4045-A8E2-2D33DD36F3C4/vs2015.pro_chs.iso

社会工程库 https://qqgroup.insight-labs.org/ http://www.findmima.com http://www.xiumima.com

初始化子模块:$ git submodule init —-只在首次检出仓库时运行一次就行 更新子模块:$ git submodule update —-每次更新或切换分支后都需要运行一下

linphone desktop git clone git://git.linphone.org/linphone.git –recursive git clone git://git.linphone.org/linphone-desktop.git –recursive git clone git://git.linphone.org/linphone-iphone.git –recursive git clone git://git.linphone.org/linphone-android.git –recursive

ffmpeg http://ffmpeg.zeranoe.com/builds x265 http://x265.org x264 http://videolan.org/developers/x264.html opus http://opus-codec.org/ speex http://speex.org/ zlib http://zlib.net/ x265 http://x265.org/ opus http://opus-codec.org/ speex http://speex.org/ zlib http://zlib.net/ Lavfilters https://github.com/Nevcairiel/LAVFilters XMBC git://github.com/xbmc/xbmc.git
MPC_HC https://github.com/mpc-hc/mpc-hc.git VLC http://www.videolan.org/vlc/download-sources.html live555 http://www.live555.com/ gstreamer http://gstreamer.freedesktop.org/modules/

linphone SIP http://www.linphone.org/technical-corner/linphone/downloads git clone git://git.linphone.org/linphone.git –recursive git clone git://git.linphone.org/linphone-iphone.git –recursive git clone git://git.linphone.org/linphone-android.git –recursive git clone https://github.com/strukturag/libde265.git git clone https://github.com/OpenHEVC/openHEVC.git git clone https://github.com/illuspas/libyuv-android.git git clone http://git.chromium.org/external/libyuv.git

for android neon common_CFLAGS += -DLIBYUV_NEON -D__ARM_NEON__ -march=armv7-a -mfpu=neon -mfloat-abi=softfp -mvectorize-with-neon-quad common_LDFLAGS += -Wl,–fix-cortex-a8 LOCAL_ARM_NEON := true

ngrok

ngrok是一个ddns服务,可以像花生壳一样,为内网机器绑定一个公网域名,方便开发调试远程接口(如微信开发)。

很多人抱怨,微信开发的时候无法在本地实现接口调试,必须将项目放置到一个公网服务器上,有一个正式的域名能否访问,还得是80端口,这样微信那边才能和我们的项目进行通信。

但是,一般情况下我们的开发环境是在内网,外网无法访问到我们的电脑,而且就算是adsl拨号,电信运营商也不允许使用80端口对外提供服务。

在这种蛋疼的条件下,机智的程序员们还是有解决办法的:

用POSTMAN模拟接口访问,局限性很大,比如微信OAUTH授权流程走一个看看?

本机安装花生壳客户端,为本机绑定一个动态域名,这样只要访问指定的花生壳域名,由花生壳进行反向代理,外面的请求通过花生壳进行转发到本机127.0.0.1,响应包同样通过花生壳进行转发。

我选择第二种,但是我用不了花生壳,花生壳并没有提供mac os的客户端,所以只能寻找替代方案,答案就是 ngrok。

ngrok是一款开源的,用go语言开发的动态域名服务,含服务端和客户端,当然,是老外开发的。

ngrok官网提供了一个官方服务,我们只需要从官网下载客户端即可,很是方便。

但不幸的是,ngrok官方服务器国内已无法连通,不知道是人家屏蔽了我们,还是我们屏蔽了人家,大家心中有数哈。

这种境况下,我们大天朝码农还是有办法的,既然人家开源了,那我们自己编译服务端放在自己vps上就是,域名很多人都有吧,不然还搞个毛的web开发。

天朝已经有位玩家提供了免费的服务,大家到这里查看:http://www.tunnel.mobi/ 。我也用了一段时间,但是昨天突然抽风,给我的开发进度造成了一点损失,所以我决定自己编译一个服务端,接下来进入正题,介绍下我的编译流程。

编译分两块:

服务端

配套客户端

客户端是需要和服务端匹配的,在编译的时候是要指定域名证书的,所以不用担心别人用其他的客户端去连你的服务端。

编译准备

一个vps,阿里云或者其他,linux系统即可,这里我推荐debian或ubuntu.

一个域名,将域名泛解析到你的服务器上。

为服务器安装git和go

我就直接在我的博客服务器上编译了,我准备使用

ngrok.ekan001.com

作为动态域名的根,记得泛解析。

首先我们需要在服务器上安装go环境,我建议下载最新版的go包,直接解压到服务器

/usr/local/go

即可,然后将

/usr/local/go/bin/*

复制到

/usr/bin/

go的下载地址:http://www.golangtc.com/download 下载和你服务器系统对应的版本即可,这里我是linux 64位的,就选择下载

go1.4.2.linux-amd64.tar.gz

即可。

接着,下载ngrok源码:

cd /usr/local/src/

git clone https://github.com/inconshreveable/ngrok.git

export GOPATH=/usr/local/src/ngrok/

export NGROK_DOMAIN=”ngrok.ekan001.com”

cd ngrok

开始编译

首先为根域名生成证书

openssl genrsa -out rootCA.key 2048

openssl req -x509 -new -nodes -key rootCA.key -subj “/CN=$NGROK_DOMAIN” -days 5000 -out rootCA.pem

openssl genrsa -out device.key 2048

openssl req -new -key device.key -subj “/CN=$NGROK_DOMAIN” -out device.csr

openssl x509 -req -in device.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out device.crt -days 5000

cp rootCA.pem assets/client/tls/ngrokroot.crt

cp device.crt assets/server/tls/snakeoil.crt

cp device.key assets/server/tls/snakeoil.key

编译服务端

服务端比较容易,先指定一下我的环境变量,然后再make:

1
2
3
GOOS=linux GOARCH=amd64

#如果是32位系统,这里 GOARCH=386

然后make

1
make release-server

按理讲,应该就可以编译成功了,但是我发现,编译的时候很多依赖包是需要自动下载的,有一个包

log4go

是在 code.google 上的,我们天朝服务器是无法访问google的,大家懂,所以我这里是这么处理的:修改源码,将该包改为 github 上的地址:

找到

/usr/local/src/ngrok/src/ngrok/log/logger.go

,第五行import中的 log 包,改为:

log “github.com/keepeye/log4go”

然后重新

make release-server

接下来可能还会遇到一个问题,编译时在

go get gopkg.in/yaml.v1

步骤时候卡住,这是因为git版本太低,请将服务器git版本升级到1.7.9.5以上。

编译成功后,应该可以在

/usr/local/src/ngrok/bin

下生成了一个

ngrokd

可执行文件,这就是我们的服务端了,现在启动服务端试试:

bin/ngrokd -domain=”$NGROK_DOMAIN” -httpAddr=”:8000”

如果没有报错的话,会出现以下输出:

[10:05:48 CST 2015/04/17] [INFO] (ngrok/log.(*PrefixLogger).Info:83) [registry] [tun] No affinity cache specified

[10:05:48 CST 2015/04/17] [INFO] (ngrok/log.(*PrefixLogger).Info:83) [metrics] Reporting every 30 seconds

[10:05:48 CST 2015/04/17] [INFO] (ngrok/log.Info:112) Listening for public http connections on [::]:8000

[10:05:48 CST 2015/04/17] [INFO] (ngrok/log.Info:112) Listening for public https connections on [::]:443

[10:05:48 CST 2015/04/17] [INFO] (ngrok/log.Info:112) Listening for control and proxy connections on [::]:4443

现在先

ctrl+c

退出,我们还需要编译客户端呢。

编译客户端

我的本机系统是 mac osx ,64位,对应的 GOOS 和 GOARCH 是这样的:

GOOS=darwin GOARCH=amd64

然后我直接编译:

make release-client

编译是成功了,但是我发现编译出来的东西是

bin/ngrok

,它还是linux平台的包,我mac上无法执行。原因是我们没有为go生成交叉编译环境,需要补充下面的一个步骤:

前文说到,我们的 go 安装在

/usr/local/go

,现在我们执行下面的命令:

cd /usr/local/go/src

GOOS=darwin GOARCH=amd64 ./make.bash

然后回去ngrok目录,接着编译:

cd -

GOOS=darwin GOARCH=amd64 make release-client

完了以后可以在

/usr/local/src/ngrok/bin/darwin_amd64/

下发现

ngrok

文件,将它拷贝到我的mac上。

如果是windows下的客户端,是这样的:

cd /usr/local/go/src

GOOS=windows GOARCH=amd64 ./make.bash

cd -

GOOS=windows GOARCH=amd64 make release-client

#同理,这里的amd64是64位系统,32位改成386

#应该会在 bin/windows_amd64 目录下生成ngrok客户端程序

到这里,编译就完成了,现在让我们把应用跑起来:

按之前的方式启动服务端ngrokd,有以下提示:

Listening for public http connections on [::]:8000

Listening for control and proxy connections on [::]:4443

记住这两个端口 8000 和 4443 。

现在我们来到自己电脑上,准备启动客户端。在启动之前,我们需要为客户端编写一个配置文件 ngrok.cfg :

server_addr: “ngrok.ekan001.com:4443”

trust_host_root_certs: false

注意,配置文件中用的是4443端口。

然后启动客户端,假设我们要分配一个域名

test.ngrok.ekan001.com

执行以下命令:

./ngrok -config=./ngrok.cfg -subdomain=test 80

解释一下参数:

-config 就是上面配置文件ngrok.cfg的路径

-subdomain 就是需要分配域名的前缀部分

80 就是本机websever的端口,比如apache监听的端口,一会ngrok会将请求映射到该端口上。

如果没错误,应该会出现以下的输出:

Tunnel Status online

Version 1.7/1.7

Forwarding http://test.ngrok.ekan001.com:8000 -> 127.0.0.1:80

Forwarding https://test.ngrok.ekan001.com:8000 -> 127.0.0.1:80

Web Interface 127.0.0.1:4040

Conn 0

Avg Conn Time 0.00ms

现在,访问

test.ngrok.ekan001.com:8000

,ngrok服务端就会讲请求转发到我们本地客户端,再由客户端转发给我们的webserver 80端口。所以,我们只需要配置apache或nginx,添加虚拟主机,绑定域名

test.ngrok.ekan001.com

即可。

事情还没有结束!

我们前面提到,微信开发中的api地址,是不认80以外的端口的,所以如果我们将

test.ngrok.ekan001.com:8000

作为接口域名,得到的只能是 非法地址 或 配置失败 这个提示。

为什么在启动服务端的时候,端口不指定为80呢?就像下面:

bin/ngrokd -domain=”$NGROK_DOMAIN” -httpAddr=”:80”

很遗憾,因为这台vps不是只用来做ngrok服务的,我博客还在上面呢,80端口已经被nginx占用了。

那怎么办?

不得不提nginx是个牛逼的软件,我们可以在nginx中配置一个server,就绑定

test.ngrok.ekan001.com

域名,然后将所有请求转发到后端

:8000

端口上,这就是反向代理。我发一下自己的nginx配置:

#ngrok.ekan001.com.conf

upstream nodejs {

1
2
3
server 127.0.0.1:8000;

keepalive 64;

}

server {

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
listen 80;

server_name *.ngrok.ekan001.com;

access_log /home/wwwlogs/ngrok.ekan001.log;

location / {

    proxy_set_header X-Real-IP $remote_addr;

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_set_header Host  $http_host:8000;

    proxy_set_header X-Nginx-Proxy true;

    proxy_set_header Connection "";

    proxy_pass      http://nodejs;



}

}

总结:

生命在于折腾,enjoy it!

参考: [1] http://www.haiyun.me/archives/1012.html [2] http://blog.csdn.net/savechina/article/details/5612399 [3] http://www.golangtc.com/download

本文由作者按照 CC BY 4.0 进行授权