Node.js C++ Addons

Install node-gyp

  1. npm install -g node-gyp

Prepare SourceCode

  1. [root@localhost test_nodejs]# ls
  2. binding.gyp  hello.cc  hello.js

hello.cc

  1. #include <node.h>
  2.  
  3. namespace demo {
  4.  
  5. using v8::FunctionCallbackInfo;
  6. using v8::Isolate;
  7. using v8::Local;
  8. using v8::Object;
  9. using v8::String;
  10. using v8::Value;
  11.  
  12. void Method(const FunctionCallbackInfo<Value>& args) {
  13.   Isolate* isolate = args.GetIsolate();
  14.   args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world"));
  15. }
  16.  
  17. void init(Local<Object> exports) {
  18.   NODE_SET_METHOD(exports, "hello", Method);
  19. }
  20.  
  21. NODE_MODULE(NODE_GYP_MODULE_NAME, init)
  22.  
  23. }  // namespace demo

hello.js

  1. // hello.js
  2. const addon = require('./build/Release/addon');
  3.  
  4. console.log(addon.hello());
  5. // Prints: 'world'

binding.gyp

  1. {
  2.   "targets": [
  3.     {
  4.       "target_name": "addon",
  5.       "sources": [ "hello.cc" ]
  6.     }
  7.   ]
  8. }

Compile & Test

  1. [root@localhost test_nodejs]# node-gyp configure
  2. gyp info it worked if it ends with ok
  3. gyp info using node-gyp@3.6.2
  4. gyp info using node@8.11.1 | linux | x64
  5. gyp info spawn /usr/bin/python2
  6. gyp info spawn args [ '/usr/local/node-v8.11.1-linux-x64/lib/node_modules/node-gyp/gyp/gyp_main.py',
  7. gyp info spawn args   'binding.gyp',
  8. gyp info spawn args   '-f',
  9. gyp info spawn args   'make',
  10. gyp info spawn args   '-I',
  11. gyp info spawn args   '/mnt/hgfs/linux_tmp/test_nodejs/build/config.gypi',
  12. gyp info spawn args   '-I',
  13. gyp info spawn args   '/usr/local/node-v8.11.1-linux-x64/lib/node_modules/node-gyp/addon.gypi',
  14. gyp info spawn args   '-I',
  15. gyp info spawn args   '/root/.node-gyp/8.11.1/include/node/common.gypi',
  16. gyp info spawn args   '-Dlibrary=shared_library',
  17. gyp info spawn args   '-Dvisibility=default',
  18. gyp info spawn args   '-Dnode_root_dir=/root/.node-gyp/8.11.1',
  19. gyp info spawn args   '-Dnode_gyp_dir=/usr/local/node-v8.11.1-linux-x64/lib/node_modules/node-gyp',
  20. gyp info spawn args   '-Dnode_lib_file=/root/.node-gyp/8.11.1/<(target_arch)/node.lib',
  21. gyp info spawn args   '-Dmodule_root_dir=/mnt/hgfs/linux_tmp/test_nodejs',
  22. gyp info spawn args   '-Dnode_engine=v8',
  23. gyp info spawn args   '--depth=.',
  24. gyp info spawn args   '--no-parallel',
  25. gyp info spawn args   '--generator-output',
  26. gyp info spawn args   'build',
  27. gyp info spawn args   '-Goutput_dir=.' ]
  28. gyp info ok 
  29. [root@localhost test_nodejs]# 
  30. [root@localhost test_nodejs]# ls
  31. binding.gyp  build  hello.cc  hello.js
  32. [root@localhost test_nodejs]# ls build/
  33. addon.target.mk  binding.Makefile  config.gypi  Makefile
  34.  
  35. [root@localhost test_nodejs]# node-gyp build
  36. gyp info it worked if it ends with ok
  37. gyp info using node-gyp@3.6.2
  38. gyp info using node@8.11.1 | linux | x64
  39. gyp info spawn make
  40. gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
  41. make: Entering directory `/mnt/hgfs/linux_tmp/test_nodejs/build'
  42.   CXX(target) Release/obj.target/addon/hello.o
  43.   SOLINK_MODULE(target) Release/obj.target/addon.node
  44.   COPY Release/addon.node
  45. make: Leaving directory `/mnt/hgfs/linux_tmp/test_nodejs/build'
  46. gyp info ok 
  47.  
  48. [root@localhost test_nodejs]# ls build/
  49. addon.target.mk  binding.Makefile  config.gypi  Makefile  Release
  50. [root@localhost test_nodejs]# ls build/Release/
  51. addon.node  obj.target
  52.  
  53. [root@localhost test_nodejs]# node hello.js
  54. world

How to Use KVM to Create Virtual Machine

Configure Firewall

  1. systemctl stop firewalld.service
  2. systemctl disable firewalld.service
  3. firewall-cmd --state
  4.  
  5. sed -i '/^SELINUX=.*/c SELINUX=disabled' /etc/selinux/config
  6. sed -i 's/^SELINUXTYPE=.*/SELINUXTYPE=disabled/g' /etc/selinux/config
  7. grep --color=auto '^SELINUX' /etc/selinux/config
  8. setenforce 0

Install KVM & Virt

  1. yum install qemu-kvm libvirt -y
  2. yum install virt-install -y

Start Virt Service

  1. systemctl start libvirtd && systemctl enable libvirtd
  2.  
  3. [root@localhost ~]# ifconfig
  4. ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
  5.         inet 192.168.220.202  netmask 255.255.255.0  broadcast 192.168.220.255
  6.         inet6 fe80::c269:7c04:a06b:dce7  prefixlen 64  scopeid 0x20<link>
  7.         ether 00:0c:29:4e:32:2a  txqueuelen 1000  (Ethernet)
  8.         RX packets 3394211  bytes 4731781088 (4.4 GiB)
  9.         RX errors 0  dropped 0  overruns 0  frame 0
  10.         TX packets 264816  bytes 35363147 (33.7 MiB)
  11.         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  12.  
  13. lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
  14.         inet 127.0.0.1  netmask 255.0.0.0
  15.         inet6 ::1  prefixlen 128  scopeid 0x10<host>
  16.         loop  txqueuelen 1  (Local Loopback)
  17.         RX packets 68  bytes 5920 (5.7 KiB)
  18.         RX errors 0  dropped 0  overruns 0  frame 0
  19.         TX packets 68  bytes 5920 (5.7 KiB)
  20.         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  21.  
  22. virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
  23.         inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
  24.         ether 52:54:00:a5:ea:48  txqueuelen 1000  (Ethernet)
  25.         RX packets 0  bytes 0 (0.0 B)
  26.         RX errors 0  dropped 0  overruns 0  frame 0
  27.         TX packets 0  bytes 0 (0.0 B)
  28.         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Create Disk

  1. [root@localhost ~]# qemu-img create -f raw /opt/CentOS-7-x86_64.raw 10G
  2. Formatting '/opt/CentOS-7-x86_64.raw', fmt=raw size=10737418240

Upload System ISO

  1. [root@localhost ~]# ls /ISO
  2. CentOS-7-x86_64-DVD-1708.iso

Begin Install OS

  1. [root@localhost ~]# virt-install --virt-type kvm --name CentOS-7-x86_64 --ram 1024 --cdrom=/ISO/CentOS-7-x86_64-DVD-1708.iso --disk path=/opt/CentOS-7-x86_64.raw --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole
  2.  
  3. Starting install...
  4. Domain installation still in progress. You can reconnect to 
  5. the console to complete the installation process.

Connect With VNC






List Virtual Machine

  1. [root@localhost ~]# virsh list --all
  2.  Id    Name                           State
  3. ----------------------------------------------------
  4.  -     CentOS-7-x86_64                shut off

Start Virtual Machine

  1. [root@localhost ~]# virsh start CentOS-7-x86_64
  2. Domain CentOS-7-x86_64 started
  3.  
  4. [root@localhost ~]# virsh list --all
  5.  Id    Name                           State
  6. ----------------------------------------------------
  7.  3     CentOS-7-x86_64                running

Test Virtual Machine


List Virbr

  1. [root@localhost ~]# brctl show
  2. bridge name     bridge id               STP enabled     interfaces
  3. virbr0          8000.525400a5ea48       yes             virbr0-nic
  4.                                                         vnet0

List br0 Status

  1. [root@localhost ~]# vi create-br0.sh
  2.  
  3. [root@localhost ~]# cat create-br0.sh
  4. brctl addbr br0
  5. brctl addif br0 ens33
  6. ip addr del dev ens33 192.168.220.202/24
  7. ifconfig br0 192.168.220.202/24 up
  8. route add default gw 192.168.220.2
  9.  
  10. [root@localhost ~]# chmod +x create-br0.sh 
  11. [root@localhost ~]# ./create-br0.sh

Create br0

  1. [root@localhost network-scripts]# ifconfig
  2. br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
  3.         inet 192.168.220.202  netmask 255.255.255.0  broadcast 192.168.220.255
  4.         inet6 fe80::20c:29ff:fe4e:322a  prefixlen 64  scopeid 0x20<link>
  5.         ether 00:0c:29:4e:32:2a  txqueuelen 1000  (Ethernet)
  6.         RX packets 144  bytes 12890 (12.5 KiB)
  7.         RX errors 0  dropped 0  overruns 0  frame 0
  8.         TX packets 96  bytes 17020 (16.6 KiB)
  9.         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  10.  
  11. ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
  12.         inet6 fe80::c269:7c04:a06b:dce7  prefixlen 64  scopeid 0x20<link>
  13.         ether 00:0c:29:4e:32:2a  txqueuelen 1000  (Ethernet)
  14.         RX packets 3432987  bytes 4745607852 (4.4 GiB)
  15.         RX errors 0  dropped 0  overruns 0  frame 0
  16.         TX packets 297978  bytes 44267836 (42.2 MiB)
  17.         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  18.  
  19. lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
  20.         inet 127.0.0.1  netmask 255.0.0.0
  21.         inet6 ::1  prefixlen 128  scopeid 0x10<host>
  22.         loop  txqueuelen 1  (Local Loopback)
  23.         RX packets 68  bytes 5920 (5.7 KiB)
  24.         RX errors 0  dropped 0  overruns 0  frame 0
  25.         TX packets 68  bytes 5920 (5.7 KiB)
  26.         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  27.  
  28. virbr0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
  29.         inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
  30.         ether 52:54:00:a5:ea:48  txqueuelen 1000  (Ethernet)
  31.         RX packets 3970  bytes 167470 (163.5 KiB)
  32.         RX errors 0  dropped 0  overruns 0  frame 0
  33.         TX packets 4261  bytes 11695250 (11.1 MiB)
  34.         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  35.  
  36. vnet0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
  37.         inet6 fe80::fc54:ff:feaf:a499  prefixlen 64  scopeid 0x20<link>
  38.         ether fe:54:00:af:a4:99  txqueuelen 1000  (Ethernet)
  39.         RX packets 3970  bytes 223050 (217.8 KiB)
  40.         RX errors 0  dropped 0  overruns 0  frame 0
  41.         TX packets 5218  bytes 11745246 (11.2 MiB)
  42.         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Vir Edit to Bridge Network

  1. [root@localhost ~]# virsh edit CentOS-7-x86_64
  2. Domain CentOS-7-x86_64 XML configuration edited.
Before Edit

After Edit

Restart Virtual Machine

  1. [root@localhost ~]# virsh shutdown CentOS-7-x86_64 
  2. Domain CentOS-7-x86_64 is being shutdown
  3.  
  4. [root@localhost ~]# virsh start CentOS-7-x86_64
  5. Domain CentOS-7-x86_64 started

Look at the Virtual Machine IP


How to Secure Tunnels to Localhost

Install Golang 1.4

  1. tar -C /usr/local -xzf go1.4.3.linux-amd64.tar.gz 
  2. mkdir $HOME/go
  3. echo 'export GOROOT=/usr/local/go'>> ~/.bashrc
  4. echo 'export GOPATH=$HOME/go'>> ~/.bashrc 
  5. echo 'export PATH=$PATH:$GOROOT/bin'>> ~/.bashrc 
  6. source /root/.bashrc

Install Git

  1. yum install mercurial git bzr subversion -y

Install Ngrok

  1. cd /usr/local/src/
  2. git clone https://github.com/inconshreveable/ngrok.git

Generate CERT

  1. export NGROK_DOMAIN="qinuu.com"
  2.  
  3. cd ngrok/
  4. openssl genrsa -out rootCA.key 2048
  5. openssl req -x509 -new -nodes -key rootCA.key -subj "/CN=$NGROK_DOMAIN" -days 5000 -out rootCA.pem
  6. openssl genrsa -out device.key 2048
  7. openssl req -new -key device.key -subj "/CN=$NGROK_DOMAIN" -out device.csr
  8. openssl x509 -req -in device.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out device.crt -days 5000
  9.  
  10. cp rootCA.pem assets/client/tls/ngrokroot.crt
  11. cp device.crt assets/server/tls/snakeoil.crt
  12. cp device.key assets/server/tls/snakeoil.key

Make Linux Server

  1. GOOS=linux GOARCH=amd64
  2. make release-server release-client

Make Windows Client

  1. cd /usr/local/go/src
  2. GOOS=windows GOARCH=amd64 CGO_ENABLED=0 ./make.bash
  3. cd  /usr/local/src/ngrok/
  4. GOOS=windows GOARCH=amd64 make release-server release-client

Start Ngrok Service

  1. nohup bin/ngrokd -domain="qinuu.com" -httpAddr=":8080" -httpsAddr=":8443" &

Download Windows Client

  1. ls -al bin/windows_amd64/

Configure CLient CFG File

Start Windows Client

How to Configure Static IP in VMware NAT Mode

Just Do It !

  1. [root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
  2. TYPE=Ethernet
  3. PROXY_METHOD=none
  4. BROWSER_ONLY=no
  5. BOOTPROTO=static
  6. DEFROUTE=yes
  7. IPV4_FAILURE_FATAL=no
  8. IPV6INIT=yes
  9. IPV6_AUTOCONF=yes
  10. IPV6_DEFROUTE=yes
  11. IPV6_FAILURE_FATAL=no
  12. IPV6_ADDR_GEN_MODE=stable-privacy
  13. NAME=ens33
  14. UUID=4e333024-b8c8-45e4-baee-e46ece81432c
  15. DEVICE=ens33
  16. ONBOOT=yes
  17. IPADDR=192.168.220.200
  18. NETMASK=255.255.255.0
  19. GATEWAY=192.168.220.2
  20. DNS1=8.8.8.8

Create Local NTP

Controller node

Install Chrony

  1. [root@localhost ~]# yum install chrony -y

Edit the /etc/chrony.conf file

  1. [root@localntp ~]# diff /etc/chrony.conf /etc/chrony.conf_bak
  2. 3,7c3,6
  3. < #server 0.centos.pool.ntp.org iburst
  4. < #server 1.centos.pool.ntp.org iburst
  5. < #server 2.centos.pool.ntp.org iburst
  6. < #server 3.centos.pool.ntp.org iburst
  7. < server 192.168.220.201 iburst
  8. ---
  9. > server 0.centos.pool.ntp.org iburst
  10. > server 1.centos.pool.ntp.org iburst
  11. > server 2.centos.pool.ntp.org iburst
  12. > server 3.centos.pool.ntp.org iburst
  13. 28d26
  14. < allow 192.168.220.0/24

Start the NTP service

  1. [root@localhost ~]# systemctl enable chronyd.service
  2. [root@localhost ~]# systemctl start chronyd.service
  3. [root@localhost ~]# systemctl restart chronyd.service

Other nodes

Install Chrony

  1. [root@localhost ~]# yum install chrony -y

Edit the /etc/chrony.conf file

  1. [root@localhost ~]# diff /etc/chrony.conf /etc/chrony.conf_bak
  2. 3,7c3,6
  3. < #server 0.centos.pool.ntp.org iburst
  4. < #server 1.centos.pool.ntp.org iburst
  5. < #server 2.centos.pool.ntp.org iburst
  6. < #server 3.centos.pool.ntp.org iburst
  7. < server 192.168.220.201 iburst
  8. ---
  9. > server 0.centos.pool.ntp.org iburst
  10. > server 1.centos.pool.ntp.org iburst
  11. > server 2.centos.pool.ntp.org iburst
  12. > server 3.centos.pool.ntp.org iburst

Start the NTP service

  1. [root@localhost ~]# systemctl enable chronyd.service
  2. [root@localhost ~]# systemctl start chronyd.service
  3. [root@localhost ~]# systemctl restart chronyd.service

Verify operation

controller node

  1. [root@localhost ~]# chronyc sources
  2. 210 Number of sources = 1
  3. MS Name/IP address         Stratum Poll Reach LastRx Last sample               
  4. ===============================================================================
  5. ^? localhost.localdomain         0   8     0     -     +0ns[   +0ns] +/-    0ns

other nodes

  1. [root@localhost ~]# chronyc sources
  2. 210 Number of sources = 1
  3. MS Name/IP address         Stratum Poll Reach LastRx Last sample               
  4. ===============================================================================
  5. ^? 192.168.220.201               0   8     0     -     +0ns[   +0ns] +/-    0ns

Create Local Repository

Configure Firewall

  1. [root@localhost ~]# systemctl stop firewalld.service
  2. [root@localhost ~]# systemctl disable firewalld.service

Install Httpd & Start Service

  1. [root@localhost ~]# yum install httpd -y
  2. [root@localhost ~]# systemctl enable httpd.service
  3. [root@localhost ~]# systemctl restart httpd.service
  4. [root@localhost ~]# systemctl status httpd.service

Now, Take the Browser

http://192.168.220.200

Install Reposync

  1. [root@localhost ~]# yum install yum-utils -y

Create Repo Dir

  1. [root@localhost ~]# mkdir /var/www/html/myrepo
  2. [root@localhost ~]# cd /var/www/html/myrepo

List Repo

  1. [root@localhost myrepo]# yum repolist
  2. Loaded plugins: fastestmirror
  3. Loading mirror speeds from cached hostfile
  4.  * base: mirrors.163.com
  5.  * extras: mirrors.163.com
  6.  * updates: mirrors.aliyun.com
  7. repo id                                                 repo name                                                  status
  8. base/7/x86_64                                           CentOS-7 - Base                                            9,591
  9. extras/7/x86_64                                         CentOS-7 - Extras                                            284
  10. updates/7/x86_64                                        CentOS-7 - Updates                                         1,490
  11. repolist: 11,365

Sync Repo

  1. [root@localhost myrepo]# reposync --repoid=base
  2. [root@localhost myrepo]# reposync --repoid=updates
  3. [root@localhost myrepo]# reposync --repoid=extras
  4. [root@localhost myrepo]# pwd
  5. /var/www/html/myrepo
  6. [root@localhost myrepo]# ls -al
  7. total 0
  8. drwxr-xr-x. 5 root root 47 Dec  6 20:18 .
  9. drwxr-xr-x. 3 root root 20 Dec  6 03:12 ..
  10. drwxr-xr-x. 3 root root 22 Dec  6 03:48 base
  11. drwxr-xr-x. 3 root root 22 Dec  6 20:18 extras
  12. drwxr-xr-x. 3 root root 22 Dec  6 20:06 updates

Install Createrepo

  1. [root@localhost ~]# yum install createrepo -y

Create Repo

  1. [root@localhost ~]# createrepo /var/www/html/myrepo/base
  2. Spawning worker 0 with 9591 pkgs
  3. Workers Finished
  4. Saving Primary metadata
  5. Saving file lists metadata
  6. Saving other metadata
  7. Generating sqlite DBs
  8. Sqlite DBs complete
  9. [root@localhost ~]# createrepo /var/www/html/myrepo/updates
  10. Spawning worker 0 with 1490 pkgs
  11. Workers Finished
  12. Saving Primary metadata
  13. Saving file lists metadata
  14. Saving other metadata
  15. Generating sqlite DBs
  16. Sqlite DBs complete
  17. [root@localhost ~]# createrepo /var/www/html/myrepo/extras
  18. Spawning worker 0 with 284 pkgs
  19. Workers Finished
  20. Saving Primary metadata
  21. Saving file lists metadata
  22. Saving other metadata
  23. Generating sqlite DBs
  24. Sqlite DBs complete

Now, Take the Browser

http://192.168.220.200/myrepo/

Update Client Repo

  1. [root@localhost ~]# diff /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo_bak 
  2. 15c15
  3. < baseurl=http://192.168.220.200/myrepo/base
  4. ---
  5. > mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
  6. 17c17
  7. < gpgcheck=0
  8. ---
  9. > gpgcheck=1
  10. 23c23
  11. < baseurl=http://192.168.220.200/myrepo/updates
  12. ---
  13. > mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
  14. 25c25
  15. < gpgcheck=0
  16. ---
  17. > gpgcheck=1
  18. 31c31
  19. < baseurl=http://192.168.220.200/myrepo/extras
  20. ---
  21. > mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
  22. 33c33
  23. < gpgcheck=0
  24. ---
  25. > gpgcheck=1

Make Client Cache

  1. [root@localhost ~]# yum clean all && yum makecache

Client Example

  1. [root@localhost ~]# yum install vim
  2.