Calling a C Function From Lua

Prepare So

  1. #include <math.h>
  2. #include <lua.h>
  3. #include <lauxlib.h>
  4. #include <lualib.h>
  5.  
  6. static int my_sum(lua_State *L){
  7.     int d1 = luaL_checknumber(L, 1);
  8.     int d2 = luaL_checknumber(L, 2);
  9.     lua_pushnumber(L, d1+d2);
  10.     return 1;
  11. }
  12.  
  13. static int my_info(lua_State *L){
  14.     lua_pushstring(L, "qinuu");
  15.     return 1;
  16. }
  17.  
  18. static const struct luaL_Reg test_lib[] = {
  19.     {"my_sum" , my_sum},
  20.     {"my_info" , my_info},
  21.     {NULL, NULL}
  22. };
  23.  
  24. int luaopen_test_lib(lua_State *L){
  25.     //luaL_newlib(L, test_lib); // 5.2
  26.     luaL_register(L, "test_lib",test_lib); // lua 5.1
  27.     return 1;
  28. }

Test

  1. local my_lib = require "test_lib"
  2.  
  3. print(type(test_lib))
  4.  
  5. print(test_lib.my_sum(23,17))
  6.  
  7. print(test_lib.my_info())

How to expose a local server behind a NAT or firewall to the internet

Download FRP Server & Client

Modify frps.ini

  1. [root@VM_0_5_centos frp_0.25.3_linux_amd64]# cat frps.ini
  2. [common]
  3. bind_port = xxxxxx
  4. dashboard_port = xxxxxx
  5. dashboard_user = xxxxxxxxxxxxxxxx
  6. dashboard_pwd = xxxxxxxxxxxxxxxx
  7. allow_ports = xxxxx-xxxxx
  8. token = xxxxxxxxxxxxxxxx
  9. max_pool_count = 10
  10. pool_count = 3

Start frps

  1. [root@VM_0_5_centos frp_0.25.3_linux_amd64]# ./frps -c ./frps.ini

Modify frpc.ini

  1. root@imx6ul7d:~/frp# cat frpc.ini 
  2. [common]
  3. server_addr = qinuu.com
  4. server_port = xxxxxx
  5. token = xxxxxxxxxxxxxxxx
  6. [ssh]
  7. type = tcp
  8. local_ip = 127.0.0.1
  9. local_port = 22
  10. remote_port = xxxxxx

Start frpc

root@imx6ul7d:~/frp# ./frpc -c ./frpc.ini

Start a systemd service

[root@VM_0_5_centos frp_0.25.3_linux_amd64]# cat /lib/systemd/system/frps.service
[Unit]
Description=Frp Server Service
After=network.target
 
[Service]
Type=simple
User=nobody
Restart=on-failure
RestartSec=5s
ExecStart=/usr/local/frp/frp_0.25.3_linux_amd64/frps -c /usr/local/frp/frp_0.25.3_linux_amd64/frps.ini
 
[Install]
WantedBy=multi-user.targethis
 
[root@VM_0_5_centos frp_0.25.3_linux_amd64]# systemctl start frps
[root@VM_0_5_centos frp_0.25.3_linux_amd64]# systemctl enable frps
[root@VM_0_5_centos frp_0.25.3_linux_amd64]# systemctl status frps

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