├── websocket-terminal
├── dist
│ └── .gitignore
├── package.json
├── index.html
├── webpack.config.js
├── src
│ └── entry.js
└── index.js
├── simple-sshd
├── .gitignore
├── Makefile
├── README.md
└── server.cpp
├── websocket-terminal-cpp-server
├── exec.sh
├── frontend.hpp
├── pty.hpp
├── base64.hpp
├── Makefile
├── sha1.hpp
├── pty.cpp
├── frontend.html
├── http.hpp
├── ws.hpp
├── base64.cpp
├── http.cpp
├── frontend.h
├── main.cpp
├── sha1.cpp
└── ws.cpp
├── Daemon
├── Daemon
│ ├── Resources
│ │ └── icon.ico
│ ├── App.config
│ ├── Properties
│ │ ├── Settings.settings
│ │ ├── Settings.Designer.cs
│ │ ├── AssemblyInfo.cs
│ │ ├── Resources.Designer.cs
│ │ └── Resources.resx
│ ├── App.xaml
│ ├── App.xaml.cs
│ ├── MainWindow.xaml
│ ├── Daemon.csproj
│ └── MainWindow.xaml.cs
└── Daemon.sln
├── bandwidth
├── package.json
├── README.md
└── index.js
├── reversi
├── package.json
├── dist
│ └── index.html
└── index.js
├── azure-ddns
├── package.json
└── index.js
├── music-read-trans
├── package.json
├── webpack.config.js
├── index.html
└── src
│ └── entry.js
├── flac-player-sdl
├── README.md
├── Makefile
└── main.cpp
├── pcap-port-statistics
├── README.md
└── main.cpp
├── wav-decode
├── README.md
├── index.html
└── main.js
├── README.md
├── bilibili-regular-block
└── tv.bilibili.player.xml
└── .gitignore
/websocket-terminal/dist/.gitignore:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/simple-sshd/.gitignore:
--------------------------------------------------------------------------------
1 | *.o
2 | server
3 | ssh
4 | .vscode
--------------------------------------------------------------------------------
/websocket-terminal-cpp-server/exec.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | export TERM=xterm-256color
4 |
5 | /bin/bash --login
--------------------------------------------------------------------------------
/Daemon/Daemon/Resources/icon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ntzyz/playground/HEAD/Daemon/Daemon/Resources/icon.ico
--------------------------------------------------------------------------------
/bandwidth/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "dependencies": {
3 | "pcap": "https://github.com/mranney/node_pcap.git"
4 | }
5 | }
6 |
--------------------------------------------------------------------------------
/reversi/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "dependencies": {
3 | "express": "^4.16.4",
4 | "socket.io": "^2.2.0"
5 | }
6 | }
7 |
--------------------------------------------------------------------------------
/websocket-terminal-cpp-server/frontend.hpp:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | extern unsigned char frontend_html[];
4 | extern unsigned int frontend_html_len;
5 |
--------------------------------------------------------------------------------
/azure-ddns/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "dependencies": {
3 | "azure-arm-dns": "^3.2.0",
4 | "express": "^4.16.4",
5 | "ms-rest-azure": "^2.6.0"
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/websocket-terminal-cpp-server/pty.hpp:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | struct pty_t {
4 | bool ready = false;
5 | int master_fd;
6 |
7 | pty_t();
8 | void spawn();
9 | ~pty_t();
10 | };
--------------------------------------------------------------------------------
/music-read-trans/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "dependencies": {
3 | "vexflow": "^1.2.90",
4 | "webpack": "^4.41.6"
5 | },
6 | "devDependencies": {
7 | "webpack-cli": "^3.3.11"
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/flac-player-sdl/README.md:
--------------------------------------------------------------------------------
1 | FLAC Player
2 | ===========
3 |
4 | Decode and playback flac files with FLAC and SDL.
5 |
6 | ### Requirements
7 | FLAC, SDL1.2
8 |
9 | ### Compile
10 | ```
11 | make
12 | ```
13 |
--------------------------------------------------------------------------------
/Daemon/Daemon/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Daemon/Daemon/Properties/Settings.settings:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/websocket-terminal-cpp-server/base64.hpp:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 | #include
5 | #include
6 |
7 | using byte = unsigned char;
8 |
9 | namespace base64 {
10 |
11 | std::string encode(const std::vector &src);
12 |
13 | }
--------------------------------------------------------------------------------
/pcap-port-statistics/README.md:
--------------------------------------------------------------------------------
1 | Pcap port statistics
2 | ====================
3 |
4 | Collect all traffic data by port.
5 |
6 | ### Requirements
7 |
8 | libpcap
9 |
10 | ### Build
11 |
12 | ```
13 | g++ -std=c++1z -o port_stat main.cpp -lpthread `pcap-config --libs`
14 | ```
--------------------------------------------------------------------------------
/wav-decode/README.md:
--------------------------------------------------------------------------------
1 | ### Description
2 |
3 | A WAV decoder and player with oscilloscope visualizer in pure JavaScript.
4 |
5 | [View demo(Chrome/Chromium only)](https://ntzyz.github.io/playground/wav-decode/)
6 |
7 | ### API used
8 | 1. XMLHttpRequest
9 | 2. Promise
10 | 3. Yield
11 | 4. AudioContext
12 | 5. Canvas
13 |
--------------------------------------------------------------------------------
/websocket-terminal/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "dependencies": {
3 | "css-loader": "^0.28.5",
4 | "express": "^4.15.4",
5 | "node-pty": "^0.7.0",
6 | "socket.io": "^2.0.3",
7 | "style-loader": "^0.18.2",
8 | "webpack": "^3.5.5",
9 | "webpack-dev-server": "^2.7.1",
10 | "xterm": "^2.9.2"
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/simple-sshd/Makefile:
--------------------------------------------------------------------------------
1 | CC = gcc
2 | CCX = g++
3 | CFLAGS = -Wall -O2 -std=c++11
4 | OBJS = server.o
5 | PROG = server
6 | LDFLAGS = -lssh
7 |
8 | all: $(PROG)
9 |
10 | $(PROG): $(OBJS)
11 | $(CCX) $(CFLAGS) $(OBJS) -o $@ $(LDFLAGS) $(LDLIBS)
12 |
13 | server.o: server.cpp
14 | $(CCX) $(CFLAGS) -c server.cpp
15 |
16 | clean:
17 | rm -f *.o $(PROG)
18 |
--------------------------------------------------------------------------------
/websocket-terminal/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/websocket-terminal/webpack.config.js:
--------------------------------------------------------------------------------
1 | const path = require('path');
2 |
3 | module.exports = {
4 | entry: "./src/entry.js",
5 | output: {
6 | path: path.join(__dirname, 'dist'),
7 | filename: "bundle.js"
8 | },
9 | module: {
10 | loaders: [
11 | { test: /\.css$/, loader: "style-loader!css-loader" }
12 | ]
13 | }
14 | };
15 |
--------------------------------------------------------------------------------
/music-read-trans/webpack.config.js:
--------------------------------------------------------------------------------
1 | const path = require('path');
2 |
3 | module.exports = {
4 | entry: "./src/entry.js",
5 | output: {
6 | path: path.join(__dirname, 'dist'),
7 | filename: "bundle.js"
8 | },
9 | devServer: {
10 | contentBase: path.join(__dirname, '.'),
11 | index: 'index.html',
12 | compress: true,
13 | port: 9000,
14 | host: '0.0.0.0',
15 | }
16 | };
--------------------------------------------------------------------------------
/Daemon/Daemon/App.xaml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/Daemon/Daemon/App.xaml.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Configuration;
4 | using System.Data;
5 | using System.Linq;
6 | using System.Threading.Tasks;
7 | using System.Windows;
8 |
9 | namespace Daemon
10 | {
11 | ///
12 | /// Interaction logic for App.xaml
13 | ///
14 | public partial class App : Application
15 | {
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/websocket-terminal-cpp-server/Makefile:
--------------------------------------------------------------------------------
1 | CC=gcc
2 | CXX=g++
3 | LD=gcc
4 |
5 | TARGET=main
6 | CFLAGS=-Wall -O0 -ggdb -static
7 | LDFLAGS=-fPIC -lpthread -lm -lutil -static
8 | OBJS=main.o sha1.o base64.o pty.o ws.o http.o frontend.o
9 |
10 | .PHONY: clean
11 |
12 | $(TARGET): $(OBJS)
13 | $(CXX) -o $(TARGET) $(OBJS) $(LDFLAGS)
14 |
15 | %.o: %.cpp
16 | $(CXX) $(CFLAGS) -c $< -o $@
17 |
18 | frontend.cpp: frontend.html
19 | xxd -i frontend.html > frontend.cpp
20 |
21 | clean:
22 | rm -rf $(OBJS) $(TARGET) frontend.cpp
23 |
--------------------------------------------------------------------------------
/simple-sshd/README.md:
--------------------------------------------------------------------------------
1 | simple sshd
2 | ===========
3 |
4 | A simple Secure Shell Server built with libssh.
5 | ```
6 | ntzyz@ntzyz-solaris ~ % ssh localhost -p 2022
7 | ntzyz@localhost's password:
8 | PTY allocation request failed on channel 0
9 | [NSH]$ 123
10 | ECHO: 123
11 | [NSH]$ ping
12 | Pong!
13 | [NSH]$ exit
14 | exiting...
15 | Connection to localhost closed.
16 | ```
17 |
18 | #### Dependence
19 | libssh-dev
20 |
21 | #### Build and run
22 | ```bash
23 | $ sudo apt install libssh-dev
24 | $ make
25 | $ ./server
26 | ```
27 |
--------------------------------------------------------------------------------
/flac-player-sdl/Makefile:
--------------------------------------------------------------------------------
1 | CC=gcc
2 | CXX=g++
3 | LD=gcc
4 |
5 | SDL_CFLAGS=$(shell pkg-config sdl --cflags)
6 | SDL_LDFLAGS=$(shell pkg-config sdl --libs)
7 | FLAC_CFLAGS=$(shell pkg-config flac --cflags)
8 | FLAC_LDFLAGS=$(shell pkg-config flac --libs)
9 |
10 | TARGET=main
11 | CFLAGS=-Wall -O0 -ggdb $(SDL_CFLAGS) $(FLAC_CFLAGS)
12 | LDFLAGS=-fPIC $(SDL_LDFLAGS) $(FLAC_LDFLAGS)
13 | OBJS=main.o
14 |
15 | .PHONY: clean
16 |
17 | $(TARGET): $(OBJS)
18 | $(CC) -o $(TARGET) $(OBJS) $(LDFLAGS)
19 |
20 | %.o: %.c
21 | $(CC) $(CFLAGS) -c $< -o $@
22 |
23 | clean:
24 | rm -rf $(OBJS) $(TARGET)
--------------------------------------------------------------------------------
/websocket-terminal/src/entry.js:
--------------------------------------------------------------------------------
1 | import Terminal from 'xterm';
2 | import 'xterm/src/xterm.css';
3 | import io from 'socket.io-client';
4 |
5 | Terminal.loadAddon('fit');
6 |
7 | const socket = io(window.location.href);
8 |
9 | const term = new Terminal({
10 | cols: 80,
11 | rows: 24,
12 | });
13 | term.open(document.getElementById('#terminal'));
14 | term.on('resize', size => {
15 | socket.emit('resize', [size.cols, size.rows]);
16 | })
17 |
18 | term.on('data', data => socket.emit('input', data));
19 |
20 | socket.on('output', arrayBuffer => {
21 | term.write(arrayBuffer);
22 | });
23 |
24 | window.addEventListener('resize', () => {
25 | term.fit()
26 | });
27 | term.fit()
28 |
--------------------------------------------------------------------------------
/websocket-terminal/index.js:
--------------------------------------------------------------------------------
1 | const express = require('express');
2 | const site = express();
3 | const http = require('http').Server(site);
4 | const io = require('socket.io')(http);
5 | const net = require('net');
6 | const pty = require('node-pty');
7 |
8 | site.use('/', express.static('.'));
9 |
10 | io.on('connection', function (socket) {
11 | let ptyProcess = pty.spawn('bash', ['--login'], {
12 | name: 'xterm-color',
13 | cols: 80,
14 | rows: 24,
15 | cwd: process.env.HOME,
16 | env: process.env
17 | });
18 | ptyProcess.on('data', data => socket.emit('output', data));
19 | socket.on('input', data => ptyProcess.write(data));
20 | socket.on('resize', size => {
21 | console.log(size);
22 | ptyProcess.resize(size[0], size[1])
23 | });
24 | });
25 |
26 | http.listen(8123);
27 |
--------------------------------------------------------------------------------
/websocket-terminal-cpp-server/sha1.hpp:
--------------------------------------------------------------------------------
1 | /*
2 | sha1.hpp - header of
3 |
4 | ============
5 | SHA-1 in C++
6 | ============
7 |
8 | 100% Public Domain.
9 |
10 | Original C Code
11 | -- Steve Reid
12 | Small changes to fit into bglibs
13 | -- Bruce Guenter
14 | Translation to simpler C++ Code
15 | -- Volker Diels-Grabsch
16 | Safety fixes
17 | -- Eugene Hopkinson
18 | */
19 |
20 | #ifndef SHA1_HPP
21 | #define SHA1_HPP
22 |
23 |
24 | #include
25 | #include
26 | #include
27 |
28 |
29 | class SHA1
30 | {
31 | public:
32 | SHA1();
33 | void update(const std::string &s);
34 | void update(std::istream &is);
35 | std::string final();
36 | static std::string from_file(const std::string &filename);
37 |
38 | private:
39 | uint32_t digest[5];
40 | std::string buffer;
41 | uint64_t transforms;
42 | };
43 |
44 |
45 | #endif /* SHA1_HPP */
46 |
--------------------------------------------------------------------------------
/Daemon/Daemon.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 14
4 | VisualStudioVersion = 14.0.25420.1
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Daemon", "Daemon\Daemon.csproj", "{48455FF1-66AE-4245-B35B-021CE8AEFFE3}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {48455FF1-66AE-4245-B35B-021CE8AEFFE3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {48455FF1-66AE-4245-B35B-021CE8AEFFE3}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {48455FF1-66AE-4245-B35B-021CE8AEFFE3}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {48455FF1-66AE-4245-B35B-021CE8AEFFE3}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | EndGlobal
23 |
--------------------------------------------------------------------------------
/bandwidth/README.md:
--------------------------------------------------------------------------------
1 | ## Bandwidth
2 |
3 | Find out who is consuming most of our bandwidth. Sample output:
4 |
5 | ```
6 | 172.16.10.1 0.00 B/s
7 | 172.16.10.112 0.00 B/s
8 | 172.16.10.170 0.00 B/s
9 | 172.16.10.215 7.39 MB/s
10 | 172.16.10.247 0.00 B/s
11 | 172.16.10.41 0.00 B/s
12 | 172.16.10.81 0.00 B/s
13 | 224.0.0.1 0.00 B/s
14 | ```
15 |
16 | ### Useage
17 |
18 | If you believe your router is powerful enough to run node.js, just run it on your router:
19 |
20 | 1. Install `python`, `git`, `git-http`, `node`, `gcc` with `opkg`.
21 | 2. Download the source code of `libpacp`, compile and install it.
22 | 3. Install node packages by `npm i`.
23 | 3. Run `index.js` with root privilege.
24 |
25 | Or, you can:
26 |
27 | 1. Install `iptables-mod-tee` on your OpenWrt/LEDE router.
28 | 2. Run `iptables -I FORWARD -j TEE --gateway $(echo $SSH_CLIENT | awk '{ print $1 }')` on your OpenWrt/LEDE router.
29 | 3. Run `index.js` with root privilege.
30 | 4. Run `iptables -D FORWARD -j TEE --gateway $(echo $SSH_CLIENT | awk '{ print $1 }')` on your OpenWrt/LEDE router.
31 |
--------------------------------------------------------------------------------
/websocket-terminal-cpp-server/pty.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | #include "pty.hpp"
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 |
12 | pty_t::pty_t () {
13 | }
14 |
15 | void pty_t::spawn() {
16 | struct winsize winp;
17 | winp.ws_col = 80;
18 | winp.ws_row = 24;
19 | winp.ws_xpixel = 0;
20 | winp.ws_ypixel = 0;
21 |
22 | pid_t pid = forkpty(&master_fd, nullptr, nullptr, &winp);
23 |
24 | if (pid == -1) {
25 | perror("forkpty");
26 | return;
27 | } else if (pid == 0) { // master
28 | char path[1024];
29 | char *params[] = {
30 | path,
31 | nullptr
32 | };
33 |
34 | getcwd(path, sizeof (path));
35 | strcat(path, "/exec.sh");
36 |
37 | execvp(path, params);
38 | perror("execvp");
39 | } else {
40 | int flags = fcntl(master_fd, F_GETFL, 0);
41 | fcntl(master_fd, F_SETFL, flags | O_NONBLOCK);
42 |
43 | puts("pts created.");
44 | ready = true;
45 | }
46 | }
47 |
48 | pty_t::~pty_t() {
49 | close(master_fd);
50 | puts("pts closed.");
51 | }
--------------------------------------------------------------------------------
/azure-ddns/index.js:
--------------------------------------------------------------------------------
1 | const DnsManagementClient = require('azure-arm-dns');
2 | const msRestAzure = require("ms-rest-azure");
3 | const express = require('express');
4 | const site = express();
5 |
6 | const subscriptionId = '<---->';
7 | const resourcesGroup = '<---->';
8 | const zoneName = '<---->';
9 | let dnsClient = null;
10 |
11 | site.post('/update_dns', (req, res) => {
12 | if (!req.query.ip_addr) {
13 | res.send({ status: 'ok' });
14 | } else {
15 | dnsClient.recordSets.update(resourcesGroup, zoneName, 'prefix.', 'A', {
16 | aRecords: [{
17 | ipv4Address: req.query.ip_addr
18 | }]
19 | }).then((response) => {
20 | res.send({
21 | status: 'ok',
22 | azure_response: response,
23 | });
24 | }).catch((error) => {
25 | res.send({
26 | status: 'error',
27 | azure_error: error,
28 | });
29 | })
30 | }
31 | });
32 |
33 | msRestAzure.interactiveLogin({
34 | domain: '<---->'
35 | }).then((creds) => {
36 | dnsClient = new DnsManagementClient(creds, subscriptionId);
37 | site.listen(3399, '127.0.0.1');
38 | }).catch((error) => {
39 | console.error(error);
40 | })
41 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Playground
2 | ==========
3 |
4 | Codes for fun.
5 |
6 | 1. [wav-decode](wav-decode): WAV decoder and player with oscilloscope visualizer in pure JavaScript.
7 | 1. [simple-sshd](simple-sshd): Simple secure shell server.
8 | 1. [bilibili-regular-block](bilibili-regular-block): Blocking some annoying danmaku with these regular expressions.
9 | 1. [websocket-terminal](websocket-terminal): HTML5 remote terminal with xterm.js and WebSocket.
10 | 1. [Daemon](Daemon): Supervisor for processes like nginx & java.
11 | 1. [bandwidth](bandwidth): Simple bandwidth monitor utility.
12 | 1. [pcap-port-statistics](pcap-port-statistics): Collect all traffic data by port with libpcap.
13 | 1. [flac-player-sdl](flac-player-sdl): Decode and playback flac files with FLAC and SDL.
14 | 1. [reversi](reversi): Simple(Stupid?) strategy board game called [Reversi](https://en.wikipedia.org/wiki/Reversi), requires at least two players.
15 | 1. [Azure DDNS](azure-ddns): HTTP Server allowing you to update DNS records on Azure. Use it with `/etc/ppp/ip-up.d/` and `cURL`.
16 | 1. [websocket-terminal-cpp-server](websocket-terminal-cpp-server): WebSocket Terminal written with C++ and Linux API, server only.
17 |
--------------------------------------------------------------------------------
/websocket-terminal-cpp-server/frontend.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
33 |
34 |
--------------------------------------------------------------------------------
/Daemon/Daemon/Properties/Settings.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // This code was generated by a tool.
4 | // Runtime Version:4.0.30319.42000
5 | //
6 | // Changes to this file may cause incorrect behavior and will be lost if
7 | // the code is regenerated.
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace Daemon.Properties
12 | {
13 |
14 |
15 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
16 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")]
17 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase
18 | {
19 |
20 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
21 |
22 | public static Settings Default
23 | {
24 | get
25 | {
26 | return defaultInstance;
27 | }
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/websocket-terminal-cpp-server/http.hpp:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 | #include