An awesome blog about software development and network security.

My method referenced many tutorials, fixed the errors and worked successfully.

Install mailutils

1
sudo apt install mailutils

Configure username and password

1
sudo nano /etc/postfix/sasl_passwd

Input SMTP server, account and password.

1
[smtp.126.com]:465 username@126.com:password

The password of the 126 mailbox needs to generate an authorization code in the settings.
Then run postmap.

1
sudo postmap /etc/postfix/sasl_passwd
Read More

In daily life, we may need to create a socks server to provide network for other hosts.
I summarized the method of creating socks service through dante-server under Ubuntu 20.04.

install dante-server

1
sudo apt install dante-server

add user for dante-server

Use this method to create a user to prevent it from logging in.

1
sudo useradd -s /sbin/nologin dante-socks

Edit the configuration file

1
sudo nano /etc/danted.conf

minimal configuration for dante-server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
logoutput: syslog stdout
internal: ztuku335yr port = 1080
external: ens3
user.notprivileged: dante-socks
clientmethod: none
socksmethod: none
#allow connections from local network
client pass {
from: 10.0.0.229/24 to: 0.0.0.0/0
log: error # connect disconnect
}
socks pass {
from: 0.0.0.0/0 to: 0.0.0.0/0
command: bind connect udpassociate
log: error # connect disconnect iooperation
}
socks pass {
from: 0.0.0.0/0 to: 0.0.0.0/0
command: bindreply udpreply
log: error # connect disconnect iooperation
}
Read More

Install nvme-cli

1
sudo apt-get install nvme-cli

List disks

1
sudo nvme list

View the details of the hard drive

1
sudo nvme smart-log /dev/nvme0

Auto refresh status

1
sudo watch -n 1 nvme smart-log /dev/nvme0

Only view the temperature info

1
sudo nvme smart-log /dev/nvme0 | grep "^temperature"
Read More
post @ 2021-05-18

To create a private Ubuntu image, the commonly used tools are debmirror and apt-mirror.
As of today (2021-05-17), these two tools have their own advantages and disadvantages.

debmirror

debmirror document
debmirror has many downloading methods, and it will compare the local file with the file on the server before downloading so that only the missing files are downloaded. However, it does not seem to support multi-threaded downloading. Debmirror is downloaded one by one, so the download speed is relatively slow.

apt-mirror

apt-mirror document
apt-mirror supports 20 threads to download at the same time by default. If you choose a suitable mirror station, the download speed can reach the maximum of my network. However, it does not seem to make a comparison. Although it can speed up the download start speed, sometimes some files may be lost, resulting in an incomplete mirror, so the client
cannot be updated.

how to write the config file

In order to download the complete package, we need to modify the configuration file in /etc/apt/mirror.list. Use this format to ensure that the i386 package will be downloaded.

1
2
deb-amd64 http://mirrors.aliyun.com/ubuntu focal main restricted universe multiverse
deb-i386 http://mirrors.aliyun.com/ubuntu focal main restricted universe multiverse

Combine them together

Therefore, in order to achieve a balance between efficiency and completeness, we can combine apt-mirror and debmirror.
We can first use apt-mirror to download quickly, then we can use debmirror to check and download the missing packages.
Maybe this is the best way to download the Ubuntu mirror.

Read More
post @ 2021-04-30

Commit code

1
2
git add .
git commit -m "feat: xxxxxx"

How to revoke after commit

1
git reset --soft HEAD^

This successfully revoked the commit. Note that only the commit operation is withdrawn, and the code modification remains.

git reset parameters

--mixed
do not delete the workspace to change the code, cancel the commit, and cancel the git add . operation. This is the default parameter.

--soft
do not delete the workspace to change the code, cancel the commit, and do not cancel the git add ..

--hard
delete workspace change code, revoke commit, revoke git add .. After completing this operation, it is restored to the last commit state.

Read More
post @ 2021-02-14

Using NodeSource

Refer to the NodeSource documentation for more information on the available versions.

Node.js v14.x:

1
2
3
4
5
6
7
# Using Ubuntu
curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs

# Using Debian, as root
curl -fsSL https://deb.nodesource.com/setup_14.x | bash -
apt-get install -y nodejs

Upgrade npm

1
npm install -g npm
Read More

LVM overview

lvm

Physical Volume (PV)

Refers to a disk partition, or a device (such as RAID) that has the same function as a disk partition. It is the basic storage logical block of LVM, but compared with basic physical storage media (such as partitions, disks, etc.), it contains LVM-related Management parameters.

Volume Group (VG)

Similar to a physical disk in a non-LVM system, it is composed of one or more physical volumes PV. One or more LVs (logical volumes) can be created on the volume group.

Logical Volume (LV)

Similar to disk partitions in non-LVM systems, logical volumes are built on the volume group VG. A file system (such as /home or /usr, etc.) can be established on the logical volume LV.

Read More

When using golang to write complex projects, it is often useful to use multi-coroutine concurrency scenarios. At this time, it is easy to cause the problem of coroutine leaks due to negligence, and then produce similar memory leaks. This article focuses on the investigation of coroutine leaks, and provides ideas and practices for visual analysis of golang program memory.

Introduction to pprof

pprof is a tool for visualization and analysis of profiling data.
pprof reads a collection of profiling samples in profile.proto format and generates reports to visualize and help analyze the data. It can generate both text and graphical reports (through the use of the dot visualization package).

How to use pprof

Add monitoring code

First, we need to add monitoring code in the golang program, and expose it through the http interface.

1
2
3
4
5
6
7
8
9
10
11
package main

import _ "net/http/pprof"
import "net/http"

func main() {
go func() {
_ = http.ListenAndServe("0.0.0.0:8081", nil)
}()
// your code
}

Then we start the program that needs to be analyzed, and we are ready to analyze it.

Read More

When you install the Ubuntu system on a physical machine, don’t forget to do these things to improve your computer’s performance.
My experiments found that the improvement was very noticeable, based on Ubuntu 20.10.

use s-tui to monitor and test your max CPU frequency

s-tui

how to install

1
2
sudo apt install python3-pip stress
sudo pip3 install s-tui

how to use

1
sudo s-tui

You can use the arrow keys and space bar to switch between Monitor mode and Stress mode. The Stress mode performs a stress test on the CPU.

use indicator-cpufreq to set your CPU frequency

indicator-cpufreq

how to install

1
2
sudo apt install indicator-cpufreq
sudo reboot

how to use

Look at the status bar icon in the upper right corner and click on the emerging icon to set the CPU’s performance mode.

Read More
post @ 2020-12-22

Why migrate to qmgo

  • Mgo is a convenient library for golang to operate mongodb, but it does not support the latest features of mongodb, such as Transaction.
  • My old project is written in mgo. If I want to migrate to the official mongo-driver, its syntax is more primitive, the usage difference is large, and the migration cost will be higher.
  • I found a library called qmgo, which is based on the official mongo-driver package, but it is closer to mgo in terms of syntax and suitable for simple and rude migrations.

Below I will record the methods used in the migration process, which I found out by myself. If you have any questions, please correct me.

Batch replacement

  • Batch replace github.com/globalsign/mgo/bson with go.mongodb.org/mongo-driver/bson
  • Batch replace bson.NewObjectId() with primitive.NewObjectID()
  • Batch replace bson.ObjectId with primitive.ObjectID
  • Batch replace .Find(bson.M with .Find(c, bson.M
  • Batch replace .Find(search to .Find(c, search
  • Batch replace .Insert( to .InsertOne(c,
  • Batch replace .Update( to .UpdateOne(c,
  • Batch replace .RemoveAll( to .RemoveAll(c,
  • Batch replace .Remove( to .Remove(c,
  • Batch replace errors.Is(err, mgo.ErrNotFound) with qmgo.IsErrNoDocuments(err)
  • Batch replace .EnsureIndex(mgo.Index{ for
    .CreateOneIndex(c, options.IndexModel{

Copy and paste

  • Where there is no context, copy and paste everywhere c := context.Background()
  • If it is in the interface method of gin, you can directly use gin’s c *gin.Context (but if multi-coroutine operation needs to Copy gin’s Context)

Function substitution

  • Replace the bson.IsObjectIdHex() method:
1
2
3
4
5
6
7
8
9
10
11
12
13
package db

import (
"go.mongodb.org/mongo-driver/bson/primitive"
)

func IsObjectIDHex(hex string) bool {
_, err := primitive.ObjectIDFromHex(hex)
if err != nil {
return false
}
return true
}
  • Replace the bson.ObjectIdHex() method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package db

import (
"fmt"
"go.mongodb.org/mongo-driver/bson/primitive"
)

func ObjectIDHex(hex string) primitive.ObjectID {
id, err := primitive.ObjectIDFromHex(hex)
if err != nil {
panic(fmt.Sprintf("Invalid input to ObjectIDHex: %+v", hex))
}
return id
}

These functions can be placed in the package of the auxiliary method or the package of the database operation, and then replace the references in batches.

Summary

At this point, the project completed the migration within two hours and started running smoothly.
Thanks to the author of qmgo for making the wheels for me.
If I have the opportunity, I will also open up some wheels.

Read More
⬆︎TOP