Sunday, May 28, 2023

Golang: selenium test with Google Chrome browser


package main
 
import (
  "errors"
  "fmt"
  "log"
  "os"
 
  "github.com/tebeka/selenium"
)
 
const (
  chromeDriverPath = `C:\WebDriver\chromedriver.exe`
  port = 22222
)
 
func main() {
  opts := []selenium.ServiceOption {
    selenium.Output(os.Stderr),
  }
 
  selenium.SetDebug(true)
  service, err := selenium.NewChromeDriverService(chromeDriverPath, port, opts...)
  if err != nil {
    log.Fatal(err)
  }
  defer service.Stop()
 
  caps := selenium.Capabilities {
    "browserName": "chrome",
    // "acceptInsecureCerts": true,   // when using self-signed certificate
  }
 
  wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", port))
  if err != nil {
    log.Fatal(err)
  }
 
  defer wd.Quit()
 
  err = wd.Get("https://localhost/")
  if err != nil {
    log.Fatal(err)
  }
 
  elm, err := wd.FindElement(selenium.ByXPATH, "/html/body/h1")
  if err != nil {
    log.Fatal(err)
  }
 
  text, err := elm.Text()
  if err != nil {
    log.Fatal(err)
  }
 
  if text != "Hello World!" {
    fmt.Println("Failed")
  } else {
    fmt.Println("Passed")
  }
}
 
  

Monday, March 20, 2023

golang: sftp connecting to a remote server


package main

import (
  "github.com/pkg/sftp"
  "golang.org/x/crypto/ssh"
  "log"
)

func main() {
 
  // for demo purpose only. Such config is not so secure.
  config := &ssh.ClientConfig {
    User: "myusername",
    Auth: []ssh.AuthMethod {
      ssh.Password("mypassword") },
    HostKeyCallback: ssh.InsecureIgnoreHostKey(),
  }

  // Connect to the server
  conn, err := ssh.Dial("tcp", "myhost:12345", config)

  if err != nil {
    log.Fatal(err)
  }

  defer conn.Close()

  // Open SFTP session
  session, err := sftp.NewClient(conn)

  if err != nil {
    log.Fatal(err)
  }

  defer session.Close()

  fileInfo, err := session.Lstat("my-remote-file")

  if err != nil {
    log.Fatal(err)
  }

  log.Println(fileInfo)
}



Sunday, March 19, 2023

golang: compute PBKDF2 of a password


 package main

import (
  "bytes"
  "crypto/rand"
  "crypto/sha256"
  "encoding/hex"
  "fmt"
  "golang.org/x/crypto/pbkdf2"
  "golang.org/x/term"
  "log"
  "strings"
  "syscall"
)

func main() {
  fmt.Print("Input password: ")

  password, err := term.ReadPassword(int(syscall.Stdin))

  if err != nil {
    log.Fatal(err)
  }

  fmt.Print("\nConfirm password: ")

  confirmPw, err := term.ReadPassword(int(syscall.Stdin))
 
  if err != nil {
    log.Fatal(err)
  }
 
  if !bytes.Equal(password, ConfirmPw) {
    log.Fatal("Error: inputs mismatch")
  }

  salt := make([]byte, 32)

  _, err = rand.Read(salt)

  if err != nil {
    log.Fatal(err)
  }

  pwPbkdf2 := pbkdf2.Key(password, salt, 10240, 32, sha256.New)

  fmt.Println("\nSalt: ", strings.ToUpper(hex.EncodeToString(salt))
  fmt.Println("PBKDF2: ", strings.ToUpper(hex.EncodeToString(pwPbkdf2))
}




Saturday, March 18, 2023

golang: example of query and update with MySQL DB


package main
 
import {
  "database/sql"
  "log"
  _ "github.com/go-sql-driver/mysql"
}
 
func dbConn() *sql.DB {
  db, err := sql.Open("mysql", 
             "myusername:mypassword@tcp(127.0.0.1:3306)/MYDB")
 
  if err != nil {
    log.Fatal(err)
  }
 
  return db
}
 
func DoQuery() (ids []int) {
  ids = nil
 
  db := dbConn()
 
  defer db.Close()
 
  res, err := db.Query("select id from mytable where name=?", "Puppy")
 
  if err != nil {
    log.Fatal(err)
  }
 
  for res.Next() {
    var id int
 
    res.Scan(&id)
 
    ids = append(ids, id)
  }

  return
}

func DoUpdate(id int) {
  db := dbConn()
 
  defer db.Close()
 
  res, err := db.Exec("update mytable set name=? where id=?", "Kitty", id)
 
  if err != nil {
    log.Fatal(err)
  }
 
  count, err := res.RowsAffected()
 
  if err != nil {
    log.Fatal(err)
  }

  log.Println(count)
}

func main() {
  ids := DoQuery()
 
  for _, id := range ids {
    DoUpdate(id)
  }
}
 


Friday, March 17, 2023

Debug SSL connection with ssltap


1. Run ssltap on port 19240:

 $ ssltap -s -p 19240 <remote host>:<remote port>

 2. In a browser, access the remote-host:remote-port by connecting to ssltap:

https://localhost:19240

 3. Check the ssltap's output


Thursday, March 16, 2023

Oracle: dump stored procedure


SELECT text FROM sys.all_source 

 WHERE owner = 'schema-owner'

   AND name = 'package-name'

   AND type = 'PACKAGE BODY'

 ORDER BY line;

Wednesday, March 15, 2023

How to stop Google Chrome from automatically redirecting to HTTPS


1. In a new browser tab, enter URL: chrome://net-internals/#hsts

2. Scroll down to "Delete domain security policies" and enter the domain that you don't want automatic redirection. Then click the Delete button.

3. If the problem still exists, try to clean the browsing data/cache by going to Settings => Privacy and security.


Tuesday, March 14, 2023

nginx configuration example for a simple proxy


Build nginx from source code:

$ ./configure --prefix=/path/to/install --with-http_ssl_module --without-http_rewrite_module --with-debug

$ make; make install


Configuration locates at /path/to/install/conf/nginx.conf.

Example of the configuration (for demo purpose only):

server {
  listen  443 ssl;
  server_name localhost;
 
  ssl_certificate  ngcert.pem;
  ssl_certificate  ngprivkey.pem;
 
  # disable HSTS when we are using self-signed certificate
  add_header Strict-Transport-Security  "max-age=0";
 
  ssl_session_cache  shared:SSL:1m;
  ssl_session_timeout  5m;
 
  ssl_ciphers  HIGH:!aNULL:!MD5;  # adjust to remove week ciphers
  ssl_prefer_server_ciphers  on; 

  location / {
    root  html;
    index  index.html;
  }

  location / {
    proxy_pass  https://destination-url/;
  }
}

Run nginx:
$ nginx

   

Wednesday, November 30, 2022

Windows: Force WiFi to work only on 5GHz


1. From Control Panel, open Device Manager as Administrator;

2. Expand Network Adapters;

3. Double click on the WiFi device you want to change;

4. Click on the Advanced tab;

5. Select 802.11a/b/g Wireless Mode. Change its value to: 5GHz 802.11a

6. Select 802.11n/ac/ax Wireless Mode. Change its value to: 802.11ac

7. Click OK to save the settings.

The selected 2 modes work only on 5GHz. Select other modes to allow it work on 2.4GHz again.


Monday, October 10, 2022

Windows VirtualBox VM Downloads


Windows (with IE8/9/10/11/Edge) Virtual Machine Download URLs:

Official: https://developer.microsoft.com/en-us/microsoft-edge/tools/vms/

GitHub: https://github.com/magnetikonline/linux-microsoft-ie-virtual-machines 

Archive.org: https://archive.org/details/ie11.win81.virtualbox


For Speaker and Microphone:
VirtualBox Settings:
Audio
Enable Audio
Host Audio Driver: Pulse Audio
Audio Controller: Intel HD Audio

---

For WebCam (Linux Host):
Add your user to the vboxusers group:
$ sudo usermod -a -G vboxusers $(whoami)

Logout from your desktop and login again. If you open a terminal you can check that the group appears at the end of your group list:
$ groups

VirtualBox Settings:
USB
Enable USB Controller
Add the WebCam (by the 2nd button)



Note: Default username: IEUser; password: Passw0rd! .


 
Get This <