├── .gitignore ├── LICENSE ├── README.md ├── WorkspaceONE_Query_PostgreSQL.go ├── frsocks.go ├── vCenter_Query_PostgreSQL.go └── windows_build.bat /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, build with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2018, 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | * Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Homework-of-Go 2 | Go code examples of my blog. 3 | 4 | ### windows_build.bat 5 | 6 | Use to build Go executables for multiple platforms 7 | 8 | ### frsocks.go 9 | 10 | reference:https://github.com/brimstone/rsocks 11 | 12 | Add forward socks5 mode and some changes to the reference. 13 | 14 | Support forward and reverse socks5 proxy. 15 | 16 | ### vCenter_Query_PostgreSQL.go 17 | 18 | Use to query the data of PostgreSQL database on vCenter. 19 | 20 | ### WorkspaceONE_Query_PostgreSQL.go 21 | 22 | Use to query the data of PostgreSQL database on Workspace ONE. 23 | -------------------------------------------------------------------------------- /WorkspaceONE_Query_PostgreSQL.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "database/sql" 5 | "fmt" 6 | "io/ioutil" 7 | _ "github.com/lib/pq" 8 | ) 9 | 10 | 11 | func connectDB() *sql.DB{ 12 | fmt.Println("[*] Get password of the db") 13 | b, err := ioutil.ReadFile("/usr/local/horizon/conf/db.pwd") 14 | if err != nil { 15 | fmt.Print(err) 16 | } 17 | 18 | password := string(b) 19 | fmt.Println("[+] " + password) 20 | 21 | var host = "localhost" 22 | var port int = 5432 23 | var user = "horizon" 24 | var dbname = "saas" 25 | 26 | psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+ 27 | "password=%s dbname=%s sslmode=disable", 28 | host, port, user, password, dbname) 29 | 30 | fmt.Println("[*] psqlInfo:" + psqlInfo) 31 | db, err := sql.Open("postgres", psqlInfo) 32 | if err != nil { 33 | panic(err) 34 | } 35 | 36 | err = db.Ping() 37 | if err != nil { 38 | panic(err) 39 | } 40 | fmt.Println("[+] Successfully connected!") 41 | return db 42 | } 43 | 44 | 45 | func queryUser(db *sql.DB){ 46 | fmt.Println("[*] Querying User") 47 | var strUsername,strEmail,idUser,createdDate,domain string 48 | var searchStr = `SELECT "strUsername","strEmail","idUser","createdDate","domain" FROM "Users"` 49 | 50 | fmt.Println(" " + searchStr) 51 | rows,err:=db.Query(searchStr) 52 | if err!= nil{ 53 | panic(err) 54 | } 55 | defer rows.Close() 56 | for rows.Next(){ 57 | err:= rows.Scan(&strUsername,&strEmail,&idUser,&createdDate,&domain) 58 | if err!= nil{ 59 | //fmt.Println(err) 60 | } 61 | fmt.Println(" - idUser : " + idUser) 62 | fmt.Println(" strUsername: " + strUsername) 63 | fmt.Println(" strEmail : " + strEmail) 64 | fmt.Println(" createdDate: " + createdDate) 65 | fmt.Println(" domain : " + domain) 66 | } 67 | err = rows.Err() 68 | if err!= nil{ 69 | panic(err) 70 | } 71 | } 72 | 73 | 74 | func main() { 75 | db:=connectDB() 76 | queryUser(db) 77 | } -------------------------------------------------------------------------------- /frsocks.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "log" 7 | "os" 8 | "net" 9 | "io" 10 | socks5 "go-socks5" 11 | "yamux" 12 | ) 13 | 14 | var session *yamux.Session 15 | 16 | func createForwardSocks(address string) error { 17 | server, err := socks5.New(&socks5.Config{}) 18 | if err != nil { 19 | return err 20 | } 21 | log.Println("Create a socks5 proxy on localhost port",address) 22 | if err := server.ListenAndServe("tcp", "0.0.0.0:"+address); err != nil { 23 | return err 24 | } 25 | return nil 26 | } 27 | 28 | func connectForSocks(address string) error { 29 | server, err := socks5.New(&socks5.Config{}) 30 | if err != nil { 31 | return err 32 | } 33 | var conn net.Conn 34 | log.Println("Connecting to far end") 35 | conn, err = net.Dial("tcp", address) 36 | if err != nil { 37 | return err 38 | } 39 | log.Println("Starting server") 40 | session, err = yamux.Server(conn, nil) 41 | if err != nil { 42 | return err 43 | } 44 | for { 45 | stream, err := session.Accept() 46 | log.Println("Acceping stream") 47 | if err != nil { 48 | return err 49 | } 50 | log.Println("Passing off to socks5") 51 | go func() { 52 | err = server.ServeConn(stream) 53 | if err != nil { 54 | log.Println(err) 55 | } 56 | }() 57 | } 58 | } 59 | 60 | // Catches yamux connecting to us 61 | func listenForSocks(address string) { 62 | log.Println("Listening for the far end") 63 | ln, err := net.Listen("tcp", "0.0.0.0:"+address) 64 | if err != nil { 65 | return 66 | } 67 | for { 68 | conn, err := ln.Accept() 69 | log.Println("Got a client") 70 | if err != nil { 71 | fmt.Fprintf(os.Stderr, "Errors accepting!") 72 | } 73 | // Add connection to yamux 74 | session, err = yamux.Client(conn, nil) 75 | } 76 | } 77 | 78 | // Catches clients and connects to yamux 79 | func listenForClients(address string) error { 80 | log.Println("Waiting for clients") 81 | ln, err := net.Listen("tcp", address) 82 | if err != nil { 83 | return err 84 | } 85 | for { 86 | conn, err := ln.Accept() 87 | if err != nil { 88 | return err 89 | } 90 | // TODO dial socks5 through yamux and connect to conn 91 | 92 | if session == nil { 93 | conn.Close() 94 | continue 95 | } 96 | log.Println("Got a client") 97 | 98 | log.Println("Opening a stream") 99 | stream, err := session.Open() 100 | if err != nil { 101 | return err 102 | } 103 | // connect both of conn and stream 104 | go func() { 105 | log.Println("Starting to copy conn to stream") 106 | io.Copy(conn, stream) 107 | conn.Close() 108 | }() 109 | go func() { 110 | log.Println("Starting to copy stream to conn") 111 | io.Copy(stream, conn) 112 | stream.Close() 113 | log.Println("Done copying stream to conn") 114 | }() 115 | } 116 | } 117 | 118 | func main() { 119 | 120 | sockstype := flag.String("sockstype", "", "fsocks or rsocks,eg. rsocks") 121 | listen := flag.String("listen", "", "listen port for receiver,eg. 1080") 122 | socks := flag.String("socks", "", "socks address:port,eg. 127.0.0.1:2222") 123 | connect := flag.String("connect", "", "connect address:port,eg. 1.1.1.1:1080") 124 | flag.Usage = func() { 125 | fmt.Println("frsocks - forward and reverse socks5 server/client") 126 | fmt.Println("reference:https://github.com/brimstone/rsocks") 127 | fmt.Println("add forward socks5 mode and some changes to the reference") 128 | fmt.Println("author:3gstudent") 129 | fmt.Println("") 130 | fmt.Println("Usage:") 131 | fmt.Println("Mode1:[Forward Socks5 Mode]") 132 | fmt.Println("1) Create a socks5 proxy on localhost and port 1080.") 133 | fmt.Println("eg.") 134 | fmt.Println("frsocks -sockstype fsocks -listen 1080") 135 | fmt.Println("2) Connect to 127.0.0.1:1080 on the client with any socks5 client.") 136 | fmt.Println("Mode2:[Reverse Socks5 Mode]") 137 | fmt.Println("1) Create a socks redirection on the client.") 138 | fmt.Println("eg.") 139 | fmt.Println("frsocks -sockstype rsocks -listen 1111 -socks 127.0.0.1:2222") 140 | fmt.Println("2) Connect to the client(1.1.1.1:1111)on the transit server.") 141 | fmt.Println("eg.") 142 | fmt.Println("frsocks -sockstype rsocks -connect 1.1.1.1:1111") 143 | fmt.Println("3) Connect to 127.0.0.1:2222 on the client with any socks5 client.") 144 | } 145 | flag.Parse() 146 | 147 | if *sockstype == "fsocks" { 148 | log.Println("[Forward Socks5 Mode]") 149 | if *listen != "" { 150 | log.Fatal(createForwardSocks(*listen)) 151 | } 152 | }else if *sockstype == "rsocks" { 153 | log.Println("[Reverse Socks5 Mode]") 154 | 155 | if *listen != "" { 156 | log.Println("Start to listen for clients") 157 | go listenForSocks(*listen) 158 | log.Fatal(listenForClients(*socks)) 159 | } 160 | 161 | if *connect != "" { 162 | log.Println("Connect to the far end") 163 | log.Fatal(connectForSocks(*connect)) 164 | } 165 | }else{ 166 | flag.Usage() 167 | os.Exit(1) 168 | } 169 | } 170 | -------------------------------------------------------------------------------- /vCenter_Query_PostgreSQL.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "database/sql" 5 | "fmt" 6 | "strings" 7 | "io/ioutil" 8 | _ "github.com/lib/pq" 9 | ) 10 | 11 | 12 | func connectDB() *sql.DB{ 13 | fmt.Println("[+] Get the config") 14 | b, err := ioutil.ReadFile("/etc/vmware-vpx/vcdb.properties") 15 | if err != nil { 16 | fmt.Print(err) 17 | } 18 | 19 | str := string(b) 20 | fmt.Println(str) 21 | index1 := strings.Index(str,"password") 22 | index2 := strings.Index(str,"password.encrypted") 23 | password := b[index1+11:index2] 24 | 25 | var host = "localhost" 26 | var port int = 5432 27 | var user = "vc" 28 | var dbname = "VCDB" 29 | 30 | psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+ 31 | "password=%s dbname=%s sslmode=disable", 32 | host, port, user, password, dbname) 33 | 34 | fmt.Println("[*] psqlInfo:" + psqlInfo) 35 | db, err := sql.Open("postgres", psqlInfo) 36 | if err != nil { 37 | panic(err) 38 | } 39 | 40 | err = db.Ping() 41 | if err != nil { 42 | panic(err) 43 | } 44 | fmt.Println("[+] Successfully connected!") 45 | return db 46 | } 47 | 48 | 49 | func queryVM(db *sql.DB){ 50 | var file_name,guest_os,ip_address,power_state string 51 | 52 | fmt.Println("[*] Querying VM") 53 | rows,err:=db.Query("SELECT file_name,guest_os,ip_address,power_state FROM vc.vpx_vm") 54 | 55 | if err!= nil{ 56 | panic(err) 57 | } 58 | defer rows.Close() 59 | for rows.Next(){ 60 | err:= rows.Scan(&file_name,&guest_os,&ip_address,&power_state) 61 | if err!= nil{ 62 | //fmt.Println(err) 63 | } 64 | fmt.Println(" - file_name : " + file_name) 65 | fmt.Println(" guest_os : " + guest_os) 66 | fmt.Println(" ip_address : " + ip_address) 67 | fmt.Println(" power_state : " + power_state) 68 | } 69 | err = rows.Err() 70 | if err!= nil{ 71 | panic(err) 72 | } 73 | } 74 | 75 | 76 | func queryESXI(db *sql.DB){ 77 | var name,username,password,password_last_upd_dt string 78 | 79 | fmt.Println("[*] Querying ESXI") 80 | rows,err:=db.Query("SELECT name,username,password,password_last_upd_dt FROM vc.vpxv_hosts") 81 | 82 | if err!= nil{ 83 | panic(err) 84 | } 85 | defer rows.Close() 86 | for rows.Next(){ 87 | err:= rows.Scan(&name,&username,&password,&password_last_upd_dt) 88 | if err!= nil{ 89 | //fmt.Println(err) 90 | } 91 | fmt.Println(" - name : " + name) 92 | fmt.Println(" username : " + username) 93 | fmt.Println(" password : " + password) 94 | fmt.Println(" password_last: " + password_last_upd_dt) 95 | 96 | } 97 | err = rows.Err() 98 | if err!= nil{ 99 | panic(err) 100 | } 101 | } 102 | 103 | 104 | func main() { 105 | db:=connectDB() 106 | queryVM(db) 107 | queryESXI(db) 108 | } -------------------------------------------------------------------------------- /windows_build.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | if not exist %1 ( 3 | SET OUTNAME=%1 4 | SET CGO_ENABLED=0 5 | SET GOOS=android 6 | SET GOARCH=arm 7 | go build -o %OUTNAME%_%GOOS%_%GOARCH% 8 | 9 | SET GOOS=darwin 10 | SET GOARCH=386 11 | go build -o %OUTNAME%_%GOOS%_%GOARCH% 12 | 13 | SET GOOS=darwin 14 | SET GOARCH=amd64 15 | go build -o %OUTNAME%_%GOOS%_%GOARCH% 16 | 17 | SET GOOS=darwin 18 | SET GOARCH=arm 19 | go build -o %OUTNAME%_%GOOS%_%GOARCH% 20 | 21 | SET GOOS=darwin 22 | SET GOARCH=arm64 23 | go build -o %OUTNAME%_%GOOS%_%GOARCH% 24 | 25 | SET GOOS=dragonfly 26 | SET GOARCH=amd64 27 | go build -o %OUTNAME%_%GOOS%_%GOARCH% 28 | 29 | SET GOOS=freebsd 30 | SET GOARCH=386 31 | go build -o %OUTNAME%_%GOOS%_%GOARCH% 32 | 33 | SET GOOS=freebsd 34 | SET GOARCH=amd64 35 | go build -o %OUTNAME%_%GOOS%_%GOARCH% 36 | 37 | SET GOOS=freebsd 38 | SET GOARCH=arm 39 | go build -o %OUTNAME%_%GOOS%_%GOARCH% 40 | 41 | SET GOOS=linux 42 | SET GOARCH=386 43 | go build -o %OUTNAME%_%GOOS%_%GOARCH% 44 | 45 | SET GOOS=linux 46 | SET GOARCH=amd64 47 | go build -o %OUTNAME%_%GOOS%_%GOARCH% 48 | 49 | SET GOOS=linux 50 | SET GOARCH=arm 51 | go build -o %OUTNAME%_%GOOS%_%GOARCH% 52 | 53 | SET GOOS=linux 54 | SET GOARCH=arm64 55 | go build -o %OUTNAME%_%GOOS%_%GOARCH% 56 | 57 | SET GOOS=linux 58 | SET GOARCH=ppc64 59 | go build -o %OUTNAME%_%GOOS%_%GOARCH% 60 | 61 | SET GOOS=linux 62 | SET GOARCH=ppc64le 63 | go build -o %OUTNAME%_%GOOS%_%GOARCH% 64 | 65 | SET GOOS=linux 66 | SET GOARCH=mips 67 | go build -o %OUTNAME%_%GOOS%_%GOARCH% 68 | 69 | SET GOOS=linux 70 | SET GOARCH=mipsle 71 | go build -o %OUTNAME%_%GOOS%_%GOARCH% 72 | 73 | SET GOOS=linux 74 | SET GOARCH=mips64 75 | go build -o %OUTNAME%_%GOOS%_%GOARCH% 76 | 77 | SET GOOS=linux 78 | SET GOARCH=mips64le 79 | go build -o %OUTNAME%_%GOOS%_%GOARCH% 80 | 81 | SET GOOS=linux 82 | SET GOARCH=s390x 83 | go build -o %OUTNAME%_%GOOS%_%GOARCH% 84 | 85 | SET GOOS=netbsd 86 | SET GOARCH=386 87 | go build -o %OUTNAME%_%GOOS%_%GOARCH% 88 | 89 | SET GOOS=netbsd 90 | SET GOARCH=amd64 91 | go build -o %OUTNAME%_%GOOS%_%GOARCH% 92 | 93 | SET GOOS=netbsd 94 | SET GOARCH=arm 95 | go build -o %OUTNAME%_%GOOS%_%GOARCH% 96 | 97 | SET GOOS=openbsd 98 | SET GOARCH=386 99 | go build -o %OUTNAME%_%GOOS%_%GOARCH% 100 | 101 | SET GOOS=openbsd 102 | SET GOARCH=amd64 103 | go build -o %OUTNAME%_%GOOS%_%GOARCH% 104 | 105 | SET GOOS=openbsd 106 | SET GOARCH=arm 107 | go build -o %OUTNAME%_%GOOS%_%GOARCH% 108 | 109 | SET GOOS=plan9 110 | SET GOARCH=386 111 | go build -o %OUTNAME%_%GOOS%_%GOARCH% 112 | 113 | SET GOOS=plan9 114 | SET GOARCH=amd64 115 | go build -o %OUTNAME%_%GOOS%_%GOARCH% 116 | 117 | SET GOOS=solaris 118 | SET GOARCH=amd64 119 | go build -o %OUTNAME%_%GOOS%_%GOARCH% 120 | 121 | SET GOOS=windows 122 | SET GOARCH=386 123 | go build -o %OUTNAME%_%GOOS%_%GOARCH% 124 | 125 | SET GOOS=windows 126 | SET GOARCH=amd64 127 | go build -o %OUTNAME%_%GOOS%_%GOARCH% 128 | ) 129 | 130 | 131 | --------------------------------------------------------------------------------