├── web ├── sm_watcher │ ├── __init__.py │ ├── requirements.txt │ ├── templates │ │ ├── index.html │ │ ├── main.html │ │ └── modules │ │ │ └── entries.html │ ├── README.md │ ├── static │ │ └── scripts │ │ │ └── entries.js │ ├── index.py │ └── sm_watcher.py ├── httpserver │ ├── auto.sh │ ├── run │ ├── index.html │ └── simplehttpserver.py ├── getweb.sh └── ss_watcher.py ├── security ├── openssl │ ├── ca.srl │ ├── REAME.md │ ├── v3.ext │ ├── server-csr.conf │ ├── simple-https-server.py │ ├── server.csr │ ├── ca.crt │ ├── server.crt │ ├── ca.key │ ├── server.key │ ├── gen_certs.sh │ └── server.pem └── cfssl │ ├── test-csr.json │ ├── ca-csr.json │ ├── cfssl.sh │ └── signing-profiles.json ├── python ├── wsgi │ ├── config.ini │ ├── router.py │ ├── versions.py │ ├── wsgi.py │ └── server.py ├── mqtt │ ├── server.py │ └── client.py ├── regex.py ├── tls_server │ ├── Makefile │ ├── client.py │ ├── client.crt │ ├── client.pem │ ├── server.crt │ ├── server.pem │ ├── server.py │ ├── client.key │ ├── server.key │ └── flask.py ├── amqp │ ├── pika │ │ ├── send.py │ │ └── receive.py │ └── kombu │ │ ├── receive.py │ │ └── send.py ├── json │ └── simple_test.py ├── concurrency │ └── performance_comparison.py ├── push_agent.py ├── encoding.py ├── pull_agent.py ├── remote_watcher.py ├── json_flatter.py ├── stat_parse │ └── ifconfig.py ├── add_watermark.py └── gitbook_gen.py ├── system ├── package.list ├── limits.conf ├── sysctl.conf ├── init.sh ├── nss ├── socket │ ├── Makefile │ ├── client.c │ └── server.c ├── netlink │ ├── Makefile │ └── gnKernel.c ├── getIP │ ├── Makefile │ ├── getAllIP.c │ └── getSpecIP.c ├── cpu │ ├── cpu2.sh │ └── cpu1.sh ├── ether_bridge │ ├── Makefile │ ├── README │ ├── headers.h │ ├── LICENSE │ └── etherbridge.c ├── .bashrc ├── .bash_aliases ├── .bash_color ├── rm.sh └── service ├── golang ├── plugin │ ├── auto.sh │ ├── plugin.go │ └── main.go ├── grpc │ └── src │ │ └── hello │ │ ├── go.mod │ │ ├── hello │ │ └── hello.proto │ │ ├── Makefile │ │ ├── server │ │ └── server.go │ │ └── client │ │ └── client.go └── check.sh ├── .gitignore ├── c ├── sendRawPkt │ ├── Makefile │ └── sendRawPkt.c ├── io │ └── read.c ├── mcast │ ├── Makefile │ ├── mcast.h │ ├── main.c │ └── mcast.c └── getopt │ ├── getopt_long.c │ └── Makefile ├── docker ├── restore_images.sh ├── save_all_images.sh ├── docker-cleanup.sh └── ovs_perf.sh ├── algorithm ├── bf-gdt │ ├── Makefile │ ├── bf-gdt.h │ ├── bf.h │ ├── main.c │ ├── bf-gdt.c │ └── bf.c ├── bit_op.py ├── bitmap.c ├── match.py └── sort.py ├── programing ├── Makefile.template └── autopreview.vim ├── shell └── functions └── README.md /web/sm_watcher/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /security/openssl/ca.srl: -------------------------------------------------------------------------------- 1 | DDE813F27F074981 2 | -------------------------------------------------------------------------------- /web/httpserver/auto.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | supervise . 3 | -------------------------------------------------------------------------------- /web/httpserver/run: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | python simplehttpserver.py 3 | -------------------------------------------------------------------------------- /python/wsgi/config.ini: -------------------------------------------------------------------------------- 1 | [app:testapp] 2 | paste.app_factory = router:API.factory 3 | -------------------------------------------------------------------------------- /web/sm_watcher/requirements.txt: -------------------------------------------------------------------------------- 1 | beautifulsoup4>=4.0.0 2 | requests>=2.10.0 3 | tornado>=4.0.0 4 | -------------------------------------------------------------------------------- /system/package.list: -------------------------------------------------------------------------------- 1 | apt-fast install 2 | axel install 3 | bash_completion install 4 | ssh install 5 | -------------------------------------------------------------------------------- /web/sm_watcher/templates/index.html: -------------------------------------------------------------------------------- 1 | {% extends "main.html" %} 2 | 3 | {% block body %} 4 | {% module show_entry() %} 5 | {% end %} 6 | -------------------------------------------------------------------------------- /security/openssl/REAME.md: -------------------------------------------------------------------------------- 1 | 2 | Run following cmds to generate the key/certs for CA and server. 3 | 4 | ```bash 5 | bash gen_certs.sh 6 | ``` 7 | -------------------------------------------------------------------------------- /golang/plugin/auto.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | echo "Build plugin source code as .so file" 4 | go build -buildmode=plugin -o plugin.so plugin.go 5 | 6 | echo "Test load plugin and call methods" 7 | go run main.go 8 | -------------------------------------------------------------------------------- /python/mqtt/server.py: -------------------------------------------------------------------------------- 1 | import mosquitto 2 | 3 | client = mosquitto.Mosquitto("test", clean_session=True) 4 | client.connect("127.0.0.1", 1883) 5 | 6 | client.publish("test", "hello world", 1) 7 | 8 | client.loop_forever() 9 | -------------------------------------------------------------------------------- /python/regex.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #-*- coding: utf-8 -*- 3 | import re 4 | 5 | regex = re.compile('[a-z]{10}') 6 | t1 = re.match(regex,'1234567890') 7 | t2 = re.match(regex,'abcdefghij') 8 | 9 | print t1 10 | print t2 11 | -------------------------------------------------------------------------------- /golang/grpc/src/hello/go.mod: -------------------------------------------------------------------------------- 1 | module hello 2 | 3 | go 1.13 4 | 5 | require ( 6 | github.com/golang/protobuf v1.5.3 7 | golang.org/x/net v0.17.0 8 | google.golang.org/grpc v1.56.3 9 | ) 10 | 11 | replace hello/hello => ./hello 12 | -------------------------------------------------------------------------------- /system/limits.conf: -------------------------------------------------------------------------------- 1 | # This should be put as `/etc/security/limits.conf` 2 | 3 | * hard nofile 1048576 4 | * soft nofile 1048576 5 | * soft nproc 10485760 6 | * hard nproc 10485760 7 | * soft stack 32768 8 | * hard stack 32768 9 | ``` 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## generic files to ignore 2 | *~ 3 | *.lock 4 | *.DS_Store 5 | *.swp 6 | *.out 7 | *.bak 8 | *.tmp 9 | tags 10 | 11 | #c/c++ specific 12 | *.o 13 | 14 | #java specific 15 | *.class 16 | 17 | #python specific 18 | *.pyc 19 | .idea 20 | -------------------------------------------------------------------------------- /python/tls_server/Makefile: -------------------------------------------------------------------------------- 1 | server: 2 | python3 server.py 3 | 4 | client: 5 | python3 client.py 6 | 7 | keys: 8 | openssl req -new -x509 -days 3650 -nodes -out client.pem -keyout client.key 9 | openssl req -new -x509 -days 3650 -nodes -out server.pem -keyout server.key -------------------------------------------------------------------------------- /c/sendRawPkt/Makefile: -------------------------------------------------------------------------------- 1 | TARGET = sendRawPkt 2 | 3 | $(TARGET): $(TARGET).c 4 | gcc -o $(TARGET) $(TARGET).c 5 | 6 | test: $(TARGET) 7 | #sudo tcpdump -nettti eth0 '(ether dst host 00:11:22:33:44:55)'& 8 | sudo ./$(TARGET) eth0 9 | 10 | clean: 11 | @rm $(TARGET) *.o >/dev/null 2>&1 & 12 | -------------------------------------------------------------------------------- /docker/restore_images.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # This script will load into the tar.gz files as images. 3 | 4 | 5 | [ $# -lt 1 ] && echo "Please specify the dir path of the gz files" && exit 0; 6 | 7 | for gz in `ls $1` ; do 8 | [[ ${gz#*.} == "tar.gz" ]] && echo "Loading $gz back to images"; 9 | sudo docker load < $gz 10 | done 11 | -------------------------------------------------------------------------------- /security/openssl/v3.ext: -------------------------------------------------------------------------------- 1 | authorityKeyIdentifier=keyid,issuer 2 | basicConstraints=CA:FALSE 3 | keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment 4 | subjectAltName= DNS:localhost, DNS:oci-lbr, DNS:clustertst02-bcsb-1, DNS:clustertst02-bcsb-2, DNS:clustertst02-bcsb-3, IP:127.0.0.1, IP:129.213.9.145, IP:129.213.62.129, IP:129.213.53.75, IP:129.213.51.73 5 | -------------------------------------------------------------------------------- /golang/plugin/plugin.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | var Value int 4 | 5 | // Add returns the sum of two numbers 6 | func Add(x, y int) int { 7 | return x+y 8 | } 9 | 10 | // Sub returns the subtracted result of two numbers 11 | func Sub(x, y int) int { 12 | return x-y 13 | } 14 | 15 | // GetValue returns the value 16 | func GetValue() int { 17 | return Value 18 | } 19 | -------------------------------------------------------------------------------- /system/sysctl.conf: -------------------------------------------------------------------------------- 1 | # This should be put as `/etc/sysctl.conf`. 2 | 3 | vm.swappiness=10 4 | fs.file-max = 2000000 5 | kernel.threads-max = 2091845 6 | kernel.pty.max = 210000 7 | net.ipv4.ip_local_port_range = 10000 65535 8 | net.ipv4.tcp_tw_reuse = 0 9 | net.ipv4.tcp_tw_recycle = 0 10 | net.ipv4.tcp_max_tw_buckets = 5000 11 | net.ipv4.tcp_fin_timeout = 30 12 | net.ipv4.tcp_max_syn_backlog = 8192 13 | -------------------------------------------------------------------------------- /c/io/read.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main(){ 4 | 5 | FILE* f_gid = fopen("/tmp/lc_gid.dat","r"); 6 | unsigned int gid = 0; 7 | if(f_gid != NULL) { 8 | fscanf(f_gid,"%u",&gid); 9 | //fread(&gid,sizeof(unsigned int),1,f_gid); 10 | printf("ovsd bf_gdt_init with outside id=%u\n",gid); 11 | fclose(f_gid); 12 | } else { 13 | return; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /security/cfssl/test-csr.json: -------------------------------------------------------------------------------- 1 | { 2 | "CN": "test.example.com", 3 | "hosts": [ 4 | "test.example.com", 5 | "www.test.example.com" 6 | ], 7 | "key": { 8 | "algo": "ecdsa", 9 | "size": 256 10 | }, 11 | "names": [ 12 | { 13 | "C": "US", 14 | "L": "San Francisco", 15 | "O": "test.example.com", 16 | "OU": "test.example.com", 17 | "ST": "California" 18 | } 19 | ] 20 | } -------------------------------------------------------------------------------- /web/httpserver/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 |

6 | 7 | 8 | LOGO 9 | 10 | 11 | Webpage Visit results 12 |

13 |
14 | 15 | 16 | -------------------------------------------------------------------------------- /python/amqp/pika/send.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import pika 3 | 4 | connection = pika.BlockingConnection(pika.ConnectionParameters( 5 | 'localhost')) 6 | channel = connection.channel() 7 | channel.queue_declare(queue='hello') 8 | 9 | channel.basic_publish(exchange='', 10 | routing_key='hello', 11 | body='Hello World!') 12 | print " [x] Sent 'Hello World!'" 13 | connection.close() 14 | 15 | -------------------------------------------------------------------------------- /python/wsgi/router.py: -------------------------------------------------------------------------------- 1 | import routes 2 | 3 | import wsgi 4 | import versions 5 | 6 | class API(wsgi.Router): 7 | 8 | def __init__(self, mapper=None): 9 | if(mapper == None): # create mapper 10 | mapper = routes.Mapper() 11 | versions_resource = versions.create_resource() # create resource 12 | mapper.connect("/",controller=versions_resource,action="index")# create mapping 13 | super(API,self).__init__(mapper) 14 | -------------------------------------------------------------------------------- /security/cfssl/ca-csr.json: -------------------------------------------------------------------------------- 1 | { 2 | "CN": "ca.example.com", 3 | "names": [ 4 | { 5 | "C": "US", 6 | "L": "San Francisco", 7 | "O": "example.com", 8 | "OU": "ca.example.com", 9 | "ST": "California" 10 | } 11 | ], 12 | "hosts": [ 13 | "example.com", 14 | "www.example.com" 15 | ], 16 | "key": { 17 | "algo": "ecdsa", 18 | "size": 256 19 | }, 20 | "ca": { 21 | "expiry": "262800h" 22 | } 23 | } -------------------------------------------------------------------------------- /system/init.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | #Please run this script with root privilege 3 | 4 | release='saucy' 5 | user='baohua' 6 | 7 | sudo echo 'baohua ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers 8 | 9 | 10 | sudo apt-add-repository ppa:apt-fast/stable -y 11 | 12 | sudo aptitude update; 13 | sudo dpkg --set-selections < package.list && sudo aptitude upgrade 14 | 15 | cp -f ./.bash* /home/${user}/ 16 | 17 | 18 | cp rm.sh /usr/local/bin/ 19 | cp getweb.sh /usr/local/bin/ 20 | -------------------------------------------------------------------------------- /golang/grpc/src/hello/hello/hello.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package helloworld; 4 | 5 | // The greeting service definition. 6 | service Greeter { 7 | // Sends a greeting 8 | rpc SayHello (HelloRequest) returns (HelloReply) {} 9 | } 10 | 11 | // The request message containing the user's name. 12 | message HelloRequest { 13 | string name = 1; 14 | } 15 | 16 | // The response message containing the greetings 17 | message HelloReply { 18 | string message = 1; 19 | } -------------------------------------------------------------------------------- /golang/grpc/src/hello/Makefile: -------------------------------------------------------------------------------- 1 | proto: 2 | protoc --go_out=plugins=grpc:. ./hello/*.proto 3 | install:build 4 | cp grpc_server ${GOPATH}/bin 5 | cp grpc_client ${GOPATH}/bin 6 | build: 7 | # GOPATH=/go 8 | go build -o grpc_server server/server.go 9 | go build -o grpc_client client/client.go 10 | server: 11 | # GOPATH=$(PWD)/../../ 12 | go run server/server.go 13 | client: 14 | # GOPATH=$(PWD)/../../ 15 | go run client/client.go 16 | 17 | .PHONY: all proto server client test 18 | -------------------------------------------------------------------------------- /python/amqp/kombu/receive.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #File: receiver.py 3 | 4 | import kombu 5 | 6 | connection = kombu.BrokerConnection('librabbitmq://localhost') 7 | simple_queue = connection.SimpleQueue('test') 8 | 9 | print '[*] waiting for messages, if exit press CTRL+C' 10 | 11 | def message_process(body, message): 12 | print '[receive]: %s' % body 13 | message.ack() 14 | 15 | simple_queue.consumer.callbacks = [message_process] 16 | simple_queue.get() 17 | simple_queue.close() 18 | -------------------------------------------------------------------------------- /c/mcast/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS = -O2 -Wall -pedantic -lpthread 3 | #CFLAG = -Wall 4 | 5 | SRCS=$(wildcard *.c) 6 | OBJS=$(patsubst %.c,%.o, $(SRCS)) 7 | 8 | TARGET = main 9 | 10 | all:$(TARGET) 11 | 12 | $(TARGET): $(OBJS) 13 | cc -o $(TARGET) $(CFLAGS) $(OBJS) 14 | 15 | %.o: %.c 16 | $(CC) -c $< -o $@ $(CFLAGS) 17 | 18 | test: $(TARGET) 19 | ./$(TARGET) -g 239.0.0.1 -p 5000 -t 1 -c "payload here" #ip, port, interval, content 20 | 21 | clean: 22 | rm $(TARGET) $(OBJS) > /dev/null 2>&1 & 23 | -------------------------------------------------------------------------------- /python/amqp/kombu/send.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #File: sender.py 3 | #prerequisites: rabbitmq-server python-rabbitmq python-kombu 4 | 5 | import kombu 6 | import sys 7 | 8 | connection = kombu.Connection('librabbitmq://localhost') 9 | simple_queue = connection.SimpleQueue('test') 10 | 11 | if len(sys.argv) < 2: 12 | print 'message is empty!' 13 | sys.exit(0) 14 | 15 | message = sys.argv[1] 16 | simple_queue.put(message) 17 | print '[send]: ' + message + '\n' 18 | simple_queue.close() 19 | -------------------------------------------------------------------------------- /system/nss: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | #This script will show all ip address inside each network namespace 3 | #If give a parameter, then it will show the network namespace containing the key 4 | 5 | for name in `ip netns show` 6 | do 7 | [[ $name == qdhcp-* || $name == qrouter-* ]] && result=`ip netns exec $name ip addr` 8 | if [ $# -gt 0 ]; then 9 | [[ -n "`echo $result|grep $1`" ]] && echo $name && echo -e $result && break 10 | else 11 | echo $name 12 | echo -e $result 13 | fi 14 | done 15 | -------------------------------------------------------------------------------- /docker/save_all_images.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # This script will save all your local Docker images as tar.gz files. 3 | 4 | echo "Checking your images" 5 | 6 | for img in `sudo docker images | awk '$1 != "REPOSITORY" { print $1 }' | sort | uniq` ; do 7 | zip_file=${img//\//_}.tar.gz 8 | echo "Saving $img to ${zip_file}" 9 | sudo docker save $img | gzip > ${zip_file} 10 | done 11 | 12 | echo "Save all images into local gz files done." 13 | echo "You can restore them as images using docker load *.tar.gz." 14 | -------------------------------------------------------------------------------- /system/socket/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAG = -Wall 3 | #CFLAG = -Wall -pedantic -ansi 4 | #LDFLAGS=-lnsl -lm -lpthread -m32 5 | SRCS=$(wildcard *.c) 6 | OBJS=$(patsubst %.c,%.o, $(SRCS)) 7 | TARGET=$(patsubst %.c,%, $(SRCS)) 8 | 9 | all: $(TARGET) 10 | 11 | %.o: %.c 12 | $(CC) -c $< -o $@ $(CFLAGS) 13 | 14 | %: %.o 15 | $(CC) $< -o $@ $(LDFLAGS) 16 | 17 | test: $(TARGET) 18 | ./server & 19 | ./client 127.0.0.1 "[Client to Server] hello." 20 | killall server 21 | 22 | clean: 23 | rm *.o $(TARGET) > /dev/null 2>&1 & 24 | -------------------------------------------------------------------------------- /web/sm_watcher/README.md: -------------------------------------------------------------------------------- 1 | # sm_watcher 2 | 3 | A real-time smart web watcher with keyword filting. 4 | 5 | Currently it supports the boards of newsmth.net. 6 | 7 | ## Run 8 | 9 | First, install all required dependencies. 10 | 11 | ```sh 12 | $ pip install -r requirements.txt 13 | ``` 14 | 15 | Then start the watcher 16 | ```sh 17 | $ python sm_watcher.py & 18 | ``` 19 | 20 | And the web server. 21 | ```sh 22 | $ python index.py 23 | ``` 24 | 25 | Now open `http://localhost:9000`, the result will be shown and updated in real-time, enjoy! -------------------------------------------------------------------------------- /security/openssl/server-csr.conf: -------------------------------------------------------------------------------- 1 | [ req ] 2 | prompt = no 3 | default_bits = 2048 4 | default_keyfile = server.key 5 | distinguished_name = req_distinguished_name 6 | req_extensions = req_ext 7 | 8 | [ req_distinguished_name ] 9 | C=CN 10 | ST=BJ 11 | L=BJ 12 | O=O 13 | OU=O 14 | CN=server 15 | 16 | [ req_ext ] 17 | subjectAltName = @alt_names 18 | 19 | [alt_names] 20 | DNS.1 = localhost 21 | DNS.2 = server 22 | DNS.3 = oci-lbr 23 | IP.1 = 127.0.0.1 24 | IP.2 = 129.213.9.145 25 | IP.3 = 129.213.62.129 26 | IP.4 = 129.213.53.75 27 | IP.5 = 129.213.51.73 28 | -------------------------------------------------------------------------------- /python/amqp/pika/receive.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import pika 3 | 4 | def callback(ch, method, properties, body): 5 | print " [x] Received %r" % (body,) 6 | 7 | 8 | connection = pika.BlockingConnection(pika.ConnectionParameters( 9 | 'localhost')) 10 | channel = connection.channel() 11 | channel.queue_declare(queue='hello') 12 | 13 | print ' [*] Waiting for messages. To exit press CTRL+C' 14 | 15 | channel.basic_consume(callback, 16 | queue='hello', 17 | no_ack=True) 18 | 19 | channel.start_consuming() 20 | 21 | -------------------------------------------------------------------------------- /python/json/simple_test.py: -------------------------------------------------------------------------------- 1 | import json 2 | 3 | obj = [[1,2,3],123,123.123,'abc',{'key1':(1,2,3),'key2':(4,5,6)}] 4 | 5 | print "original obj =", repr(obj) 6 | 7 | #use dumps to encode object into json format, sorting the dict key, and ignore non-str keys 8 | json_encoded = json.dumps(obj,sort_keys=True,,skipkeys=True) 9 | 10 | print "after json encoding" 11 | print "obj = ",json_encoded 12 | 13 | #use loads to decode json format into original object 14 | json_decoded = json.loads(json_encoded) 15 | 16 | print type(json_decoded) 17 | 18 | print "after json decoding" 19 | print "obj = ",json_decoded 20 | -------------------------------------------------------------------------------- /python/mqtt/client.py: -------------------------------------------------------------------------------- 1 | import paho.mqtt.client as mqtt 2 | 3 | def on_connect(client, userdata, rc): 4 | print("Connected with result code "+str(rc)) 5 | # Subscribing in on_connect() means that if we lose the connection and 6 | # reconnect then subscriptions will be renewed. 7 | client.subscribe("test") 8 | 9 | # The callback for when a PUBLISH message is received from the server. 10 | def on_message(client, userdata, msg): 11 | print(msg.topic+" "+str(msg.payload)) 12 | 13 | client = mqtt.Client() 14 | client.on_connect = on_connect 15 | client.on_message = on_message 16 | 17 | client.connect("127.0.0.1", 1883, 60) 18 | 19 | client.loop_forever() 20 | -------------------------------------------------------------------------------- /security/openssl/simple-https-server.py: -------------------------------------------------------------------------------- 1 | # taken from http://www.piware.de/2011/01/creating-an-https-server-in-python/ 2 | # generate server.xml with the following command: 3 | # openssl req -new -x509 -keyout server.pem -out server.pem -days 365 -nodes 4 | # run as follows: 5 | # python simple-https-server.py 6 | # then in your browser, visit: 7 | # https://localhost:4443 8 | 9 | import BaseHTTPServer, SimpleHTTPServer 10 | import ssl 11 | 12 | httpd = BaseHTTPServer.HTTPServer(('0.0.0.0', 1080), 13 | SimpleHTTPServer.SimpleHTTPRequestHandler) 14 | httpd.socket = ssl.wrap_socket (httpd.socket, certfile='./server.pem', 15 | server_side=True) 16 | httpd.serve_forever() 17 | -------------------------------------------------------------------------------- /python/concurrency/performance_comparison.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | __author__ = 'baohua' 3 | 4 | def test(i,j): 5 | if i+j < i-j: 6 | return i+j 7 | 8 | if __name__ == '__main__': 9 | import threading, time 10 | t0 = time.time() 11 | for i in range(10000): 12 | t = threading.Thread(target=test, args = (i,i+1)) 13 | t.start() 14 | print "Threading: %.3fs taken" % (time.time() - t0) 15 | 16 | import eventlet, time 17 | eventlet.monkey_patch() 18 | 19 | t0 = time.time() 20 | pool = eventlet.GreenPool() 21 | for i in range(10000): 22 | pool.spawn(test, i, i+1) 23 | pool.waitall() 24 | print "Eventlet: %.3fs taken" % (time.time() - t0) 25 | -------------------------------------------------------------------------------- /python/wsgi/versions.py: -------------------------------------------------------------------------------- 1 | import httplib 2 | import json 3 | import webob.dec 4 | 5 | from webob import Response 6 | 7 | class Controller(object): 8 | def __init__(self): 9 | # TODO 10 | self.version = "0.1" 11 | 12 | def index(self,req): 13 | response = Response(request=req, 14 | status=httplib.MULTIPLE_CHOICES, 15 | content_type='application/json') 16 | response.body = json.dumps(dict(versions=self.version)) 17 | return response 18 | 19 | @webob.dec.wsgify 20 | def __call__(self, request): 21 | # TODO 22 | return self.index(request) 23 | 24 | def create_resource(): 25 | return Controller() 26 | -------------------------------------------------------------------------------- /system/netlink/Makefile: -------------------------------------------------------------------------------- 1 | #for kernel space 2 | KERNELBUILD := /lib/modules/`uname -r`/build/ 3 | TARGET = test 4 | obj-m := $(TARGET).o 5 | $(TARGET)-objs := gnKernel.o 6 | #CFLAG = -Wall -pedantic -ansi 7 | CFLAG = -Wall 8 | 9 | default:user 10 | 11 | #for kernel space 12 | kernel: 13 | @make -C $(KERNELBUILD) M=$(shell pwd) modules 14 | 15 | insert: kernel 16 | sudo insmod $(TARGET).ko 17 | 18 | remove: 19 | sudo rmmod $(TARGET).ko 20 | 21 | #for user space 22 | user: gnUser.o 23 | cc -o gnUser $(CFLAG) gnUser.o 24 | 25 | gnUser.o: gnUser.c 26 | cc -o gnUser.o $(CFLAG) -c gnUser.c 27 | 28 | test: insert user 29 | ./gnUser 30 | make remove 31 | 32 | clean: 33 | rm gnUser $(TARGET).mod.c *.o *.ko Module.symvers modules.order > /dev/null 2>&1 & 34 | -------------------------------------------------------------------------------- /security/cfssl/cfssl.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | echo "CFSSL: https://github.com/cloudflare/cfssl" 4 | 5 | echo "Clean existing generated files" 6 | rm -f ca.csr ca.pem ca-key.pem 7 | rm -f test.csr test.pem test-key.pem 8 | 9 | if ! command -v cfssl >/dev/null 2>&1; then 10 | echo "Installing (Need have golang env first)" 11 | go get -u github.com/cloudflare/cfssl/cmd/... 12 | fi 13 | 14 | if [ ! -f ca.pem ]; then 15 | echo "Generate self-sign cert for the ca" 16 | cfssl genkey -initca ca-csr.json | cfssljson -bare ca 17 | fi 18 | 19 | echo "Generate signed cert for test identity" 20 | cfssl gencert \ 21 | -config signing-profiles.json \ 22 | -profile client \ 23 | -ca ca.pem \ 24 | -ca-key ca-key.pem \ 25 | test-csr.json | cfssljson -bare test 26 | -------------------------------------------------------------------------------- /python/push_agent.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #Daemon script. Push some ms to remote host. 3 | #Usage: ./push_agent.py host_ip msg 4 | 5 | import sys 6 | import os 7 | import string 8 | import datetime 9 | import urllib 10 | import simplejson as json 11 | 12 | #the user/ip of the remote host that needs to be monitored 13 | user="root" 14 | #remotehost="192.168.57.10" 15 | remotefile="/tmp/cpu.dat" 16 | 17 | if len(sys.argv) < 3: 18 | sys.exit('Usage: %s host_ip msg' % sys.argv[0]) 19 | 20 | remotehost=sys.argv[1] 21 | msg=sys.argv[2] 22 | 23 | work_path = os.path.split(os.path.realpath(__file__))[0]+"/" 24 | os.system("cd "+work_path) 25 | 26 | #cp remote file to local 27 | os.system("ssh %s@%s 'echo %s > %s' 2>&1 > /dev/null" %(user,remotehost,msg,remotefile)) 28 | -------------------------------------------------------------------------------- /c/getopt/getopt_long.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | char *l_opt_arg; 7 | char* const short_options = "vn:"; 8 | struct option long_options[] = { 9 | {"version",0, NULL,'v'}, 10 | {"name",1, NULL,'n'}, 11 | {0,0,0,0}, 12 | }; 13 | int c; 14 | while((c = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) 15 | { 16 | switch (c) { 17 | case 'v': 18 | printf("The version is 0x1.\n"); 19 | break; 20 | case 'n': 21 | l_opt_arg = optarg; 22 | printf("The name input is %s!\n", l_opt_arg); 23 | break; 24 | } 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /python/encoding.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #-*-coding:utf-8-*- 3 | #if there is no default encoding declaration, then local system encoding will be used as default. 4 | 5 | import sys 6 | print sys.getdefaultencoding() 7 | 8 | #test the unicode object 9 | s = u"中文" #an unicode object, just use encode 10 | try: 11 | print "s=",s #use default encoding (utf-8 here) to interpret 12 | print "s.utf-8=",s.encode('utf-8') 13 | print "s.gbk=",s.encode('gbk') 14 | except: 15 | pass 16 | finally: 17 | pass 18 | 19 | #test the string: character sequence 20 | s = "中文" #string will be encoded with default file encoding (utf-8 here), decode into unicode first. 21 | try: 22 | print "s=",s 23 | print "s.utf-8=",s.decode('utf-8').encode('utf-8') 24 | print "s.gbk=",s.decode('utf-8').encode('gbk') 25 | except: 26 | pass 27 | finally: 28 | pass 29 | -------------------------------------------------------------------------------- /algorithm/bf-gdt/Makefile: -------------------------------------------------------------------------------- 1 | #for kernel space 2 | KERNELBUILD := /lib/modules/`uname -r`/build/ 3 | TARGET = test 4 | obj-m := $(TARGET).o 5 | $(TARGET)-objs := main.o bf.o bf-gdt.o 6 | 7 | default:user 8 | 9 | kernel: 10 | @make -C $(KERNELBUILD) M=$(shell pwd) modules 11 | 12 | insert: 13 | sudo insmod $(TARGET).ko 14 | 15 | remove: 16 | sudo rmmod $(TARGET).ko 17 | 18 | #for user space 19 | user: bf-gdt.o main.o bf.o 20 | cc -o bf-gdt -Wall -pedantic bf.o bf-gdt.o main.o 21 | 22 | bf-gdt.o: bf-gdt.c bf-gdt.h 23 | cc -o bf-gdt.o -Wall -pedantic -ansi -c bf-gdt.c 24 | 25 | bf.o: bf.c bf.h 26 | cc -o bf.o -Wall -pedantic -ansi -c bf.c 27 | 28 | main.o: main.c bf-gdt.h 29 | cc -o main.o -Wall -pedantic -ansi -c main.c 30 | 31 | test: user 32 | ./bf-gdt 33 | 34 | clean: 35 | rm bf-gdt $(TARGET).mod.c *.o *.ko Module.symvers modules.order *.d *.cmd > /dev/null 2>&1 & 36 | -------------------------------------------------------------------------------- /system/getIP/Makefile: -------------------------------------------------------------------------------- 1 | #for kernel space 2 | KERNELBUILD := /lib/modules/`uname -r`/build/ 3 | TARGET = test 4 | obj-m := $(TARGET).o 5 | $(TARGET)-objs := kgetSpecIP.o 6 | #CFLAG = -Wall -pedantic -ansi 7 | CFLAG = -Wall 8 | 9 | default:user 10 | 11 | #for kernel space 12 | kernel: 13 | @make -C $(KERNELBUILD) M=$(shell pwd) modules 14 | 15 | insert: kernel 16 | sudo insmod $(TARGET).ko 17 | 18 | remove: 19 | sudo rmmod $(TARGET).ko 20 | 21 | #for user space 22 | user: getSpecIP.o getAllIP 23 | cc -o getSpecIP $(CFLAG) getSpecIP.o 24 | cc -o getAllIP $(CFLAG) getAllIP.o 25 | 26 | getSpecIP.o: getSpecIP.c 27 | cc -o getSpecIP.o $(CFLAG) -c getSpecIP.c 28 | 29 | getAllIP.o: getAllIP.c 30 | cc -o getAllIP.o $(CFLAG) -c getAllIP.c 31 | 32 | test: insert user 33 | ./getSpecIP 34 | make remove 35 | 36 | clean: 37 | rm getSpecIP getAllIP $(TARGET).mod.c *.o *.ko Module.symvers modules.order > /dev/null 2>&1 & 38 | -------------------------------------------------------------------------------- /system/cpu/cpu2.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | #write the cpu utilization number into file and the screen 3 | 4 | prev_total=0 5 | prev_idle=0 6 | while true; do 7 | cpu=`cat /proc/stat | head -n1 | sed 's/cpu //'` 8 | user=`echo $cpu | awk '{print $1}'` 9 | system=`echo $cpu | awk '{print $2}'` 10 | nice=`echo $cpu | awk '{print $3}'` 11 | idle=`echo $cpu | awk '{print $4}'` 12 | wait=`echo $cpu | awk '{print $5}'` 13 | irq=`echo $cpu | awk '{print $6}'` 14 | srq=`echo $cpu | awk '{print $7}'` 15 | zero=`echo $cpu | awk '{print $8}'` 16 | total=$(($user+$system+$nice+$idle+$wait+$irq+$srq+$zero)) 17 | diff_idle=$(($idle-$prev_idle)) 18 | diff_total=$(($total-$prev_total)) 19 | usage=$(($((1000*$(($diff_total-$diff_idle))/$diff_total+5))/10)) 20 | #clear 21 | echo "Usage: $usage%" 22 | echo -n $usage > /tmp/cpu.dat 23 | prev_total=$total 24 | prev_idle=$idle 25 | sleep 5 26 | done 27 | -------------------------------------------------------------------------------- /algorithm/bit_op.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | """ 3 | Test to implement several efficient functions with bit operations. 4 | 5 | Run the script by 6 | python alg_bit_op.py 7 | If no outputs, it means all tests are passed. 8 | """ 9 | 10 | def setRightestBit(n): 11 | """ 12 | Set the rightest bit 1 of the number to 0. 13 | This can be utlized to test if a number is 2-power. 14 | @param n: The num. 15 | @return: The set result. 16 | 17 | >>> [setRightestBit(n) for n in range(5)] 18 | [0, 0, 0, 2, 0] 19 | """ 20 | return n&(n-1) 21 | 22 | def isolateRightPart(n): 23 | """ 24 | Isolate the right part (starting from bit 1) in the number. 25 | @param n: The num. 26 | @return: The set result. 27 | 28 | >>> [isolateRightPart(n) for n in range(5)] 29 | [0, 1, 2, 1, 4] 30 | """ 31 | return n&(-1*n) 32 | 33 | 34 | if __name__ == "__main__": 35 | import doctest 36 | doctest.testmod() 37 | -------------------------------------------------------------------------------- /system/cpu/cpu1.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | #write the cpu utilization number into file and the screen 3 | 4 | PREV_TOTAL=0 5 | PREV_IDLE=0 6 | 7 | while true; do 8 | CPU=(`cat /proc/stat | grep '^cpu '`) # Get the total CPU statistics. 9 | unset CPU[0] # Discard the "cpu" prefix. 10 | IDLE=${CPU[4]} # Get the idle CPU time. 11 | 12 | # Calculate the total CPU time. 13 | TOTAL=0 14 | for VALUE in "${CPU[@]}"; do 15 | let "TOTAL=$TOTAL+$VALUE" 16 | done 17 | 18 | # Calculate the CPU usage since we last checked. 19 | let "DIFF_IDLE=$IDLE-$PREV_IDLE" 20 | let "DIFF_TOTAL=$TOTAL-$PREV_TOTAL" 21 | let "DIFF_USAGE=(1000*($DIFF_TOTAL-$DIFF_IDLE)/$DIFF_TOTAL+5)/10" 22 | echo -en "\rCPU: $DIFF_USAGE% \b\b" 23 | echo -n $DIFF_USAGE > /tmp/cpu.dat 24 | 25 | # Remember the total and idle CPU times for the next check. 26 | PREV_TOTAL="$TOTAL" 27 | PREV_IDLE="$IDLE" 28 | 29 | # Wait before checking again. 30 | sleep 5 31 | done 32 | -------------------------------------------------------------------------------- /web/sm_watcher/static/scripts/entries.js: -------------------------------------------------------------------------------- 1 | 32 | -------------------------------------------------------------------------------- /algorithm/bitmap.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SHIFT 5 //64-bit os should change this to 6 4 | #define WORD_LEN 32 //64-bit os should change this to 64 5 | #define MASK 0x1f 6 | #define N 100000 //length of bitmap 7 | 8 | int a[1 + N/WORD_LEN];//memory allocation 9 | 10 | //set the bit to 1 11 | void set(int i) { 12 | a[i>>SHIFT] |= (1<<(i & MASK)); 13 | } 14 | 15 | //clear the bit to 0 16 | void clear(int i) { 17 | a[i>>SHIFT] &= ~(1<<(i & MASK)); 18 | } 19 | 20 | //test if the bit is 1 21 | int test(int i){ 22 | return a[i>>SHIFT] & (1<<(i & MASK)); 23 | } 24 | 25 | int main() { 26 | int i; 27 | for (i = 0; i < N; i++) 28 | clear(i); 29 | printf("Please input the bit ids (ctrl+d to finish):", i); 30 | while (scanf("%d", &i) != EOF) 31 | set(i); 32 | printf("The bit set: "); 33 | for (i = 0; i < N; i++) 34 | if (test(i)) 35 | printf("%d ", i); 36 | printf("\n"); 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /system/ether_bridge/Makefile: -------------------------------------------------------------------------------- 1 | # etherbridge - Makefile 2 | CC=gcc 3 | INCLUDES=-I./ -I/usr/include 4 | CFLAGS=$(INCLUDES) -O2 -Wall -g 5 | LDFLAGS=-L/usr/lib -lpcap -lnsl -lm -lpthread 6 | SRCS=$(wildcard *.c) 7 | #SRCS=source.c 8 | OBJS=$(patsubst %.c,%.o, $(SRCS)) 9 | DEPS=$(patsubst %.c,%.d, $(SRCS)) 10 | TARGET=etherbridge 11 | RM=rm -f 12 | INSTALL_DIR = /usr/bin 13 | 14 | all: $(DEPS) $(TARGET) 15 | 16 | ################ below needs no change in most case ################ 17 | 18 | $(TARGET): $(OBJS) 19 | $(CC) -o $(TARGET) $(OBJS) $(LDFLAGS) 20 | 21 | %.o: %.c 22 | $(CC) -c $< -o $@ $(CFLAGS) 23 | 24 | %: %.o 25 | $(CC) $< -o $@ $(LDFLAGS) 26 | 27 | %.d: %.c 28 | $(CC) -MM $(CFLAGS) $< >$@.$$$$;\ 29 | sed 's,\($*\)\.o[ :]*,\1.o $@: ,g' < $@.$$$$ > $@;\ 30 | $(RM) $@.$$$$; 31 | 32 | .PHONY: all clean install uninstall 33 | 34 | clean: 35 | $(RM) $(TARGET) *.o *.bak *.c~ *.h~ *.d 36 | 37 | install: 38 | cp -f $(TARGET) $(INSTALL_DIR)/ 39 | uninstall: 40 | rm -f $(INSTALL_DIR)/$(TARGET) 41 | -------------------------------------------------------------------------------- /golang/grpc/src/hello/server/server.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "log" 7 | "net" 8 | 9 | "google.golang.org/grpc" 10 | pb "hello/hello" 11 | ) 12 | 13 | const ( 14 | port = ":7050" 15 | ) 16 | 17 | type server struct{ } 18 | 19 | // implement server side interface methods 20 | func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) { 21 | log.Printf("Received msg from: %s\n", in.GetName()) 22 | return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil 23 | } 24 | 25 | // start a gRPC server side: create socket, create, register and start the service 26 | func main() { 27 | lis, err := net.Listen("tcp", port) 28 | if err != nil { 29 | log.Fatalf("failed to listen: %v\n", err) 30 | } 31 | s := grpc.NewServer() 32 | pb.RegisterGreeterServer(s, &server{}) 33 | fmt.Printf("Starting listen on port: %s\n", port) 34 | if err := s.Serve(lis); err != nil { 35 | log.Fatalf("failed to serve: %v", err) 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /python/tls_server/client.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | 3 | import socket 4 | import ssl 5 | import time 6 | 7 | host_addr = '144.25.32.206' 8 | host_port = 8888 9 | server_sni_hostname = 'example.com' 10 | server_cert = 'server.crt' 11 | client_cert = 'client.crt' 12 | client_key = 'client.key' 13 | 14 | context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH, cafile=server_cert) 15 | context.load_cert_chain(certfile=client_cert, keyfile=client_key) 16 | 17 | while True: 18 | try: 19 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 20 | conn = context.wrap_socket(s, server_side=False, 21 | server_hostname=server_sni_hostname) 22 | conn.connect((host_addr, host_port)) 23 | if conn: 24 | print("SSL established. Peer: {}".format(conn.getpeercert())) 25 | print("Sending: 'Hello, world!") 26 | conn.send(b"Hello, world!") 27 | print("Closing connection") 28 | exit() 29 | conn.close() 30 | s.close() 31 | except Exception as e: 32 | print(e) 33 | time.sleep(3) 34 | -------------------------------------------------------------------------------- /python/wsgi/wsgi.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import routes.middleware 3 | import webob.dec 4 | import webob.exc 5 | 6 | 7 | class Router(object): # base class for WSGI APP, mapping from url to resource 8 | 9 | def __init__(self, mapper=None): 10 | self.map = mapper #resource map 11 | self._router = routes.middleware.RoutesMiddleware(self._dispatch, 12 | self.map) #register url callback 13 | @classmethod 14 | def factory(cls, global_conf, **local_conf): # entrance 15 | return cls() # construct an app 16 | 17 | @webob.dec.wsgify # wrap the request and response in WSGI style 18 | def __call__(self,req): # callable 19 | return self._router 20 | 21 | @staticmethod 22 | @webob.dec.wsgify 23 | def _dispatch(req): 24 | # TODO 25 | match = req.environ['wsgiorg.routing_args'][1] 26 | if not match: 27 | return webob.exc.HTTPNotFound() 28 | app = match['controller'] 29 | return app 30 | -------------------------------------------------------------------------------- /python/tls_server/client.crt: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIICnjCCAYYCCQDBXcdxY6g7CjANBgkqhkiG9w0BAQsFADARMQ8wDQYDVQQDDAZj 3 | bGllbnQwHhcNMjAwMTI0MDA1MjMyWhcNMjEwMTIzMDA1MjMyWjARMQ8wDQYDVQQD 4 | DAZjbGllbnQwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDIqvLKLjTx 5 | sZv1lGs0tjMsuc4m9Dpeo/QX9W3SUV/KBHpYpr3BEOWzGy32rEHRvPCVVNCXkRYx 6 | 6pK6ujaSpo7GttKwyKwQvEk8FlLbquW3glj9o9aGaAPU/xv0b/6E1JBlfKujqm98 7 | BfDNZD9U3TKf+ZRfJ2qjPqpfvUlJ9mdttFOavRCO+2ySIgm+WoYT1ruUenSW89Om 8 | ie/x5uEcsbr7M6doQeUcO1xcyPwSzhAI8qk8Zyajx2xtwl2Rq9rZl6uKIGtrY1XY 9 | eLxi+kPkQGMQUJ+gv1ED5MhztBrKK3TPfS4C3r9oAd7gpOAxgcpdufWhAPUTnC3t 10 | qAlKwSoy/h83AgMBAAEwDQYJKoZIhvcNAQELBQADggEBAALAeMhR/IrplhUdlq3Y 11 | vkFuXLcrSgIjRFW9VdY7Ie9HNv2boO+fBUK4BSFhtWpyDg/g4ga2AFvbdU1+aWMA 12 | Gk84/34lwkAobhWg4t2AkWQ+2y56RnxhyyA7yFVKnoZE7EkggnDaJ7qxrwYM9O3K 13 | fSiRqofx7YxwtjT3DL4X9BPoUVy8N3BT+KsmWSiMhanHKQALZYAPnIqhU8W+10uD 14 | jg5gZthEmAgUE+2RwCmz6TFCwt+ZINPmGD0KaD1NtUfmFYTcpnt3ORCR+XiCP824 15 | mWL3XauErP07+jBZSkv2ZVxdzcCADeeQRDOPjth1mdmZ5d3rajViBs4GJgUOQAhf 16 | ywQ= 17 | -----END CERTIFICATE----- 18 | -------------------------------------------------------------------------------- /python/tls_server/client.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIICnjCCAYYCCQDBXcdxY6g7CjANBgkqhkiG9w0BAQsFADARMQ8wDQYDVQQDDAZj 3 | bGllbnQwHhcNMjAwMTI0MDA1MjMyWhcNMjEwMTIzMDA1MjMyWjARMQ8wDQYDVQQD 4 | DAZjbGllbnQwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDIqvLKLjTx 5 | sZv1lGs0tjMsuc4m9Dpeo/QX9W3SUV/KBHpYpr3BEOWzGy32rEHRvPCVVNCXkRYx 6 | 6pK6ujaSpo7GttKwyKwQvEk8FlLbquW3glj9o9aGaAPU/xv0b/6E1JBlfKujqm98 7 | BfDNZD9U3TKf+ZRfJ2qjPqpfvUlJ9mdttFOavRCO+2ySIgm+WoYT1ruUenSW89Om 8 | ie/x5uEcsbr7M6doQeUcO1xcyPwSzhAI8qk8Zyajx2xtwl2Rq9rZl6uKIGtrY1XY 9 | eLxi+kPkQGMQUJ+gv1ED5MhztBrKK3TPfS4C3r9oAd7gpOAxgcpdufWhAPUTnC3t 10 | qAlKwSoy/h83AgMBAAEwDQYJKoZIhvcNAQELBQADggEBAALAeMhR/IrplhUdlq3Y 11 | vkFuXLcrSgIjRFW9VdY7Ie9HNv2boO+fBUK4BSFhtWpyDg/g4ga2AFvbdU1+aWMA 12 | Gk84/34lwkAobhWg4t2AkWQ+2y56RnxhyyA7yFVKnoZE7EkggnDaJ7qxrwYM9O3K 13 | fSiRqofx7YxwtjT3DL4X9BPoUVy8N3BT+KsmWSiMhanHKQALZYAPnIqhU8W+10uD 14 | jg5gZthEmAgUE+2RwCmz6TFCwt+ZINPmGD0KaD1NtUfmFYTcpnt3ORCR+XiCP824 15 | mWL3XauErP07+jBZSkv2ZVxdzcCADeeQRDOPjth1mdmZ5d3rajViBs4GJgUOQAhf 16 | ywQ= 17 | -----END CERTIFICATE----- 18 | -------------------------------------------------------------------------------- /python/tls_server/server.crt: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIICqDCCAZACCQDHmb8sZYFaljANBgkqhkiG9w0BAQsFADAWMRQwEgYDVQQDDAtl 3 | eGFtcGxlLmNvbTAeFw0yMDAxMjQwMDUyMThaFw0yMTAxMjMwMDUyMThaMBYxFDAS 4 | BgNVBAMMC2V4YW1wbGUuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKC 5 | AQEAwMM8pW19L1bkDgiY51Ps+UTMNdOPN/lTJD81Mv4ibUYUAs0SZsmGiDXDeNF/ 6 | awi6lbAwMjDIHympILkjzog/56PpMaRTOAOZpfWTM2LC6o1mdj00j/xi7agWH00W 7 | Fl/idBSY8nnKydWFNCFJMEOFUsabco2qNvQA0T9IfHvhHWEw2T6oe7cxM2TQaS2F 8 | bm7IGcoCZLNSAioaDBc2eQZqDdCPe537YGBWxtvw6c09hDRRKHs3Vl8dmWIFV/Yw 9 | luQWqIRSBjCHluwqSYQjm0sfUrKb70huQeDlk1vPBZK72dgiRnd3cWFgLY2i22nm 10 | HYR8nkOTCRIr8rEovVyUM7dj+wIDAQABMA0GCSqGSIb3DQEBCwUAA4IBAQCcxDgA 11 | QxChniddn8fBTSQWDMbxuKJr82pmc1ad08UGufyt/h5jBx+WirVvPfjR4LDGB28h 12 | 0rxBmT1z6fZ6RcRh2fIObX/ovUPn2sdQ8TNJmA8+6Yu2eLvZtlRGx5Wl28MZRi/w 13 | WTEJhanzhlMigykqSSt8e1jMxOCTTppglZZ9Wm3fqvMkvKbbekZw4eMlnRa9pI9d 14 | 6pIr96jJNzlQ+FgEb05N8Dhwr1vyjdkv6efjq7eSUpBjRjXuJT55nxCY/+doCRV2 15 | xB7U9dyu4fipY2dsIwFeto7HW4sELpAi7HtX1sOAzksrS6UH4+rPTmmnsujwOr31 16 | 2Bnmf9pohx03yzf3 17 | -----END CERTIFICATE----- 18 | -------------------------------------------------------------------------------- /python/tls_server/server.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIICqDCCAZACCQDHmb8sZYFaljANBgkqhkiG9w0BAQsFADAWMRQwEgYDVQQDDAtl 3 | eGFtcGxlLmNvbTAeFw0yMDAxMjQwMDUyMThaFw0yMTAxMjMwMDUyMThaMBYxFDAS 4 | BgNVBAMMC2V4YW1wbGUuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKC 5 | AQEAwMM8pW19L1bkDgiY51Ps+UTMNdOPN/lTJD81Mv4ibUYUAs0SZsmGiDXDeNF/ 6 | awi6lbAwMjDIHympILkjzog/56PpMaRTOAOZpfWTM2LC6o1mdj00j/xi7agWH00W 7 | Fl/idBSY8nnKydWFNCFJMEOFUsabco2qNvQA0T9IfHvhHWEw2T6oe7cxM2TQaS2F 8 | bm7IGcoCZLNSAioaDBc2eQZqDdCPe537YGBWxtvw6c09hDRRKHs3Vl8dmWIFV/Yw 9 | luQWqIRSBjCHluwqSYQjm0sfUrKb70huQeDlk1vPBZK72dgiRnd3cWFgLY2i22nm 10 | HYR8nkOTCRIr8rEovVyUM7dj+wIDAQABMA0GCSqGSIb3DQEBCwUAA4IBAQCcxDgA 11 | QxChniddn8fBTSQWDMbxuKJr82pmc1ad08UGufyt/h5jBx+WirVvPfjR4LDGB28h 12 | 0rxBmT1z6fZ6RcRh2fIObX/ovUPn2sdQ8TNJmA8+6Yu2eLvZtlRGx5Wl28MZRi/w 13 | WTEJhanzhlMigykqSSt8e1jMxOCTTppglZZ9Wm3fqvMkvKbbekZw4eMlnRa9pI9d 14 | 6pIr96jJNzlQ+FgEb05N8Dhwr1vyjdkv6efjq7eSUpBjRjXuJT55nxCY/+doCRV2 15 | xB7U9dyu4fipY2dsIwFeto7HW4sELpAi7HtX1sOAzksrS6UH4+rPTmmnsujwOr31 16 | 2Bnmf9pohx03yzf3 17 | -----END CERTIFICATE----- 18 | -------------------------------------------------------------------------------- /system/getIP/getAllIP.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | int main(void) 11 | { 12 | int s; 13 | struct ifconf conf; 14 | struct ifreq *ifr; 15 | char buff[BUFSIZ]; 16 | int num; 17 | int i; 18 | 19 | s = socket(PF_INET, SOCK_DGRAM, 0); 20 | conf.ifc_len = BUFSIZ; 21 | conf.ifc_buf = buff; 22 | 23 | ioctl(s, SIOCGIFCONF, &conf); 24 | num = conf.ifc_len / sizeof(struct ifreq); 25 | ifr = conf.ifc_req; 26 | 27 | for(i=0;i < num;i++) 28 | { 29 | struct sockaddr_in *sin = (struct sockaddr_in *)(&ifr->ifr_addr); 30 | 31 | ioctl(s, SIOCGIFFLAGS, ifr); 32 | if(((ifr->ifr_flags & IFF_LOOPBACK) == 0) && (ifr->ifr_flags & IFF_UP)) 33 | { 34 | printf("%s (%s)\n", 35 | ifr->ifr_name, 36 | inet_ntoa(sin->sin_addr)); 37 | } 38 | ifr++; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /system/getIP/getSpecIP.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | int main(void) 12 | { 13 | char *eth="eth0:d"; 14 | char *ipaddr = malloc(20); 15 | int sock_fd; 16 | struct sockaddr_in my_addr; 17 | struct ifreq ifr; 18 | 19 | /* Get socket file descriptor */ 20 | if ((sock_fd = socket(PF_INET, SOCK_DGRAM, 0)) == -1) { 21 | perror("socket"); 22 | return -1; 23 | } 24 | 25 | /* Get IP Address */ 26 | strncpy(ifr.ifr_name, eth, IF_NAMESIZE); 27 | ifr.ifr_name[IFNAMSIZ-1]='\0'; 28 | 29 | if (ioctl(sock_fd, SIOCGIFADDR, &ifr) < 0) { 30 | printf(":No Such Device %s\n",eth); 31 | return -1; 32 | } 33 | 34 | memcpy(&my_addr, &ifr.ifr_addr, sizeof(my_addr)); 35 | strcpy(ipaddr, inet_ntoa(my_addr.sin_addr)); 36 | printf("%s\n",ipaddr); 37 | free(ipaddr); 38 | close(sock_fd); 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /docker/docker-cleanup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # A Docker host clean up scripts from yeasy.github.io 3 | # This script will 4 | # 1. delete all stopped containers 5 | # 2. delete all untagged/dangling images 6 | 7 | 8 | if [ ! -e "/var/run/docker.sock" ]; then 9 | echo "Cannot find docker socket(/var/run/docker.sock), exit now!" 10 | exit 1 11 | fi 12 | 13 | if docker version >/dev/null; then 14 | echo "docker is running properly" 15 | else 16 | echo "Cannot run docker command, exit now!" 17 | exit 1 18 | fi 19 | 20 | 21 | 22 | # Cleanup exited/dead containers 23 | EXITED_CONTAINERS="`docker ps -a -q -f status=exited -f status=dead | xargs echo`" 24 | if [ "$EXITED_CONTAINERS" != "" ]; then 25 | echo "Delete all stopped containers..." 26 | docker rm -v $EXITED_CONTAINERS 27 | echo "Delete all stopped containers...Done" 28 | fi 29 | 30 | UNTAGGED_IMAGES="`docker images -q -f dangling=true | xargs echo`" 31 | if [ "$UNTAGGED_IMAGES" != "" ]; then 32 | echo "Delete all untagged images" 33 | docker rmi $UNTAGGED_IMAGES 34 | echo "Delete all untagged images...Done" 35 | fi 36 | -------------------------------------------------------------------------------- /system/.bashrc: -------------------------------------------------------------------------------- 1 | # don't put duplicate lines or lines starting with space in the history. 2 | # See bash(1) for more options 3 | HISTCONTROL=ignoreboth 4 | 5 | # append to the history file, don't overwrite it 6 | shopt -s histappend 7 | 8 | # for setting history length see HISTSIZE and HISTFILESIZE in bash(1) 9 | HISTSIZE=1000 10 | HISTFILESIZE=2000 11 | 12 | # check the window size after each command and, if necessary, 13 | # update the values of LINES and COLUMNS. 14 | shopt -s checkwinsize 15 | 16 | # make less more friendly for non-text input files, see lesspipe(1) 17 | [ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)" 18 | 19 | # Source global definitions 20 | if [ -f /etc/bashrc ]; then 21 | . /etc/bashrc 22 | fi 23 | 24 | # Enable bash automatical completion 25 | if [ -f /etc/bash_completion ] && ! shopt -oq posix; then 26 | . /etc/bash_completion 27 | fi 28 | 29 | # Some useful aliases 30 | if [ -f ~/.bash_aliases ]; then 31 | . ~/.bash_aliases 32 | fi 33 | 34 | # Enable colorful bash 35 | if [ -f ~/.bash_color ]; then 36 | . ~/.bash_color 37 | fi 38 | 39 | #variables 40 | -------------------------------------------------------------------------------- /c/getopt/Makefile: -------------------------------------------------------------------------------- 1 | # This is a Makefile template. 2 | # Feel free to distribute or modify the code.:) 3 | # Version: 0.2 4 | # Author: Baohua Yang@THU 5 | # Created: Sep 1, 2008 6 | # Updated: May 31, 2011 7 | 8 | CC=gcc 9 | INCLUDES=-I./ 10 | CFLAGS=$(INCLUDES) -O2 -Wall -g -m32 11 | LDFLAGS=-lnsl -lm -lpthread -m32 12 | SRCS=$(wildcard *.c) 13 | OBJS=$(patsubst %.c,%.o, $(SRCS)) 14 | DEPS=$(patsubst %.c,%.d, $(SRCS)) 15 | RM=rm -f 16 | 17 | #change this line, set your destination prog. 18 | TARGET=$(patsubst %.c,%, $(SRCS)) 19 | 20 | all: $(DEPS) $(TARGET) 21 | #all:$(TARGET) 22 | 23 | ################ below needs no change in most case ################ 24 | 25 | test: $(TARGET) 26 | for i in [$(TARGET)]; do \ 27 | ./$(i)\ 28 | done; 29 | 30 | $(TARGET): $(OBJS) 31 | $(CC) -o $(TARGET) $(OBJS) $(LDFLAGS) 32 | 33 | %.o: %.c 34 | $(CC) -c $< -o $@ $(CFLAGS) 35 | 36 | %: %.o 37 | $(CC) $< -o $@ $(LDFLAGS) 38 | 39 | %.d: %.c 40 | $(CC) -MM $(CFLAGS) $< >$@.$$$$;\ 41 | sed 's,\($*\)\.o[ :]*,\1.o $@: ,g' < $@.$$$$ > $@;\ 42 | $(RM) $@.$$$$; 43 | 44 | .PHONY : clean 45 | clean: 46 | $(RM) $(TARGET) *.o *.bak *.c~ *.h~ *.d 47 | -------------------------------------------------------------------------------- /python/wsgi/server.py: -------------------------------------------------------------------------------- 1 | import os 2 | import logging 3 | import sys 4 | from paste import deploy 5 | from wsgiref.simple_server import make_server 6 | 7 | LOG = logging.getLogger(__name__) 8 | 9 | module_dir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]), 10 | os.pardir,os.pardir)) 11 | 12 | sys.path.insert(0,module_dir) # 13 | 14 | bind_host = "127.0.0.1" 15 | bind_port = 8080 16 | 17 | def server(app_name, conf_file): 18 | app = load_paste_app(app_name,conf_file) 19 | serve = make_server(bind_host,bind_port,app) 20 | serve.serve_forever() 21 | 22 | def load_paste_app(app_name, conf_file): 23 | LOG.debug("Loading %(app_name) from %(conf_file)", 24 | {'app_name':app_name, 'conf_file':conf_file}) 25 | 26 | try: 27 | app = deploy.loadapp("config:%s" % os.path.abspath(conf_file), name=app_name) 28 | return app 29 | except (LookupError, ImportError) as e: 30 | LOG.error(str(e)) 31 | raise RuntimeError(str(e)) 32 | 33 | if __name__ == '__main__': 34 | app_name = "testapp" 35 | conf_file = "config.ini" 36 | server(app_name,conf_file) 37 | -------------------------------------------------------------------------------- /security/openssl/server.csr: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE REQUEST----- 2 | MIIC6zCCAdMCAQAwUDELMAkGA1UEBhMCQ04xCzAJBgNVBAgMAkJKMQswCQYDVQQH 3 | DAJCSjEKMAgGA1UECgwBTzEKMAgGA1UECwwBTzEPMA0GA1UEAwwGc2VydmVyMIIB 4 | IjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvfUqBdgXS4kjurzFdPFEx+In 5 | mIsszY1m5aoEAuaFTbqCN4Qxd3SlMl3i5et4nYdS0FxdzA48bcAZ/RCDun7H7klk 6 | l2v9qEd7/Uf/ptYLYkOKGlMmfqbQR04oIc1AFt5L8dV2sLsLiaOOdZ+nHwAGuy/M 7 | 10unuUFVMXg/oHP6gz+nimnyJc8cx9WiDm6gsG606cAsUkKsQZ/Fqw+Ih/A95Nze 8 | MyWHhK+mRkONaKuSpwwxPn54kGtV0TBnJCIPKzDzPjGnbM1ebJV/4jacU58II+lb 9 | NtkgnPBp8pj92Sqj34/QVYZaQVKhe/V5LImq8g/2UIJ+bvIZU75tnJ4+3aJhzQID 10 | AQABoFYwVAYJKoZIhvcNAQkOMUcwRTBDBgNVHREEPDA6gglsb2NhbGhvc3SCBnNl 11 | cnZlcoIHb2NpLWxicocEfwAAAYcEgdUJkYcEgdU+gYcEgdU1S4cEgdUzSTANBgkq 12 | hkiG9w0BAQsFAAOCAQEAR6UUg/mTQhCBm9069Wz18LpBnrcBDLZrVnY9kStdeLBt 13 | Ki+KVs5IAdzT31uOUCNo5Kvauu1SlGXhwthbMoGJKLE+AG3RT+0tzOgVL0S8oJww 14 | oApHtGCxfdhGkA/3kR1xP94RYtqWp9IXU5qT1dvP5AQhzX8vvE5sDCA91pmhatBz 15 | kSynr8+zL3VkD8pglwFu8zVcBAOZ/CTmEfd+zjbdXoVKee2kYOeQFAYDswwV7R9S 16 | 3fSZek28UcscLSDdTvg+Nh4VdKjf1s5YyLrPc8DPWfHkUzzihnQjQIyRMyOHS6If 17 | iwJjx+PA7H6HzXYo5FwbVduBThdP2RS3kt2ca7iFSw== 18 | -----END CERTIFICATE REQUEST----- 19 | -------------------------------------------------------------------------------- /programing/Makefile.template: -------------------------------------------------------------------------------- 1 | # This is a Makefile template. 2 | # Feel free to distribute or modify the code.:) 3 | # Version: 0.2 4 | # Author: Baohua Yang@THU 5 | # Created: Sep 1, 2008 6 | # Updated: May 31, 2011 7 | 8 | CC=gcc 9 | INCLUDES=-I./ -I/usr/include 10 | CFLAGS=$(INCLUDES) -O2 -Wall -g 11 | LDFLAGS=-L/usr/lib -lpcap -lnsl -lm -lpthread 12 | SRCS=$(wildcard *.c) 13 | #SRCS=source.c 14 | OBJS=$(patsubst %.c,%.o, $(SRCS)) 15 | DEPS=$(patsubst %.c,%.d, $(SRCS)) 16 | TARGET=prog 17 | RM=rm -f 18 | INSTALL_DIR = /usr/bin 19 | 20 | all: $(DEPS) $(TARGET) 21 | #all:$(TARGET) 22 | 23 | ################ below needs no change in most case ################ 24 | 25 | $(TARGET): $(OBJS) 26 | $(CC) -o $(TARGET) $(OBJS) $(LDFLAGS) 27 | 28 | %.o: %.c 29 | $(CC) -c $< -o $@ $(CFLAGS) 30 | 31 | %: %.o 32 | $(CC) $< -o $@ $(LDFLAGS) 33 | 34 | %.d: %.c 35 | $(CC) -MM $(CFLAGS) $< >$@.$$$$;\ 36 | sed 's,\($*\)\.o[ :]*,\1.o $@: ,g' < $@.$$$$ > $@;\ 37 | $(RM) $@.$$$$; 38 | 39 | .PHONY: all clean install uninstall 40 | clean: 41 | $(RM) $(TARGET) *.o *.bak *.c~ *.h~ *.d 42 | install: 43 | cp -f $(TARGET) $(INSTALL_DIR)/ 44 | uninstall: 45 | rm -f $(INSTALL_DIR)/$(TARGET) 46 | -------------------------------------------------------------------------------- /security/openssl/ca.crt: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIDSjCCAjICCQCeA4urViupmzANBgkqhkiG9w0BAQsFADBnMQswCQYDVQQGEwJV 3 | UzEOMAwGA1UECAwFU3RhdGUxETAPBgNVBAcMCExvY2F0aW9uMRAwDgYDVQQKDAdP 4 | cmdOYW1lMRYwFAYDVQQLDA1JVCBEZXBhcnRtZW50MQswCQYDVQQDDAJjYTAeFw0x 5 | ODAyMDcwMjE3MDNaFw0xOTAyMDcwMjE3MDNaMGcxCzAJBgNVBAYTAlVTMQ4wDAYD 6 | VQQIDAVTdGF0ZTERMA8GA1UEBwwITG9jYXRpb24xEDAOBgNVBAoMB09yZ05hbWUx 7 | FjAUBgNVBAsMDUlUIERlcGFydG1lbnQxCzAJBgNVBAMMAmNhMIIBIjANBgkqhkiG 8 | 9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0XDqWp5beOBwuTFjRyyCezyESSmZfOStFJFn 9 | ByT495Mout8hACCvW+L7lwPe8+TOTNIbumXRi2r9oD4feC+HW2Sz+b/aT2ywJDNg 10 | UqdJW0GYD4p6utVKWeGHlSQWUlfyT/umon430Qw2x/gj4QRZRk7q5A+TH69roVsF 11 | fz0ULQNX0R4ENWa9uqBUza9S9Kds1ZecPcjC5po2Kz/4w3GKL5ITWPgUnBLKW+aG 12 | NRTLeYuHM/MJgVRRt+Opvsd10HtSdJu+64oO6P6EO7R1ht1qGTVwmyksJEf0cyGi 13 | sCoTe/FZkVIVfgzu/ghqk49WBrQOSWqdIM9ASK+OXRP39r2YKQIDAQABMA0GCSqG 14 | SIb3DQEBCwUAA4IBAQBGl8tbERU5sQnVngK5bgisDw2b1KTL558af7mnN1FYHDH7 15 | 1aGk13LZjuHqFc/QAcX2dlN7OORHgxrW9pnqjoff4izd8lu6VMv1mIMSCxFQVeqj 16 | raaF3lispTgudQ2BH3IloJQ+ZciCGPLdBd2B3mcSOb48tgyipHwEohj/VQBHECz2 17 | nkIzAhJA6q72zj/6uPTwXJmORF2mFRGiBTA0rxSvk0JIv+bI2V7D5f+p0gf6yJM/ 18 | l2qzxHanbgF2UzE/STczXJJjHfS0u92V0wZ5wtuM/E5cERQRQ8BJTarAjksVTqvH 19 | sY7wiXLohFnMCsJysg9xQO81ECstlD2LL23OFoFV 20 | -----END CERTIFICATE----- 21 | -------------------------------------------------------------------------------- /golang/plugin/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "plugin" 6 | ) 7 | 8 | func main() { 9 | // Load the plugin and lookup the public methods 10 | p, err := plugin.Open("./plugin.so") 11 | if err != nil { 12 | panic(err) 13 | } 14 | 15 | // Lookup return a Symbol, need to convert to func type first 16 | value, err := p.Lookup("Value") //value is a local pointer to the Value 17 | if err != nil { 18 | panic(err) 19 | } 20 | 21 | addSymbol, err := p.Lookup("Add") 22 | if err != nil { 23 | panic(err) 24 | } 25 | add := addSymbol.(func(int, int) int) 26 | 27 | subSymbol, err := p.Lookup("Sub") 28 | if err != nil { 29 | panic(err) 30 | } 31 | sub := subSymbol.(func(int, int) int) 32 | 33 | getValueSymbol, err := p.Lookup("GetValue") 34 | if err != nil { 35 | panic(err) 36 | } 37 | getValue := getValueSymbol.(func() int) 38 | 39 | // Only this way will affect the plugin's Value variable 40 | *value.(*int) = 10 41 | fmt.Printf("value=%d, getValue=%d\n", value, getValue()) 42 | 43 | value = add(1,2) 44 | fmt.Printf("value=%d, getValue=%d\n", value, getValue()) 45 | 46 | value = sub(1,2) 47 | fmt.Printf("value=%d, getValue=%d\n", value, getValue()) 48 | 49 | } 50 | -------------------------------------------------------------------------------- /web/sm_watcher/templates/main.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Smart Watcher 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | {% block header %}{% end %} 29 |
30 | 31 | {% block body %}{% end %} 32 | 33 |
34 | {% block footer %}{% end %} 35 |
36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /web/sm_watcher/templates/modules/entries.html: -------------------------------------------------------------------------------- 1 |
2 |
3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |

标题

时间

11 |
12 |
13 | 14 | 45 | -------------------------------------------------------------------------------- /security/cfssl/signing-profiles.json: -------------------------------------------------------------------------------- 1 | { 2 | "signing": { 3 | 4 | "default": { 5 | "expiry": "8760h" 6 | }, 7 | 8 | "profiles": { 9 | 10 | "intermediate": { 11 | "usages": [ 12 | "cert sign", 13 | "crl sign" 14 | ], 15 | "expiry": "87600h", 16 | "ca_constraint": { 17 | "is_ca": true 18 | } 19 | }, 20 | 21 | "last_intermediate": { 22 | "usages": [ 23 | "cert sign", 24 | "crl sign" 25 | ], 26 | "expiry": "87600h", 27 | "ca_constraint": { 28 | "is_ca": true, 29 | "max_path_len": 0, 30 | "max_path_len_zero": true 31 | } 32 | }, 33 | 34 | "client-server": { 35 | "usages": [ 36 | "signing", 37 | "key encipherment", 38 | "client auth", 39 | "server auth" 40 | ], 41 | "expiry": "87600h" 42 | }, 43 | 44 | "client": { 45 | "usages": [ 46 | "signing", 47 | "key encipherment", 48 | "client auth" 49 | ], 50 | "expiry": "8760h" 51 | }, 52 | 53 | "server": { 54 | "usages": [ 55 | "signing", 56 | "key encipherment", 57 | "server auth" 58 | ], 59 | "expiry": "87600h" 60 | } 61 | 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /system/socket/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define MAXLINE 4096 10 | 11 | int main(int argc, char** argv) 12 | { 13 | int sockfd, n; 14 | char *sendline; 15 | struct sockaddr_in servaddr; 16 | 17 | if( argc != 3){ 18 | printf("usage: ./client \n"); 19 | exit(0); 20 | } 21 | 22 | if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){ 23 | printf("create socket error: %s(errno: %d)\n", strerror(errno),errno); 24 | exit(0); 25 | } 26 | 27 | memset(&servaddr, 0, sizeof(servaddr)); 28 | servaddr.sin_family = AF_INET; 29 | servaddr.sin_port = htons(6666); 30 | if( inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0){ 31 | printf("inet_pton error for %s\n",argv[1]); 32 | exit(0); 33 | } 34 | 35 | if( connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0){ 36 | printf("connect error: %s(errno: %d)\n",strerror(errno),errno); 37 | exit(0); 38 | } 39 | 40 | printf("send msg to server: %s\n",argv[2]); 41 | sendline = argv[2]; 42 | if( send(sockfd, sendline, strlen(sendline), 0) < 0) 43 | { 44 | printf("send msg error: %s(errno: %d)\n", strerror(errno), errno); 45 | exit(0); 46 | } 47 | 48 | close(sockfd); 49 | exit(0); 50 | } 51 | -------------------------------------------------------------------------------- /system/socket/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define MAXLINE 4096 10 | 11 | int main(int argc, char** argv) 12 | { 13 | int listenfd, connfd; 14 | struct sockaddr_in servaddr; 15 | char buff[4096]; 16 | int n; 17 | 18 | if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ){ 19 | printf("create socket error: %s(errno: %d)\n",strerror(errno),errno); 20 | exit(0); 21 | } 22 | 23 | memset(&servaddr, 0, sizeof(servaddr)); 24 | servaddr.sin_family = AF_INET; 25 | servaddr.sin_addr.s_addr = htonl(INADDR_ANY); 26 | servaddr.sin_port = htons(6666); 27 | 28 | if( bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1){ 29 | printf("bind socket error: %s(errno: %d)\n",strerror(errno),errno); 30 | exit(0); 31 | } 32 | 33 | if( listen(listenfd, 10) == -1){ 34 | printf("listen socket error: %s(errno: %d)\n",strerror(errno),errno); 35 | exit(0); 36 | } 37 | 38 | printf("======waiting for client's request======\n"); 39 | while(1){ 40 | if( (connfd = accept(listenfd, (struct sockaddr*)NULL, NULL)) == -1){ 41 | printf("accept socket error: %s(errno: %d)",strerror(errno),errno); 42 | continue; 43 | } 44 | n = recv(connfd, buff, MAXLINE, 0); 45 | buff[n] = '\0'; 46 | printf("recv msg from client: %s\n", buff); 47 | close(connfd); 48 | } 49 | 50 | close(listenfd); 51 | } 52 | -------------------------------------------------------------------------------- /c/mcast/mcast.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2008, 2009, 2010, 2011, 2012 IBM CRL. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at: 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #ifndef MCAST_H 17 | #define MCAST_H 18 | #include 19 | 20 | #ifndef MCAST_GROUP_IP 21 | #define MCAST_GROUP_IP "239.0.0.1" 22 | #endif 23 | 24 | #ifndef MCAST_GROUP_PORT 25 | #define MCAST_GROUP_PORT 5000 26 | #endif 27 | 28 | /** 29 | * 224.0.0.* reserved 30 | * 224.0.1.* public multicast for internet 31 | * 224.0.2.0 ~ 238.255.255.255 temporary multicast for users 32 | * 239.*.*.* local multicast 33 | */ 34 | struct mcast_msg { 35 | unsigned long hdr; 36 | unsigned char *payload; 37 | }; 38 | 39 | struct mc_send_arg { 40 | unsigned long group_ip; //multicast group 41 | unsigned int port; //multicast port 42 | int len_msg; 43 | struct mcast_msg *msg; 44 | bool *stop; 45 | }; 46 | 47 | struct mc_recv_arg { 48 | unsigned long group_ip; //multicast group 49 | unsigned int port; 50 | bool *stop; 51 | }; 52 | 53 | void mc_send(struct mc_send_arg* arg); 54 | void mc_recv(struct mc_recv_arg* arg); 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /security/openssl/server.crt: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIEWDCCA0CgAwIBAgIJAN3oE/J/B0mBMA0GCSqGSIb3DQEBBQUAMGcxCzAJBgNV 3 | BAYTAlVTMQ4wDAYDVQQIDAVTdGF0ZTERMA8GA1UEBwwITG9jYXRpb24xEDAOBgNV 4 | BAoMB09yZ05hbWUxFjAUBgNVBAsMDUlUIERlcGFydG1lbnQxCzAJBgNVBAMMAmNh 5 | MB4XDTE4MDIwNzAzNDgxMFoXDTE4MDMwOTAzNDgxMFowUDELMAkGA1UEBhMCQ04x 6 | CzAJBgNVBAgMAkJKMQswCQYDVQQHDAJCSjEKMAgGA1UECgwBTzEKMAgGA1UECwwB 7 | TzEPMA0GA1UEAwwGc2VydmVyMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKC 8 | AQEAvfUqBdgXS4kjurzFdPFEx+InmIsszY1m5aoEAuaFTbqCN4Qxd3SlMl3i5et4 9 | nYdS0FxdzA48bcAZ/RCDun7H7klkl2v9qEd7/Uf/ptYLYkOKGlMmfqbQR04oIc1A 10 | Ft5L8dV2sLsLiaOOdZ+nHwAGuy/M10unuUFVMXg/oHP6gz+nimnyJc8cx9WiDm6g 11 | sG606cAsUkKsQZ/Fqw+Ih/A95NzeMyWHhK+mRkONaKuSpwwxPn54kGtV0TBnJCIP 12 | KzDzPjGnbM1ebJV/4jacU58II+lbNtkgnPBp8pj92Sqj34/QVYZaQVKhe/V5LImq 13 | 8g/2UIJ+bvIZU75tnJ4+3aJhzQIDAQABo4IBHDCCARgwgYEGA1UdIwR6MHiha6Rp 14 | MGcxCzAJBgNVBAYTAlVTMQ4wDAYDVQQIDAVTdGF0ZTERMA8GA1UEBwwITG9jYXRp 15 | b24xEDAOBgNVBAoMB09yZ05hbWUxFjAUBgNVBAsMDUlUIERlcGFydG1lbnQxCzAJ 16 | BgNVBAMMAmNhggkAngOLq1YrqZswCQYDVR0TBAIwADALBgNVHQ8EBAMCBPAwegYD 17 | VR0RBHMwcYIJbG9jYWxob3N0ggdvY2ktbGJyghNjbHVzdGVydHN0MDItYmNzYi0x 18 | ghNjbHVzdGVydHN0MDItYmNzYi0yghNjbHVzdGVydHN0MDItYmNzYi0zhwR/AAAB 19 | hwSB1QmRhwSB1T6BhwSB1TVLhwSB1TNJMA0GCSqGSIb3DQEBBQUAA4IBAQDO3Rof 20 | V2ONEVmPjk09fH28D7ppF7T1VVUQhC1G5SdtMwbJjgP1JbaC/IxQMzu71R1EseUf 21 | cDx7kdcqjxwMVtP3R+52cRklYroffLiYOdYtx4wLVfgrf95Fm77yJGiXkYP/P0GK 22 | nmMjxIeZicqVaohmFqQf1voXXQ/nhupkAVaGDe1wspD+Sii1jon38lEjaeA65xGo 23 | rHuF98WazQIUFPCWqs4zfK0h2/Hf9f7Y9ryZ7Aw1fmryKI+MqAyUzW0I8nbw28iR 24 | ci1sBpwENfgOkuZn/Tf/KLsAdcdau7a/L+aB1OiZHben2M1YKK9K65NAvf+pxx3/ 25 | BbLQ2JvSQSJPStmW 26 | -----END CERTIFICATE----- 27 | -------------------------------------------------------------------------------- /algorithm/bf-gdt/bf-gdt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2012 IBM CRL. 3 | * 4 | * This program is free software; you can redistribute it and/or 5 | * modify it under the terms of version 2 of the GNU General Public 6 | * License as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, but 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 | * General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License 14 | * along with this program; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 16 | * 02110-1301, USA 17 | */ 18 | 19 | #ifndef BF_GDT_H 20 | #define BF_GDT_H 21 | 22 | #include "bf.h" 23 | 24 | #ifndef BF_GDT_MAX_FILTERS 25 | #define BF_GDT_MAX_FILTERS 64 26 | #endif 27 | 28 | #ifndef LC_DP_DFT_ID 29 | #define LC_DP_DFT_ID 0 30 | #endif 31 | 32 | #ifndef LC_GROUP_DFT_ID 33 | #define LC_GROUP_DFT_ID 0 34 | #endif 35 | 36 | #ifndef LC_BF_DFT_PORT_NO 37 | #define LC_BF_DFT_PORT_NO 0 38 | #endif 39 | 40 | struct bf_gdt{ 41 | u32 gid; /*id of the group, dp should not care*/ 42 | u32 nbf; /*number of the bloom_filters*/ 43 | struct bloom_filter **bf_array; /*the bloom_filter array*/ 44 | }; 45 | 46 | struct bf_gdt *bf_gdt_init(u32 gid); 47 | int bf_gdt_destroy(struct bf_gdt *gdt); 48 | 49 | struct bloom_filter *bf_gdt_add_filter(struct bf_gdt *gdt, u16 port_no, u32 len); 50 | int bf_gdt_add_item(struct bf_gdt *gdt, u32 bf_id, const char *s); 51 | 52 | struct bloom_filter *bf_gdt_check(struct bf_gdt *gdt, const char *s); 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /python/tls_server/server.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | 3 | import socket 4 | from socket import AF_INET, SOCK_STREAM, SO_REUSEADDR, SOL_SOCKET, SHUT_RDWR 5 | import ssl 6 | 7 | listen_addr = '0.0.0.0' 8 | listen_port = 12001 9 | server_cert = 'server.crt' 10 | server_key = 'server.key' 11 | client_certs = 'client.crt' 12 | 13 | context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) 14 | context.verify_mode = ssl.CERT_REQUIRED 15 | context.load_cert_chain(certfile=server_cert, keyfile=server_key) 16 | context.load_verify_locations(cafile=client_certs) 17 | 18 | bindsocket = socket.socket() 19 | bindsocket.bind((listen_addr, listen_port)) 20 | bindsocket.listen(5) 21 | 22 | while True: 23 | try: 24 | print("Waiting for client") 25 | newsocket, fromaddr = bindsocket.accept() 26 | print("Client connected: {}:{}".format(fromaddr[0], fromaddr[1])) 27 | conn = context.wrap_socket(newsocket, server_side=True) 28 | print("SSL established. Peer: {}".format(conn.getpeercert())) 29 | buf = b'' # Buffer to hold received client data 30 | try: 31 | while True: 32 | data = conn.recv(4096) 33 | if data: 34 | # Client sent us data. Append to buffer 35 | buf += data 36 | else: 37 | # No more data from client. Show buffer and close connection. 38 | print("Received:", buf) 39 | break 40 | finally: 41 | print("Closing connection") 42 | conn.shutdown(socket.SHUT_RDWR) 43 | conn.close() 44 | except ssl.SSLEOFError as e: 45 | print(e) 46 | finally: 47 | print("Something wrong is received") 48 | -------------------------------------------------------------------------------- /python/pull_agent.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #Daemon script. Watch on the specific folder (e.g., /tmp/file), 3 | #Parse the msg and do processing when it changes. 4 | #Usage: ./pull_agent.py host_ip file 5 | 6 | import sys 7 | import os 8 | import string 9 | import datetime 10 | import urllib 11 | import simplejson as json 12 | 13 | #the user/ip of the remote host that needs to be monitored 14 | user="root" 15 | #remotehost="9.186.105.166" 16 | #remotefile="/tmp/cpu.dat" 17 | 18 | if len(sys.argv) < 3: 19 | sys.exit('Usage: %s host_ip file' % sys.argv[0]) 20 | 21 | remotehost=sys.argv[1] 22 | remotefile=sys.argv[2] 23 | 24 | localfile=remotehost+"_"+remotefile.replace("/","_") 25 | bakfile=localfile+".bak" 26 | tmpfile=localfile+".tmp" 27 | difffile=localfile+".diff" 28 | action="echo 'do some action'" 29 | 30 | tmp_path = os.path.split(os.path.realpath(__file__))[0]+"/tmp/" 31 | if not os.path.exists("tmp"): 32 | os.makedirs("tmp"); 33 | 34 | os.chdir("tmp") 35 | 36 | #cp remote file to local 37 | os.system("scp %s@%s:%s ./%s 2>&1 > /dev/null" %(user,remotehost,remotefile,tmpfile)) 38 | os.system("sort -n %s |uniq > %s" %(tmpfile,localfile)) 39 | os.system("cp %s %s" %(localfile,bakfile)) 40 | 41 | nLoop = 3 42 | while nLoop > 0: 43 | os.system("rm -f %s" %(difffile)) 44 | os.system("scp %s@%s:%s ./%s 2>&1 > /dev/null" %(user,remotehost,remotefile,tmpfile)) 45 | os.system("sort -n %s |uniq > %s" %(tmpfile,localfile)) 46 | os.system("diff %s %s > %s" %(localfile,bakfile,difffile)) 47 | f = file(difffile) 48 | line = f.readline(); 49 | if len(line) != 0: 50 | os.system("%s" %(action)) 51 | f.close() 52 | os.system("cp %s %s" %(localfile,bakfile)) 53 | os.system("echo ...") 54 | os.system("sleep 2") 55 | nLoop = nLoop - 1 56 | -------------------------------------------------------------------------------- /security/openssl/ca.key: -------------------------------------------------------------------------------- 1 | -----BEGIN PRIVATE KEY----- 2 | MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDRcOpanlt44HC5 3 | MWNHLIJ7PIRJKZl85K0UkWcHJPj3kyi63yEAIK9b4vuXA97z5M5M0hu6ZdGLav2g 4 | Ph94L4dbZLP5v9pPbLAkM2BSp0lbQZgPinq61UpZ4YeVJBZSV/JP+6aifjfRDDbH 5 | +CPhBFlGTurkD5Mfr2uhWwV/PRQtA1fRHgQ1Zr26oFTNr1L0p2zVl5w9yMLmmjYr 6 | P/jDcYovkhNY+BScEspb5oY1FMt5i4cz8wmBVFG346m+x3XQe1J0m77rig7o/oQ7 7 | tHWG3WoZNXCbKSwkR/RzIaKwKhN78VmRUhV+DO7+CGqTj1YGtA5Jap0gz0BIr45d 8 | E/f2vZgpAgMBAAECggEAeC1HfAnjyN9YLxuksMFceaRG78g790sr+WJZcOMJLN42 9 | /8tqny0iEiKTZJrnAqGmsrPZD0K/UzVoJNugqRD0pT+KeeNZPDvMcmk8exDcac2g 10 | mEJInA4pJJvhdXWIjWVDeBuZCSJNmtlcUbATvlkFdO9xnr1FGWhJ5QNaiyEcA5se 11 | Lzm5I5jXZceAHJ0G8dHZ0mXe1C95x0Cx8q7tdOU6E9QkfeoLNs2rBnEzKSo9MY+T 12 | 72EKkZJXyKtlXIj2VuzPxu63ifjKKULUcbEFGnrfkc0zjsmTBTvrbvmfMqTokUT8 13 | 0moJEiPsqWNyxAzUsJx+E/zw4FLK+7iPkXKUR5tSpQKBgQD27hRYaPz4DtuP/0q9 14 | vBy6f9+EQ5SG5izrmt4WPzrmAhGIx4jEkF2t8KSJw1ZOEMOgqrPDBQaQTpCsnblv 15 | +X9v3bfa8IeVM2OErUW3m1rgfGmJEWj4Kizj88kkaQrSPAtwwGGDKrI4xKqC6EnL 16 | knzuvRB52AD/xofQmi3779DHzwKBgQDZIlJoaiVA7ps8fyv+iRq4g7Mkmub9HWdX 17 | Danf0mTmMNKeT01VxZ+8fgSayWclVzZE1BxOFeJIC5wFQ8H7kx4tUzZhxEmz1tVc 18 | o9idenMJSpUl/3lL2/7/uySvrAfO3DXl7w67l5SHuB3rUkIVl3NDhDCNFGAw+8vw 19 | UIXV8aymhwKBgQCsrowN08Xslz9WcRrMmuugROKDtVr+B6OsVA4xkqBWz2WBuMBd 20 | bfQFOv9l0/Pb/f9AsX+qxy3y5u5nuJlAxEMpcO0tL/4xCdHzq9TQdNVVncuT7lfx 21 | DFV+SDMGwmqt4Q3DGNZYixg9eNMhXt6UfWne7trBypvQQv8hk2DLCceAowKBgElh 22 | NABdfuGGwaecQr3uj70zpOMGBnIb8cSVtYcSj07LySH/It+NvgHxhJTa46TW6Fwy 23 | Yq5w7tvyMHQHBhODEHu413QK5e2E+O4p4Ra3PxEwx17kC5X7bx31nNxRgwIBoaVn 24 | OM9k1qlXK6VPU04AtM9oaRAEhs2HCtDYJaK73eEZAoGAW0s5Raxi/K+HCQPO9f0O 25 | N2FBs1Jbnq1AeDNLT66IKg97wcR6/M+zhP8PRQoZ3N6t0L1KTcg3kw1KqeLUsKfw 26 | yBxuhtxzox+lOv9+/IB8U4rFaNl/nNpOTJXDeFumqYH7iy6RpGGdT+uSBM1aLM00 27 | ZknvRzYiE+oYbn1gcnbm3fI= 28 | -----END PRIVATE KEY----- 29 | -------------------------------------------------------------------------------- /python/tls_server/client.key: -------------------------------------------------------------------------------- 1 | -----BEGIN PRIVATE KEY----- 2 | MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDIqvLKLjTxsZv1 3 | lGs0tjMsuc4m9Dpeo/QX9W3SUV/KBHpYpr3BEOWzGy32rEHRvPCVVNCXkRYx6pK6 4 | ujaSpo7GttKwyKwQvEk8FlLbquW3glj9o9aGaAPU/xv0b/6E1JBlfKujqm98BfDN 5 | ZD9U3TKf+ZRfJ2qjPqpfvUlJ9mdttFOavRCO+2ySIgm+WoYT1ruUenSW89Omie/x 6 | 5uEcsbr7M6doQeUcO1xcyPwSzhAI8qk8Zyajx2xtwl2Rq9rZl6uKIGtrY1XYeLxi 7 | +kPkQGMQUJ+gv1ED5MhztBrKK3TPfS4C3r9oAd7gpOAxgcpdufWhAPUTnC3tqAlK 8 | wSoy/h83AgMBAAECggEBAMB5YAnXikbt0icbJj1YKfvTeM5aYgZXApFcgutBzDxM 9 | UUHpJYci+v/xqQO6tCNGNAk4Yh+PNZfJZNYgOKovgjFOOQcCZDBlOWWeD/nNkSuU 10 | pmEbHnCrIknSLHEoXxvve1Us4MFKgEaYuCF571kqFh3l2Y/vkdRlDixotfxwkUJi 11 | CQlWKPBrooZDI1ITD/x5rGdA2Y2rPl4Kd5DN/vFh9PWsM6UFPDiEkWsO+0s3OQ3D 12 | wwCbYSqRjt+LKXfq7xdjAiYPlvnJOYAFiibBYQkLNFbEOQv5q0k/XzlbRA39dyif 13 | EenRAgQcaKEYJ/9u/f6z5pqCAuNyigwigVT8Fe9SmWkCgYEA+h9V+pVVX7XXmeXv 14 | H7sjSMk+T6oSbANd+mwXFrYM8L+/+f1SBQp5sEg5WiNS4ffITgvtZ3PqLyjqB2GW 15 | MGbwLij6vwjtCME3gs4LrCMnQyoGlyticCxLAKgOAsI9Iiu3mRykU2pht8wRKJvK 16 | WB4SlIZsM0YGXC3C7lpxVeuptpUCgYEAzWIbk7CxIsAge+MH5Y5n9y2e8ojRBFyH 17 | /IfDoZjmA2xtRBtLz3ql6yPcF4X8US4UrvVuZdMmvNOfNTmRWuUQ51ZAEi7MP+Yq 18 | PC7RCR0yPO6wPJfvasw2HN/ui68pOf0ZxRCinvURTa1hnfymA6t/XbB+nxuWdOor 19 | SMnrPDskh5sCgYBhidjImHWrlkkWneErWZn/52KiRGtcrc93ObGjTKktNUmTHmNe 20 | zd/4i6G58e5/alY1gVc6MazQaT8iU77y7yGYjqiB2OQ+tsKEAv/wR+EmwvvA9c00 21 | e0FgZyjXkJGwgqXdUpsOYHzs5OqIEIDO7iNGwkjtkdAnID1VH/X0kKslJQKBgQCC 22 | 73nvRSUYp23OLWXaZlhHj/EWZD0Ez8zuJMuGmUiChDyAv/or0uA2MU1ePdAnHP6Y 23 | r+VqyxuWJEAKmaHeVczZ/vWn4efbEOx6I8qmSdO+XtlCOAatinUxIV4d9VSOIJqu 24 | NLXIKyqV3+Qs6WYZTttLKnr/CoJ4FNX1eK0cYAQg4wKBgB/uHZc7WoTZUsjs2Y07 25 | 8JbrFhDF0/SqbxwrEjXMcYspSfRiOjwKOEMftnT3cGYrqrXqheRBtibJsOenmE53 26 | xd6EIIJBCKeofcVCRUE06KfloGYugQkGg7lJSPB4OwI9YmMBmXVR3Ak6UB7PzI8X 27 | AruLe7x/YwQ1sWCthxIgRA+e 28 | -----END PRIVATE KEY----- 29 | -------------------------------------------------------------------------------- /python/tls_server/server.key: -------------------------------------------------------------------------------- 1 | -----BEGIN PRIVATE KEY----- 2 | MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDAwzylbX0vVuQO 3 | CJjnU+z5RMw10483+VMkPzUy/iJtRhQCzRJmyYaINcN40X9rCLqVsDAyMMgfKakg 4 | uSPOiD/no+kxpFM4A5ml9ZMzYsLqjWZ2PTSP/GLtqBYfTRYWX+J0FJjyecrJ1YU0 5 | IUkwQ4VSxptyjao29ADRP0h8e+EdYTDZPqh7tzEzZNBpLYVubsgZygJks1ICKhoM 6 | FzZ5BmoN0I97nftgYFbG2/DpzT2ENFEoezdWXx2ZYgVX9jCW5BaohFIGMIeW7CpJ 7 | hCObSx9SspvvSG5B4OWTW88FkrvZ2CJGd3dxYWAtjaLbaeYdhHyeQ5MJEivysSi9 8 | XJQzt2P7AgMBAAECggEBAKM/9cmoW3/PF98mutAXDLfi4wum+0ENX7Nv8I8aGW/w 9 | DJx+rtnsTIpo9f4Jdt7gCp0hFQA8vg9eSgUTptaQH4b/E0xefbgPnH2n6B15w9e/ 10 | Hwp+NVjIB8R8ORgTQeR7Gh7zHsEkEnEy6tfGZOSCGpSK7TVr8r8NkO+lp679fx5Q 11 | K+P9Fkou6ggsXwO01OIf0H7xERfYFjPBFkH8fBFY5LO+hweA/6rciQexD8mNrTYj 12 | dj/g/8os1ao2l+IBmAUp10kgY+F9W9NxJJCK87jLfDZv4wP3aCjEQZYysegoWnks 13 | bfTEJCjNVwa/s7pPVpl1Sx02tsQUov6FArZSwsOAX4kCgYEA56qH+L3KmifGBHfR 14 | O+y2SxZubqIKBGs3Z+dQnyHD52J63bCCGsU3dXJN6W+QKPToZvWxvO8u52zW6mmb 15 | 5iPSDoEjhYqAByoDBPzMqBkS05tWrstwvFdSyK1YS8xMerKyQTNrJI9usHMp1Xg5 16 | aKfGPabNMj9wQEQ+epIlFSZRzP8CgYEA1QKYshRAuk53Jd12phAvIkei9T0N3u9n 17 | ENSdMxeH7R/RpMI63yUFtPHJBOV29ROc5/lBgKlH1j3BalNVZaBsqHmglUYTAaaj 18 | oynNqX6+72spkjGxCJh3jC3LbP9XiepGQeRBWQuEVPJ33ScHsbNT8mFxBMNAcnx7 19 | coeH8wianQUCgYAuDIy4melC2TMYQkTZ94SIym2rGhSJqlI6GhXBe/G39bBcjrBd 20 | 5+PNOapZA28RZRUpGh95YcXeBJl3wQRXK7meXncUHWKzidtNy35Wz50ZaOBDIHdJ 21 | SMZQ03wgnPx6yzh5k2Hxw30KH+op35rxvZ+/M3/IRCno7GfK72/lk1dhOwKBgG/t 22 | llj0xXJgasFN1tu541qK8/kTZOeBOtIgNp4zPix+rMV7fj3lEVOcPTnCbbin3qoS 23 | UrYNvX65IpoBQf1Nx+rbRmNJ2BDlBFVnUPdAb4oU8FounEzc+OyC38LdhZ0lqldT 24 | Xncw0w9j/VILAUMB7RmO1Dlhc8r02FU9HFW7bqsVAoGAFTNPhbcjs5UI8C5BU1fq 25 | zeMDmEQhZMiorQEYZHvA1NXO2C8s3baywzQdeml69mkTQwjOyBMacOOAUPnt+YRH 26 | HmcN8Gxw7KuqqDmdO1c2EiMDAJ7y12RzGUDhbPKLt6AxmglTgzdBuhmeoLBMSiAd 27 | ciQzpi+ZN6FAQiKwiUvxbII= 28 | -----END PRIVATE KEY----- 29 | -------------------------------------------------------------------------------- /security/openssl/server.key: -------------------------------------------------------------------------------- 1 | -----BEGIN PRIVATE KEY----- 2 | MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC99SoF2BdLiSO6 3 | vMV08UTH4ieYiyzNjWblqgQC5oVNuoI3hDF3dKUyXeLl63idh1LQXF3MDjxtwBn9 4 | EIO6fsfuSWSXa/2oR3v9R/+m1gtiQ4oaUyZ+ptBHTighzUAW3kvx1XawuwuJo451 5 | n6cfAAa7L8zXS6e5QVUxeD+gc/qDP6eKafIlzxzH1aIObqCwbrTpwCxSQqxBn8Wr 6 | D4iH8D3k3N4zJYeEr6ZGQ41oq5KnDDE+fniQa1XRMGckIg8rMPM+MadszV5slX/i 7 | NpxTnwgj6Vs22SCc8GnymP3ZKqPfj9BVhlpBUqF79XksiaryD/ZQgn5u8hlTvm2c 8 | nj7domHNAgMBAAECggEBAL2I1KA2J7Avqt3VIrP3VCiYx+dV3qNOVoALbqsyNYoD 9 | 8Y/RL8ZANeCWeyHsHFIZxxCtSM2k2rirN4Mgqj835uckheDsWJFCmj1zTGu8IGmw 10 | eCiiFMPEUAcFvddUQ4FN1rr6wc2/I9j2v9svLIIq9YxqOloLW9Plk0qj7+B6OFgh 11 | PcU9oSceSpTbRodqCTTrIy1+PT6ntazZKTpHcoNGzNANExqb+7mzGWUXCW/bFEFf 12 | 6krbI13rIf8vxwcjLzGn4eMOz78OTVByfd3ynoK3Pw+Eeqv+JTG4FO/nBelidffj 13 | ng8OM9/Mj3VrbFGrKz7GbQ63eBrzCu2zKzQR4/wEowUCgYEA9WyNXrznCqfiASXG 14 | xxlUecGuYjuPnKU7eQq8VD+k7WyPrfJ6xW0XyWZTNlsIOGufBdDgNpV+H+gguP05 15 | ltnXl05dakOZSg2sH89KWDLbrG30T2vzrx6ydxUm+sKq/kQAdzOvf6KZAV5hUoNK 16 | Ne1hffk9wop0+UmKM2l6WNTmNYsCgYEAxiS5D+ar3FJUTkgqYedbV/C+COljcS8Z 17 | jzeaNrXyG9qmAWdyovhITbeqGL1zavh0EKqwawsg40PuYBLE2c+63Z9X8/Xf9VqY 18 | H70LIyfAj1lxGt72/j8Z6MvCCJk2T7ePn1buqi1I8Afc+CrSXE++UHuMwd1z/f0s 19 | wA+4vklQIQcCgYA0C6MnNhQg9F0/NQ2kZ0C82U/r0QmxhHDKSHaLvztwhhcqIkPa 20 | 3jmvIh3/ZuqlXF1K1HLX4HfwuD9IO5sc3HGVyq6QvGkjhSa9UC5J6e8f7+lhdlkl 21 | B/N8wWWUw/eTGESpArLy9D5SRfuJjgIM0ZxdJJ/uQ4Ju4yOFp5akg/GV5QKBgD20 22 | R/Fcu150QQ7TyGcOvlJnhTaPP8mKimIgRaUOCiSEdbfwODHqMdIKBS2JKf1A/BwI 23 | m8HqCEbd68j9b1IJL78+lQxDIIhuuvr/Sw2anovFpcRhdPd+PRpTAczdsZ0no9FS 24 | KCbUzWb6e+TIqRPYPYMgxpfE2A9bnWj+PGD9wbdXAoGARdJamaPm/+gK8DGIju5t 25 | EAG7v99f2e8NHlBz5YaLLqlhMLzEgs35nqwiHDbDI7Em4+dVuhvL4tN9lEL0GCTN 26 | WARHlU4UQKxMVrqEYA9P0gdxVeSPw6TKraDIaco45TwkfAVSpeLwOtX3cXuZvEFS 27 | q9Vx61OW3zWUZvE7uGPTs84= 28 | -----END PRIVATE KEY----- 29 | -------------------------------------------------------------------------------- /algorithm/bf-gdt/bf.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2012 IBM CRL. 3 | * 4 | * This program is free software; you can redistribute it and/or 5 | * modify it under the terms of version 2 of the GNU General Public 6 | * License as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, but 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 | * General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License 14 | * along with this program; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 16 | * 02110-1301, USA 17 | */ 18 | 19 | #ifndef BF_H 20 | #define BF_H 21 | 22 | #ifndef SIZE_CHAR 23 | #define SIZE_CHAR 8 24 | #endif 25 | 26 | /* only for userspace compatibility */ 27 | #ifndef __KERNEL__ 28 | typedef unsigned int u32; 29 | typedef unsigned short u16; 30 | typedef unsigned char u8; 31 | #endif 32 | 33 | typedef unsigned int (*hashfunc_t)(const char *); 34 | 35 | struct bloom_filter{ 36 | u32 bf_id; /*id*/ 37 | u32 len; /*length of the bit array*/ 38 | u16 port_no; /*how to reach the switch*/ 39 | u8 *array; /*the bit array*/ 40 | u32 nfuncs; /*number of hash functions*/ 41 | hashfunc_t *funcs; /*hash functions array*/ 42 | }; 43 | 44 | u32 sax_hash(const char *key); 45 | u32 sdbm_hash(const char *key); 46 | 47 | struct bloom_filter *bf_create(u32 bf_id, u32 len, u16 port_no, u32 nfuncs); 48 | int bf_destroy(struct bloom_filter *bf); 49 | int bf_add(struct bloom_filter *bf, const char *s); 50 | int bf_check(struct bloom_filter *bf, const char *s); 51 | int bf_set_array(struct bloom_filter *bf, const u8 *array, u32 len); 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /python/remote_watcher.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # Daemon script. 3 | # Watch the specified file on remote host, 4 | # trigger action when there finds some keyword in it. 5 | 6 | import sys 7 | sys.path.append("..") 8 | 9 | import os 10 | import string 11 | import datetime 12 | import urllib 13 | import simplejson as json 14 | import time 15 | 16 | remote_ip="10.0.1.2" 17 | remote_file="/var/log/message" 18 | 19 | cache_file="cache" 20 | cache_file_bk="cache.bak" 21 | 22 | GMT_FORMAT= '[%Y-%m-%d %H:%M:%S] ' 23 | keyword = '[*event*]' 24 | nLoop = 500000 25 | nEvent = 0 26 | 27 | work_path = os.path.split(os.path.realpath(__file__))[0]+"/" 28 | os.system("cd "+work_path) 29 | 30 | print "Get remote file (Need automatical auth first)." 31 | os.system("scp root@%s:%s %s 2>&1 > /dev/null" %(remote_ip,remote_file,cache_file)) 32 | os.system("cp %s %s" %(cache_file,cache_file_bk)) 33 | 34 | def trigger_action(line): 35 | """ 36 | Do some process when an event happens 37 | @param line: The line triggering the event 38 | """ 39 | titlelist = line.split(' ') 40 | pass 41 | 42 | def main(): 43 | #Recursively obtain and parse the log msg, to find alert. 44 | while nLoop > 0: 45 | os.system("rm -f diff.txt 2>&1 > /dev/null") 46 | os.system("scp root@%s:%s %s 2>&1 > /dev/null" %(remote_ip,remote_file,cache_file)) 47 | os.system("diff %s %s >> diff.txt" %(cache_file,cache_file_bk)) 48 | f = open("diff.txt") 49 | for line in f: 50 | if len(line) == 0: 51 | break 52 | if line.find(keyword) > 0: 53 | nEvent = nEvent + 1 54 | trigger_action(line) 55 | 56 | os.system("cp %s %s" %(cache_file,cache_file_bk)) 57 | os.system("sleep 2") 58 | nLoop = nLoop - 1 59 | print "%d events triggered" % nEvent 60 | 61 | if __name__ == '__main__': 62 | main() 63 | -------------------------------------------------------------------------------- /python/json_flatter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*-coding:utf-8-*- 3 | 4 | import json 5 | import os 6 | import sys 7 | 8 | 9 | # Print the tree recursively with given path prefix 10 | def check_tree(tree, prefix, f_write): 11 | if isinstance(tree, dict): 12 | for k, v in tree.items(): 13 | prefix_path = prefix + "." + k 14 | if isinstance(v, dict) or isinstance(v, list): # continue sub-tree 15 | check_tree(v, prefix_path, f_write) 16 | else: # leaf 17 | f_write.write("{}={}\n".format(prefix_path, v)) 18 | elif isinstance(tree, list): 19 | for i, v in enumerate(tree): 20 | prefix_path = "{}[{}]".format(prefix, i) 21 | if isinstance(v, dict) or isinstance(v, list): # continue sub-tree 22 | check_tree(v, prefix_path, f_write) 23 | else: # leaf 24 | f_write.write("{}={}\n".format(prefix_path, v)) 25 | else: # json only allow dict or list structure 26 | print("Wrong format of json tree") 27 | 28 | 29 | # Process all json files under the path 30 | def process(directory): 31 | for f in os.listdir(directory): 32 | if f.endswith(".block.json"): 33 | file_name = os.path.join(json_dir, f) 34 | f_read = open(file=file_name, mode="r", encoding='utf-8') 35 | f_write = open(file=file_name+"-flat.json", mode="w", encoding='utf-8') 36 | check_tree(json.load(f_read), "", f_write) 37 | f_read.close() 38 | f_write.close() 39 | else: 40 | print("Ignore non-json file {}".format(f)) 41 | 42 | 43 | # Usage python json_flatter.py [path_containing_json_files] 44 | # Print all json elements in flat structure 45 | # e.g., 46 | # { 47 | # "a": { 48 | # "b": ["c", "d"] 49 | # } 50 | # } 51 | # ==> 52 | # a.b[0]=c 53 | # a.b[1]=d 54 | if __name__ == '__main__': 55 | json_dir = "../raft/channel-artifacts/" 56 | if len(sys.argv) > 1: 57 | json_dir = sys.argv[1] 58 | 59 | print("Will process json files under {}".format(json_dir)) 60 | process(json_dir) 61 | -------------------------------------------------------------------------------- /algorithm/match.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | """ 3 | Pattern matching algorithm testing. 4 | """ 5 | 6 | def kmpGetNext(P): 7 | """ Generate the overlap function/next array with the given pattern. 8 | @P: Matching pattern 9 | @return: Next array 10 | """ 11 | n,i,j =[-1 for j in range(len(P))],0,-1 12 | while i < len(P): 13 | while j>=0 and P[i]!=P[j]: 14 | j = n[j] 15 | i+=1 16 | j+=1 17 | n[i] = j 18 | 19 | 20 | def BF(P,T): 21 | """ Brute force searching. 22 | @P: Pattern 23 | @T: Text to examine 24 | """ 25 | collector = [] 26 | for i in range(len(T)): 27 | for j in range(len(P)): 28 | if T[i+j] != P[j]: 29 | break 30 | if j == len(P)-1 and T[i+j] == P[j]: #matching 31 | #print P,"match",T[i:] 32 | collector.append(T[i:]) 33 | return collector 34 | 35 | def kmpSearch(P,T): 36 | """ KMP searching. 37 | @P: Pattern 38 | @T: Text to examine 39 | """ 40 | n = kmpGetNext(P) #generate the next array 41 | collector = [] 42 | for i in range(len(T)): 43 | j = 0 44 | while j < len(P): 45 | if T[i+j] != P[j]: 46 | j = n[j] 47 | i += 1 48 | j += 1 49 | if j == len(P): 50 | collector.append(T[i-j:]) 51 | print T[i-j:] 52 | return collector 53 | 54 | def validate(alg,P,T): 55 | """Validate if the list is sorted correctly. 56 | @L: List to validate 57 | @return: True or False 58 | """ 59 | if eval(alg+'(P,T)') == BF(P,T): 60 | return True 61 | else: 62 | return False 63 | 64 | #The main function starts here. 65 | if __name__ == "__main__": 66 | P = "abc" 67 | T = "abcdabcabd" 68 | print "Brute Force searching" 69 | print BF(P,T) 70 | #print "KMP searching" 71 | #print kmpSearch(P,T) 72 | print validate('BF',P,T) 73 | 74 | -------------------------------------------------------------------------------- /system/.bash_aliases: -------------------------------------------------------------------------------- 1 | # some more ls aliases 2 | alias ll='ls -alF' 3 | alias la='ls -A' 4 | alias l='ls -CF' 5 | alias v='vim -O' 6 | alias ping='ping -n' 7 | alias pa='ps aux|grep' 8 | alias sinstall='sudo apt-fast install -y' 9 | alias supdate='sudo apt-fast update; sudo apt-fast upgrade' 10 | alias rm='/usr/local/bin/rm.sh' 11 | alias sgw='ssh -X baohua@9.186.105.181' 12 | alias sn='ssh -X root@9.186.105.54' 13 | alias st='ssh baohua@server2.verkstad.net' 14 | alias so='ssh -X baohua@9.186.105.154' 15 | alias snfs='ssh -X root@9.186.105.78' 16 | alias sx='ssh -X root@9.186.105.81' 17 | 18 | # Openstack Aliases 19 | alias novh='nova hypervisor-list' 20 | alias novm='nova-manage service list' 21 | alias vsh="sudo virsh list" 22 | alias prof="vi ~/.bash_profile" 23 | alias nmap="nmap -sT " 24 | alias src="source ~/.bashrc" 25 | alias lsof6='lsof -P -iTCP -sTCP:LISTEN | grep 66' 26 | alias vsh="sudo virsh list" 27 | alias ns="sudo ip netns exec " 28 | alias ipt="sudo iptables --line-numbers -vnL" 29 | 30 | # OVS Aliases 31 | alias ovapd="sudo ovs-appctl bridge/dump-flows " 32 | alias ovaps="sudo ovs-appctl fdb/show " 33 | alias ovaf='sudo ovs-ofctl add-flow' 34 | alias ovdpd=" sudo ovs-dpctl dump-flows " 35 | alias ovdps='sudo ovs-dpctl show' 36 | alias ovofd="sudo ovs-ofctl dump-flows" 37 | alias ovofs="sudo ovs-ofctl show" 38 | alias ovs='sudo ovs-vsctl show' 39 | alias ovsd='sudo ovsdb-client dump' 40 | alias logs="sudo journalctl -n 300 --no-pager" 41 | alias ologs="tail -n 300 /var/log/openvswitch/ovs-vswitchd.log" 42 | alias ovtun="sudo ovs-ofctl dump-flows br-tun" 43 | alias ovint="sudo ovs-ofctl dump-flows br-int" 44 | alias dfl="sudo ovs-ofctl -O OpenFlow13 del-flows " 45 | alias ovls="sudo ovs-ofctl -O OpenFlow13 dump-flows br-int" 46 | alias ofport=" sudo ovs-ofctl -O OpenFlow13 dump-ports br-int" 47 | alias del=" sudo ovs-ofctl -O OpenFlow13 del-flows " 48 | alias ovdelm=" sudo ovs-vsctl del-manager" 49 | alias ovaddm=" sudo ovs-vsctl set-manager tcp:172.16.58.1:6640" 50 | alias ovstart='sudo /usr/share/openvswitch/scripts/ovs-ctl start' 51 | alias ovsm="ovs_mon.sh " 52 | -------------------------------------------------------------------------------- /algorithm/bf-gdt/main.c: -------------------------------------------------------------------------------- 1 | #ifdef __KERNEL__ 2 | #include 3 | #include 4 | #include 5 | #else 6 | #include 7 | #include 8 | #endif 9 | 10 | #include "bf-gdt.h" 11 | 12 | #ifdef __KERNEL__ 13 | int __init start(void) 14 | #else 15 | int main(void) 16 | #endif 17 | { 18 | struct bf_gdt *gdt; 19 | 20 | if(!(gdt=bf_gdt_init(0))) { 21 | #ifdef __KERNEL__ 22 | printk("ERROR: Could not create bf-gdt\n"); 23 | #else 24 | printf("ERROR: Could not create bf-gdt\n"); 25 | #endif 26 | return -1; 27 | } else { 28 | #ifdef __KERNEL__ 29 | printk("created bf-gdt.\n"); 30 | #else 31 | printf("created bf-gdt.\n"); 32 | #endif 33 | } 34 | 35 | bf_gdt_add_filter(gdt,0,1024); 36 | bf_gdt_add_filter(gdt,1,2048); 37 | #ifdef __KERNEL__ 38 | printk("add bf into bf-gdt.\n"); 39 | #else 40 | printf("add bf into bf-gdt.\n"); 41 | #endif 42 | 43 | unsigned char q1[] = {0x0a,0x00,0x27,0x00,0x00,0x01}; 44 | unsigned char q2[] = {0x08,0x00,0x27,0xab,0xb6,0xa5}; 45 | unsigned char p1[13]={0}; 46 | unsigned char p2[13]={0}; 47 | sprintf(p1,"%02x%02x%02x%02x%02x%02x",q1[0],q1[1],q1[2],q1[3],q1[4],q1[5]); 48 | sprintf(p2,"%02x%02x%02x%02x%02x%02x",q2[0],q2[1],q2[2],q2[3],q2[4],q2[5]); 49 | bf_gdt_add_item(gdt,0, p2); 50 | 51 | struct bloom_filter *result = bf_gdt_check(gdt,p2); 52 | if(!result) { 53 | #ifdef __KERNEL__ 54 | printk("No match word \"%s\"\n", p2); 55 | #else 56 | printf("len=%u,No match word \"%s\"\n",strlen(p2), p2); 57 | #endif 58 | } else { 59 | #ifdef __KERNEL__ 60 | printk("[Match] word \"%s\" in bf %u\n", p2,result->bf_id); 61 | #else 62 | printf("[Match] word \"%s\" in bf %u\n", p2,result->bf_id); 63 | #endif 64 | } 65 | 66 | bf_gdt_destroy(gdt); 67 | return 0; 68 | } 69 | 70 | #ifdef __KERNEL__ 71 | void __exit end(void) 72 | { 73 | printk("Unload module successful.\n"); 74 | return; 75 | } 76 | 77 | MODULE_LICENSE("GPL"); 78 | MODULE_AUTHOR("author"); 79 | 80 | module_init(start); 81 | module_exit(end); 82 | #endif 83 | -------------------------------------------------------------------------------- /golang/grpc/src/hello/client/client.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "crypto/tls" 6 | "crypto/x509" 7 | "fmt" 8 | "google.golang.org/grpc/credentials" 9 | "io/ioutil" 10 | "log" 11 | "os" 12 | "time" 13 | 14 | "google.golang.org/grpc" 15 | pb "hello/hello" 16 | ) 17 | 18 | const ( 19 | defaultAddress = "nginx1:7050" 20 | defaultName = "client" 21 | serverCert = "/go/src/hello/server1.crt" 22 | ) 23 | 24 | func loadTLSCredentials(caFile string) (credentials.TransportCredentials, error) { 25 | // Load certificate of the CA who signed server's certificate 26 | pemServerCA, err := ioutil.ReadFile(caFile) 27 | if err != nil { 28 | return nil, err 29 | } 30 | 31 | certPool := x509.NewCertPool() 32 | if !certPool.AppendCertsFromPEM(pemServerCA) { 33 | return nil, fmt.Errorf("failed to add server CA's certificate") 34 | } 35 | 36 | // Create the credentials and return it 37 | config := &tls.Config{ 38 | RootCAs: certPool, 39 | InsecureSkipVerify: true, 40 | } 41 | 42 | return credentials.NewTLS(config), nil 43 | } 44 | 45 | func main() { 46 | // Set up a connection to the server. 47 | address := defaultAddress 48 | if len(os.Args) > 1 { 49 | address = os.Args[1] 50 | } 51 | tlsCredentials, err := loadTLSCredentials(serverCert) 52 | if err != nil { 53 | log.Fatal("cannot load TLS credentials: ", err) 54 | } 55 | conn, err := grpc.Dial(address, grpc.WithTransportCredentials(tlsCredentials)) 56 | // non-tls 57 | // conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock()) 58 | 59 | if err != nil { 60 | log.Fatalf("did not connect: %v", err) 61 | } 62 | defer conn.Close() 63 | c := pb.NewGreeterClient(conn) 64 | 65 | // Contact the server and print out its response. 66 | name := defaultName 67 | if len(os.Args) > 2 { 68 | name = os.Args[1] 69 | } 70 | ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) 71 | defer cancel() 72 | r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name}) 73 | if err != nil { 74 | log.Fatalf("could not greet: %v", err) 75 | } 76 | log.Printf("Greeting from server: %s", r.GetMessage()) 77 | } 78 | -------------------------------------------------------------------------------- /shell/functions: -------------------------------------------------------------------------------- 1 | # Some useful functions for shell 2 | # yeasy@github 3 | # 4 | 5 | 6 | # colorfully output string 7 | echo_r () { 8 | [ $# -ne 1 ] && return 0 9 | echo -e "\033[31m$1\033[0m" 10 | } 11 | echo_g () { 12 | [ $# -ne 1 ] && return 0 13 | echo -e "\033[32m$1\033[0m" 14 | } 15 | echo_y () { 16 | [ $# -ne 1 ] && return 0 17 | echo -e "\033[33m$1\033[0m" 18 | } 19 | echo_b () { 20 | [ $# -ne 1 ] && return 0 21 | echo -e "\033[34m$1\033[0m" 22 | } 23 | 24 | # check if a command existed 25 | command_exists() { 26 | command -v "$@" > /dev/null 2>&1 27 | } 28 | 29 | # perform some very rudimentary platform detection 30 | os_detect () { 31 | lsb_dist='' 32 | if command_exists lsb_release; then 33 | lsb_dist="$(lsb_release -si)" 34 | fi 35 | if [ -z "$lsb_dist" ] && [ -r /etc/lsb-release ]; then 36 | lsb_dist="$(. /etc/lsb-release && echo "$DISTRIB_ID")" 37 | fi 38 | if [ -z "$lsb_dist" ] && [ -r /etc/debian_version ]; then 39 | lsb_dist='debian' 40 | fi 41 | if [ -z "$lsb_dist" ] && [ -r /etc/fedora-release ]; then 42 | lsb_dist='fedora' 43 | fi 44 | if [ -z "$lsb_dist" ] && [ -r /etc/centos-release ]; then 45 | lsb_dist='centos' 46 | fi 47 | if [ -z "$lsb_dist" ] && [ -r /etc/os-release ]; then 48 | lsb_dist="$(. /etc/os-release && echo "$ID")" 49 | fi 50 | 51 | lsb_dist="$(echo "$lsb_dist" | tr '[:upper:]' '[:lower:]')" 52 | case "$lsb_dist" in 53 | amzn|fedora|centos) 54 | if [ "$lsb_dist" = 'amzn' ]; then 55 | ( 56 | set -x 57 | echo "amzn" 58 | ) 59 | else 60 | ( 61 | set -x 62 | echo "fedora|centos" 63 | ) 64 | fi 65 | exit 0 66 | ;; 67 | 68 | 'opensuse project'|opensuse|'suse linux'|sled) 69 | ( 70 | set -x 71 | echo "opensusue" 72 | ) 73 | exit 0 74 | ;; 75 | 76 | ubuntu|debian|linuxmint|'elementary os'|kali) 77 | export DEBIAN_FRONTEND=noninteractive 78 | 79 | echo "ubuntu|debian" 80 | 81 | exit 0 82 | ;; 83 | 84 | gentoo) 85 | ( 86 | set -x 87 | echo "gentoo" 88 | ) 89 | exit 0 90 | ;; 91 | esac 92 | } 93 | -------------------------------------------------------------------------------- /web/getweb.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # This simple script can mirror a website to local directory. 3 | # Feel free to distribute or modify the code.:) 4 | # Version: 0.3 5 | # Author: Baohua Yang@THU 6 | # Created: May 27, 2011 7 | # Updated: Dec 20, 2016 8 | # Usage: 9 | # getweb.sh [-d save_dir] WEBSITE_URL 10 | 11 | SAVE_DIR="$HOME/Downloads/sync_web" #save to sync_website under the current directory 12 | URL="" 13 | AGENT="Mozilla/8.0" 14 | 15 | OPTION="-e robots=off" # Not tell a robot 16 | OPTION+=" -x" # Create a hierarchy of directories 17 | OPTION+=" -nH" # Disable generation of host-prefixed directories 18 | OPTION+=" -m" # Mirroring, turns on recursion and time-stamping, infinite recursion depth 19 | OPTION+=" -p" # Download media type files 20 | OPTION+=" -k" # Convert links locally 21 | OPTION+=" -c" # Continue downloading with previous files 22 | OPTION+=" -nv" # Turn off verbose 23 | OPTION+=" -np" # No try parent directory 24 | #OPTION+=" -L" # Follow relative links only 25 | OPTION+=" -t 3" # Try times 26 | OPTION+=" -E" # adjust-extension, will add html suffix for dynamic page like js/asp... 27 | OPTION+=" -U $AGENT" # Set agent header 28 | #OPTION+=" --load-cookies=$HOME/Downloads/cookies.txt" # Using cookie 29 | 30 | #echo $OPTION 31 | 32 | command -v wget >/dev/null 2>&1 || { echo >&2 "wget is not installed. Please install it first."; exit 1; } 33 | 34 | while getopts d: opt 35 | do 36 | case "$opt" in 37 | d) SAVE_DIR="$OPTARG" 38 | ;; 39 | h) echo "Usage: $0 [-d save_dir] WEBSITE_URL" 40 | exit;; 41 | \?) 42 | echo "Usage: $0 [-d save_dir] WEBSITE_URL" 43 | exit;; 44 | esac 45 | done 46 | 47 | test ! -d ${SAVE_DIR} && echo "save_dir ${SAVE_DIR} not exists, will create it..." && mkdir -p ${SAVE_DIR} && echo "save_dir ${SAVE_DIR} is created" 48 | test ! -w ${SAVE_DIR} && echo "[Error]: save_dir isn't writeable, will exit." && exit 1 49 | OPTION+=" -P ${SAVE_DIR}" 50 | 51 | shift `expr $OPTIND - 1` && URL=$1 52 | [ -z "$URL" ] && echo "URL is not given, will exit." && exit 1 53 | 54 | # Run wget cmd with options 55 | echo "=== Start getting the website $URL" 56 | wget $OPTION $URL 57 | 58 | echo "=== Done! The website is saved to ${SAVE_DIR}" 59 | -------------------------------------------------------------------------------- /system/.bash_color: -------------------------------------------------------------------------------- 1 | # set variable identifying the chroot you work in (used in the prompt below) 2 | if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then 3 | debian_chroot=$(cat /etc/debian_chroot) 4 | fi 5 | 6 | # set a fancy prompt (non-color, unless we know we "want" color) 7 | case "$TERM" in 8 | xterm-color) color_prompt=yes;; 9 | esac 10 | 11 | # uncomment for a colored prompt, if the terminal has the capability; turned 12 | # off by default to not distract the user: the focus in a terminal window 13 | # should be on the output of commands, not on the prompt 14 | #force_color_prompt=yes 15 | 16 | if [ -n "$force_color_prompt" ]; then 17 | if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then 18 | # We have color support; assume it's compliant with Ecma-48 19 | # (ISO/IEC-6429). (Lack of such support is extremely rare, and such 20 | # a case would tend to support setf rather than setaf.) 21 | color_prompt=yes 22 | else 23 | color_prompt= 24 | fi 25 | fi 26 | 27 | if [ "$color_prompt" = yes ]; then 28 | PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' 29 | # PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u\[\033[01;36m\]@\[\033[00;33m\]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' 30 | else 31 | #PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' 32 | PS1='${debian_chroot:+($debian_chroot)}\[\e[36;1m\]\u@\[\e[32;1m\]\h:\[\e[31;1m\]\W\[\e[0m\]\$' 33 | #PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u\[\033[01;36m\]@\[\033[00;33m\]\h\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]\$ ' 34 | fi 35 | unset color_prompt force_color_prompt 36 | 37 | # If this is an xterm set the title to user@host:dir 38 | case "$TERM" in 39 | xterm*|rxvt*) 40 | PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1" 41 | ;; 42 | *) 43 | ;; 44 | esac 45 | 46 | # enable color support of ls and also add handy aliases 47 | if [ -x /usr/bin/dircolors ]; then 48 | test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)" 49 | alias ls='ls --color=auto' 50 | #alias dir='dir --color=auto' 51 | #alias vdir='vdir --color=auto' 52 | 53 | alias grep='grep --color=auto' 54 | alias fgrep='fgrep --color=auto' 55 | alias egrep='egrep --color=auto' 56 | fi 57 | -------------------------------------------------------------------------------- /security/openssl/gen_certs.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo "===Generate a self signed certificate as the CA" 4 | [ -f ca.crt ] || openssl req -nodes -newkey rsa:2048 -subj "/C=US/ST=State/L=Location/O=OrgName/OU=IT Department/CN=ca" -keyout ca.key -x509 -days 365 -out ca.crt 5 | 6 | #echo "Review genrated certificate" 7 | #openssl x509 -text -noout -in ca.crt 8 | 9 | echo "===Generate private key for server" 10 | #[ -f server.key ] || openssl genrsa -nodes -out server.key 2048 11 | [ -f server.key ] || openssl req -nodes -newkey rsa:2048 -subj "/C=US/ST=State/L=Location/O=OrgName/OU=IT Department/CN=server" -x509 -keyout server.key 12 | #[ -f server.key ] || openssl req -newkey rsa:2048 -subj "/C=US/ST=State/L=Location/O=OrgName/OU=IT Department/CN=server" -nodes -keyout server.key -x509 -days 365 -out server-tmp.crt 13 | 14 | # Optionally inspect the generated private key 15 | # openssl rsa -in ca.key -noout -text 16 | 17 | # Optionally inspect the generated public key 18 | # openssl rsa -in example.org.pubkey -pubin -noout -text 19 | 20 | #echo "Generate private key and csr for server: use server address as CN field (can use wildcard)" 21 | #openssl req -new -key server.key -out server.csr 22 | 23 | echo "===Generate the config file for the server csr, indicating server key file" 24 | if [ ! -f server-csr.conf ] ; then 25 | cat <./server-csr.conf 26 | [ req ] 27 | prompt = no 28 | default_bits = 2048 29 | default_keyfile = server.key 30 | distinguished_name = req_distinguished_name 31 | req_extensions = req_ext 32 | 33 | [ req_distinguished_name ] 34 | C=CN 35 | ST=BJ 36 | L=BJ 37 | O=O 38 | OU=O 39 | CN=server 40 | 41 | [ req_ext ] 42 | subjectAltName = @alt_names 43 | 44 | [alt_names] 45 | DNS.1 = localhost 46 | DNS.2 = server 47 | DNS.3 = oci-lbr 48 | IP.1 = 127.0.0.1 49 | IP.2 = 129.213.9.145 50 | IP.3 = 129.213.62.129 51 | IP.4 = 129.213.53.75 52 | IP.5 = 129.213.51.73 53 | EOF 54 | fi 55 | 56 | echo "===Generate csr based on the config" 57 | [ -f server.csr ] || openssl req -new -nodes -out server.csr -config server-csr.conf 58 | 59 | echo "===Sign a server certificate based on csr by CA" 60 | [ -f server.crt ] || openssl x509 -req -in server.csr -extfile v3.ext -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt 61 | 62 | echo "===Combine key and cert together as server certificate" 63 | if [ ! -f server.pem ]; then 64 | cp server.key server.pem 65 | cat server.crt >> server.pem 66 | fi 67 | -------------------------------------------------------------------------------- /system/rm.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # This scirpt provides a safe way for the 3 | # linux command 'rm'. It's under GNU license. 4 | # Feel free to distribute or modify the code.:) 5 | # Author: Baohua Yang@THU 6 | # Created: Nov 10, 2010 7 | # Version: 0.1 8 | # Usage: 9 | # Put this script to /usr/local/sbin/; 10 | # Run 'chmod a+x /usr/local/sbin/rm'; 11 | # Enjoy! 12 | 13 | TRASH_DIR=".local/share/Trash/files" 14 | #Size of large file which will be deleted permanently 15 | FILE_SIZE_LIMIT=512000 16 | 17 | while getopts 'fiIrh' OPT; do 18 | case $OPT in 19 | f) #mean force remove 20 | continue;; 21 | i) #prompt before every removal 22 | read -p "Sure to delete? [Y/n] " option 23 | if [ -n "$option" ] && [ "$option" == "Y" ] || [ "$option" == "y" ]; then 24 | continue; 25 | else 26 | echo "Operation Canceled." 27 | exit 28 | fi;; 29 | I) #mean to rm recursively 30 | continue;; 31 | r) #mean to rm recursively 32 | continue;; 33 | h) #mean to print help 34 | echo "This is a simple safe rm script" 35 | echo "Pls check the source code";; 36 | ?) #other parameters 37 | continue;; 38 | esac 39 | done 40 | 41 | #move parameters left 42 | shift $(($OPTIND - 1)) 43 | if [ $# -lt 1 ]; then #check if there's destination file 44 | echo "`which $0`: missing operand" 45 | echo Try "'rm -h' for more information." 46 | exit 47 | fi 48 | 49 | #check the trash dir 50 | [ -d ~/$TRASH_DIR ] || mkdir -p ~/$TRASH_DIR 51 | 52 | for filename in "$@" 53 | do 54 | if [ -e $filename ]; then #if exist 55 | size=`du -s $filename | awk '{print $1}'` #get size in KB 56 | if [ $size -gt $FILE_SIZE_LIMIT ]; then #larger than FILE_SIZE_LIMIT 57 | read -p "File is too large, delete permanently?[Y/n] " option 58 | if [ -n "$option" ] && [ "$option" == "Y" ] || [ "$option" == "y" ]; then 59 | /bin/rm -rf $filename 60 | else 61 | echo "Operation canceled" 62 | fi 63 | else #no larger than FILE_SIZE_LIMIT 64 | cp -rf $filename ~/$TRASH_DIR 65 | /bin/rm -rf $filename 66 | fi 67 | else #file not exist 68 | echo "`which $0`: cannot remove '$filename': No such file or directory" 69 | fi 70 | done 71 | -------------------------------------------------------------------------------- /python/stat_parse/ifconfig.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | import sys 3 | import os 4 | 5 | timestap_flag="========" 6 | nic_name_flag= "mtu " 7 | 8 | 9 | def main(): 10 | """ 11 | main will parse the ifconfig file and store the data into 12 | ts=[1,2,3,...], 13 | 14 | nic_stat={ 15 | 'nic_name': { 16 | 'RX packets': {'t1':xxx, ...}, 17 | 'RX bytes': {'t1':xxx, ...}, 18 | 'TX packets': {'t1':xxx, ...}, 19 | 'TX bytes': {'t1':xxx, ...}, 20 | }, 21 | ... 22 | } 23 | """ 24 | filepath = sys.argv[1] 25 | 26 | if not os.path.isfile(filepath): 27 | print("File path {} does not exist. Exiting...".format(filepath)) 28 | sys.exit() 29 | 30 | ts_list, nic_stat = [], {} 31 | 32 | with open(filepath) as f: 33 | cnt = 0 34 | for line in f: 35 | cnt += 1 36 | # print("{}:{}".format(cnt, line)) 37 | line = line.lstrip(' ').rstrip(' ').strip('\n') 38 | if timestap_flag in line: # Timestamp line 39 | ts = line[18:] # TODO: we use fixed length here. 40 | # print("Timestamp={}\n".format(ts)) 41 | ts = datetime.datetime.strptime(ts, '%a %b %d %H:%M:%S UTC %Y') 42 | ts = ts.strftime('%Y%m%d-%H:%M:%S') 43 | ts_list.append(ts) 44 | elif nic_name_flag in line: # nic name line 45 | nic_name = line.split(":")[0] 46 | # print("Nic Name={}\n".format(nic_name)) 47 | if nic_name not in nic_stat: 48 | nic_stat[nic_name] = { 49 | 'RX-packets': {}, 50 | 'RX-bytes': {}, 51 | 'TX-packets': {}, 52 | 'TX-bytes': {} 53 | } 54 | elif line.startswith('RX packets') or line.startswith('TX packets'): 55 | prefix = line[:2] # e.g., RX packets, or TX packets 56 | line_split = line[3:].split(' ') 57 | # print(line_split) 58 | pkts, bytes = line_split[1], line_split[4] # No need int here 59 | nic_stat[nic_name][prefix+'-packets'][ts] = pkts 60 | nic_stat[nic_name][prefix+'-bytes'][ts] = bytes 61 | 62 | # print(nic_stat) 63 | nic_names = sorted(nic_stat.keys()) 64 | metrics = ['RX-packets', 'RX-bytes', 'TX-packets', 'TX-bytes'] 65 | labels, line = [], 'TimeStamp' 66 | for nic_name in nic_names: 67 | for metric in metrics: 68 | labels.append([nic_name, metric]) 69 | for l in labels: 70 | line = '{} {}-{}'.format(line, l[0], l[1]) 71 | print(line) # TimeStamp eth0-RX-pkts ... 72 | for ts in ts_list: 73 | # ts nic_name+m nic_name+m ... 74 | line = ts 75 | for l in labels: # [eth0, Rx-pkts] 76 | values = nic_stat[l[0]][l[1]] 77 | if ts in values: 78 | value = values[ts] 79 | else: 80 | value = '0' # sometimes, new created nic does not have complete ts 81 | line = line+' '+value 82 | print(line) 83 | return 84 | 85 | for nic_name in nic_stat: 86 | for k in nic_stat[nic_name]: # RX packets/RX bytes/TX packets/TX bytes 87 | print(nic_name +' '+k) 88 | print(nic_stat[nic_name][k]) 89 | 90 | 91 | if __name__ == '__main__': 92 | main() -------------------------------------------------------------------------------- /c/mcast/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include "mcast.h" 9 | 10 | int main(int argc, char *argv[]) 11 | { 12 | extern char *optarg; 13 | extern int optind,opterr; 14 | int opt,interval; 15 | pthread_t send_tid, recv_tid; 16 | 17 | struct mcast_msg msg; 18 | msg.payload = malloc(1024); 19 | struct mc_send_arg arg_send={0,0,0,NULL,false}; 20 | struct mc_recv_arg arg_recv={0,0,NULL}; 21 | 22 | while ((opt = getopt(argc, argv, "g:p:t:c:")) != -1) { 23 | switch (opt) { 24 | case 'g': /*group id*/ 25 | arg_send.group_ip = inet_addr(optarg); 26 | arg_recv.group_ip = inet_addr(optarg); 27 | printf("group_ip=%s\n",optarg); 28 | break; 29 | case 'p': /*port*/ 30 | arg_send.port = atoi(optarg); 31 | arg_recv.port = atoi(optarg); 32 | printf("port=%d\n",arg_send.port); 33 | break; 34 | case 't': /*time interval*/ 35 | interval = atoi(optarg); 36 | printf("interval=%d\n",interval); 37 | break; 38 | case 'c': /*msg*/ 39 | msg.hdr = 100; 40 | memcpy(msg.payload, optarg,strlen(optarg)); 41 | break; 42 | default: /* '?' */ 43 | fprintf(stderr, "Usage: %s [-t secs] [-g group_ip] [-p port]\n", argv[0]); 44 | exit(EXIT_FAILURE); 45 | } 46 | } 47 | 48 | arg_send.msg = &msg; 49 | arg_send.len_msg = sizeof(msg); 50 | bool t1, t2; 51 | arg_send.stop = &t1; 52 | *arg_send.stop = false; 53 | arg_recv.stop = &t2; 54 | *arg_recv.stop = false; 55 | 56 | printf("ip=%lu,port=%d, interval=%d,msg is %lu:%s\n",arg_send.group_ip,arg_send.port,interval,msg.hdr,msg.payload); 57 | 58 | #ifdef __NO__DEF__XXX 59 | /* now create new process */ 60 | childpid = fork(); 61 | if (childpid >= 0) {/* fork succeeded */ 62 | if (childpid == 0) {/* the child process */ 63 | mc_recv(&arg_recv); 64 | } else {/* the parent process */ 65 | while (count++ < 10) { 66 | mc_send(&arg_send); 67 | sleep(interval); /* wait some sec. */ 68 | msg.hdr ++; 69 | } 70 | kill(childpid,SIGUSR1); 71 | } 72 | } else {/* fork returns -1 on failure */ 73 | perror("fork"); /* display error message */ 74 | } 75 | #endif 76 | 77 | pthread_create(&send_tid,NULL,mc_send,&arg_send); 78 | pthread_create(&recv_tid,NULL,mc_recv,&arg_recv); 79 | sleep(10); 80 | *arg_send.stop = true; 81 | *arg_recv.stop = true; 82 | free(msg.payload); 83 | return 0; 84 | } 85 | -------------------------------------------------------------------------------- /python/tls_server/flask.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template, request 2 | import werkzeug.serving 3 | import ssl 4 | import OpenSSL 5 | 6 | class PeerCertWSGIRequestHandler( werkzeug.serving.WSGIRequestHandler ): 7 | """ 8 | We subclass this class so that we can gain access to the connection 9 | property. self.connection is the underlying client socket. When a TLS 10 | connection is established, the underlying socket is an instance of 11 | SSLSocket, which in turn exposes the getpeercert() method. 12 | 13 | The output from that method is what we want to make available elsewhere 14 | in the application. 15 | """ 16 | def make_environ(self): 17 | """ 18 | The superclass method develops the environ hash that eventually 19 | forms part of the Flask request object. 20 | 21 | We allow the superclass method to run first, then we insert the 22 | peer certificate into the hash. That exposes it to us later in 23 | the request variable that Flask provides 24 | """ 25 | environ = super(PeerCertWSGIRequestHandler, self).make_environ() 26 | x509_binary = self.connection.getpeercert(True) 27 | x509 = OpenSSL.crypto.load_certificate( OpenSSL.crypto.FILETYPE_ASN1, x509_binary ) 28 | environ['peercert'] = x509 29 | return environ 30 | 31 | app = Flask(__name__) 32 | 33 | # to establish an SSL socket we need the private key and certificate that 34 | # we want to serve to users. 35 | # 36 | # app_key_password here is None, because the key isn't password protected, 37 | # but if yours is protected here's where you place it. 38 | app_key = './server.key' 39 | app_key_password = None 40 | app_cert = './server.crt' 41 | 42 | # in order to verify client certificates we need the certificate of the 43 | # CA that issued the client's certificate. In this example I have a 44 | # single certificate, but this could also be a bundle file. 45 | ca_cert = './server.crt' 46 | 47 | # create_default_context establishes a new SSLContext object that 48 | # aligns with the purpose we provide as an argument. Here we provide 49 | # Purpose.CLIENT_AUTH, so the SSLContext is set up to handle validation 50 | # of client certificates. 51 | ssl_context = ssl.create_default_context( purpose=ssl.Purpose.CLIENT_AUTH, 52 | cafile=ca_cert ) 53 | 54 | # load in the certificate and private key for our server to provide to clients. 55 | # force the client to provide a certificate. 56 | ssl_context.load_cert_chain( certfile=app_cert, keyfile=app_key, password=app_key_password ) 57 | ssl_context.verify_mode = ssl.CERT_REQUIRED 58 | 59 | # now we get into the regular Flask details, except we're passing in the peer certificate 60 | # as a variable to the template. 61 | @app.route('/') 62 | def hello_world(): 63 | return render_template('helloworld.html', client_cert=request.environ['peercert']) 64 | 65 | # start our webserver! 66 | if __name__ == "__main__": 67 | app.run(ssl_context=ssl_context, request_handler=PeerCertWSGIRequestHandler) -------------------------------------------------------------------------------- /web/httpserver/simplehttpserver.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #authors: yeasy.github.com 3 | #date: 2013-07-05 4 | 5 | import sys 6 | import BaseHTTPServer 7 | from SimpleHTTPServer import SimpleHTTPRequestHandler 8 | import socket 9 | import fcntl 10 | import struct 11 | import pickle 12 | from datetime import datetime 13 | from collections import OrderedDict 14 | 15 | class HandlerClass(SimpleHTTPRequestHandler): 16 | def get_ip_address(self,ifname): 17 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 18 | return socket.inet_ntoa(fcntl.ioctl( 19 | s.fileno(), 20 | 0x8915, # SIOCGIFADDR 21 | struct.pack('256s', ifname[:15]) 22 | )[20:24]) 23 | def log_message(self, format, *args): 24 | if len(args) < 3 or "200" not in args[1]: 25 | return 26 | try: 27 | request = pickle.load(open("pickle_data.txt","r")) 28 | except: 29 | request=OrderedDict() 30 | time_now = datetime.now() 31 | ts = time_now.strftime('%Y-%m-%d %H:%M:%S') 32 | server = self.get_ip_address('eth0') 33 | host=self.address_string() 34 | addr_pair = (host,server) 35 | if addr_pair not in request: 36 | request[addr_pair]=[1,ts] 37 | else: 38 | num = request[addr_pair][0]+1 39 | del request[addr_pair] 40 | request[addr_pair]=[num,ts] 41 | file=open("index.html", "w") 42 | file.write("

LOGO Webpage Visit Results

"); 43 | for pair in request: 44 | if pair[0] == host: 45 | guest = "LOCAL: "+pair[0] 46 | else: 47 | guest = pair[0] 48 | if (time_now-datetime.strptime(request[pair][1],'%Y-%m-%d %H:%M:%S')).seconds < 3: 49 | file.write("

#"+ str(request[pair][1]) +": "+str(request[pair][0])+ " requests " + "from <"+guest+"> to WebServer <"+pair[1]+">

") 50 | else: 51 | file.write("

#"+ str(request[pair][1]) +": "+str(request[pair][0])+ " requests " + "from <"+guest+"> to WebServer <"+pair[1]+">

") 52 | file.write(" "); 53 | file.close() 54 | pickle.dump(request,open("pickle_data.txt","w")) 55 | 56 | if __name__ == '__main__': 57 | try: 58 | ServerClass = BaseHTTPServer.HTTPServer 59 | Protocol = "HTTP/1.0" 60 | 61 | addr = len(sys.argv) < 2 and "0.0.0.0" or sys.argv[1] 62 | port = len(sys.argv) < 3 and 80 or int(sys.argv[2]) 63 | HandlerClass.protocol_version = Protocol 64 | httpd = ServerClass((addr, port), HandlerClass) 65 | sa = httpd.socket.getsockname() 66 | print "Serving HTTP on", sa[0], "port", sa[1], "..." 67 | httpd.serve_forever() 68 | except: 69 | exit() 70 | -------------------------------------------------------------------------------- /system/ether_bridge/README: -------------------------------------------------------------------------------- 1 | PACKETFORWARD 0.1 2 | ----------------- 3 | 4 | Introduction 5 | ------------ 6 | EtherBridge is an ethernet packet capture/forward application based on libpcap. It is a command line tool that listens on one network interface for ethernet packets and then injects them on the same or another network interface. It has options for packet capture filtering, and support bidirectional forwarding by default. 7 | 8 | 9 | Supported Platforms 10 | ------------------- 11 | EtherBridge has been compiled and tested on Linux (Ubuntu 12.04). But you are welcome to supply additional feedback if you compile and test it on other platforms succesfully. 12 | 13 | Since EtherBridge is based on libpcap, it should be portable to most other BSD and UNIX systems. 14 | 15 | 16 | Compilation and installation 17 | ---------------------------- 18 | In order to compile EtherBridge, you must have libpcap installed on your system. BSD systems like Mac OS X have libpcap preinstalled. 19 | 20 | cd to the directory of EtherBridge. 21 | To compile, type: 22 | make 23 | 24 | To install (requires root access), type: 25 | sudo make install 26 | 27 | To uninstall (requires root access), type: 28 | sudo make clean 29 | 30 | A compiled binary is supplied with this distribution. Just copy it to your system for easy access: 31 | sudo cp etherbridge /usr/bin/etherbridge 32 | 33 | 34 | Usage 35 | ----- 36 | EtherBridge will capture IP packets, show header info and content of payload. 37 | 38 | One of the uses of EtherBridge is to forward packets from a physical to a virtual interface eg. the tun/tap interface for VPN networks. This is especially useful for games that do not broadcast on all interfaces but only use the default interface and you want to play games with a friend on the internet. 39 | 40 | usage: 41 | etherbridge [options] 42 | 43 | interface: 44 | -i interface1 Capture packets from interface1. 45 | 46 | options: 47 | -I interface2 Forward packets to interface2. 48 | -n number Number of packets to capture. 49 | -h Hide packet headers. 50 | -p Hide payload. 51 | -f 'filter' Tcpdump packet filter expression. 52 | 53 | example: 54 | sudo etherbridge -i en1 -I tap0 -d 5.124.100.100 -f 'udp port 6112 and dst host 255.255.255.255' 55 | 56 | You must have root access to use EtherBridge. 57 | 58 | In this example EtherBridge will listen on the en1 network interface for UDP broadcast packets with dst and src port 6112, change the destination address to 5.124.100.100 and inject them on to the tap0 network interface. The src address is automatically changed to match the tap0 network interface. 59 | 60 | 61 | Filter 62 | ------ 63 | EtherBridge is using Tcpdump filter expressions. Below is some examples. 64 | 65 | ip Capture all IP packets. 66 | udp Capture only UDP packets. 67 | tcp Capture only TCP packets. 68 | udp port 80 Capture only UDP packets with src or dst port 80. 69 | ip host 10.1.2.3 Capture all IP packets to or from host 10.1.2.3. 70 | udp dst port 80 and src host 10.1.2.3 Capture only UDP packets to port 80 from host 10.1.2.3. 71 | 72 | Read the Tcpdump man pages for more info on filter expressions. 73 | -------------------------------------------------------------------------------- /security/openssl/server.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN PRIVATE KEY----- 2 | MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC99SoF2BdLiSO6 3 | vMV08UTH4ieYiyzNjWblqgQC5oVNuoI3hDF3dKUyXeLl63idh1LQXF3MDjxtwBn9 4 | EIO6fsfuSWSXa/2oR3v9R/+m1gtiQ4oaUyZ+ptBHTighzUAW3kvx1XawuwuJo451 5 | n6cfAAa7L8zXS6e5QVUxeD+gc/qDP6eKafIlzxzH1aIObqCwbrTpwCxSQqxBn8Wr 6 | D4iH8D3k3N4zJYeEr6ZGQ41oq5KnDDE+fniQa1XRMGckIg8rMPM+MadszV5slX/i 7 | NpxTnwgj6Vs22SCc8GnymP3ZKqPfj9BVhlpBUqF79XksiaryD/ZQgn5u8hlTvm2c 8 | nj7domHNAgMBAAECggEBAL2I1KA2J7Avqt3VIrP3VCiYx+dV3qNOVoALbqsyNYoD 9 | 8Y/RL8ZANeCWeyHsHFIZxxCtSM2k2rirN4Mgqj835uckheDsWJFCmj1zTGu8IGmw 10 | eCiiFMPEUAcFvddUQ4FN1rr6wc2/I9j2v9svLIIq9YxqOloLW9Plk0qj7+B6OFgh 11 | PcU9oSceSpTbRodqCTTrIy1+PT6ntazZKTpHcoNGzNANExqb+7mzGWUXCW/bFEFf 12 | 6krbI13rIf8vxwcjLzGn4eMOz78OTVByfd3ynoK3Pw+Eeqv+JTG4FO/nBelidffj 13 | ng8OM9/Mj3VrbFGrKz7GbQ63eBrzCu2zKzQR4/wEowUCgYEA9WyNXrznCqfiASXG 14 | xxlUecGuYjuPnKU7eQq8VD+k7WyPrfJ6xW0XyWZTNlsIOGufBdDgNpV+H+gguP05 15 | ltnXl05dakOZSg2sH89KWDLbrG30T2vzrx6ydxUm+sKq/kQAdzOvf6KZAV5hUoNK 16 | Ne1hffk9wop0+UmKM2l6WNTmNYsCgYEAxiS5D+ar3FJUTkgqYedbV/C+COljcS8Z 17 | jzeaNrXyG9qmAWdyovhITbeqGL1zavh0EKqwawsg40PuYBLE2c+63Z9X8/Xf9VqY 18 | H70LIyfAj1lxGt72/j8Z6MvCCJk2T7ePn1buqi1I8Afc+CrSXE++UHuMwd1z/f0s 19 | wA+4vklQIQcCgYA0C6MnNhQg9F0/NQ2kZ0C82U/r0QmxhHDKSHaLvztwhhcqIkPa 20 | 3jmvIh3/ZuqlXF1K1HLX4HfwuD9IO5sc3HGVyq6QvGkjhSa9UC5J6e8f7+lhdlkl 21 | B/N8wWWUw/eTGESpArLy9D5SRfuJjgIM0ZxdJJ/uQ4Ju4yOFp5akg/GV5QKBgD20 22 | R/Fcu150QQ7TyGcOvlJnhTaPP8mKimIgRaUOCiSEdbfwODHqMdIKBS2JKf1A/BwI 23 | m8HqCEbd68j9b1IJL78+lQxDIIhuuvr/Sw2anovFpcRhdPd+PRpTAczdsZ0no9FS 24 | KCbUzWb6e+TIqRPYPYMgxpfE2A9bnWj+PGD9wbdXAoGARdJamaPm/+gK8DGIju5t 25 | EAG7v99f2e8NHlBz5YaLLqlhMLzEgs35nqwiHDbDI7Em4+dVuhvL4tN9lEL0GCTN 26 | WARHlU4UQKxMVrqEYA9P0gdxVeSPw6TKraDIaco45TwkfAVSpeLwOtX3cXuZvEFS 27 | q9Vx61OW3zWUZvE7uGPTs84= 28 | -----END PRIVATE KEY----- 29 | -----BEGIN CERTIFICATE----- 30 | MIIEWDCCA0CgAwIBAgIJAN3oE/J/B0mBMA0GCSqGSIb3DQEBBQUAMGcxCzAJBgNV 31 | BAYTAlVTMQ4wDAYDVQQIDAVTdGF0ZTERMA8GA1UEBwwITG9jYXRpb24xEDAOBgNV 32 | BAoMB09yZ05hbWUxFjAUBgNVBAsMDUlUIERlcGFydG1lbnQxCzAJBgNVBAMMAmNh 33 | MB4XDTE4MDIwNzAzNDgxMFoXDTE4MDMwOTAzNDgxMFowUDELMAkGA1UEBhMCQ04x 34 | CzAJBgNVBAgMAkJKMQswCQYDVQQHDAJCSjEKMAgGA1UECgwBTzEKMAgGA1UECwwB 35 | TzEPMA0GA1UEAwwGc2VydmVyMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKC 36 | AQEAvfUqBdgXS4kjurzFdPFEx+InmIsszY1m5aoEAuaFTbqCN4Qxd3SlMl3i5et4 37 | nYdS0FxdzA48bcAZ/RCDun7H7klkl2v9qEd7/Uf/ptYLYkOKGlMmfqbQR04oIc1A 38 | Ft5L8dV2sLsLiaOOdZ+nHwAGuy/M10unuUFVMXg/oHP6gz+nimnyJc8cx9WiDm6g 39 | sG606cAsUkKsQZ/Fqw+Ih/A95NzeMyWHhK+mRkONaKuSpwwxPn54kGtV0TBnJCIP 40 | KzDzPjGnbM1ebJV/4jacU58II+lbNtkgnPBp8pj92Sqj34/QVYZaQVKhe/V5LImq 41 | 8g/2UIJ+bvIZU75tnJ4+3aJhzQIDAQABo4IBHDCCARgwgYEGA1UdIwR6MHiha6Rp 42 | MGcxCzAJBgNVBAYTAlVTMQ4wDAYDVQQIDAVTdGF0ZTERMA8GA1UEBwwITG9jYXRp 43 | b24xEDAOBgNVBAoMB09yZ05hbWUxFjAUBgNVBAsMDUlUIERlcGFydG1lbnQxCzAJ 44 | BgNVBAMMAmNhggkAngOLq1YrqZswCQYDVR0TBAIwADALBgNVHQ8EBAMCBPAwegYD 45 | VR0RBHMwcYIJbG9jYWxob3N0ggdvY2ktbGJyghNjbHVzdGVydHN0MDItYmNzYi0x 46 | ghNjbHVzdGVydHN0MDItYmNzYi0yghNjbHVzdGVydHN0MDItYmNzYi0zhwR/AAAB 47 | hwSB1QmRhwSB1T6BhwSB1TVLhwSB1TNJMA0GCSqGSIb3DQEBBQUAA4IBAQDO3Rof 48 | V2ONEVmPjk09fH28D7ppF7T1VVUQhC1G5SdtMwbJjgP1JbaC/IxQMzu71R1EseUf 49 | cDx7kdcqjxwMVtP3R+52cRklYroffLiYOdYtx4wLVfgrf95Fm77yJGiXkYP/P0GK 50 | nmMjxIeZicqVaohmFqQf1voXXQ/nhupkAVaGDe1wspD+Sii1jon38lEjaeA65xGo 51 | rHuF98WazQIUFPCWqs4zfK0h2/Hf9f7Y9ryZ7Aw1fmryKI+MqAyUzW0I8nbw28iR 52 | ci1sBpwENfgOkuZn/Tf/KLsAdcdau7a/L+aB1OiZHben2M1YKK9K65NAvf+pxx3/ 53 | BbLQ2JvSQSJPStmW 54 | -----END CERTIFICATE----- 55 | -------------------------------------------------------------------------------- /web/sm_watcher/index.py: -------------------------------------------------------------------------------- 1 | import tornado.web 2 | import tornado.websocket 3 | import tornado.httpserver 4 | import tornado.ioloop 5 | import tornado.options 6 | 7 | import os.path 8 | import json 9 | 10 | import pickle 11 | 12 | from tornado.options import define, options 13 | from tornado.ioloop import PeriodicCallback 14 | from sm_watcher import DB_FILE 15 | 16 | ws_server = 'localhost' 17 | ws_port = 9000 18 | 19 | define("port", default=ws_port, help="run on the given port", type=int) 20 | 21 | 22 | class Entries(object): 23 | callbacks = [] 24 | 25 | def register(self, callback): 26 | self.callbacks.append(callback) 27 | 28 | def unregister(self, callback): 29 | self.callbacks.remove(callback) 30 | 31 | def notifyCallbacks(self): 32 | for callback in self.callbacks: 33 | callback(self.getEntries()) 34 | 35 | def getEntries(self): 36 | entries = pickle.load(open(DB_FILE, 'rb')).values() 37 | entries.sort(key=lambda x: x['url'], reverse=True) 38 | return entries 39 | 40 | 41 | class Application(tornado.web.Application): 42 | def __init__(self): 43 | self.entries = Entries() 44 | handlers = [ 45 | (r"/", IndexHandler), 46 | (r"/ws", WSHandler), 47 | ] 48 | settings = dict( 49 | template_path=os.path.join(os.path.dirname(__file__), "templates"), 50 | static_path=os.path.join(os.path.dirname(__file__), "static"), 51 | ui_modules={'show_entry': ShowEntryModule}, 52 | debug=True, 53 | ) 54 | tornado.web.Application.__init__(self, handlers, **settings) 55 | 56 | class IndexHandler(tornado.web.RequestHandler): 57 | def get(self): 58 | self.render("index.html", ws_url='ws://{}:{}/ws'.format( 59 | ws_server, ws_port)) 60 | 61 | class ShowEntryModule(tornado.web.UIModule): 62 | def render(self): 63 | return self.render_string('modules/entries.html') 64 | 65 | def javascript_files(self): 66 | return "scripts/entries.js" 67 | 68 | class WSHandler(tornado.websocket.WebSocketHandler): 69 | 70 | def open(self): 71 | print('open connection') 72 | self.send_entries() 73 | self.callback = PeriodicCallback(self.send_entries, 5000) 74 | self.callback.start() 75 | #self.application.entries.register(self.callback) 76 | 77 | def on_close(self): 78 | print('close connection') 79 | #self.application.entries.unregister(self.callback) 80 | if hasattr(self, 'callback'): 81 | self.callback.stop() 82 | 83 | def on_message(self, message): 84 | print('message received %s' % message) 85 | 86 | def send_entries(self): 87 | entries = list(pickle.load(open(DB_FILE, 'rb')).values()) 88 | entries.sort(key=lambda x: x['gid'], reverse=True) 89 | self.write_message(json.dumps(entries)) 90 | 91 | 92 | class EntryModule(tornado.web.UIModule): 93 | def render(self, entry): 94 | return self.render_string('modules/entries.html', entry=entry) 95 | 96 | if __name__ == "__main__": 97 | tornado.options.parse_command_line() 98 | http_server = tornado.httpserver.HTTPServer(Application()) 99 | http_server.listen(options.port) 100 | tornado.ioloop.IOLoop.instance().start() 101 | print('Now will listen on port {}'.format(ws_port)) 102 | -------------------------------------------------------------------------------- /system/service: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | # 3 | # Copyright (C) 2013 AAA, Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at: 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | ### BEGIN INIT INFO 18 | # Provides: provider 19 | # Required-Start: $network $named $remote_fs $syslog 20 | # Required-Stop: $remote_fs 21 | # Default-Start: 2 3 4 5 22 | # Default-Stop: 0 1 6 23 | # Short-Description: sample service script 24 | ### END INIT INFO 25 | 26 | NAME=floodlight 27 | test -e /etc/default/$NAME && . /etc/default/$NAME 28 | 29 | PIDFILE=/var/run/$NAME.pid 30 | COMMAND="/usr/bin/echo $NAME" #The command to be run 31 | PNAME="$COMMAND" #The process name to be searched 32 | #BASEDIR= 33 | 34 | #exec su - ${RUN_AS_USER} -s /bin/bash -c "cd ${BASEDIR}; exec ${COMMAND} 2>&1 | /usr/bin/logger -t $NAME -p user.info" 35 | 36 | start () { 37 | pid=`ps aux |grep "$PNAME" |grep -v "grep"| awk '{print $2}'` 38 | if [ -n "$pid" ]; then 39 | echo "start: Job is already running, process $pid" 40 | if [ -e $PIDFILE ]; then 41 | [ $pid != `cat $PIDFILE` ] && echo $pid > $PIDFILE 42 | else 43 | echo $pid > $PIDFILE 44 | fi 45 | exit 0 46 | fi 47 | echo -n "starting" 48 | #start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS 49 | ${COMMAND} 2>&1 | /usr/bin/logger -t $NAME -p user.info & 50 | #su - ${RUN_AS_USER} -s /bin/bash -c "cd ${BASEDIR}; ${COMMAND} 2>&1 | /usr/bin/logger -t $NAME -p user.info &" 51 | for i in 1 2 3 4 5 6 7 52 | do 53 | echo -n "." 54 | sleep 1 55 | done 56 | pid=`ps aux |grep "$PNAME" |grep -v "grep"| awk '{print $2}'` 57 | echo $pid > $PIDFILE 58 | echo "..done" 59 | sleep 1 60 | echo "$NAME start/running, process $pid" 61 | } 62 | 63 | stop () { 64 | pid=`ps aux |grep "$PNAME" |grep -v "grep"| awk '{print $2}'` 65 | if [ -z "$pid" ]; then 66 | echo "stop: Unknown instance, service is not runing." 67 | ps aux |grep "$PNAME" |grep -v "grep"| awk '{print $2}'|xargs kill -9 2>&1| /usr/bin/logger -t $NAME -p user.info 68 | else 69 | kill -9 $pid 2>&1| /usr/bin/logger -t $NAME -p user.info 70 | [ -e $PIDFILE ] && rm -f $PIDFILE 71 | echo "$NAME stop/waiting" 72 | fi 73 | } 74 | 75 | status () { 76 | pid=`ps aux |grep "$PNAME" |grep -v "grep"| awk '{print $2}'` 77 | [ -n "$pid" ] && echo "$NAME start/running, process $pid" || echo "$NAME stop/waiting" 78 | } 79 | 80 | case $1 in 81 | start) 82 | start 83 | ;; 84 | stop | force-stop) 85 | stop 86 | ;; 87 | restart) 88 | stop 89 | start 90 | ;; 91 | status) 92 | status 93 | ;; 94 | *) 95 | echo "Usage: $0 {start|stop|restart|status|force-stop}" >&2 96 | exit 1 97 | ;; 98 | esac 99 | 100 | exit 0 101 | -------------------------------------------------------------------------------- /python/add_watermark.py: -------------------------------------------------------------------------------- 1 | # Usage: python3 add_watermark.py --input ~/input.pdf --watermark "watermark" 2 | import io 3 | import argparse 4 | import os 5 | import sys 6 | 7 | from PyPDF2 import PdfReader, PdfWriter 8 | from reportlab.pdfgen import canvas 9 | from reportlab.lib.pagesizes import letter 10 | from reportlab.lib.colors import Color, black 11 | from reportlab.pdfbase.ttfonts import TTFont 12 | from reportlab.pdfbase import pdfmetrics 13 | 14 | 15 | def add_watermark(pdf_input_path, watermark_text, pdf_output_path, change_firstpage): 16 | # create a pdf reader 17 | input_pdf = PdfReader(pdf_input_path) 18 | output_pdf = PdfWriter() 19 | 20 | page = input_pdf.pages[0] 21 | 22 | # Access the media box to get width and height 23 | page_width = float(page.mediabox.upper_right[0]) - float( 24 | page.mediabox.lower_left[0]) 25 | page_height = float(page.mediabox.upper_right[1]) - float( 26 | page.mediabox.lower_left[1]) 27 | 28 | # create a PDF canvas for the watermark layer 29 | packet = io.BytesIO() 30 | can = canvas.Canvas(packet, pagesize=letter) 31 | 32 | # set transparency to 10% 33 | fill_color = Color(black.red, black.green, black.blue, alpha=0.1) 34 | can.setFillColor(fill_color) 35 | 36 | # register TTF fonts 37 | pdfmetrics.registerFont(TTFont('Songti', '/System/Library/Fonts/Supplemental/Songti.ttc')) 38 | pdfmetrics.registerFont(TTFont('Helvetica', 'helvetica.ttc')) 39 | 40 | # Function to set appropriate font 41 | def set_font(can, text): 42 | if any('\u4e00' <= char <= '\u9fff' for char in text): 43 | can.setFont("Songti", 28) 44 | else: 45 | can.setFont("Helvetica", 28) 46 | 47 | # watermark text 48 | for i in range(100, int(page_width*0.7), 400): # horizontal 49 | for j in range(100, int(page_height*0.7), 300): # vertical 50 | can.saveState() 51 | can.translate(i, j) 52 | can.rotate(45) 53 | set_font(can, watermark_text) 54 | can.drawString(0, 0, watermark_text) 55 | can.restoreState() 56 | 57 | can.save() 58 | 59 | # move to the beginning of the packet 60 | packet.seek(0) 61 | new_pdf = PdfReader(packet) 62 | 63 | # add watermark 64 | for i in range(len(input_pdf.pages)): 65 | page = input_pdf.pages[i] 66 | if change_firstpage or i != 0: # skip the first page 67 | page.merge_page(new_pdf.pages[0]) 68 | output_pdf.add_page(page) 69 | 70 | # output to a new pdf file 71 | with open(pdf_output_path, "wb") as output_file: 72 | output_pdf.write(output_file) 73 | 74 | 75 | def main(): 76 | parser = argparse.ArgumentParser( 77 | description="Add a watermark to a PDF file.") 78 | parser.add_argument("--input", help="Input PDF file", 79 | default="input.pdf") 80 | parser.add_argument("--watermark", help="Watermark text", 81 | default="Watermark Text") 82 | parser.add_argument("--output", help="Output PDF file, " 83 | "default to be the input file with a _marked suffix", 84 | default="") 85 | parser.add_argument('--keep-firstpage', action='store_true', default=False, 86 | help='Also add water mark to the first page (default: False)') 87 | args = parser.parse_args() 88 | 89 | if len(sys.argv) == 1: 90 | parser.print_help() 91 | sys.exit(1) 92 | 93 | input = args.input 94 | output = args.output or f"{os.path.splitext(input)[0]}_marked.pdf" 95 | add_watermark(input, args.watermark, output, args.keep_firstpage) 96 | 97 | print(f"Output file to {output}") 98 | 99 | if __name__ == "__main__": 100 | main() 101 | -------------------------------------------------------------------------------- /algorithm/sort.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | """ Sort algorithm testing. 3 | Sort in incremental order. 4 | """ 5 | 6 | import random 7 | 8 | def genSeq(num=1000): 9 | """ Generate a random sequence for test. 10 | @num: The number of elements 11 | @return: The generated result 12 | """ 13 | t = range(num) 14 | random.shuffle(t) 15 | return t 16 | 17 | def validate(L): 18 | """Validate if the list is sorted correctly. 19 | @L: List to validate 20 | @return: True or False 21 | """ 22 | for i in range(len(L)-1): 23 | if L[i] > L[i+1]: 24 | return False 25 | return True 26 | 27 | #InsertSort 28 | def insertSort(L): 29 | """ Insert Sorting. 30 | @L: The list to sort 31 | @return: The sorted result 32 | """ 33 | for i in range(len(L)-1): #[0,i] is sorted,[i+1,n] is unsorted 34 | j = i 35 | while j>=0 and L[j] > L[i+1]: 36 | L[j+1] = L[j] 37 | j -= 1 38 | L [j+1] = L[i+1] 39 | return L 40 | 41 | #BubbleSort 42 | def bubbleSort(L): 43 | """ bubble Sorting. 44 | @L: The list to sort 45 | @return: The sorted result 46 | """ 47 | for i in range(len(L)-1): 48 | for j in range(len(L)-1-i): 49 | if L[j+1] < L[j]: 50 | L[j+1],L[j] = L[j],L[j+1] 51 | return L 52 | 53 | #QuickSort 54 | def quickSort_partition(L,i,j): 55 | """ Get the partition value in quickSort. 56 | @L: List to sort 57 | @i: Start position 58 | @j: End position 59 | @return: Pivot position 60 | """ 61 | pivot = L[i] #use value in L[i] as the pivot 62 | while i= pivot: j -= 1 #find L[j] < pivot 64 | L[i] = L[j] #L[j] is temp, candidate for pivot 65 | while i pivot 66 | L[j] = L[i] #L[i] is temp, candidate for pivot 67 | L[i] = pivot 68 | return i 69 | 70 | def quickSort_sub(L,i,j): 71 | """ Quick Sorting between i and j. 72 | @L: The list to sort 73 | @i: The start position 74 | @j: The end position 75 | """ 76 | if i 21 | #include 22 | #include 23 | #include 24 | #else 25 | #include 26 | #include 27 | #endif 28 | 29 | #include "bf-gdt.h" 30 | 31 | #define SETBIT(array, n) (array[n/sizeof(char)] |= (1<<(n%sizeof(char)))) 32 | #define GETBIT(array, n) (array[n/sizeof(char)] & (1<<(n%sizeof(char)))) 33 | 34 | /** 35 | * Init an empty bf_gdt 36 | * @param gid: the group id 37 | * @return: Pointer to the created bf_gdt. NULL if failed. 38 | */ 39 | struct bf_gdt *bf_gdt_init(u32 gid) 40 | { 41 | struct bf_gdt *gdt; 42 | #ifdef __KERNEL__ 43 | if(!(gdt=kmalloc(sizeof(struct bf_gdt),GFP_KERNEL))) 44 | #else 45 | if(!(gdt=malloc(sizeof(struct bf_gdt)))) 46 | #endif 47 | { 48 | /*printk("Error in kmalloc gdt\n");*/ 49 | return NULL; 50 | } 51 | #ifdef __KERNEL__ 52 | if(!(gdt->bf_array=kmalloc(BF_GDT_MAX_FILTERS*sizeof(struct bloom_filter*),GFP_KERNEL))) 53 | #else 54 | if(!(gdt->bf_array=malloc(BF_GDT_MAX_FILTERS*sizeof(struct bloom_filter*)))) 55 | #endif 56 | { 57 | /*printk("Error in kmalloc gdt\n");*/ 58 | return NULL; 59 | } 60 | 61 | gdt->gid = gid; 62 | gdt->nbf = 0; 63 | 64 | return gdt; 65 | } 66 | 67 | /** 68 | * Create and add a new bloom_filter into the given gdt 69 | * @param gdt: the gdt to update 70 | * @param port_no: the port_no of the new bloom_filter 71 | * @param len: the len of the new bloom_filter 72 | * @return: Pointer to the created bloom_filter. NULL if failed. 73 | */ 74 | struct bloom_filter *bf_gdt_add_filter(struct bf_gdt *gdt, u16 port_no, u32 len) 75 | { 76 | if (!gdt) { 77 | return NULL; 78 | } 79 | struct bloom_filter *bf = bf_create(gdt->nbf,len,port_no,2); 80 | if (bf) { 81 | if(gdt->nbf < BF_GDT_MAX_FILTERS) { 82 | gdt->bf_array[gdt->nbf++] = bf; 83 | return bf; 84 | } else { 85 | bf_destroy(bf); 86 | } 87 | } 88 | return NULL; 89 | } 90 | 91 | /** 92 | * destroy a bf_gdt. 93 | * @param gdt: the bf_gdt to destroy. 94 | */ 95 | int bf_gdt_destroy(struct bf_gdt *gdt) 96 | { 97 | if (gdt->bf_array) { 98 | u32 i = 0; 99 | for (i=0;inbf;i++){ 100 | bf_destroy(gdt->bf_array[i]); 101 | } 102 | } 103 | #ifdef __KERNEL__ 104 | if (gdt->bf_array) 105 | kfree(gdt->bf_array); 106 | if (gdt) 107 | kfree(gdt); 108 | #else 109 | if (gdt->bf_array) 110 | free(gdt->bf_array); 111 | if (gdt) 112 | free(gdt); 113 | #endif 114 | return 0; 115 | } 116 | 117 | /** 118 | * add a new string to gdt's some bf 119 | * @param gdt: the bf_gdt to update 120 | * @param bf_id: the dp's content updated 121 | * @param s: the string to add 122 | */ 123 | int bf_gdt_add_item(struct bf_gdt *gdt, u32 bf_id, const char *s) 124 | { 125 | if (gdt && gdt->nbf>bf_id && gdt->bf_array) { 126 | bf_add(gdt->bf_array[bf_id],s); 127 | } 128 | 129 | return 0; 130 | } 131 | 132 | /** 133 | * test if s is in gdt-gdt. 134 | * @param gdt: the bf_gdt to test 135 | * @param s: the string to test 136 | * @return the bf if true 137 | */ 138 | struct bloom_filter *bf_gdt_check(struct bf_gdt *gdt, const char *s) 139 | { 140 | u32 i; 141 | if (gdt && gdt->bf_array) { 142 | for(i=0; inbf; ++i) { 143 | if(bf_check(gdt->bf_array[i],s)) return gdt->bf_array[i]; 144 | } 145 | } 146 | 147 | return NULL; 148 | } 149 | -------------------------------------------------------------------------------- /web/ss_watcher.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | 3 | from bs4 import BeautifulSoup 4 | 5 | import os 6 | import random 7 | import requests 8 | import shutil 9 | import sys 10 | import time 11 | 12 | 13 | 14 | headers = { 15 | 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0', 16 | 'charset':'utf8' 17 | } 18 | headers_pool = [ 19 | {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0'}, 20 | {'User-Agent': 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)'}, 21 | {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5'}, 22 | {'User-Agent': 'Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405', 23 | } 24 | ] 25 | 26 | ISOTIMEFORMAT="%Y-%m-%d %X" 27 | 28 | 29 | def get_ss(url): 30 | """Get ss list from given url 31 | 32 | :param url: 33 | :return: list of ss addrs like ['ss://xxxxxxx','ss://yyyyy'] 34 | """ 35 | while True: 36 | try: 37 | headers = headers_pool[random.randint(0, len(headers_pool)-1)] 38 | r = requests.get(url, headers=headers, timeout=10) 39 | except Exception: 40 | time.sleep(random.randint(2, 10)) 41 | continue 42 | break 43 | 44 | if r.status_code != requests.codes.ok: 45 | print('return status code {} is not OK.'.format(r.status_code)) 46 | return None 47 | r.encoding = r.apparent_encoding 48 | soup = BeautifulSoup(r.text, "lxml") 49 | row_heads = soup.body.find_all('td', text='日本樱花') 50 | rows = map(lambda x: x.find_parent("tr"), row_heads) 51 | values = map(lambda x: x.find_all('td'), rows) 52 | 53 | proxy_list = [] 54 | for v in values: 55 | c = map(lambda x: x.text, v) 56 | lc = list(c) 57 | proxy = 'ss://{}:{}@{}:{}'.format(lc[4],lc[3],lc[1],lc[2]) 58 | # proxy = ss://rc4-md5:password@ip:port 59 | proxy_list.append(proxy) 60 | return proxy_list 61 | 62 | 63 | def update_cow(ss_list): 64 | src = os.path.expanduser('~')+'/.cow/rc.bak' 65 | dst = os.path.expanduser('~')+'/.cow/rc' 66 | try: 67 | shutil.copyfile(src, dst) 68 | f_rc = open(dst, 'a') 69 | f_rc.write('# other ss proxies\n') 70 | for ss in ss_list: 71 | f_rc.write('proxy = {}\n'.format(ss)) 72 | f_rc.close() 73 | os.system('killall -9 cow') 74 | except OSError: 75 | print("Cannot copy file") 76 | 77 | 78 | def wake_till(seconds): 79 | """Wake up till reach seconds 80 | """ 81 | while True: 82 | if int(time.time()) < seconds: 83 | time.sleep(5) 84 | else: 85 | return 86 | 87 | 88 | if __name__ == '__main__': 89 | if len(sys.argv) < 2: 90 | print('should input an url') 91 | exit() 92 | else: 93 | url = sys.argv[1] 94 | ss_list_old = [] 95 | check_interval = 600 # how many seconds wait for check, auto-adjust 96 | print("Will fetch info from {}".format(url)) 97 | while True: 98 | ss_list = get_ss(url) 99 | if not ss_list: 100 | print("Get invalid ss list, wait some time to retry") 101 | time.sleep(random.randint(100,300)) 102 | continue 103 | if ss_list != ss_list_old: # find new, reduce interval 104 | print('Get new ss list') 105 | print('\n'.join(ss_list)) 106 | update_cow(ss_list) 107 | print('{}: Update'.format(time.strftime(ISOTIMEFORMAT))) 108 | ss_list_old = ss_list 109 | check_interval -= random.randint(0, 100) 110 | print('Adjust next check interval = {}'.format(check_interval)) 111 | current_time = time.time() 112 | next_seconds = int(current_time) + check_interval 113 | print('next check time = {}'.format(time.ctime(next_seconds))) 114 | wake_till(next_seconds) 115 | 116 | else: # duplicated content, we're checking too quick 117 | next_wait = random.randint(300, 600) 118 | check_interval += next_wait 119 | if check_interval >= 3600*3: # at most wait for 3 hour 120 | check_interval = random.randint(3600*2, 3600*3) 121 | print('Get duplicated content') 122 | print('Adjust next check interval = {}'.format(check_interval)) 123 | time.sleep(next_wait) 124 | -------------------------------------------------------------------------------- /system/ether_bridge/headers.h: -------------------------------------------------------------------------------- 1 | /* 2 | * headers.h 3 | * 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | /* ethernet headers are always exactly 14 bytes */ 11 | #ifndef SIZE_ETHERNET 12 | #define SIZE_ETHERNET 14 13 | #endif 14 | 15 | /* ethernet addresses are 6 bytes */ 16 | #ifndef ETHER_ADDR_LEN 17 | #define ETHER_ADDR_LEN 6 18 | #endif 19 | 20 | /* Ethernet header */ 21 | struct sniff_ethernet { 22 | u_char ether_dhost[ETHER_ADDR_LEN]; /* destination host address */ 23 | u_char ether_shost[ETHER_ADDR_LEN]; /* source host address */ 24 | u_short ether_type; /* IP? ARP? RARP? etc */ 25 | }; 26 | 27 | /* IP header */ 28 | struct sniff_ip { 29 | u_char ip_vhl; /* version << 4 | header length >> 2 */ 30 | u_char ip_tos; /* type of service */ 31 | u_short ip_len; /* total length */ 32 | u_short ip_id; /* identification */ 33 | u_short ip_off; /* fragment offset field */ 34 | #define IP_RF 0x8000 /* reserved fragment flag */ 35 | #define IP_DF 0x4000 /* dont fragment flag */ 36 | #define IP_MF 0x2000 /* more fragments flag */ 37 | #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */ 38 | u_char ip_ttl; /* time to live */ 39 | u_char ip_p; /* protocol */ 40 | u_short ip_sum; /* checksum */ 41 | struct in_addr ip_src,ip_dst; /* source and dest address */ 42 | }; 43 | #define IP_HL(ip) (((ip)->ip_vhl) & 0x0f) 44 | #define IP_V(ip) (((ip)->ip_vhl) >> 4) 45 | 46 | /* ARP Header, (assuming Ethernet+IPv4) */ 47 | #define ARP_REQUEST 1 /* ARP Request */ 48 | #define ARP_REPLY 2 /* ARP Reply */ 49 | struct arphdr { 50 | u_int16_t htype; /* Hardware Type */ 51 | u_int16_t ptype; /* Protocol Type */ 52 | u_char hlen; /* Hardware Address Length */ 53 | u_char plen; /* Protocol Address Length */ 54 | u_int16_t oper; /* Operation Code */ 55 | u_char sha[6]; /* Sender hardware address */ 56 | u_char spa[4]; /* Sender IP address */ 57 | u_char tha[6]; /* Target hardware address */ 58 | u_char tpa[4]; /* Target IP address */ 59 | }; 60 | 61 | #define MAXBYTES2CAPTURE 2048 62 | 63 | /* TCP header */ 64 | typedef u_int tcp_seq; 65 | 66 | struct sniff_tcp { 67 | u_short th_sport; /* source port */ 68 | u_short th_dport; /* destination port */ 69 | tcp_seq th_seq; /* sequence number */ 70 | tcp_seq th_ack; /* acknowledgement number */ 71 | u_char th_offx2; /* data offset, rsvd */ 72 | #define TH_OFF(th) (((th)->th_offx2 & 0xf0) >> 4) 73 | u_char th_flags; 74 | #define TH_FIN 0x01 75 | #define TH_SYN 0x02 76 | #define TH_RST 0x04 77 | #define TH_PUSH 0x08 78 | #define TH_ACK 0x10 79 | #define TH_URG 0x20 80 | #define TH_ECE 0x40 81 | #define TH_CWR 0x80 82 | #define TH_FLAGS (TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG|TH_ECE|TH_CWR) 83 | u_short th_win; /* window */ 84 | u_short th_sum; /* checksum */ 85 | u_short th_urp; /* urgent pointer */ 86 | }; 87 | 88 | /* UDP header */ 89 | struct sniff_udp { 90 | unsigned short int uh_sport; 91 | unsigned short int uh_dport; 92 | unsigned short int uh_len; 93 | unsigned short int uh_check; 94 | }; /* total udp header length: 8 bytes (=64 bits) */ 95 | 96 | #define APP_NAME "EtherBridge 0.1" 97 | #define APP_DESC "Ethernet packet capture and forward application based on libpcap, can connect two ports bidirectionally." 98 | #define APP_COPYRIGHT "Copyright (c)" 99 | 100 | /* default snap length (maximum bytes per packet to capture) */ 101 | #ifndef BUFSIZ 102 | #define BUFSIZ 2048 103 | #endif 104 | 105 | void print_payload(const u_char *payload, int len); 106 | void fprint_ascii_line(const u_char *payload, int len, int offset); 107 | void print_hex_ascii_line(const u_char *payload, int len, int offset); 108 | void print_info(void); 109 | void print_usage(char *name); 110 | void send_packet(pcap_t *handle_dev2, const u_char *packet, size_t size); 111 | int parse_pkt(const u_char *packet,int hide_header,int hide_payload); 112 | int parse_ip(const u_char *packet, int hide_header,int hide_payload); 113 | int parse_arp(const u_char *packet, int hide_header,int hide_payload); 114 | void parse_tcp(const u_char *packet,const struct sniff_ip *ip,int size_ip,int hide_header,int hide_payload); 115 | void parse_udp(const u_char *packet,const struct sniff_ip *ip,int size_ip, int hide_header,int hide_payload); 116 | -------------------------------------------------------------------------------- /golang/check.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # This script refs from https://github.com/google/trillian/blob/master/scripts/presubmit.sh. 4 | 5 | set -eu 6 | 7 | # check whether all dependent tools exist 8 | check_deps() { 9 | local failed=0 10 | check_cmd golint github.com/golang/lint/golint || failed=10 11 | check_cmd misspell github.com/client9/misspell/cmd/misspell || failed=11 12 | check_cmd gocyclo github.com/fzipp/gocyclo || failed=12 13 | check_cmd stringer golang.org/x/tools/cmd/stringer || failed=13 14 | return $failed 15 | } 16 | 17 | # check existence of given cmd 18 | check_cmd() { 19 | local cmd="$1" 20 | local repo="$2" 21 | if ! type -p "${cmd}" > /dev/null; then 22 | echo "${cmd} not found, try to 'go get -u ${repo}'" 23 | return 1 24 | fi 25 | } 26 | 27 | usage() { 28 | echo "$0 [--coverage] [--fix] [--no-build] [--no-linters] [--no-generate]" 29 | } 30 | 31 | main() { 32 | check_deps 33 | 34 | local coverage=0 35 | local fix=0 36 | local run_build=1 37 | local run_linters=1 38 | local run_generate=1 39 | while [[ $# -gt 0 ]]; do 40 | case "$1" in 41 | --coverage) 42 | coverage=1 43 | ;; 44 | --fix) 45 | fix=1 46 | ;; 47 | --help) 48 | usage 49 | exit 0 50 | ;; 51 | --no-build) 52 | run_build=0 53 | ;; 54 | --no-linters) 55 | run_linters=0 56 | ;; 57 | --no-generate) 58 | run_generate=0 59 | ;; 60 | *) 61 | usage 62 | exit 1 63 | ;; 64 | esac 65 | shift 1 66 | done 67 | 68 | local go_dirs="$(go list ./... | \ 69 | grep -v /vendor/)" 70 | local go_srcs="$(find . -name '*.go' | \ 71 | grep -v mock_ | \ 72 | grep -v .pb.go | \ 73 | grep -v .pb.gw.go | \ 74 | grep -v _string.go | \ 75 | grep -v vendor/ | \ 76 | tr '\n' ' ')" 77 | local proto_srcs="$(find . -name '*.proto' | \ 78 | grep -v vendor/ | \ 79 | tr '\n' ' ')" 80 | 81 | if [[ "$fix" -eq 1 ]]; then 82 | check_cmd goimports golang.org/x/tools/cmd/goimports 83 | 84 | echo 'running gofmt' 85 | gofmt -s -w ${go_srcs} 86 | echo 'running goimports' 87 | goimports -w ${go_srcs} 88 | fi 89 | 90 | if [[ "${run_build}" -eq 1 ]]; then 91 | local goflags='' 92 | if [[ "${GOFLAGS:+x}" ]]; then 93 | goflags="${GOFLAGS}" 94 | fi 95 | 96 | echo 'running go build' 97 | go build ${goflags} ${go_dirs} 98 | 99 | echo 'running go test' 100 | 101 | # Individual package profiles are written to "$profile.out" files under 102 | # /tmp/trillian_profile. 103 | # An aggregate profile is created at /tmp/coverage.txt. 104 | mkdir -p /tmp/trillian_profile 105 | rm -f /tmp/trillian_profile/* 106 | 107 | for d in ${go_dirs}; do 108 | # Create a different -coverprofile for each test (if enabled) 109 | local coverflags= 110 | if [[ ${coverage} -eq 1 ]]; then 111 | # Transform $d to a smaller, valid file name. 112 | # For example: 113 | # * github.com/google/trillian becomes trillian.out 114 | # * github.com/google/trillian/cmd/createtree/keys becomes 115 | # trillian-cmd-createtree-keys.out 116 | local profile="${d}.out" 117 | profile="${profile#github.com/*/}" 118 | profile="${profile//\//-}" 119 | coverflags="-covermode=atomic -coverprofile='/tmp/trillian_profile/${profile}'" 120 | fi 121 | 122 | # Do not run go test in the loop, instead echo it so we can use xargs to 123 | # add some parallelism. 124 | echo go test \ 125 | -short \ 126 | -timeout=${GO_TEST_TIMEOUT:-5m} \ 127 | ${coverflags} \ 128 | ${goflags} "$d" 129 | done | xargs -I '{}' -P ${GO_TEST_PARALLELISM:-10} bash -c '{}' 130 | 131 | [[ ${coverage} -eq 1 ]] && \ 132 | cat /tmp/trillian_profile/*.out > /tmp/coverage.txt 133 | fi 134 | 135 | if [[ "${run_linters}" -eq 1 ]]; then 136 | echo 'running golint' 137 | printf '%s\n' ${go_srcs} | xargs -I'{}' golint --set_exit_status '{}' 138 | 139 | echo 'running go vet' 140 | printf '%s\n' ${go_srcs} | xargs -I'{}' go vet '{}' 141 | 142 | echo 'running gocyclo' 143 | printf '%s\n' ${go_srcs} | xargs -I'{}' bash -c 'gocyclo -over 25 {}' 144 | 145 | echo 'running misspell' 146 | printf '%s\n' ${go_srcs} | xargs -I'{}' misspell -error -i cancelled,CANCELLED -locale US '{}' 147 | 148 | echo 'checking license header' 149 | local nolicense="$(grep -L 'Apache License' ${go_srcs} ${proto_srcs})" 150 | if [[ "${nolicense}" ]]; then 151 | echo "Missing license header in: ${nolicense}" 152 | exit 2 153 | fi 154 | fi 155 | 156 | if [[ "${run_generate}" -eq 1 ]]; then 157 | echo 'running go generate' 158 | go generate -run="protoc" ${go_dirs} 159 | go generate -run="mockgen" ${go_dirs} 160 | go generate -run="stringer" ${go_dirs} 161 | fi 162 | 163 | } 164 | 165 | main "$@" -------------------------------------------------------------------------------- /c/mcast/mcast.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2008, 2009, 2010, 2011, 2012 IBM CRL. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at: 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include "mcast.h" 24 | 25 | pthread_mutex_t mutex; 26 | 27 | /** 28 | * send mcast msg. 29 | * @param group multicast group ip 30 | * @param port multicast port 31 | * @param msg content to send 32 | * @param len_msg length of the content 33 | * @return 0 if success 34 | */ 35 | void mc_send(struct mc_send_arg* arg) 36 | { 37 | int sock_id; 38 | struct sockaddr_in addr; 39 | socklen_t len; 40 | int ret; 41 | 42 | if (!arg) { 43 | return ; 44 | } 45 | 46 | /* open a socket. only udp support multicast */ 47 | if ((sock_id = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 48 | perror("socket error"); 49 | return; 50 | } 51 | 52 | /* build address */ 53 | memset((void*)&addr, 0, sizeof(addr)); 54 | addr.sin_family = AF_INET; 55 | /*addr.sin_addr.s_addr = inet_addr(group_ip);*/ 56 | addr.sin_addr.s_addr = arg->group_ip; 57 | addr.sin_port = htons(arg->port); 58 | 59 | len = sizeof(addr); 60 | /* it's very easy, just send the data to the address:port */ 61 | while (!*arg->stop) { 62 | //printf("Send to %s:%u with %lu:%s\n", inet_ntoa(addr.sin_addr.s_addr), ntohs(addr.sin_port),arg->msg->hdr, arg->msg->payload); 63 | pthread_mutex_lock (&mutex); 64 | ret = sendto(sock_id, arg->msg, arg->len_msg, 0, (struct sockaddr *)&addr, len); 65 | pthread_mutex_unlock(&mutex); 66 | if (ret < 0) { 67 | perror("sendto error"); 68 | return; 69 | } 70 | sleep(1); 71 | } 72 | 73 | close(sock_id); 74 | return; 75 | } 76 | 77 | /** 78 | * receive mcast msg, parse and store it. 79 | * @param group multicast group 80 | * @param port multicast port 81 | */ 82 | void mc_recv(struct mc_recv_arg* arg) 83 | { 84 | int sock_id; 85 | struct sockaddr_in addr, sender; 86 | struct ip_mreq ipmr; 87 | socklen_t len; 88 | int ret; 89 | int count; 90 | struct mcast_msg *msg = malloc(sizeof(struct mcast_msg)); 91 | 92 | /* Step 1: open a socket, and bind */ 93 | if ((sock_id = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 94 | perror("socket error"); 95 | return; 96 | } 97 | 98 | memset((void*)&addr, 0, sizeof(addr)); 99 | addr.sin_family = AF_INET; 100 | addr.sin_addr.s_addr = htonl(INADDR_ANY); 101 | addr.sin_port = htons(arg->port); 102 | 103 | if (bind(sock_id, (struct sockaddr *)&addr, sizeof(addr)) < 0) { 104 | perror("bind error"); 105 | return; 106 | } 107 | 108 | /* Step 2: fill in a struct ip_mreq */ 109 | memset((void*)&ipmr, 0, sizeof(ipmr)); 110 | ipmr.imr_multiaddr.s_addr = arg->group_ip; /* multicast group ip */ 111 | ipmr.imr_interface.s_addr = htonl(INADDR_ANY); 112 | 113 | /* Step 3: call setsockopt with IP_ADD_MEMBERSHIP to support receiving multicast */ 114 | if (setsockopt(sock_id, IPPROTO_IP, IP_ADD_MEMBERSHIP, &ipmr, sizeof(ipmr)) < 0) { 115 | perror("setsockopt:IP_ADD_MEMBERSHIP"); 116 | if (errno == EBADF) 117 | printf("EBADF\n"); 118 | else if (errno == EFAULT) 119 | printf("EFAULT\n"); 120 | else if (errno == EINVAL) 121 | printf("EINVAL\n"); 122 | else if (errno == ENOPROTOOPT) 123 | printf("ENOPROTOOPT\n"); 124 | else if (errno == ENOTSOCK) 125 | printf("ENOTSOCK\n"); 126 | return; 127 | } 128 | 129 | /* Step 4: call recvfrom to receive multicast packets */ 130 | len = sizeof(sender); 131 | count = 0; 132 | while (!*arg->stop) { 133 | ret = recvfrom(sock_id, msg, sizeof(struct mcast_msg), 0, (struct sockaddr *)&sender, &len); 134 | pthread_mutex_lock (&mutex); 135 | pthread_mutex_unlock (&mutex); 136 | if (ret < 0) { 137 | perror("recvfrom error"); 138 | return; 139 | } 140 | printf("[%d] Receive from %s:%d with %lu:%s\n", count, inet_ntoa(sender.sin_addr.s_addr), ntohs(sender.sin_port),msg->hdr,msg->payload); 141 | count ++; 142 | } 143 | 144 | /* Step 5: call setsockopt with IP_DROP_MEMBERSHIP to drop from multicast */ 145 | if (setsockopt(sock_id, IPPROTO_IP, IP_DROP_MEMBERSHIP, &ipmr, sizeof(ipmr)) < 0) { 146 | perror("setsockopt:IP_DROP_MEMBERSHIP"); 147 | return; 148 | } 149 | 150 | /* Step 6: close the socket */ 151 | close(sock_id); 152 | free(msg); 153 | 154 | return; 155 | } 156 | -------------------------------------------------------------------------------- /programing/autopreview.vim: -------------------------------------------------------------------------------- 1 | " Description: 2 | " Vim plugin for previewing definitions of variables or functions on 3 | " the cursor automatically like Source Insight, it's great conveniet 4 | " when insight source codes. 5 | " 6 | " Maintainer: Yang Baohua 7 | " Last Change: 2008 July 29 8 | " Version: v1.0 9 | " 10 | " Acknowledgements: 11 | " Thanks to Vim-help & Mail-list 12 | " thinelephant[At]newsmth 13 | " Dieken[At]newsmth 14 | " 15 | " Usage: 16 | " Drop this file to your plugin directory of vim. 17 | " 18 | " Set g:AutoPreview_enabled to 1 or 0 and g:AutoPreview_allowed_filetypes 19 | " in your vimrc, here is the default setting: 20 | " 21 | " let g:AutoPreview_enabled = 1 22 | " let g:AutoPreview_allowed_filetypes = ["c", "cpp", "java"] 23 | " 24 | " The file type of current buffer can be checked with `:echo &ft`, and 25 | " you can call `:AutoPreviewToggle` command to enable or disable this 26 | " plugin at runtime, for example: 27 | " 28 | " nnoremap :AutoPreviewToggle 29 | " inoremap :AutoPreviewTogglei 30 | " 31 | " You'd better set 'updatetime' option to 1000 or even less for good 32 | " responsibility, see `:help updatetime` for details, for example: 33 | " 34 | " set updatetime=500 35 | " 36 | " ChangeLog: 37 | " 2008-05-07 Yang Baohua 38 | " release v0.1 39 | " * initial inspiration and implementation 40 | " 41 | " 2008-05-09 Liu Yubao 42 | " release v0.2 43 | " * cleanup, optimize and enhance 44 | " 45 | " 2008-07-29 Yang Baohua 46 | " release v1.0: a stable and total version 47 | " * add highlight effect with previewword 48 | " * deal with folded codes 49 | " * some changes with the use guide document 50 | 51 | if exists ("loaded_autopreview") || !has("autocmd") || !exists(":filetype") 52 | finish 53 | endif 54 | let loaded_autopreview = 1 55 | 56 | 57 | if !exists("g:AutoPreview_enabled") 58 | let g:AutoPreview_enabled = 0 59 | endif 60 | if !exists("g:AutoPreview_allowed_filetypes") 61 | let g:AutoPreview_allowed_filetypes = ["c", "cpp", "java"] 62 | endif 63 | 64 | command! -nargs=0 -bar AutoPreviewToggle :call s:AutoPreviewToggle() 65 | 66 | if g:AutoPreview_enabled 67 | augroup AutoPreview 68 | au! FileType * :call s:SetCursorHoldAutoCmd() 69 | augroup END 70 | endif 71 | 72 | 73 | func s:AutoPreviewToggle() 74 | if g:AutoPreview_enabled 75 | silent! exe "pclose" 76 | silent! :au! AutoPreview 77 | else 78 | silent! call s:PreviewWord() 79 | augroup AutoPreview 80 | au! FileType * :call s:SetCursorHoldAutoCmd() 81 | let i = 1 82 | let n = bufnr("$") 83 | while (i <= n) 84 | if buflisted(i) && index(g:AutoPreview_allowed_filetypes, 85 | \ getbufvar(i, "&ft")) >= 0 && 86 | \ !getbufvar(i, "&previewwindow") 87 | exe "au! CursorHold nested :call s:PreviewWord()" 88 | exe "au! CursorHoldI nested :call s:PreviewWord()" 89 | endif 90 | let i = i + 1 91 | endwhile 92 | augroup END 93 | endif 94 | let g:AutoPreview_enabled = !g:AutoPreview_enabled 95 | endfunc 96 | 97 | 98 | func s:SetCursorHoldAutoCmd() 99 | if &previewwindow 100 | return 101 | endif 102 | 103 | augroup AutoPreview 104 | if index(g:AutoPreview_allowed_filetypes, &ft) >= 0 105 | " auto refresh the ptag window 106 | au! CursorHold nested :call s:PreviewWord() 107 | au! CursorHoldI nested :call s:PreviewWord() 108 | ":echo "set autocmd for " . &ft . " in " . expand("%") 109 | else 110 | au! CursorHold 111 | au! CursorHoldI 112 | ":echo "unset autocmd for " . &ft . " in " . expand("%") 113 | endif 114 | augroup END 115 | endfunc 116 | 117 | func s:PreviewWord() 118 | if &previewwindow 119 | return 120 | endif 121 | let w = expand("") " get the word under cursor 122 | if w =~ '\a' " if the word contains a letter 123 | try 124 | silent! exe "ptag " . w 125 | catch 126 | return 127 | endtry 128 | let oldwin = winnr() "get current window 129 | silent! wincmd P "jump to preview window 130 | if &previewwindow "if jump to preview windows successfully 131 | if has("folding") 132 | silent! .foldopen "unfold 133 | endif 134 | call search("$","b") "to the end of last line 135 | let w = substitute(w,'\\','\\\\',"") 136 | call search('\<\V' . w . '\>') "cursor on the match word 137 | 138 | match none "delete the current highlight marks 139 | 140 | "high light the match word in the previewwindow 141 | hi previewWord term=bold ctermbg=green guibg=green 142 | exe 'match previewWord "\%' . line(".") . 'l\%' . col(".") . 'c\k*"' 143 | exec oldwin.'wincmd w' 144 | "back from preview window 145 | endif 146 | endif 147 | endfun 148 | 149 | -------------------------------------------------------------------------------- /system/netlink/gnKernel.c: -------------------------------------------------------------------------------- 1 | /** 2 | * This file demo how to register a new generic netlink family in kernel, 3 | * using libnl APIs. 4 | */ 5 | #include 6 | #include 7 | #include 8 | 9 | #define VERSION_NR 0x1 10 | #ifndef NL_FAMILY_NAME 11 | #define NL_FAMILY_NAME "TEST_FAMILY" 12 | #endif 13 | 14 | /* attributes (variables): the index in this enum is used as a reference for the type, 15 | * userspace application has to indicate the corresponding type 16 | * the policy is used for security considerations 17 | */ 18 | enum { 19 | TEST_ATTR_UNSPEC, 20 | TEST_ATTR_MSG, 21 | __TEST_ATTR_MAX, 22 | }; 23 | #define TEST_ATTR_MAX (__TEST_ATTR_MAX - 1) 24 | 25 | /* attribute policy: defines which attribute has which type (e.g int, char * etc) 26 | * possible values defined in net/netlink.h 27 | */ 28 | static struct nla_policy test_nla_policy[TEST_ATTR_MAX + 1] = { 29 | [TEST_ATTR_MSG] = { .type = NLA_NUL_STRING }, 30 | }; 31 | 32 | /* family*/ 33 | static struct genl_family test_family = { 34 | .id = GENL_ID_GENERATE, //genetlink should generate an id 35 | .hdrsize = 0, 36 | .name = NL_FAMILY_NAME, //the name of this family, used by userspace application 37 | .version = VERSION_NR, //version number 38 | .maxattr = TEST_ATTR_MAX, 39 | }; 40 | 41 | /* commands: enumeration of all commands (functions), 42 | * used by userspace application to identify command to be ececuted 43 | */ 44 | enum { 45 | TEST_CMD_UNSPEC, 46 | TEST_CMD_ECHO, 47 | __TEST_CMD_MAX, 48 | }; 49 | #define TEST_CMD_MAX (__TEST_CMD_MAX - 1) 50 | 51 | 52 | static int test_echo(struct sk_buff *skb_2, struct genl_info *info); 53 | 54 | /* commands: mapping between the command enumeration and the actual function*/ 55 | static struct genl_ops test_ops_echo = { 56 | .cmd = TEST_CMD_ECHO, 57 | .flags = 0, 58 | .policy = test_nla_policy, 59 | .doit = test_echo, 60 | .dumpit = NULL, 61 | }; 62 | 63 | /* an echo command, receives a message, prints it and sends another message back */ 64 | static int test_echo(struct sk_buff *skb_2, struct genl_info *info) 65 | { 66 | struct nlattr *na; 67 | struct sk_buff *skb; 68 | int rc; 69 | void *msg_head; 70 | char * mydata; 71 | 72 | if (info == NULL) 73 | goto out; 74 | 75 | /*for each attribute there is an index in info->attrs which points to a nlattr structure 76 | *in this structure the data is given 77 | */ 78 | na = info->attrs[TEST_ATTR_MSG]; 79 | if (na) { 80 | mydata = (char *)nla_data(na); 81 | if (mydata == NULL) 82 | printk("error while receiving data\n"); 83 | else 84 | printk("[Kernelspace]: received: %s\n", mydata); 85 | } else { 86 | printk("no info->attrs %i\n", TEST_ATTR_MSG); 87 | } 88 | 89 | /* send a message back*/ 90 | /* allocate some memory, since the size is not yet known use NLMSG_GOODSIZE*/ 91 | skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); 92 | if (skb == NULL) 93 | goto out; 94 | 95 | /* create the message headers: 96 | struct sk_buff *, int (sending) pid, int sequence number, struct genl_family *, int flags, 97 | u8 command index (why do we need this?) 98 | */ 99 | msg_head = genlmsg_put(skb, 0, info->snd_seq+1, &test_family, 0, TEST_CMD_ECHO); 100 | if (msg_head == NULL) { 101 | rc = -ENOMEM; 102 | goto out; 103 | } 104 | /* add a TEST_ATTR_MSG attribute (actual value to be sent) */ 105 | rc = nla_put_string(skb, TEST_ATTR_MSG, "Received your msg, and hello Userspace."); 106 | if (rc != 0) 107 | goto out; 108 | 109 | /* finalize the message */ 110 | genlmsg_end(skb, msg_head); 111 | 112 | /* send the message back */ 113 | rc = genlmsg_unicast(&init_net, skb, info->snd_pid); 114 | if (rc != 0) 115 | goto out; 116 | return 0; 117 | 118 | out: 119 | printk("an error occured in test_echo:\n"); 120 | return 0; 121 | } 122 | 123 | static int __init gnKernel_init(void) 124 | { 125 | int rc; 126 | 127 | /*register new family*/ 128 | rc = genl_register_family(&test_family); 129 | if (rc != 0) { 130 | printk("failed in registering new family with error %i.\n",rc); 131 | return -1; 132 | } 133 | 134 | /*register functions (commands) of the new family*/ 135 | rc = genl_register_ops(&test_family, &test_ops_echo); 136 | if (rc != 0){ 137 | printk("failed in registering operations with error %i.\n",rc); 138 | genl_unregister_family(&test_family); 139 | return -1; 140 | } 141 | 142 | printk("TEST GENL MODULE INIT() SUCCESSFULLY.\n"); 143 | return 0; 144 | } 145 | 146 | static void __exit gnKernel_exit(void) 147 | { 148 | int ret; 149 | 150 | /*unregister the operations.*/ 151 | ret = genl_unregister_ops(&test_family, &test_ops_echo); 152 | if(ret != 0){ 153 | printk("failed in unregistering ops with error %i.\n",ret); 154 | return; 155 | } 156 | 157 | /*unregister the family.*/ 158 | ret = genl_unregister_family(&test_family); 159 | if(ret != 0){ 160 | printk("failed in unregistering family with error %i.\n",ret); 161 | return; 162 | } 163 | 164 | printk("TEST GENL MODULE EXIT() SUCCESSFULLY.\n"); 165 | } 166 | 167 | module_init(gnKernel_init); 168 | module_exit(gnKernel_exit); 169 | MODULE_LICENSE("GPL"); 170 | -------------------------------------------------------------------------------- /docker/ovs_perf.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # This script will start two iperf containers, connected by an openvswitch bridge. 3 | 4 | #local ovs bridge setting 5 | OVS_BR=br0 6 | PORT_SERVER="port_server" 7 | PORT_CLIENT="port_client" 8 | PORT_MIRROR="port_mirror" 9 | CONTAINER_SERVER="container_server" 10 | CONTAINER_CLIENT="container_client" 11 | CONTAINER_MIRROR="container_mirror" 12 | 13 | #sflow collector parameters 14 | COLLECTOR_IP=9.186.100.100 15 | COLLECTOR_PORT=6343 16 | AGENT_IP=eth0 17 | HEADER_BYTES=128 18 | SAMPLING_N=8 19 | POLLING_SECS=10 20 | TIMEOUT=10 21 | 22 | 23 | 24 | ## DO NOT MODIFY THE FOLLOWING PART, UNLESS YOU KNOW WHAT IT MEANS ## 25 | echo_r () { 26 | [ $# -ne 1 ] && return 0 27 | echo -e "\033[31m$1\033[0m" 28 | } 29 | echo_g () { 30 | [ $# -ne 1 ] && return 0 31 | echo -e "\033[32m$1\033[0m" 32 | } 33 | echo_y () { 34 | [ $# -ne 1 ] && return 0 35 | echo -e "\033[33m$1\033[0m" 36 | } 37 | echo_b () { 38 | [ $# -ne 1 ] && return 0 39 | echo -e "\033[34m$1\033[0m" 40 | } 41 | 42 | get_port_uuid() { 43 | [ $# -ne 1 ] && echo_r "Wrong parameter number is given: $#" && exit 1 44 | local port_name=$1 45 | echo `ovs-vsctl get port "${port_name}" _uuid` 46 | } 47 | 48 | set_mirror() { 49 | [ $# -ne 4 ] && echo_r "Wrong parameter number is given: $#" && exit 1 50 | local OVS_BR=$1 51 | local PORT_SERVER=$2 52 | local PORT_CLIENT=$3 53 | local PORT_MIRROR=$4 54 | ovs-vsctl -- set Bridge ${OVS_BR} mirrors=@m \ 55 | -- --id=@port_server get port ${PORT_SERVER}_l \ 56 | -- --id=@port_client get port ${PORT_CLIENT}_l \ 57 | -- --id=@port_mirror get port ${PORT_MIRROR}_l \ 58 | -- --id=@m create Mirror name=mymirror select_src_port=@port_server select_dst_port=@port_server output-port=@port_mirror 59 | } 60 | 61 | set_sflow() { 62 | [ $# -ne 1 ] && echo_r "Wrong parameter number is given: $#" && exit 1 63 | local OVS_BR=$1 64 | ovs-vsctl -- --id=@sflow create sflow \ 65 | agent=${AGENT_IP} target=\"${COLLECTOR_IP}:${COLLECTOR_PORT}\" header=${HEADER_BYTES} sampling=${SAMPLING_N} polling=${POLLING_SECS} \ 66 | -- set bridge ${OVS_BR} sflow=@sflow 67 | } 68 | 69 | set_netflow() { 70 | [ $# -ne 1 ] && echo_r "Wrong parameter number is given: $#" && exit 1 71 | local OVS_BR=$1 72 | sudo ovs-vsctl -- set Bridge ${OVS_BR} netflow=@nf -- --id=@nf \ 73 | create NetFlow targets=\"${COLLECTOR_IP}:${COLLECTOR_PORT}\" \ 74 | active-timeout=${TIMEOUT} 75 | } 76 | 77 | echo_g "Required: openvswitch, docker, ovs-docker, sar" 78 | 79 | echo_g "Cleaning environment..." 80 | echo_b "Cleaning the existing containers..." 81 | sudo docker rm -f ${CONTAINER_SERVER} ${CONTAINER_CLIENT} 82 | 83 | echo_g "Cleaning the ovs bridge ${OVS_BR}..." 84 | sudo ovs-vsctl --if-exists del-br ${OVS_BR} 85 | 86 | echo_b "Installing ovs-docker into /usr/bin..." 87 | install ovs-docker /usr/bin/ 88 | 89 | # Check requisites here...TBD 90 | #[ ! -x ovs-docker ] && \ 91 | # echo "Required ovs-docker script is not found" && \ 92 | # echo "Download from https://github.com/openvswitch/ovs/raw/master/utilities/ovs-docker" && \ 93 | # exit; 94 | 95 | echo_g "Creating an ovs bridge ${OVS_BR}..." 96 | sudo ovs-vsctl --may-exist add-br ${OVS_BR} 97 | 98 | echo_g "Configuring internal port to 172.17.0.1/16..." 99 | sudo ifconfig ${OVS_BR} 172.17.0.1/16 100 | 101 | echo_g "Boot two raw containers without networking..." 102 | cid1=`sudo docker run -d --net=none --privileged=true --name=${CONTAINER_SERVER} gdanii/iperf:latest /bin/sh -c "while true; do echo active; sleep 60; done"` 103 | cid2=`sudo docker run -d --net=none --privileged=true --name=${CONTAINER_CLIENT} gdanii/iperf:latest /bin/sh -c "while true; do echo active; sleep 60; done"` 104 | 105 | echo_g "Adding nic insides containers, and binding to the ovs bridge..." 106 | sudo ovs-docker add-port ${OVS_BR} eth0 $cid1 172.17.0.2/16 172.17.0.1 ${PORT_SERVER} 107 | sudo ovs-docker add-port ${OVS_BR} eth0 $cid2 172.17.0.3/16 172.17.0.1 ${PORT_CLIENT} 108 | 109 | echo_g "Boot a raw containers for mirroring..." 110 | cid3=`sudo docker run -d --net=none --privileged=true --name=${CONTAINER_MIRROR} gdanii/iperf:latest /bin/sh -c "while true; do echo active; sleep 60; done"` 111 | echo_g "Creating a mirror port: ${PORT_MIRROR}..." 112 | sudo ovs-docker add-port ${OVS_BR} eth0 $cid3 172.17.0.4/16 172.17.0.1 ${PORT_MIRROR} 113 | 114 | ### core capturing part ### 115 | #set_mirror ${OVS_BR} ${PORT_SERVER} ${PORT_CLIENT} ${PORT_MIRROR} 116 | #set_sflow ${OVS_BR} 117 | set_netflow ${OVS_BR} 118 | 119 | sleep 5 120 | 121 | echo_g "Starting iperf server on 172.17.0.2..." 122 | sudo docker exec $cid1 iperf -s & 123 | sleep 1 124 | 125 | ### core statistic part ### 126 | sar -u 1 22 > stat_result.txt & 127 | sleep 1 128 | 129 | echo_g "Starting iperf client on 172.17.0.3 for 20 seconds..." 130 | sudo docker exec $cid2 iperf -t 20 -c 172.17.0.2 131 | 132 | echo_g "Removing the binding interfaces..." 133 | sudo ovs-docker del-port ${OVS_BR} eth0 $cid1 134 | sudo ovs-docker del-port ${OVS_BR} eth0 $cid2 135 | sudo ovs-docker del-port ${OVS_BR} eth0 $cid3 136 | 137 | echo_g "Killing the containers..." 138 | sudo docker rm -f $cid1 $cid2 $cid3 139 | 140 | echo_g "Clearing the mirroring rules..." 141 | ovs-vsctl clear Bridge ${OVS_BR} mirrors 142 | 143 | echo_g "Clearing the sflow rules..." 144 | sudo ovs-vsctl -- clear Bridge ${OVS_BR} sflow 145 | 146 | echo_g "Clearing the netflow rules..." 147 | sudo ovs-vsctl clear Bridge ${OVS_BR} netflow 148 | 149 | echo_g "Removing the ovs bridge ${OVS_BR}..." 150 | sudo ovs-vsctl --if-exists del-br ${OVS_BR} 151 | 152 | -------------------------------------------------------------------------------- /c/sendRawPkt/sendRawPkt.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This program is free software: you can redistribute it and/or modify 3 | * it under the terms of the GNU General Public License as published by 4 | * the Free Software Foundation, either version 3 of the License, or 5 | * (at your option) any later version. 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | #define DEST_MAC0 0x00 21 | #define DEST_MAC1 0x11 22 | #define DEST_MAC2 0x22 23 | #define DEST_MAC3 0x33 24 | #define DEST_MAC4 0x44 25 | #define DEST_MAC5 0x55 26 | 27 | #define DEFAULT_IF "eth0" 28 | #define BUF_SIZ 1024 29 | 30 | /* this function generates header checksums */ 31 | unsigned short csum (unsigned short *buf, int nwords) 32 | { 33 | unsigned long sum; 34 | for (sum = 0; nwords > 0; nwords--) 35 | sum += *buf++; 36 | sum = (sum >> 16) + (sum & 0xffff); 37 | sum += (sum >> 16); 38 | return ~sum; 39 | } 40 | 41 | int get_gw_ip(char *eth, char *ipaddr) 42 | { 43 | int sock_fd; 44 | struct sockaddr_in my_addr; 45 | struct ifreq ifr; 46 | 47 | /**//* Get socket file descriptor */ 48 | if ((sock_fd = socket(PF_INET, SOCK_DGRAM, 0)) == -1) { 49 | perror("socket"); 50 | return -1; 51 | } 52 | 53 | /**//* Get IP Address */ 54 | strncpy(ifr.ifr_name, eth, IF_NAMESIZE); 55 | ifr.ifr_name[IFNAMSIZ-1]='\0'; 56 | 57 | if (ioctl(sock_fd, SIOCGIFADDR, &ifr) < 0) { 58 | printf(":No Such Device %s\n",eth); 59 | return -1; 60 | } 61 | 62 | memcpy(&my_addr, &ifr.ifr_addr, sizeof(my_addr)); 63 | strcpy(ipaddr, inet_ntoa(my_addr.sin_addr)); 64 | close(sock_fd); 65 | return 0; 66 | } 67 | 68 | /** 69 | * main function. 70 | */ 71 | int main(int argc, char *argv[]) 72 | { 73 | int sockfd; 74 | struct ifreq if_idx; 75 | struct ifreq if_mac; 76 | int tx_len = 0; 77 | char sendbuf[BUF_SIZ]; 78 | struct sockaddr_ll socket_address; 79 | char ifName[IFNAMSIZ]; 80 | char ipaddr[20]; 81 | 82 | 83 | /* Get interface name from input parameter */ 84 | if (argc > 1) 85 | strcpy(ifName, argv[1]); 86 | else 87 | strcpy(ifName, DEFAULT_IF); 88 | 89 | /* Open RAW socket to send on */ 90 | if ((sockfd = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW)) == -1) { 91 | perror("socket"); 92 | } 93 | 94 | /* Get the index of the interface to send on */ 95 | memset(&if_idx, 0, sizeof(struct ifreq)); 96 | strncpy(if_idx.ifr_name, ifName, IFNAMSIZ-1); 97 | if (ioctl(sockfd, SIOCGIFINDEX, &if_idx) < 0) 98 | perror("SIOCGIFINDEX"); 99 | /* Get the MAC address of the interface to send on */ 100 | memset(&if_mac, 0, sizeof(struct ifreq)); 101 | strncpy(if_mac.ifr_name, ifName, IFNAMSIZ-1); 102 | if (ioctl(sockfd, SIOCGIFHWADDR, &if_mac) < 0) 103 | perror("SIOCGIFHWADDR"); 104 | 105 | memset(sendbuf, 0, BUF_SIZ); 106 | /* Construct the Ethernet header */ 107 | struct ether_header *eh = (struct ether_header *) sendbuf; 108 | eh->ether_shost[0] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[0]; 109 | eh->ether_shost[1] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[1]; 110 | eh->ether_shost[2] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[2]; 111 | eh->ether_shost[3] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[3]; 112 | eh->ether_shost[4] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[4]; 113 | eh->ether_shost[5] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[5]; 114 | eh->ether_dhost[0] = DEST_MAC0; 115 | eh->ether_dhost[1] = DEST_MAC1; 116 | eh->ether_dhost[2] = DEST_MAC2; 117 | eh->ether_dhost[3] = DEST_MAC3; 118 | eh->ether_dhost[4] = DEST_MAC4; 119 | eh->ether_dhost[5] = DEST_MAC5; 120 | /* Ethertype field */ 121 | eh->ether_type = htons(ETH_P_IP); 122 | tx_len += sizeof(struct ether_header); 123 | 124 | /* IP header field */ 125 | get_gw_ip(ifName,ipaddr); 126 | struct iphdr *iph = (struct iphdr *) (sendbuf + sizeof(struct ether_header)); 127 | iph->ihl=5; 128 | iph->version=4; 129 | iph->tos=0; 130 | iph->id = htonl(12345); 131 | iph->frag_off = 0; 132 | iph->ttl = 255; 133 | iph->protocol = IPPROTO_UDP; 134 | //iph->protocol = IPPROTO_ENCAP; 135 | //iph->protocol = IPPROTO_ETHERIP; 136 | iph->saddr=inet_addr(ipaddr); 137 | iph->daddr=inet_addr("10.0.0.2"); 138 | iph->check=0; //csum here 139 | tx_len += sizeof(struct iphdr); 140 | 141 | /* UDP header*/ 142 | struct udphdr *udph = (struct udphdr *) (sendbuf + sizeof(struct iphdr) + sizeof(struct ether_header)); 143 | /* UDP Header */ 144 | udph->source = htons(3423); 145 | udph->dest = htons(5342); 146 | udph->check = 0; // skip 147 | tx_len += sizeof(struct udphdr); 148 | 149 | /* Packet data */ 150 | sendbuf[tx_len++] = 0xde; 151 | sendbuf[tx_len++] = 0xad; 152 | sendbuf[tx_len++] = 0xbe; 153 | sendbuf[tx_len++] = 0xef; 154 | 155 | /* Length of UDP payload and header */ 156 | udph->len = htons(tx_len - sizeof(struct ether_header) - sizeof(struct iphdr)); 157 | /* Length of IP payload and header */ 158 | iph->tot_len = htons(tx_len - sizeof(struct ether_header)); 159 | /* Calculate IP checksum on completed header */ 160 | iph->check = csum((unsigned short *)(sendbuf+sizeof(struct ether_header)), sizeof(struct iphdr)>>1); 161 | 162 | /* Index of the network device */ 163 | socket_address.sll_ifindex = if_idx.ifr_ifindex; 164 | /* Address length*/ 165 | socket_address.sll_halen = ETH_ALEN; 166 | /* Destination MAC */ 167 | socket_address.sll_addr[0] = DEST_MAC0; 168 | socket_address.sll_addr[1] = DEST_MAC1; 169 | socket_address.sll_addr[2] = DEST_MAC2; 170 | socket_address.sll_addr[3] = DEST_MAC3; 171 | socket_address.sll_addr[4] = DEST_MAC4; 172 | socket_address.sll_addr[5] = DEST_MAC5; 173 | 174 | /* Send packet */ 175 | if (sendto(sockfd, sendbuf, tx_len, 0, (struct sockaddr*)&socket_address, sizeof(struct sockaddr_ll)) < 0) 176 | printf("Send failed\n"); 177 | 178 | return 0; 179 | } 180 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Code Snippet 2 | ============ 3 | 4 | Some simple but useful source codes, scripts or templates, on networking, programming, algorithm, etc. 5 | 6 | Most of them are written by me, while some one may be collected from Internet or modified based or public-available codes. 7 | 8 | All files are free to use, distribute or modify, with keeping the declare part on the file head. However, the authors WILL NOT be responsible for any result by these files. If you think there might be some one affecting your privilege, please let me know. 9 | 10 | 11 | ## [algorithm](algorithm) 12 | Some code blocks for test algorithms, such as sorting, searching, etc.. 13 | 14 | Name | Description 15 | -- | -- 16 | bf-gdt | Example to show how to use the bloom-filter based gdt data structure. 17 | bit_op | show operation on bit 18 | bitmap | show usage of bitmap 19 | sort | sorting 20 | match | searching 21 | 22 | ## [C](c) 23 | 24 | C programing. 25 | 26 | Name | Description 27 | -- | -- 28 | getopt | Show how to use getopt functions. 29 | io | Test read and write functions. 30 | mcast | Test the multi-cast packet sending and receiving, using a thread separately. 31 | [sendRawPkt](c/sendRawPkt) | Example to show how to construct a raw socket packet, with manually setting the mac, ip and udp header. 32 | 33 | ## [Docker](docker) 34 | Some docker related scripts and tools. 35 | 36 | Name | Description 37 | -- | -- 38 | docker_cleanup | Do some cleanup work on Docker Host, e.g., removing stopped containers and untagged images. 39 | ovs_perf | Test two containers connecting through OpenvSwitch with iperf 40 | save_all_images | Save all local Docker images into tar.gz files. 41 | restore_images | This script will load into the tar.gz files as images. 42 | 43 | ## [golang](golnag) 44 | Some golang examples 45 | 46 | Name | Description 47 | -- | -- 48 | plugin | Demo how to use plugin to generate a .so file and then load it. 49 | grpc | Demo how to use gRPC with the go module. 50 | 51 | ## [programing](programing) 52 | Some simple tools to help programing more efficiently. 53 | 54 | Name | Description 55 | -- | -- 56 | autopreview | A light script plugin for Vim that could automatically preview the definition of variables or functions in a tiny window like source insight. Also be hosted at the [vim homepage](http://www.vim.org/scripts/script.php?script_id=2228). 57 | Makefile.template | Simple template of Makefile. 58 | 59 | ## [python](python) 60 | Simple codes to show how to use python libraries, e.g., regrex, encoding. 61 | 62 | Name | Description 63 | -- | -- 64 | [add_watermark](python/add_watermark.py) | Add repeated watermark to pdf file 65 | [amqp](python/amqp) | Demo the usage of AMQP by the kombu and pika. 66 | [concurrency](python/concurrency) | Some scripts on concurrency programming, such as threading, eventlet... 67 | [gitbook_gen](python/gitbook_gen.py) | Generate an initialized gitbook project based on the given source code path. 68 | [json](python/json) | Example to show how to use json encoding/decoding in python. 69 | [json_flatter](python/json_flatter.py) | Flat the structure of json files under given path. 70 | [mqtt](python/mqtt) | Demo the usage of MQTT. 71 | [prefixCutter](python/prefixCutter.py) | Calculate the cut prefixes by given two prefixes. 72 | [pull_agent](python/pull_agent.py) | Daemon agent to pull some content from remote host using scp, and do action after check changes. 73 | [push_agent](python/push_agent.py) | Daemon agent to push some msg to remote host file. 74 | [remote_watcher](python/remote_watcher.py) | Watch the specified file on remote using scp, trigger action when there finds keyword in it. 75 | [stat_parse](python/stat_parse) | Parse the ifconfig results and get the pkts/bytes number. 76 | [tls_server](python/tls_server) | A simple tls server by python, which enable tls, and allow server to check client's certificate. 77 | [wsgi](python/wsgi) | A demo to show how to use wsgi and paste deployment to provide a web service. Run `python server.py` and get the version information by visiting localhost:8080. 78 | 79 | ## [shell](shell) 80 | Useful shell functions. 81 | 82 | ```bash 83 | source functions 84 | ``` 85 | 86 | ## [security](security) 87 | Demo of usage of security tools including cfssl and openssl. 88 | 89 | Name | Description 90 | -- | -- 91 | [cfssl](security/cfssl) | Use cfssl to generate certificate. 92 | [openssl](security/openssl) | Use openssl to generate certificate and serve a website with TLS. 93 | 94 | ## [system](system) 95 | Some system related tools, such as init a fresh os. 96 | 97 | Name | Description 98 | -- | -- 99 | cpu | Get overall cpu utilization at linux platform. 100 | ether_bridge | Connect to network interface and forward ethernet packets between them bidirectionally, supporting options. 101 | getIP | Some scripts to get local ip address of the network interfaces. 102 | [netlink](system/netlink) | Test bidirectional communication between userspace and kernelspace, using low-level netlink APIs in the userspace, and libnl APIs in the kernelspace. 103 | [nss](system/nss) | This script will show all ip address inside each network namespace. If give a parameter, then it will show the network namespace containing the key. This is useful for openstack debugging. 104 | rm.sh | A much safer rm script, which can automatically backup removed files, in case deleting useful data (beta version). 105 | service | Example of a system service script in Debian/Ubuntu system, supporting running complicated command with arbitrary parameters. 106 | socket | Example of socket programing using C 107 | sysctl.conf | Optimized configuration for /etc/sysctl.conf. 108 | limits.conf | Optimized configuration for /etc/security/limits.conf. 109 | 110 | ## [Web](web) 111 | 112 | Website related. 113 | 114 | Name | Description 115 | -- | -- 116 | getweb.sh | Simple but powerful script, which can automatically download a website by URL and save it to local directory for offline reference. 117 | httpserver | A simple webserver by python, which tracks request number of each visitor's IP address. 118 | sm_watcher | Track (and filter) the board articles on the given url (newsmth now), and show them in a webpage in realtime (using jquery and HTML5 websocket). 119 | ss_watcher | Auto fetch proxy information and update local config. 120 | 121 | -------------------------------------------------------------------------------- /algorithm/bf-gdt/bf.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2012 IBM CRL. 3 | * 4 | * This program is free software; you can redistribute it and/or 5 | * modify it under the terms of version 2 of the GNU General Public 6 | * License as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, but 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 | * General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License 14 | * along with this program; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 16 | * 02110-1301, USA 17 | */ 18 | 19 | #ifdef __KERNEL__ 20 | #include 21 | #include 22 | #include 23 | #include 24 | #else 25 | #include 26 | #include 27 | #endif 28 | 29 | #include "bf.h" 30 | 31 | #define SETBIT(array, i) (array[i/SIZE_CHAR] |= (1<<(i%SIZE_CHAR))) 32 | #define GETBIT(array, i) (array[i/SIZE_CHAR] & (1<<(i%SIZE_CHAR))) 33 | 34 | /** 35 | * hash function: SAX. 36 | */ 37 | u32 sax_hash(const char *key) 38 | { 39 | u32 h=0; 40 | while(*key) h^=(h<<5)+(h>>2)+(unsigned char)*key++; 41 | return h; 42 | } 43 | 44 | /** 45 | * hash function: sdbm. 46 | */ 47 | u32 sdbm_hash(const char *key) 48 | { 49 | u32 h=0; 50 | while(*key) h=(unsigned char)*key++ + (h<<6) + (h<<16) - h; 51 | return h; 52 | } 53 | 54 | hashfunc_t hashFunctions[] = {sax_hash,sdbm_hash}; 55 | 56 | /** 57 | * Create array bloom_filter. 58 | * @param bf_id: bf_id for the bloom_filter 59 | * @param len: number of the bits in the bloom_filter 60 | * @param port_no: port_no associated. 61 | * @param nfuncs: number of the hash functions 62 | * @return: Pointer to the created bloom_filter. NULL if failed. 63 | */ 64 | struct bloom_filter *bf_create(u32 bf_id, u32 len, u16 port_no, u32 nfuncs) 65 | { 66 | struct bloom_filter *bf; 67 | int i; 68 | 69 | #ifdef __KERNEL__ 70 | if(!(bf=kmalloc(sizeof(struct bloom_filter),GFP_KERNEL))) { 71 | #else 72 | if(!(bf=malloc(sizeof(struct bloom_filter)))) { 73 | #endif 74 | /*printk("Error in kmalloc bf\i");*/ 75 | return NULL; 76 | } 77 | #ifdef __KERNEL__ 78 | if(!(bf->array=kcalloc((len+sizeof(char)-1)/sizeof(char), sizeof(char),GFP_KERNEL))) { 79 | /*printk("Error in kcalloc bf\i");*/ 80 | kfree(bf); 81 | #else 82 | if(!(bf->array=calloc((len+sizeof(char)-1)/sizeof(char), sizeof(char)))) { 83 | free(bf); 84 | #endif 85 | return NULL; 86 | } 87 | memset(bf->array,0,(len+sizeof(char)-1)/sizeof(char)); 88 | #ifdef __KERNEL__ 89 | if(!(bf->funcs=(hashfunc_t*)kmalloc(nfuncs*sizeof(hashfunc_t),GFP_KERNEL))) { 90 | kfree(bf->array); 91 | kfree(bf); 92 | #else 93 | if(!(bf->funcs=(hashfunc_t*)malloc(nfuncs*sizeof(hashfunc_t)))) { 94 | free(bf->array); 95 | free(bf); 96 | #endif 97 | /*printk("Error in kmalloc hashfuncs\i");*/ 98 | return NULL; 99 | } 100 | 101 | for(i=0; ifuncs[i]=hashFunctions[i]; 103 | } 104 | 105 | bf->bf_id = bf_id; 106 | bf->port_no = port_no; 107 | bf->len=len; 108 | bf->nfuncs=nfuncs; 109 | 110 | return bf; 111 | } 112 | 113 | /** 114 | * destroy a bloom_filter. 115 | * @param bf: the bloom_filter to destroy 116 | */ 117 | int bf_destroy(struct bloom_filter *bf) 118 | { 119 | #ifdef __KERNEL__ 120 | kfree(bf->array); 121 | kfree(bf->funcs); 122 | kfree(bf); 123 | #else 124 | free(bf->array); 125 | free(bf->funcs); 126 | free(bf); 127 | #endif 128 | return 0; 129 | } 130 | 131 | /** 132 | * add a new string to bf 133 | * @param bf: the bloom_filter to update 134 | * @param s: the string to add 135 | */ 136 | int bf_add(struct bloom_filter *bf, const char *s) 137 | { 138 | u32 i; 139 | 140 | /*printf("sizeof char= %u\n",sizeof(char)); */ 141 | for(i=0; infuncs; ++i) { 142 | /*printf("set bit %u\n",bf->funcs[i](s)%bf->len);*/ 143 | SETBIT(bf->array,bf->funcs[i](s)%bf->len); 144 | int k = bf->funcs[i](s)%bf->len; 145 | /*printf("bit[%u] = %u\n",k/8,bf->array[k/8]);*/ 146 | } 147 | 148 | #ifdef DEBUG 149 | printf("add %s into bf with id=0x%x.\n",s,bf->bf_id); 150 | char tmp[256]={0}; 151 | for (i=0;i<128;i++){ 152 | if(i%16==0){ 153 | printf("%u:\t",i/16); 154 | } 155 | printf("%x",bf->array[i]); 156 | if((i+1)%16==0){ 157 | printf("\n"); 158 | } 159 | } 160 | #endif 161 | 162 | return 0; 163 | } 164 | 165 | /** 166 | * test if s is in bf. 167 | * @param bf: the bloom_filter to test 168 | * @param s: the string to test 169 | * @return 1 if true 170 | */ 171 | int bf_check(struct bloom_filter *bf, const char *s) 172 | { 173 | u32 i; 174 | 175 | if (!bf || !bf->array) 176 | return 0; 177 | 178 | for(i=0; infuncs; ++i) { 179 | if(!(GETBIT(bf->array, bf->funcs[i](s)%bf->len))) { 180 | printf("test bit[%u]=%u",bf->funcs[i](s)%bf->len,GETBIT(bf->array, bf->funcs[i](s)%bf->len)); 181 | return 0; 182 | } 183 | } 184 | 185 | return 1; 186 | } 187 | 188 | /** 189 | * set the array content. 190 | * @param bf: bf who owns the array 191 | * @param array: new array content 192 | * @param len: new length 193 | * @return 0 if success, else -1 194 | */ 195 | int bf_set_array(struct bloom_filter *bf, const u8 *array, u32 len) 196 | { 197 | if (!bf || !array) 198 | return -1; 199 | u8 * tmp_array = NULL; 200 | #ifdef __KERNEL__ 201 | if(!(tmp_array=kcalloc((len+sizeof(char)-1)/sizeof(char), sizeof(char),GFP_KERNEL))) { 202 | kfree(tmp_array); 203 | /*printk("Error when kcalloc in bf_set_array()\n");*/ 204 | return -1; 205 | } 206 | kfree(bf->array); 207 | #else 208 | if(!(tmp_array=calloc((len+sizeof(char)-1)/sizeof(char), sizeof(char)))) { 209 | free(tmp_array); 210 | /*printk("Error when kcalloc in bf_set_array()\n");*/ 211 | return -1; 212 | } 213 | free(bf->array); 214 | #endif 215 | memcpy(tmp_array,array,(len+sizeof(char)-1)/sizeof(char)); 216 | bf->array = tmp_array; 217 | bf->len = len; 218 | return 0; 219 | } 220 | -------------------------------------------------------------------------------- /system/ether_bridge/LICENSE: -------------------------------------------------------------------------------- 1 | * This software is a modification of Tim Carstens' "sniffer.c" 2 | * demonstration source code, released as follows: 3 | * 4 | * sniffer.c 5 | * Copyright (c) 2002 Tim Carstens 6 | * 2002-01-07 7 | * Demonstration of using libpcap 8 | * timcarst -at- yahoo -dot- com 9 | * 10 | * "sniffer.c" is distributed under these terms: 11 | * 12 | * Redistribution and use in source and binary forms, with or without 13 | * modification, are permitted provided that the following conditions 14 | * are met: 15 | * 1. Redistributions of source code must retain the above copyright 16 | * notice, this list of conditions and the following disclaimer. 17 | * 2. Redistributions in binary form must reproduce the above copyright 18 | * notice, this list of conditions and the following disclaimer in the 19 | * documentation and/or other materials provided with the distribution. 20 | * 4. The name "Tim Carstens" may not be used to endorse or promote 21 | * products derived from this software without prior written permission 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 | * SUCH DAMAGE. 34 | * 35 | * 36 | * This software, "sniffex.c", is a derivative work of "sniffer.c" and is 37 | * covered by the following terms: 38 | * 39 | * Redistribution and use in source and binary forms, with or without 40 | * modification, are permitted provided that the following conditions 41 | * are met: 42 | * 1. Because this is a derivative work, you must comply with the "sniffer.c" 43 | * terms reproduced above. 44 | * 2. Redistributions of source code must retain the Tcpdump Group copyright 45 | * notice at the top of this source file, this list of conditions and the 46 | * following disclaimer. 47 | * 3. Redistributions in binary form must reproduce the above copyright 48 | * notice, this list of conditions and the following disclaimer in the 49 | * documentation and/or other materials provided with the distribution. 50 | * 4. The names "tcpdump" or "libpcap" may not be used to endorse or promote 51 | * products derived from this software without prior written permission. 52 | * 53 | * THERE IS ABSOLUTELY NO WARRANTY FOR THIS PROGRAM. 54 | * BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 55 | * FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 56 | * OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 57 | * PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 58 | * OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 59 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 60 | * TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 61 | * PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 62 | * REPAIR OR CORRECTION. 63 | * 64 | * IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 65 | * WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 66 | * REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 67 | * INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 68 | * OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 69 | * TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 70 | * YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 71 | * PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 72 | * POSSIBILITY OF SUCH DAMAGES. 73 | * 74 | * 75 | * This software, "PacketForward", is a derivative work of "sniffex.c" and is 76 | * covered by the following terms: 77 | * 78 | * Redistribution and use in source and binary forms, with or without 79 | * modification, are permitted provided that the following conditions 80 | * are met: 81 | * 1. Because this is a derivative work, you must comply with the "sniffer.c" 82 | * terms reproduced above. 83 | * 2. Redistributions of source code must retain the Tcpdump Group copyright 84 | * notice at the top of this source file, this list of conditions and the 85 | * following disclaimer. 86 | * 3. Redistributions in binary form must reproduce the above copyright 87 | * notice, this list of conditions and the following disclaimer in the 88 | * documentation and/or other materials provided with the distribution. 89 | * 4. The names "libnet" or "libpcap" may not be used to endorse or promote 90 | * products derived from this software without prior written permission. 91 | * 92 | * THERE IS ABSOLUTELY NO WARRANTY FOR THIS PROGRAM. 93 | * BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 94 | * FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 95 | * OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 96 | * PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 97 | * OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 98 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 99 | * TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 100 | * PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 101 | * REPAIR OR CORRECTION. 102 | * 103 | * IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 104 | * WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 105 | * REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 106 | * INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 107 | * OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 108 | * TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 109 | * YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 110 | * PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 111 | * POSSIBILITY OF SUCH DAMAGES. 112 | * 113 | -------------------------------------------------------------------------------- /python/gitbook_gen.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | This script will generate a gitbook projects based on the given dir. 4 | The generated dir contains the same structure and the corresponding markdown 5 | files. 6 | 7 | To update the structure, please keep a copy of your README.md and SUMMARY.md. 8 | This tool should never overwrite other files. 9 | 10 | Usage: python gitbook_gen [src_dir1] [src_dir2] ... 11 | 12 | The generated books will have names like `src_dir1_gitbook`, 'src_dir2_gitbook' 13 | 14 | Author: yeasy@github.com 15 | """ 16 | 17 | import __future__ # noqa 18 | import os 19 | import sys 20 | from fnmatch import fnmatch 21 | 22 | ROOT_PATH = os.getcwd() 23 | PROJECT = "" 24 | README = "README.md" 25 | SUMMARY = "SUMMARY.md" 26 | IGNORE_DIRS = ['*build*', '*__pycache__*', '*vendor*', '*test*', '*mock*', 27 | 'docs/*'] # path with these patterns will be ignored 28 | IGNORE_FILES = ['*_test.*', '*.md', '*.png', '*.rst'] # path with these patterns will be ignored 29 | 30 | generator_info = \ 31 | "The book structure is generated by [gitbook_gen]" \ 32 | "(https://github.com/yeasy/code_snippet/#gitbook_gen)." 33 | 34 | 35 | def init_gitbook_dir(dir_path, title): 36 | """ Initialized a gitbook dir, including: 37 | 38 | * README.md 39 | * SUMMARY.md 40 | 41 | :param dir_path: whole dir path 42 | :param title: project title 43 | :return: 44 | """ 45 | if not os.path.exists(dir_path): 46 | os.makedirs(dir_path) 47 | create_file(README, 48 | "# {}\n\n{}\n".format(title, generator_info), forced=False) 49 | create_file(SUMMARY, "# Summary\n\n* [Introduction](README.md)\n", 50 | forced=True) 51 | 52 | 53 | def refine_dirname(name): 54 | """ Refine a structrual path as a valid name string 55 | 56 | e.g., ./yeasy_book/ --> yeasy_book 57 | /tmp/a/b --> tmp_a_b 58 | 59 | :param name: directory name to refine 60 | :return: refined result 61 | """ 62 | result = name.strip('./\\' + os.sep) # tmp/a/b 63 | result = result.replace('/', os.sep) # tmp/a/b 64 | result = result.replace('\\', os.sep) # tmp/a/b 65 | return result.replace(os.sep, '_') 66 | 67 | 68 | def has_pattern(path_name, patterns=IGNORE_DIRS): 69 | """ Test whether path_name has a given pattern 70 | 71 | :param path_name: 72 | :param patterns: 73 | :return: 74 | """ 75 | 76 | def test_pattern(pattern): 77 | return fnmatch(path_name.lower(), pattern) 78 | result = list(filter(test_pattern, patterns)) 79 | if 'build/' in path_name: 80 | print(path_name) 81 | print(result) 82 | return len(result) > 0 83 | 84 | 85 | def process_dir(root_dir, level=1): 86 | """ Process the directory, checking sub dirs and files recursively. 87 | 88 | :param root_dir: current root dir 89 | :param level: current depth of the dir from the root 90 | :return: 91 | """ 92 | if level > 4: # do not process very deep dir 93 | return 94 | valid_dirs = list(filter(lambda x: not x.startswith('.'), os.listdir(root_dir))) 95 | list_dir = sorted(list(filter(lambda x: os.path.isdir(os.path.join(root_dir, x)), 96 | valid_dirs))) 97 | list_file = sorted(list(filter(lambda x: os.path.isfile(os.path.join(root_dir, x)) and 98 | not x.startswith('_'), valid_dirs))) 99 | for e in list_dir: # dirs 100 | if has_pattern(e, IGNORE_DIRS): 101 | continue 102 | path = os.path.join(root_dir, e).replace('.' + os.sep, '') 103 | if level == 4: 104 | create_file(PROJECT + os.sep + path + '.md', '#' * level + ' ' + e + ' 包\n') 105 | line = '* [%s](%s.md)' % (e, path.replace('\\', '/')) 106 | else: 107 | if not os.path.exists(PROJECT+os.sep+path): 108 | os.makedirs(PROJECT + os.sep + path) 109 | create_file(PROJECT + os.sep + path + os.sep + 'README.md', 110 | '#' * level + ' ' + e + ' 包\n', forced=False) 111 | line = '* [%s](%s/README.md)' % (e, path.replace('\\', '/')) 112 | update_file(SUMMARY, ' ' * 4 * (level - 1) + line) 113 | process_dir(path, level+1) 114 | for e in list_file: # files 115 | if has_pattern(e, IGNORE_FILES): 116 | continue 117 | name, suffix = os.path.splitext(e) # test .py 118 | path = os.path.join(root_dir, name).replace('.' + os.sep, '') \ 119 | + suffix.replace('.', '_') # test\test_py 120 | create_file(PROJECT + os.sep + path + '.md', '#' * level + ' ' + e + '\n') 121 | line = '* [%s](%s.md)' % (e, path.replace('\\', '/')) 122 | update_file(SUMMARY, ' ' * 4 * (level - 1) + line) 123 | 124 | 125 | def create_file(file_path, content='\n', forced=False): 126 | """ Create a file at the path, and write the content. 127 | If not forced, when file already exists, then do nothing. 128 | 129 | :param file_path: The whole path of the file 130 | :param content: Content to write into the file 131 | :param forced: Whether to force to overwrite file content, default to False 132 | :return: None 133 | """ 134 | if os.path.isfile(file_path) and not forced: 135 | print("Warn: {} already exists, stop writing content={}".format( 136 | file_path, content)) 137 | return 138 | with open(file_path, 'w') as f: 139 | f.write(content) 140 | 141 | 142 | def update_file(file_path, content, append=True, debug=False): 143 | """ Update the content into the file_path 144 | 145 | Potentially this can be merged with create_file, but maybe too heavy 146 | 147 | :param file_path: The whole path of the file 148 | :param content: content to append to SUMMARY file 149 | :param debug: Whether to output the result 150 | :param append: Whether to append the content 151 | :return: None 152 | """ 153 | if append: 154 | with open(file_path, 'a') as f: 155 | f.write(content + '\n') 156 | else: 157 | with open(file_path, 'w') as f: 158 | f.write(content + '\n') 159 | if debug: 160 | print(content) 161 | 162 | 163 | if __name__ == '__main__': 164 | if len(sys.argv) > 1: 165 | for d in sys.argv[1:]: 166 | if not os.path.exists(d): 167 | print("WARN: dir name {} does not exist".format(d)) 168 | continue 169 | path_str = refine_dirname(d) 170 | PROJECT = ROOT_PATH + os.sep \ 171 | + path_str + '_gitbook' # a_b_gitbook 172 | README = PROJECT + os.sep + 'README.md' 173 | SUMMARY = PROJECT + os.sep + 'SUMMARY.md' 174 | print("Will init the output dir={}".format(PROJECT)) 175 | init_gitbook_dir(PROJECT, d) 176 | os.chdir(d) 177 | process_dir('.') 178 | else: 179 | print("Put the input dir name(s) as parameters") 180 | print("Usage: python gitbook_gen [source_dir1] [source_dir2] ... ") 181 | -------------------------------------------------------------------------------- /system/ether_bridge/etherbridge.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Based on tcpdump sniffer, arpsniffer code and Micky Holdorf's packetforward project. 3 | * 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include "headers.h" 16 | 17 | char *dev = NULL; /* capture device1 name */ 18 | char *dev2 = NULL; /* capture device2 name */ 19 | 20 | pcap_t *handle_dev=NULL; /* packet capture handle_dev */ 21 | pcap_t *handle_dev2=NULL; /* packet sending handle_dev2 */ 22 | 23 | int hide_header = 0; 24 | int hide_payload = 1; 25 | int capture_only = 1; 26 | int unidirectional = 0; 27 | int num_packets = -1; /* number of packets to capture */ 28 | char *bpfFilter = NULL; /* filter expression */ 29 | 30 | extern char *optarg; 31 | extern int optind, opterr; 32 | 33 | void got_packet_dev(u_char *args, const struct pcap_pkthdr *header, const u_char *packet); 34 | void got_packet_dev2(u_char *args, const struct pcap_pkthdr *header, const u_char *packet); 35 | void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet, pcap_t *fwd_dev); 36 | 37 | /** 38 | * got packet at dev. 39 | */ 40 | void got_packet_dev(u_char *args, const struct pcap_pkthdr *header, const u_char *packet) { 41 | printf("got_packet_dev\n"); 42 | got_packet(args, header, packet,handle_dev2); 43 | } 44 | 45 | /** 46 | * got packet at dev2. 47 | */ 48 | void got_packet_dev2(u_char *args, const struct pcap_pkthdr *header, const u_char *packet) { 49 | printf("got_packet_dev2\n"); 50 | got_packet(args, header, packet,handle_dev); 51 | } 52 | /* 53 | * dissect/print packet 54 | */ 55 | void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet,pcap_t *fwd_dev) { 56 | 57 | static int count = 1; /* packet counter */ 58 | printf("Received pkt F#%i:\n",count); 59 | if (parse_pkt(packet,hide_header,hide_payload)>=0) { 60 | if (!capture_only && fwd_dev != NULL) { 61 | send_packet(fwd_dev,packet,header->len); 62 | } 63 | } 64 | 65 | count++; 66 | return; 67 | } 68 | 69 | /** 70 | * Compile and apply the filter expression. 71 | */ 72 | int set_filter(pcap_t *handle,struct bpf_program *fp,const char*bpfFilter){ 73 | if(bpfFilter != NULL) { 74 | printf("Packet Filter: %s\n", bpfFilter); 75 | if (pcap_compile(handle, fp, bpfFilter, 0, PCAP_NETMASK_UNKNOWN) == -1) { 76 | fprintf(stderr, "Couldn't parse filter %s: %s\n", bpfFilter, pcap_geterr(handle)); 77 | return(2); 78 | } 79 | if (pcap_setfilter(handle, fp) == -1) { 80 | fprintf(stderr, "Couldn't install filter %s: %s\n", bpfFilter, pcap_geterr(handle)); 81 | return(2); 82 | } 83 | } 84 | return 0; 85 | } 86 | 87 | pcap_t * open_and_init(const char *dev_name, const char *bpfFilter) { 88 | pcap_t *handle = NULL; 89 | char errbuf[PCAP_ERRBUF_SIZE]; /* error buffer */ 90 | struct bpf_program fp; /* compiled filter program (expression) */ 91 | /* open capture device*/ 92 | if((handle= pcap_open_live(dev_name, BUFSIZ, 1, 100, errbuf))==NULL) { 93 | fprintf(stderr, "Couldn't open device %s: %s\n", dev_name, errbuf); 94 | return NULL; 95 | } 96 | 97 | /* make sure we're capturing on an Ethernet device */ 98 | if (pcap_datalink(handle) != DLT_EN10MB) { 99 | fprintf(stderr, "Device %s doesn't provide Ethernet headers - not supported\n", dev_name); 100 | return NULL; 101 | } 102 | 103 | pcap_setdirection(handle,PCAP_D_IN); 104 | 105 | /* compile and apply the filter expression */ 106 | if(set_filter(handle,&fp,bpfFilter)!=0){ 107 | fprintf(stderr, "Error when setting filter to %s\n", dev_name); 108 | return NULL; 109 | } 110 | return handle; 111 | } 112 | 113 | void *cap_and_fwd(void *device_name) { 114 | if(strcmp(device_name,dev)==0){ 115 | if(pcap_loop(handle_dev, num_packets, got_packet_dev, NULL)==-1) { 116 | fprintf(stderr, "ERROR: %s\n", pcap_geterr(handle_dev)); 117 | return NULL; 118 | } 119 | if (handle_dev != NULL) pcap_close(handle_dev); 120 | } else if(strcmp(device_name,dev2)==0){ 121 | if(unidirectional==0) { 122 | if(pcap_loop(handle_dev2, num_packets, got_packet_dev2, NULL)==-1) { 123 | fprintf(stderr, "ERROR: %s\n", pcap_geterr(handle_dev2)); 124 | return NULL; 125 | } 126 | if (handle_dev2 != NULL) pcap_close(handle_dev2); 127 | } 128 | } 129 | return NULL; 130 | } 131 | 132 | int main(int argc, char **argv) { 133 | int c; 134 | pthread_t t1,t2; 135 | 136 | /* check command-line options */ 137 | while ((c = getopt(argc, argv, "i:I:n:uhpf:")) != EOF) { 138 | switch (c) { 139 | case 'i': 140 | dev = optarg; 141 | dev2 = dev; 142 | break; 143 | case 'I': 144 | dev2 = optarg; 145 | if(dev2 == dev){ 146 | fprintf(stderr, "Should use differnet interface to forward\n"); 147 | return 0; 148 | } else { 149 | capture_only = 0; 150 | } 151 | break; 152 | case 'n': 153 | num_packets = atoi(optarg); 154 | break; 155 | case 'f': 156 | printf("num_packets= %d, dev2=%s\n", num_packets, dev2); 157 | bpfFilter = strdup(optarg); 158 | break; 159 | case 'h': 160 | hide_header = 1; 161 | break; 162 | case 'p': 163 | hide_payload = 1; 164 | break; 165 | case 'u': 166 | unidirectional = 1; 167 | break; 168 | default: 169 | print_usage(argv[0]); 170 | return 0; 171 | } 172 | } 173 | 174 | if (dev == NULL) { 175 | print_usage(argv[0]); 176 | return -1; 177 | } 178 | 179 | if(num_packets > 0) printf("Packets to capture: %d\n", num_packets); 180 | 181 | handle_dev = open_and_init(dev,bpfFilter); 182 | if (!capture_only){ 183 | handle_dev2 = open_and_init(dev2,bpfFilter); 184 | } 185 | if (!unidirectional) { 186 | if (!capture_only) printf("Capture and forward between: %s, %s\n", dev, dev2); 187 | pthread_create(&t1, NULL, cap_and_fwd, (void*) dev); 188 | pthread_create(&t2, NULL, cap_and_fwd, (void*) dev2); 189 | pthread_join(t1, NULL); 190 | pthread_join(t2, NULL); 191 | } else { 192 | if (!capture_only) printf("Capture from %s, and forward to %s\n", dev, dev2); 193 | if(pcap_loop(handle_dev, num_packets, got_packet_dev, NULL)==-1){ 194 | fprintf(stderr, "ERROR: %s\n", pcap_geterr(handle_dev)); 195 | return 2; 196 | } 197 | /* cleanup */ 198 | if (handle_dev != NULL) pcap_close(handle_dev); 199 | if (handle_dev2 != NULL) pcap_close(handle_dev2); 200 | } 201 | printf("\n\n--- Process complete. ---\n\n"); 202 | return 0; 203 | } 204 | -------------------------------------------------------------------------------- /web/sm_watcher/sm_watcher.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # Usage: ./prog board_name keyword 4 | # By default, it will show all articles at the couponslife board of newsmth 5 | # Maintained at 6 | # https://github.com/yeasy/code_snippet/blob/master/python/sm_watcher.py 7 | 8 | import random 9 | import re 10 | import signal 11 | import sys 12 | import threading 13 | import time 14 | import datetime 15 | import pickle 16 | import requests 17 | 18 | from bs4 import BeautifulSoup 19 | 20 | 21 | headers_pool = [ 22 | {'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US;' 23 | 'rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}, 24 | {'User-Agent': 'Mozilla/5.0 (compatible;MSIE 9.0; Windows NT 6.1;' 25 | 'Trident/5.0)'}, 26 | {'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US;) ' 27 | 'AppleWebKit/534.50(KHTML, like Gecko) Version/5.1;' 28 | 'Safari/534.50'}, 29 | {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) ' 30 | 'AppleWebKit/537.36 (KHTML, like Gecko) ' 31 | 'Chrome/39.0.2171.95 Safari/537.36', 32 | } 33 | ] 34 | 35 | DEFAULT_BOARD = 'CouponsLife' 36 | DEFAULT_KEYWORD = ".*" 37 | DEFAULT_URL = "http://www.newsmth.net/bbsdoc.php?board=%s&ftype=6" % \ 38 | DEFAULT_BOARD 39 | DEFAULT_PREFIX = "http://www.newsmth.net/bbstcon.php?board=%s&gid=" % \ 40 | DEFAULT_BOARD 41 | 42 | DB_FILE='/tmp/sm_watcher.data' 43 | 44 | 45 | def get_time_str(ticks=None, str_format='%H:%M:%S'): 46 | """ 47 | Get the time string of given ticks or now 48 | :param ticks: ticks to convert 49 | :param str_format: Output based on the str_format 50 | :return: 51 | """ 52 | t = ticks or time.time() 53 | return time.strftime(str_format, time.localtime(t)) 54 | 55 | 56 | class BoardWatcher(threading.Thread, object): 57 | """ 58 | Watcher for a given board, will keep printing new articles with keyword. 59 | This class is easily implemented based on Threading.Thread or 60 | Multiprocessing.Process, but now it seems not necessary. 61 | """ 62 | 63 | def __init__(self, board_name=DEFAULT_BOARD, title_keyword=DEFAULT_KEYWORD): 64 | threading.Thread.__init__(self) 65 | self.setDaemon(True) 66 | self.board = board_name 67 | self.keyword = title_keyword 68 | self.url = DEFAULT_URL.replace(DEFAULT_BOARD, self.board) 69 | self.ct_prefix = DEFAULT_PREFIX.replace(DEFAULT_BOARD, self.board) 70 | 71 | self.max_items = 20 72 | self.db_file = DB_FILE 73 | self.db = {} 74 | 75 | def run(self): 76 | print('board=%s, keyword=%s' % (self.board, self.keyword)) 77 | print("###### start at ", get_time_str(str_format='%Y-%m-%d %H:%M:%S')) 78 | last_gid = 0 79 | while True: 80 | now = datetime.datetime.now() 81 | entries_new = [] 82 | if datetime.time(1, 0) <= now.time() <= datetime.time(5, 55): 83 | time.sleep(300) 84 | else: 85 | entries_new = self.get_board() 86 | if entries_new: 87 | print('Get new entries') 88 | self.db.update(entries_new) 89 | gids = sorted(self.db.keys()) 90 | if len(gids) > self.max_items: 91 | num_remove = len(gids) - self.max_items 92 | for gid in gids[:num_remove]: 93 | del self.db[gid] 94 | self.print_out() 95 | self.save_out() 96 | time.sleep(random.uniform(2, 10)) 97 | 98 | def get_board(self): 99 | """ Get articles collection on board. 100 | 101 | Currently we only get the content on latest page 102 | 103 | :return: { 104 | 'xxx': {'gid':xxx, 105 | 'uid': uuu, 106 | 'ts': ttt, 107 | 'title': yyy 108 | }, 109 | ,...} or {} 110 | """ 111 | if not self.board: 112 | return 113 | page = self.get_page() 114 | if not page: 115 | return [] 116 | else: 117 | return self.parse(page) 118 | 119 | def get_page(self): 120 | """ Get the page content of given url. 121 | 122 | :return: raw page content or None 123 | """ 124 | if not self.url: 125 | return None 126 | headers = headers_pool[random.randint(0, len(headers_pool)-1)] 127 | try: 128 | r = requests.get(self.url, headers=headers, timeout=10) 129 | r.encoding = r.apparent_encoding 130 | 131 | if r.status_code != requests.codes.ok: 132 | print('return status code {} is not OK.'.format(r.status_code)) 133 | return None 134 | return r.text 135 | except Exception: 136 | return None 137 | 138 | def parse(self, page): 139 | """Parse a page into related content 140 | 141 | :param page: raw html page 142 | :return: { 143 | 'xxx': {'gid':xxx, 144 | 'uid': uuu, 145 | 'ts': ttt, 146 | 'title': yyy 147 | }, 148 | ,...} 149 | """ 150 | now = time.time() 151 | result = {} 152 | pat = re.compile(u"c.o\(.*?\);", re.UNICODE) 153 | soup = BeautifulSoup(page, "lxml") 154 | 155 | content = soup.body.text 156 | #content = content.decode('gb18030', 'ignore') 157 | entries = re.findall(pat, content) 158 | for e in entries: 159 | f = e.split(',') 160 | ts, title, uid, gid = int(f[4]), f[5], f[2], f[1] 161 | title, uid = title.strip("' "), uid.strip("' ") 162 | if uid != 'deliver' and uid != 'SYSOP' \ 163 | and now - ts < 7200: # only in recent two hours 164 | if self.keyword and re.search(self.keyword, title): 165 | result[gid] = { 166 | 'gid': gid, 167 | 'user_name': uid, 168 | 'ts': get_time_str(ts), 169 | 'title': title, 170 | 'url': self.ct_prefix+gid, 171 | } 172 | return result 173 | 174 | def print_out(self): 175 | """ 176 | Out put the interested entries: ts, title, uid, gid 177 | :return: 178 | """ 179 | for gid in self.db: 180 | item = self.db[gid] 181 | print('gid={}, ts={}, user_name={}, title={}'.format( 182 | gid, item['ts'], item['user_name'], item['title'])) 183 | 184 | def save_out(self): 185 | """Save into db 186 | 187 | :return: 188 | """ 189 | if not self.db or not self.db_file: 190 | print('No db file to save out') 191 | return 192 | 193 | pickle.dump(self.db, open(self.db_file, 'wb')) 194 | 195 | 196 | def signal_handler(signal_num, frame): 197 | print('You pressed Ctrl+C!') 198 | sys.exit(0) 199 | 200 | if __name__ == "__main__": 201 | board, keyword = DEFAULT_BOARD, DEFAULT_KEYWORD 202 | if len(sys.argv) == 2: 203 | board = DEFAULT_BOARD 204 | elif len(sys.argv) == 3: 205 | board, keyword = sys.argv[1], sys.argv[2] 206 | 207 | signal.signal(signal.SIGINT, signal_handler) 208 | 209 | p = BoardWatcher(board, keyword) 210 | p.start() 211 | 212 | while True: 213 | time.sleep(1) --------------------------------------------------------------------------------