Friday, January 17, 2020

Setting up shadowsocks-libev on CentOS


Official Repo for shadowsocks-libev

1. Installation
yum install epel-release -y
yum install gcc gettext autoconf libtool automake make pcre-devel asciidoc xmlto c-ares-devel libev-devel libsodium-devel mbedtls-devel -y
ln -s /usr/lib64/libmbedcrypto.so.3 /usr/lib64/libmbedcrypto.so.2

cd /etc/yum.repos.d/
wget https://copr.fedoraproject.org/coprs/librehat/shadowsocks/repo/epel-7/librehat-shadowsocks-epel-7.repo
yum update
yum install shadowsocks-libev

2. Configuration
vi /etc/shadowsocks-libev/config.json
{
    "server":"0.0.0.0",
    "server_port":8888,
    "local_port":1080,
    "password":"xxxxxxxxxxx",
    "timeout":60,
    "method":"aes-256-cfb"
}

Notes for encryption method:
aes-256-cfb is strong
chacha20 is lightweight (less CPU consuming)

3. Service
# run application on startup
systemctl enable shadowsocks-libev
systemctl start shadowsocks-libev
systemctl status shadowsocks-libev
chkconfig shadowsocks-libev on

4. Firewall
systemctl status firewalld
firewall-cmd --state
firewall-cmd --list-port

# configure firewall (if needed)
firewall-cmd --zone=public --add-port=8888/tcp --permanent
firewall-cmd --zone=public --add-port=8888/udp --permanent
firewall-cmd --reload

5. Journal/logs
journalctl | grep ss-server













Thursday, December 31, 2015

Install ncurses on Ubuntu

How To Install ncurses Library on a Linux

Ineed to compile an application with ncurses library and header files. How do I install install ncurses libs and header files on a Linux operating system? How do I write a simple hello world program using the ncurses and compile it on a Linux?
GNU ncurses is software API for controlling writing to the console screen under Unix, Linux and other operating systems. You can create text-based user interfaces (TUI) on a Linux or Unix-like system using ncurses library.

Installing the ncurses library in Debian/Ubuntu Linux
  1. You need to install the following two packages: 
    libncurses5-dev : Developer's libraries for ncurses
    libncursesw5-dev : Developer's libraries for ncursesw
  2. Open the Terminal application.
  3. Type the following apt-get command to install ncurses header and libs:
    sudo apt-get install libncurses5-dev libncursesw5-dev
Sample outputs:
Fig.01: Install ncurses library using apt-get
Fig.01: Install ncurses library using apt-get

Installing the ncurses library in CentOS/RHEL/Scientific Linux 6.x/7.x+ and Fedora Linux 21 or older

  1. You need to install the following package: 
    ncurses-devel : Developer's libraries for ncurses
  2. Open the Terminal application.
  3. Type the following yum command to install ncurses header and libs:
    sudo yum install ncurses-devel
Sample outputs:
Fig.02: Install ncurses library using yum
Fig.02: Install ncurses library using yum

Installing the ncurses library in Fedora Linux 22.x+

  1. You need to install the following package: 
    ncurses-devel : Developer's libraries for ncurses
  2. Open the Terminal application.
  3. Type the following dnf command to install ncurses header and libs:
    sudo dnf install ncurses-devel

How do compile C program and use the ncurses library?

Create a test program called hello.c as follows:
#include <ncurses.h>
 
int main(void){
 initscr();           // Start curses mode
 printw("Hello World !!!"); // Print Hello World
 refresh();           // Print it on to the real screen
 getch();           // Wait for user input
 endwin();           // End curses mode
 return 0;
}
First, make sure you install GNU/GCC C compiler on a Linux:
To link to the ncurses library pass the -lncurses option to gcc/cc command:
$ cc -o output input.c -lncurses
$ cc -o hello hello.c -lncurses

Run it:
$ ./hello
Sample outputs:
Hello World !!!
Here is another program:
/*
  CURWIN1.C
  =========
  (c) Copyright Paul Griffiths 1999
  Email: mail@paulgriffiths.net
  Moving windows with ncurses.
*/
 
#include <stdlib.h>
#include <stdio.h>
#include <curses.h>
 
int main(void) {
    WINDOW * mainwin, * childwin;
    int      ch;
 
    // Set the dimensions and initial position for our child window
    int      width = 23, height = 7;
    int      rows  = 25, cols   = 80;
    int      x = (cols - width)  / 2;
    int      y = (rows - height) / 2;
 
 
    // Initialize ncurses
    if ((mainwin = initscr()) == NULL) {
     fprintf(stderr, "Error initialising ncurses.\n");
     exit(EXIT_FAILURE);
    }
 
    // Switch of echoing and enable keypad (for arrow keys)
 
    noecho();
    keypad(mainwin, TRUE);
 
    // Make our child window, and add a border and some text to it.
 
    childwin = subwin(mainwin, height, width, y, x);
    box(childwin, 0, 0);
    mvwaddstr(childwin, 1, 4, "Move the window");
    mvwaddstr(childwin, 2, 2, "with the arrow keys");
    mvwaddstr(childwin, 3, 6, "or HOME/END");
    mvwaddstr(childwin, 5, 3, "Press 'q' to quit");
 
    refresh();
 
    // Loop until user hits 'q' to quit
 
    while ((ch = getch()) != 'q') {
 switch (ch) {
 case KEY_UP:
     if (y > 0)
  --y;
     break;
 case KEY_DOWN:
     if (y < (rows - height))
  ++y;
     break;
 case KEY_LEFT:
     if (x > 0)
  --x;
     break;
 case KEY_RIGHT:
     if (x < (cols - width))
  ++x;
     break;
 case KEY_HOME:
     x = 0;
     y = 0;
     break;
 case KEY_END:
     x = (cols - width);
     y = (rows - height);
     break;
 }
 mvwin(childwin, y, x);
    }
 
 
    /*  Clean up after ourselves  */
 
    delwin(childwin);
    delwin(mainwin);
    endwin();
    refresh();
 
    return EXIT_SUCCESS;
}
Compile and run it as follows:
$ cc -o curwin1 curwin1.c -lncurses
$ ./curwin1

Sample outputs:
Fig.03: Basic window operations in action using ncurses
Fig.03: Basic window operations in action using ncurses

See this page and GNU ncurses project home page for more information.

Comments: One of the very earliest O’Reilly books was gloriously entitled “Programming with curses”.

Wednesday, December 30, 2015

Nested Classes in C++


A nested class/struct is a class which is declared in another enclosing class.As any other members of its enclosing class, nested class has access to all members to which the enclosing class has access,The members of an enclosing class have no special access to members of a nested class;

Note: Declarations in a nested class can use only type names, static members, and enumerators from the enclosing class.

Also, nesting a class within another class does not give the enclosing class special access privileges to member functions of the nested class.
A class can be nested in every part of the surrounding class: in the public, protected or private section. Such a nested class can be considered a member of the surrounding class. The normal access and rules in classes apply to nested classes. If a class is nested in the public section of a class, it is visible outside the surrounding class. If it is nested in the protected section it is visible in subclasses, derived from the surrounding class, if it is nested in the private section, it is only visible for the members of the surrounding class.

To refer to a nested class from outside of its enclosing class scope, you must use a fully qualified name.

Under what circumstance should be nested classes be used?

// 1. Nested class are cool for hiding implementation details
// 2. Nested class have additional access restriction
// 3. Nested class don't pollute the given namespace(e.g. global namespace)

// 1.
class List{
public:
    List(): head(nullptr), tail(nullptr){}
private:
    struct Node{
        Node(int data):_data(data){}
        int _data;
        Node *next, *prev;
    };
    Node *head;
    Node *tail;

};

// 2. Introducing another scope inside a class scope
class Product{
    public:
    enum ProductType{ FANCY, AWESOME, USEFUL };
    enum ProductBoxType{BOX, BAG, CRATE};
    Product(ProductType t, ProductBoxType b, std::string name);
};
// call
Product p(Product::FANCY, Product::BOX);
// When looking at code completion proposals for Product::, one will often get all
// the possilbe enum values(BOX, FANCY, CRATE) listed and it's easy to make a
// mistak here(c++11's strongly typed enum come to save)

// You can solve this by instroducing additional scope for those enums using
// nested classes, like this:
class Product{
public:
    struct ProductType{ enum Enum{FANCY, AWESOME, USEFUL} };
    struct ProductBoxType{ enum Enum{BOX, BAG, CRATE} };
    Product(ProductType::Enum t, ProductBoxType::Enum b, std::string name);
};
//call
Product p(Product::ProductType::FANCY, Product::ProductBoxType::BOX);

// 3.PIMPL idiom
// the PIMPL(short for Private Implementation) is an idiom to remove
// the implementation details of a class from the header.
// this reduces the need of recompiling classes depending on the class's
// header whenever the "implementation" part of the header changes

// a.h
class X{
public:
    X();
    virtual ~X();
    void Interface();
    void Interface2();
private:
    struct Impl;
    std::unique_ptr<Impl> impl;
};

// a.cpp
#include "a.h"
#include <windows.h>
struct X::Impl{
   HWND hWnd; // this field is a part of the class, no need to include windows.h
   // all privatge fields, methods go here
   void privateMethod(HWND wnd);
   void privateMethod();
};

X::X() : imple(new Imple()){
    //...
}


continue...
// An inner/nested class is a friend of the class(Outer/encloding) it is
// defined within.
// but unlike Java, there is no relation between an instance of inner class
// and an instance of the outer class
// This means: an object of type Outer does not contain any objects of
// type Inner

// Important: Nested classes declare only types within class scope.
// They do not cause contained objects of the nested class to be created.

// Visibility outside of the Outer class.
// Normally, the inner class is not visible from outside of the Outer class,
// for example:
class Outer{
public:
    class Inner1; // forward declaration, visible from outside of Outer
    class Inner2 {}; // definition, not visible from outside of Outer
};
int main(){
   Inner1 *in1; // ok
   Inner2 *in2; // error;
   Outer::Inner2 *in2; // ok
}

// Inner class has Outer class as a private member
class Outer{
public:
    Outer():i(*this), var(9){}
    Outer(Outer& other):i(other), var(15){}
    void func(){i.func();}
private:
    static const char* const MYCONST;
    Inner i;
    int var;
private:
    class Inner{
    public:
        Inner(Outer& x): _outer(x){}
        void func(){}
    };
    private:
        Outer& _outer;
};
int main(){
    Outer a1;
    Outer a2(a1);
    a1.func();
    a2.func();
}

// Since C++11, an inner class has access to all members of the outer class,
// but it does not have an implicit reference to a outer class instance.
// but if you pass a refernce of the outer class's instance to the inner class's

// instance, it can access anything(private or public) in the outer class instance



int x,y; // globals
class enclose { // enclosing class
    int x; // note: private members
    static int s;
 public:
    struct inner { // nested class
        void f(int i) {
            x = i; // Error: can't write to non-static enclose::x without instance
            int a = sizeof x; // Error until C++11,
                              // OK in C++11: operand of sizeof is unevaluated,
                              // this use of the non-static enclose::x is allowed.
            s = i;   // OK: can assign to the static enclose::s
            ::x = i; // OK: can assign to global x
            y = i;   // OK: can assign to global y
        }
        void g(enclose* p, int i) {
            p->x = i; // OK: assign to enclose::x
        }
    };
};
friend functions defined within a nested class has no special access to the members of the enclosing class even if lookup from the body of a name function that is defined within a nested class can find the private members of the enclosing class.
Out-of-class definitions of the members of a nested class appear in the namespace of the enclosing class:
struct enclose {
    struct inner {
        static int x;
        void f(int i);
    };
};
int enclose::inner::x = 1; // definition
void enclose::inner::f(int i) {} // definition
Nested classes can be forward-declared and later defined, either within the same enclosing class body, or outside of it:
class enclose {
    class nested1; // forward declaration
    class nested2; // forward declaration
    class nested1 {}; // definition of nested class
};
class enclose::nested2 { }; // definition of nested class



#include <iostream>
using namespace std;
class C1{
public:
    C1():mData(500){}
    void modifyB1(int var){
       mPb1->modifyme(var);
    }
    struct B1;// forward declaration
    void setB1(B1 *ptr){
        mPb1 = ptr;
    }
    struct B1{
        B1():mB1data(1111){}
        void bfoo(){ cout << mB1data << endl; } 
        void modifyme(int newdata){
            mB1data = newdata;
        }
        void print(){
            cout << "mB1data:" << mB1data << endl;
        }
    private:
        int mB1data;
    };
private:
    int mData;
    void foo(){}
    B1 *mPb1;
    //friend class B1;

};

#include "nest1.h"

int main(){
    C1 c1;
    C1::B1 b1;
    //b1.bfoo();
    c1.setB1(&b1);
    b1.print();
    c1.modifyB1(1080); 
    b1.print();
}

Program 1
#include <iostream>
class Enclosing {     
   int x;
   class Nested { // Nested class declaration 
      int y;  
      void NestedFun(Enclosing *ptr) {
        std::cout << ptr->x;  // OK: nested class can access
                              // private members of Enclosing class
      }      
   };
};
int main(){...}

Program 2
#include<iostream>
 class Enclosing {     
   int x;
   class Nested { // Nested class declaration
      int y;  
   }; 
   void EnclosingFun(Nested *n) {
        std::cout << n->y;  // Compiler Error: y is private in Nested
   }     
};
int main(){...}


Member classes of Template Class

// inst1.h
#include <iostream>

template<typename T>
class Base3{
public:
    template<typename U>
    struct S{
        void print(const char* a){
            std::cout << a << std::endl;
        }
    };
    template<typename U>
    void print(S<U> *ptr, const char *msg){
        ptr->print(msg);
    }
private:
    // Important:
    // member variable template can only be static
    // data member!
    // template<typename U>
    // S<U> *m_ptr;
};

template class Base3<int>::S<double>; // explicit instantiation

#include "inst1.h"
int main(){
    Base3<int> b3;
    Base3<int>::S<double> s;
    s.print("what's up!");
    b3.print<double>(&s, "I am good!");
}



http://www.oopweb.com/CPP/Documents/CPPAnnotations/Volume/cplusplus16.html