แสดงบทความที่มีป้ายกำกับ golang แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ golang แสดงบทความทั้งหมด

05 เมษายน 2567

Profiling golang gin with pprof



Profiling golang gin with pprof

1. Install pprof lib for gin

go get github.com/gin-contrib/pprof


2. Register debug pprof routes

import "github.com/gin-contrib/pprof"
// ... 
r := gin.New() 
pprof.Register(r, &pprof.Options{RoutePrefix: "debug/pprof"}) 
// ...


3. Under debug mode, gin will print debug pprof routes like this



4. Load test to your server, you can use any tool like Apache Bench (ab)

5. Generate profiling report using go tool
go tool pprof -http=localhost:8081 'http://localhost:8080/debug/pprof/profile?seconds=60' 

Note
  • seconds is the time you need to collect samples (default is 30 seconds)
  • http://localhost:8080 is your web server url
  • localhost:8081 is the web report to view virtualization report

6. After 60 seconds, you will be re-direct to localhost:8081 and get virtualization report, and the profile result file will be saved to path /home/{user}/pprof/pprof.xxx.pb.gz


7. You can view the virtualization report from your gz file also by using command
go tool pprof -http=localhost:8081 /home/{user}/pprof/pprof.xxx.pb.gz



References 

22 มิถุนายน 2565

Golang decrypt RC4 sample

 Sample Golang code below is the RC4 decrypt function that accept key and encrypted hex inputs.

Sample Golang Code

import (
"crypto/cipher"
"crypto/rc4"
"encoding/hex"
"strings"
"unicode/utf8"
)

func DecryptRC4(keyHex string, valueHex string) (string, error) {
keyBs, err := hex.DecodeString(keyHex)
if err != nil {
return "", err
}

valueBs, err := hex.DecodeString(valueHex)
if err != nil {
return "", err
}

c, err := rc4.NewCipher(keyBs)
if err != nil {
return "", err
}

c.XORKeyStream(valueBs, valueBs)
if utf8.Valid(valueBs) {
return bsToString(valueBs), nil
}

return "", nil

}

func bsToString(bs []byte) string {
str := string(bs)
str = strings.Replace(str, "\u0000", "", -1)
str = strings.Replace(str, "\x05", "", -1)
return str
}

Try to play and compare result with RC4 encrypt / decrypt online tool Link

Sample Input

got, gotErr := DecryptRC4("6d796b6579", "3b1fa50a0129d01faaa686f42969")
// raw key: "mykey"
// got:"my dummy value"
// gotErr: nil




02 กุมภาพันธ์ 2565

Fix error "Go version 1.12.XX is too old for this version of Delve (minimum supported version 1.16, suppress this error with --check-go-version=false)"

 If you are using delve to debug your Golang program and found an error

Go version 1.12.XX is too old for this version of Delve (minimum supported version 1.16, suppress this error with --check-go-version=false)


The root cause is your Golang version is too old, to fix this problem you can simply upgrade your Golang or if you don't want to update your Golang, please follow these steps

1. Go to your dlv directory

$ git clone https://github.com/go-delve/delve.git

$ cd delve


2. Check out to older version 

$ git checkout v1.2.0


3. Re-install dlv

$ make install


4. Verify dlv version

$ dlv version


If you debug golang using vscode and found error "unknown command dap for dlv", please add this json section to .vscode/settings.json file

"go.delveConfig": {
"debugAdapter": "legacy"
},


Reference: https://github.com/go-delve/delve/issues/2076

21 พฤษภาคม 2563

Vulnerability in RabbitMQ : disable cleartext authentication mechanisms in the amqp configuration, Golang

To fix the vulnerability in RabbitMQ : disable cleartext authentication mechanisms in the amqp configuration, follow these steps

Create self-signed certificate files

the easy way is cloning the repository https://github.com/michaelklishin/tls-gen  then

1. go to the cloned repository and goto basic directory

2. run make PASSWORD={your_password} CN={your_domain}, see image below


3. ca, server and client certificate files will be created, see image below



Configure RabbitMQ configuration file

your rabbitmq.conf should be configured like this

loopback_users.guest = false
listeners.ssl.default = 5671
ssl_options.cacertfile = path_to_ca_certificate.pem
ssl_options.certfile = path_to_server_certificate.pem
ssl_options.keyfile = path_to_server_key.pem
ssl_options.password = XXXXX
default_pass = XXXXX
default_user = XXXXX

* Note
- listeners.tcp.default = 5672 must be removed
- ssl_options.password must match the certificate password from the step above
- a related document is available at https://www.rabbitmq.com/ssl.html

Configure the client

your client code (Golang) should be written like this

cfg := new(tls.Config)
cfg.InsecureSkipVerify = true
cfg.RootCAs = x509.NewCertPool()
ca, err := ioutil.ReadFile("path_to_ca_certificate.pem")

if err == nil {
cfg.RootCAs.AppendCertsFromPEM(ca)
} else {
return nil, err
}

cert, err := tls.LoadX509KeyPair(
"path_to_client_certificate.pem",
"path_to_client_key.pem",
)
if err == nil {
cfg.Certificates = append(cfg.Certificates, cert)
} else {
return nil, err
}

url := fmt.Sprintf(
"amqps://%s:%s@%s:%d",
"rabbit_user",
"rabbit_password",
"rabbit_host",
5671,
)
con, err := amqp.DialTLS(url, cfg)
if err != nil {
return nil, err
}

see related full Golang code at https://tinyurl.com/y6u9ack9

บทความที่ได้รับความนิยม